diff --git a/src/app/script/image_class.cpp b/src/app/script/image_class.cpp index f9ab0b6f5..3de17f352 100644 --- a/src/app/script/image_class.cpp +++ b/src/app/script/image_class.cpp @@ -26,6 +26,7 @@ #include "app/util/autocrop.h" #include "app/util/resize_image.h" #include "base/fs.h" +#include "doc/algorithm/flip_image.h" #include "doc/algorithm/flip_type.h" #include "doc/algorithm/shrink_bounds.h" #include "doc/cel.h" @@ -585,12 +586,17 @@ int Image_flip(lua_State* L) auto obj = get_obj(L, 1); doc::Image* img = obj->image(L); doc::algorithm::FlipType flipType = doc::algorithm::FlipType::FlipHorizontal; - if (lua_isinteger(L, 2) && lua_tointeger(L, 2) > 0) - flipType = doc::algorithm::FlipType::FlipVertical; + if (lua_isinteger(L, 2)) + flipType = (doc::algorithm::FlipType)lua_tointeger(L, 2); - Tx tx; - tx(new cmd::FlipImage(img, img->bounds(), flipType)); - tx.commit(); + if (obj->cel(L) == nullptr) { + doc::algorithm::flip_image(img, img->bounds(), flipType); + } + else { + Tx tx; + tx(new cmd::FlipImage(img, img->bounds(), flipType)); + tx.commit(); + } return 0; } diff --git a/tests/scripts/image.lua b/tests/scripts/image.lua index 0ab091517..fdf6c659a 100644 --- a/tests/scripts/image.lua +++ b/tests/scripts/image.lua @@ -405,8 +405,7 @@ do end -- Tests for Image:flip() -do - local img = Image(3, 3) +function test_image_flip(img) local r = Color(255, 0, 0).rgbaPixel local g = Color(0, 255, 0).rgbaPixel img:clear(0) @@ -420,6 +419,10 @@ do expect_img(img, { 0, 0, g, 0, r, 0, r, 0, 0 }) + + -- Without sprite, don't test undo + if not app.sprite then return end + app.undo() expect_img(img, { g, 0, 0, 0, r, 0, @@ -442,4 +445,9 @@ do expect_img(img, { g, 0, 0, 0, r, 0, 0, 0, r }) -end \ No newline at end of file +end + +local spr = Sprite(3, 3) -- Test with sprite (with transactions & undo/redo) +test_image_flip(app.image) +app.sprite = nil -- Test without sprite (without transactions) +test_image_flip(Image(3, 3))