Use a ImageObj with a ImageRef for scripts

Also added __gc/new/clone methods, and colorMode property.
This commit is contained in:
David Capello 2018-08-30 20:47:11 -03:00
parent 514180abfc
commit 17366056b6
4 changed files with 69 additions and 16 deletions

View File

@ -108,8 +108,8 @@ int App_get_activeImage(lua_State* L)
{ {
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
Site site = ctx->activeSite(); Site site = ctx->activeSite();
if (site.image()) if (site.cel())
push_ptr(L, site.image()); push_cel_image(L, site.cel());
else else
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;

View File

@ -22,7 +22,7 @@
struct lua_State; struct lua_State;
namespace doc { namespace doc {
class Image; class Cel;
class Sprite; class Sprite;
} }
@ -61,6 +61,7 @@ namespace script {
}; };
void push_sprite_selection(lua_State* L, doc::Sprite* sprite); 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::Point convert_args_into_point(lua_State* L, int index);
gfx::Rect convert_args_into_rect(lua_State* L, int index); gfx::Rect convert_args_into_rect(lua_State* L, int index);

View File

@ -9,77 +9,129 @@
#endif #endif
#include "app/script/luacpp.h" #include "app/script/luacpp.h"
#include "doc/cel.h"
#include "doc/image.h" #include "doc/image.h"
#include "doc/image_ref.h"
#include "doc/primitives.h" #include "doc/primitives.h"
#include "doc/sprite.h"
namespace app { namespace app {
namespace script { namespace script {
namespace { 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) 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; return 1;
} }
int Image_clone(lua_State* L)
{
auto obj = get_obj<ImageObj>(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<ImageObj>(L, 1)->~ImageObj();
return 0;
}
int Image_putPixel(lua_State* L) int Image_putPixel(lua_State* L)
{ {
auto image = get_ptr<doc::Image>(L, 1); auto obj = get_obj<ImageObj>(L, 1);
const int x = lua_tointeger(L, 2); const int x = lua_tointeger(L, 2);
const int y = lua_tointeger(L, 3); const int y = lua_tointeger(L, 3);
const doc::color_t color = lua_tointeger(L, 4); 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; return 0;
} }
int Image_getPixel(lua_State* L) int Image_getPixel(lua_State* L)
{ {
const auto image = get_ptr<doc::Image>(L, 1); const auto obj = get_obj<ImageObj>(L, 1);
const int x = lua_tointeger(L, 2); const int x = lua_tointeger(L, 2);
const int y = lua_tointeger(L, 3); 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); lua_pushinteger(L, color);
return 1; return 1;
} }
int Image_get_width(lua_State* L) int Image_get_width(lua_State* L)
{ {
const auto image = get_ptr<doc::Image>(L, 1); const auto obj = get_obj<ImageObj>(L, 1);
lua_pushinteger(L, image->width()); lua_pushinteger(L, obj->image->width());
return 1; return 1;
} }
int Image_get_height(lua_State* L) int Image_get_height(lua_State* L)
{ {
const auto image = get_ptr<doc::Image>(L, 1); const auto obj = get_obj<ImageObj>(L, 1);
lua_pushinteger(L, image->height()); lua_pushinteger(L, obj->image->height());
return 1;
}
int Image_get_colorMode(lua_State* L)
{
const auto obj = get_obj<ImageObj>(L, 1);
lua_pushinteger(L, obj->image->pixelFormat());
return 1; return 1;
} }
const luaL_Reg Image_methods[] = { const luaL_Reg Image_methods[] = {
{ "clone", Image_clone },
{ "getPixel", Image_getPixel }, { "getPixel", Image_getPixel },
{ "putPixel", Image_putPixel }, { "putPixel", Image_putPixel },
{ "__gc", Image_gc },
{ nullptr, nullptr } { nullptr, nullptr }
}; };
const Property Image_properties[] = { const Property Image_properties[] = {
{ "width", Image_get_width, nullptr }, { "width", Image_get_width, nullptr },
{ "height", Image_get_height, nullptr }, { "height", Image_get_height, nullptr },
{ "colorMode", Image_get_colorMode, nullptr },
{ nullptr, nullptr, nullptr } { nullptr, nullptr, nullptr }
}; };
} // anonymous namespace } // anonymous namespace
DEF_MTNAME(doc::Image); DEF_MTNAME(ImageObj);
void register_image_class(lua_State* L) void register_image_class(lua_State* L)
{ {
using doc::Image; using Image = ImageObj;
REG_CLASS(L, Image); REG_CLASS(L, Image);
REG_CLASS_NEW(L, Image); REG_CLASS_NEW(L, Image);
REG_CLASS_PROPERTIES(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 script
} // namespace app } // namespace app

View File

@ -30,8 +30,8 @@ int Site_get_sprite(lua_State* L)
int Site_get_image(lua_State* L) int Site_get_image(lua_State* L)
{ {
auto site = get_obj<Site>(L, 1); auto site = get_obj<Site>(L, 1);
if (site->image()) if (site->cel())
push_ptr(L, site->image()); push_cel_image(L, site->cel());
else else
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;