From 8447a097c6a8b805a4f00c6a524847f601883835 Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 26 Dec 2022 17:20:27 -0300 Subject: [PATCH] [lua] Add Image.id and Image.version getters --- src/app/script/image_class.cpp | 16 ++++++++++++++++ tests/scripts/image.lua | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/src/app/script/image_class.cpp b/src/app/script/image_class.cpp index 3aa4607b2..cf856b7a0 100644 --- a/src/app/script/image_class.cpp +++ b/src/app/script/image_class.cpp @@ -532,6 +532,20 @@ int Image_shrinkBounds(lua_State* L) return 1; } +int Image_get_id(lua_State* L) +{ + const auto obj = get_obj(L, 1); + lua_pushinteger(L, obj->imageId); + return 1; +} + +int Image_get_version(lua_State* L) +{ + const auto obj = get_obj(L, 1); + lua_pushinteger(L, obj->image(L)->version()); + return 1; +} + int Image_get_rowStride(lua_State* L) { const auto obj = get_obj(L, 1); @@ -625,6 +639,8 @@ const luaL_Reg Image_methods[] = { }; const Property Image_properties[] = { + { "id", Image_get_id, nullptr }, + { "version", Image_get_version, nullptr }, { "rowStride", Image_get_rowStride, nullptr }, { "bytes", Image_get_bytes, Image_set_bytes }, { "width", Image_get_width, nullptr }, diff --git a/tests/scripts/image.lua b/tests/scripts/image.lua index c429484f2..86875a0d2 100644 --- a/tests/scripts/image.lua +++ b/tests/scripts/image.lua @@ -9,6 +9,7 @@ dofile('./test_utils.lua') local rgba = app.pixelColor.rgba local a = Image(32, 64) +assert(a.id > 0) assert(a.width == 32) assert(a.height == 64) assert(a.colorMode == ColorMode.RGB) -- RGB by default @@ -50,6 +51,7 @@ do assert(c.width == d.width) assert(c.height == d.height) assert(c.colorMode == d.colorMode) + assert(c.id ~= d.id) -- The clone must have different ID -- Get RGB pixels for y=0,c.height-1 do @@ -75,6 +77,9 @@ end do local spr = Sprite(256, 256) local image = app.site.image + local imageID = image.id + assert(image.id > 0) + assert(image.version == 0) local copy = image:clone() assert(image:getPixel(0, 0) == 0) for y=0,copy.height-1 do @@ -83,9 +88,12 @@ do end end image:putImage(copy) + assert(image.version == 1) assert(image:getPixel(0, 0) == rgba(255, 255, 0, 255)) assert(image:getPixel(255, 255) == rgba(0, 0, 0, 255)) app.undo() + assert(image.version == 2) + assert(image.id == imageID) -- the ID doesn't change assert(image:getPixel(0, 0) == rgba(0, 0, 0, 0)) assert(image:getPixel(255, 255) == rgba(0, 0, 0, 0)) end