From 17366056b68935269c14c397d321f1e052d6d780 Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 30 Aug 2018 20:47:11 -0300 Subject: [PATCH] Use a ImageObj with a ImageRef for scripts Also added __gc/new/clone methods, and colorMode property. --- src/app/script/app_object.cpp | 4 +- src/app/script/engine.h | 3 +- src/app/script/image_class.cpp | 74 +++++++++++++++++++++++++++++----- src/app/script/site_class.cpp | 4 +- 4 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/app/script/app_object.cpp b/src/app/script/app_object.cpp index 47c84cc23..0d6599019 100644 --- a/src/app/script/app_object.cpp +++ b/src/app/script/app_object.cpp @@ -108,8 +108,8 @@ int App_get_activeImage(lua_State* L) { app::Context* ctx = App::instance()->context(); Site site = ctx->activeSite(); - if (site.image()) - push_ptr(L, site.image()); + if (site.cel()) + push_cel_image(L, site.cel()); else lua_pushnil(L); return 1; diff --git a/src/app/script/engine.h b/src/app/script/engine.h index 17747a1e3..d236bb613 100644 --- a/src/app/script/engine.h +++ b/src/app/script/engine.h @@ -22,7 +22,7 @@ struct lua_State; namespace doc { - class Image; + class Cel; class Sprite; } @@ -61,6 +61,7 @@ namespace script { }; void push_sprite_selection(lua_State* L, doc::Sprite* sprite); + void push_cel_image(lua_State* L, doc::Cel* cel); gfx::Point convert_args_into_point(lua_State* L, int index); gfx::Rect convert_args_into_rect(lua_State* L, int index); diff --git a/src/app/script/image_class.cpp b/src/app/script/image_class.cpp index 1ab2e674b..63dee11c7 100644 --- a/src/app/script/image_class.cpp +++ b/src/app/script/image_class.cpp @@ -9,77 +9,129 @@ #endif #include "app/script/luacpp.h" +#include "doc/cel.h" #include "doc/image.h" +#include "doc/image_ref.h" #include "doc/primitives.h" +#include "doc/sprite.h" namespace app { namespace script { namespace { +struct ImageObj { + doc::ImageRef image; + doc::Sprite* sprite; + ImageObj(const doc::ImageRef& image, doc::Sprite* sprite) + : image(image) + , sprite(sprite) { + } + ImageObj(ImageObj&& that) { + std::swap(image, that.image); + std::swap(sprite, that.sprite); + } + ImageObj(const ImageObj&) = delete; + ImageObj& operator=(const ImageObj&) = delete; +}; + int Image_new(lua_State* L) { - lua_pushnil(L); // TODO + const int w = lua_tointeger(L, 1); + const int h = lua_tointeger(L, 2); + const int colorMode = (lua_isnone(L, 3) ? doc::IMAGE_RGB: + lua_tointeger(L, 3)); + doc::ImageRef image(doc::Image::create((doc::PixelFormat)colorMode, w, h)); + push_obj(L, ImageObj(image, nullptr)); return 1; } +int Image_clone(lua_State* L) +{ + auto obj = get_obj(L, 1); + doc::ImageRef cloned(doc::Image::createCopy(obj->image.get())); + push_obj(L, ImageObj(cloned, nullptr)); + return 1; +} + +int Image_gc(lua_State* L) +{ + get_obj(L, 1)->~ImageObj(); + return 0; +} + int Image_putPixel(lua_State* L) { - auto image = get_ptr(L, 1); + auto obj = get_obj(L, 1); const int x = lua_tointeger(L, 2); const int y = lua_tointeger(L, 3); const doc::color_t color = lua_tointeger(L, 4); - doc::put_pixel(image, x, y, color); + doc::put_pixel(obj->image.get(), x, y, color); return 0; } int Image_getPixel(lua_State* L) { - const auto image = get_ptr(L, 1); + const auto obj = get_obj(L, 1); const int x = lua_tointeger(L, 2); const int y = lua_tointeger(L, 3); - const doc::color_t color = doc::get_pixel(image, x, y); + const doc::color_t color = doc::get_pixel(obj->image.get(), x, y); lua_pushinteger(L, color); return 1; } int Image_get_width(lua_State* L) { - const auto image = get_ptr(L, 1); - lua_pushinteger(L, image->width()); + const auto obj = get_obj(L, 1); + lua_pushinteger(L, obj->image->width()); return 1; } int Image_get_height(lua_State* L) { - const auto image = get_ptr(L, 1); - lua_pushinteger(L, image->height()); + const auto obj = get_obj(L, 1); + lua_pushinteger(L, obj->image->height()); + return 1; +} + +int Image_get_colorMode(lua_State* L) +{ + const auto obj = get_obj(L, 1); + lua_pushinteger(L, obj->image->pixelFormat()); return 1; } const luaL_Reg Image_methods[] = { + { "clone", Image_clone }, { "getPixel", Image_getPixel }, { "putPixel", Image_putPixel }, + { "__gc", Image_gc }, { nullptr, nullptr } }; const Property Image_properties[] = { { "width", Image_get_width, nullptr }, { "height", Image_get_height, nullptr }, + { "colorMode", Image_get_colorMode, nullptr }, { nullptr, nullptr, nullptr } }; } // anonymous namespace -DEF_MTNAME(doc::Image); +DEF_MTNAME(ImageObj); void register_image_class(lua_State* L) { - using doc::Image; + using Image = ImageObj; REG_CLASS(L, Image); REG_CLASS_NEW(L, Image); REG_CLASS_PROPERTIES(L, Image); } +void push_cel_image(lua_State* L, doc::Cel* cel) +{ + push_obj(L, ImageObj(cel->imageRef(), cel->sprite())); +} + } // namespace script } // namespace app diff --git a/src/app/script/site_class.cpp b/src/app/script/site_class.cpp index 4711f9293..18884e02e 100644 --- a/src/app/script/site_class.cpp +++ b/src/app/script/site_class.cpp @@ -30,8 +30,8 @@ int Site_get_sprite(lua_State* L) int Site_get_image(lua_State* L) { auto site = get_obj(L, 1); - if (site->image()) - push_ptr(L, site->image()); + if (site->cel()) + push_cel_image(L, site->cel()); else lua_pushnil(L); return 1;