From 4b2d98506fd661b247e66af79183a5d1911dca5d Mon Sep 17 00:00:00 2001 From: Christian Kaiser Date: Fri, 6 Dec 2024 17:57:02 -0300 Subject: [PATCH] Renamed "mask" to "selection" for Lua consistency, fixes --- src/app/script/app_clipboard_object.cpp | 22 ++++++++++------------ src/app/util/clipboard_native.cpp | 13 ++++++++----- src/doc/tileset_io.cpp | 4 ++-- tests/scripts/app_clipboard.lua | 6 +++--- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/app/script/app_clipboard_object.cpp b/src/app/script/app_clipboard_object.cpp index a15906927..8f24919aa 100644 --- a/src/app/script/app_clipboard_object.cpp +++ b/src/app/script/app_clipboard_object.cpp @@ -74,7 +74,7 @@ int Clipboard_get_image(lua_State* L) return 1; } - if (!result) // TODO: Can we have a false "nil" value without an error? + if (!result) return luaL_error(L, "failed to get image from clipboard"); push_image(L, image); @@ -102,13 +102,11 @@ int Clipboard_set_image(lua_State* L) if (!image) return luaL_error(L, "invalid image"); - const bool result = app::Clipboard::instance()->setNativeBitmap( - image, - nullptr, - get_current_palette(), - nullptr, - image->maskColor() // TODO: Unsure if this is sufficient. - ); + const bool result = app::Clipboard::instance()->setNativeBitmap(image, + nullptr, + get_current_palette(), + nullptr, + image->maskColor()); if (!result) return luaL_error(L, "failed to set image to clipboard"); @@ -139,7 +137,7 @@ int Clipboard_get_content(lua_State* L) app::Clipboard::instance()->getNativeBitmap(&image, &mask, &palette, &tileset); std::string text; - const bool clipResult = clip::get_text(text); + const bool clipResult = !bitmapResult ? clip::get_text(text) : false; lua_createtable(L, 0, 5); @@ -153,7 +151,7 @@ int Clipboard_get_content(lua_State* L) push_docobj(L, mask); else lua_pushnil(L); - lua_setfield(L, -2, "mask"); + lua_setfield(L, -2, "selection"); if (bitmapResult && palette) push_palette(L, palette); @@ -197,11 +195,11 @@ int Clipboard_set_content(lua_State* L) } lua_pop(L, 1); - type = lua_getfield(L, 2, "mask"); + type = lua_getfield(L, 2, "selection"); if (type != LUA_TNIL) { mask = get_mask_from_arg(L, -1); if (!mask) - return luaL_error(L, "invalid mask provided"); + return luaL_error(L, "invalid selection provided"); } lua_pop(L, 1); diff --git a/src/app/util/clipboard_native.cpp b/src/app/util/clipboard_native.cpp index e85c2cd42..3e7661650 100644 --- a/src/app/util/clipboard_native.cpp +++ b/src/app/util/clipboard_native.cpp @@ -153,12 +153,12 @@ bool Clipboard::setNativeBitmap(const doc::Image* image, switch (image->pixelFormat()) { case doc::IMAGE_RGB: { // We use the RGB image data directly - clip::image img(image->getPixelAddress(0, 0), spec); + const clip::image img(image->getPixelAddress(0, 0), spec); l.set_image(img); break; } case doc::IMAGE_GRAYSCALE: { - clip::image img(spec); + const clip::image img(spec); const doc::LockImageBits bits(image); auto it = bits.begin(); uint32_t* dst = (uint32_t*)img.data(); @@ -175,7 +175,10 @@ bool Clipboard::setNativeBitmap(const doc::Image* image, break; } case doc::IMAGE_INDEXED: { - clip::image img(spec); + if (!palette) + return false; + + const clip::image img(spec); const doc::LockImageBits bits(image); auto it = bits.begin(); uint32_t* dst = (uint32_t*)img.data(); @@ -193,7 +196,7 @@ bool Clipboard::setNativeBitmap(const doc::Image* image, l.set_image(img); break; } - default: TRACE("Unsupported pixelFormat: %d\n", image->pixelFormat()); return false; + default: return false; } return true; @@ -231,7 +234,7 @@ bool Clipboard::getNativeBitmap(doc::Image** image, if (bits & 4) *palette = doc::read_palette(is); if (bits & 8) - *tileset = doc::read_tileset(is, nullptr); + *tileset = doc::read_tileset(is, nullptr, false); if (image) return true; } diff --git a/src/doc/tileset_io.cpp b/src/doc/tileset_io.cpp index 3b1cfc957..b29986445 100644 --- a/src/doc/tileset_io.cpp +++ b/src/doc/tileset_io.cpp @@ -63,11 +63,11 @@ Tileset* read_tileset(std::istream& is, const tileset_index ntiles = read32(is); const Grid grid = read_grid(is); auto* tileset = new Tileset(sprite, grid, sprite ? ntiles : 0); - if (sprite && setId) + if (setId) tileset->setId(id); for (tileset_index ti = 0; ti < ntiles; ++ti) { - const ImageRef image(read_image(is, sprite ? setId : false)); + const ImageRef image(read_image(is, setId)); if (sprite) tileset->set(ti, image); diff --git a/tests/scripts/app_clipboard.lua b/tests/scripts/app_clipboard.lua index 31294ff49..93a469832 100644 --- a/tests/scripts/app_clipboard.lua +++ b/tests/scripts/app_clipboard.lua @@ -67,7 +67,7 @@ do -- Image copying and access end do -- Image copying and access (with .content) - -- TODO: Using the previous image for now to avoid the IMAGE_TILEMAP format not being supported. + -- Using another image to avoid the IMAGE_TILEMAP format not being supported. local beforeSprite = Sprite{ fromFile="sprites/abcd.aseprite" } local imageBefore = app.image:clone() @@ -87,7 +87,7 @@ do -- Image copying and access (with .content) app.clipboard.content = { image = imageBefore, palettte = sprite.palettes[1], - mask = sprite.selection, + selection = sprite.selection, tileset = app.layer.tileset } @@ -100,5 +100,5 @@ do -- Image copying and access (with .content) assert(imageBefore:isEqual(c.image)) expect_eq(sprite.palettes[1]:getColor(1).rgbaPixel, c.palette:getColor(1).rgbaPixel) assert(app.layer.tileset:tile(0).image:isEqual(c.tileset:tile(0).image)) - expect_eq(sprite.selection, c.mask) + expect_eq(sprite.selection, c.selection) end