diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index b52460a7..7ce304b8 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -194,6 +194,7 @@ Here, *color* can be specified in the following ways: | data:blit(src: Canvas, dst_x: int, dst_y: int) | draws the src canvas at the specified coordinates | | data:clear() | clears the canvas | | data:clear(*color*) | fills the canvas with the specified RGBA color | +| data:rect(x: int, y: int, w: int, h: int, *color*) | fills the rectangle with the specified RGBA color | | data:update() | applies changes to the canvas and uploads it to the GPU | | data:set_data(data: Bytearray | table) | replaces pixel data (width * height * 4 numbers) | | data:get_data() | creates a Bytearray object with the image's pixel data | diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index d5849541..963e8b00 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -194,6 +194,7 @@ document["worlds-panel"]:clear() | data:blit(src: Canvas, dst_x: int, dst_y: int) | рисует src-холст на указанных координатах | | data:clear() | очищает холст | | data:clear(*цвет*) | заполняет холст указанным RGBA цветом | +| data:rect(x: int, y: int, w: int, h: int, *цвет*) | заполняет прямоугольник указанным RGBA цветом | | data:update() | применяет изменения и загружает холст в видеопамять | | data:set_data(data: bytearray | table) | заменяет данные пикселей (ширина * высота * 4 чисел) | | data:get_data() | создаёт объект Bytearray с пиксельными данными изображения | diff --git a/src/graphics/core/ImageData.cpp b/src/graphics/core/ImageData.cpp index 218428e7..8a44068d 100644 --- a/src/graphics/core/ImageData.cpp +++ b/src/graphics/core/ImageData.cpp @@ -16,7 +16,7 @@ ImageData::ImageData(ImageFormat format, uint width, uint height) default: throw std::runtime_error("format is not supported"); } - data = std::make_unique(width * height * pixsize); + data = std::make_unique((width + width % 2) * (height + width % 2) * pixsize); } ImageData::ImageData(ImageFormat format, uint width, uint height, std::unique_ptr data) diff --git a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp index 52d4e5e3..54bbf654 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp @@ -125,7 +125,9 @@ static RGBA get_rgba(State* L, int first) { rgba.rgba = static_cast(tointeger(L, first)); break; case 3: - rgba.a = static_cast(tointeger(L, first + 3)); + if (lua::isnumber(L, first + 3)) { + rgba.a = static_cast(tointeger(L, first + 3)); + } [[fallthrough]]; case 2: rgba.r = static_cast(tointeger(L, first)); @@ -199,6 +201,18 @@ static int l_blit(State* L) { return 0; } +static int l_rect(State* L) { + auto& canvas = require_canvas(L, 1); + auto& image = canvas.getData(); + int x = tointeger(L, 2); + int y = tointeger(L, 3); + int w = tointeger(L, 4); + int h = tointeger(L, 5); + RGBA rgba = get_rgba(L, 6); + image.drawRect(x, y, w, h, glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a}); + return 0; +} + static int l_set_data(State* L) { auto& canvas = require_canvas(L, 1); auto& image = canvas.getData(); @@ -316,6 +330,7 @@ static std::unordered_map methods { {"line", lua::wrap}, {"blit", lua::wrap}, {"clear", lua::wrap}, + {"rect", lua::wrap}, {"update", lua::wrap}, {"create_texture", lua::wrap}, {"unbind_texture", lua::wrap},