diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 7c47d1c07..fdc9c46f9 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -159,6 +159,7 @@ if(ENABLE_SCRIPTING) script/frames_class.cpp script/image_class.cpp script/image_iterator_class.cpp + script/image_spec_class.cpp script/layer_class.cpp script/layers_class.cpp script/luacpp.cpp diff --git a/src/app/script/engine.cpp b/src/app/script/engine.cpp index 0bacdb45e..85af2ca96 100644 --- a/src/app/script/engine.cpp +++ b/src/app/script/engine.cpp @@ -92,6 +92,7 @@ void register_frame_class(lua_State* L); void register_frames_class(lua_State* L); void register_image_class(lua_State* L); void register_image_iterator_class(lua_State* L); +void register_image_spec_class(lua_State* L); void register_layer_class(lua_State* L); void register_layers_class(lua_State* L); void register_palette_class(lua_State* L); @@ -196,6 +197,7 @@ Engine::Engine() register_frames_class(L); register_image_class(L); register_image_iterator_class(L); + register_image_spec_class(L); register_layer_class(L); register_layers_class(L); register_palette_class(L); diff --git a/src/app/script/image_class.cpp b/src/app/script/image_class.cpp index f108aee7c..0ff46392e 100644 --- a/src/app/script/image_class.cpp +++ b/src/app/script/image_class.cpp @@ -36,11 +36,20 @@ struct ImageObj { int Image_new(lua_State* L) { - 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)); + doc::ImageSpec spec(doc::ColorMode::RGB, 1, 1, 0); + if (auto spec2 = may_get_obj(L, 1)) { + spec = *spec2; + } + else { + 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)); + spec.setWidth(w); + spec.setHeight(h); + spec.setColorMode((doc::ColorMode)colorMode); + } + doc::ImageRef image(doc::Image::create(spec)); push_new(L, image, nullptr); return 1; } @@ -138,6 +147,13 @@ int Image_get_colorMode(lua_State* L) return 1; } +int Image_get_spec(lua_State* L) +{ + const auto obj = get_obj(L, 1); + push_obj(L, obj->image->spec()); + return 1; +} + const luaL_Reg Image_methods[] = { { "clone", Image_clone }, { "getPixel", Image_getPixel }, @@ -152,6 +168,7 @@ const Property Image_properties[] = { { "width", Image_get_width, nullptr }, { "height", Image_get_height, nullptr }, { "colorMode", Image_get_colorMode, nullptr }, + { "spec", Image_get_spec, nullptr }, { nullptr, nullptr, nullptr } }; diff --git a/src/app/script/image_spec_class.cpp b/src/app/script/image_spec_class.cpp new file mode 100644 index 000000000..11c5f9569 --- /dev/null +++ b/src/app/script/image_spec_class.cpp @@ -0,0 +1,146 @@ +// Aseprite +// Copyright (C) 2018 David Capello +// +// This program is distributed under the terms of +// the End-User License Agreement for Aseprite. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "app/script/luacpp.h" +#include "doc/image_spec.h" + +namespace app { +namespace script { + +namespace { + +doc::ImageSpec ImageSpec_new(lua_State* L, int index) +{ + doc::ImageSpec spec(doc::ColorMode::RGB, 1, 1, 0); + // Copy other size + if (auto spec2 = may_get_obj(L, index)) { + spec = *spec2; + } + // Convert { width, height } into a Size + else if (lua_istable(L, index)) { + if (lua_getfield(L, index, "colorMode") != LUA_TNIL) + spec.setColorMode((doc::ColorMode)lua_tointeger(L, -1)); + lua_pop(L, 1); + + if (lua_getfield(L, index, "width") != LUA_TNIL) + spec.setWidth(lua_tointeger(L, -1)); + lua_pop(L, 1); + + if (lua_getfield(L, index, "height") != LUA_TNIL) + spec.setHeight(lua_tointeger(L, -1)); + lua_pop(L, 1); + + if (lua_getfield(L, index, "transparentColor") != LUA_TNIL) + spec.setMaskColor(lua_tointeger(L, -1)); + lua_pop(L, 1); + } + return spec; +} + +int ImageSpec_new(lua_State* L) +{ + push_obj(L, ImageSpec_new(L, 1)); + return 1; +} + +int ImageSpec_gc(lua_State* L) +{ + get_obj(L, 1)->~ImageSpec(); + return 0; +} + +int ImageSpec_get_colorMode(lua_State* L) +{ + const auto spec = get_obj(L, 1); + lua_pushinteger(L, (int)spec->colorMode()); + return 1; +} + +int ImageSpec_get_width(lua_State* L) +{ + const auto spec = get_obj(L, 1); + lua_pushinteger(L, spec->width()); + return 1; +} + +int ImageSpec_get_height(lua_State* L) +{ + const auto spec = get_obj(L, 1); + lua_pushinteger(L, spec->height()); + return 1; +} + +int ImageSpec_get_transparentColor(lua_State* L) +{ + const auto spec = get_obj(L, 1); + lua_pushinteger(L, spec->maskColor()); + return 1; +} + +int ImageSpec_set_colorMode(lua_State* L) +{ + auto spec = get_obj(L, 1); + spec->setColorMode((doc::ColorMode)lua_tointeger(L, 2)); + return 0; +} + +int ImageSpec_set_width(lua_State* L) +{ + auto spec = get_obj(L, 1); + spec->setWidth(lua_tointeger(L, 2)); + return 0; +} + +int ImageSpec_set_height(lua_State* L) +{ + auto spec = get_obj(L, 1); + spec->setHeight(lua_tointeger(L, 2)); + return 0; +} + +int ImageSpec_set_transparentColor(lua_State* L) +{ + auto spec = get_obj(L, 1); + spec->setMaskColor(lua_tointeger(L, 2)); + return 0; +} + +const luaL_Reg ImageSpec_methods[] = { + { "__gc", ImageSpec_gc }, + { nullptr, nullptr } +}; + +const Property ImageSpec_properties[] = { + { "colorMode", ImageSpec_get_colorMode, ImageSpec_set_colorMode }, + { "width", ImageSpec_get_width, ImageSpec_set_width }, + { "height", ImageSpec_get_height, ImageSpec_set_height }, + { "transparentColor", ImageSpec_get_transparentColor, ImageSpec_set_transparentColor }, + { nullptr, nullptr, nullptr } +}; + +} // anonymous namespace + +DEF_MTNAME(doc::ImageSpec); + +void register_image_spec_class(lua_State* L) +{ + using doc::ImageSpec; + REG_CLASS(L, ImageSpec); + REG_CLASS_NEW(L, ImageSpec); + REG_CLASS_PROPERTIES(L, ImageSpec); +} + +doc::ImageSpec convert_args_into_image_spec(lua_State* L, int index) +{ + return ImageSpec_new(L, index); +} + +} // namespace script +} // namespace app diff --git a/src/app/script/sprite_class.cpp b/src/app/script/sprite_class.cpp index cf5450ddc..b935d6f69 100644 --- a/src/app/script/sprite_class.cpp +++ b/src/app/script/sprite_class.cpp @@ -42,12 +42,23 @@ namespace { int Sprite_new(lua_State* L) { - const int w = lua_tointeger(L, 1); - const int h = lua_tointeger(L, 2); - const int colorMode = (lua_isnone(L, 3) ? IMAGE_RGB: lua_tointeger(L, 3)); + doc::ImageSpec spec(doc::ColorMode::RGB, 1, 1, 0); + if (auto spec2 = may_get_obj(L, 1)) { + spec = *spec2; + } + else { + const int w = lua_tointeger(L, 1); + const int h = lua_tointeger(L, 2); + const int colorMode = (lua_isnone(L, 3) ? IMAGE_RGB: lua_tointeger(L, 3)); + spec.setWidth(w); + spec.setHeight(h); + spec.setColorMode((doc::ColorMode)colorMode); + } std::unique_ptr sprite( - Sprite::createBasicSprite((doc::PixelFormat)colorMode, w, h, 256)); + Sprite::createBasicSprite( + (doc::PixelFormat)spec.colorMode(), + spec.width(), spec.height(), 256)); std::unique_ptr doc(new Doc(sprite.get())); sprite.release(); @@ -450,6 +461,13 @@ int Sprite_get_colorMode(lua_State* L) return 1; } +int Sprite_get_spec(lua_State* L) +{ + const auto sprite = get_ptr(L, 1); + push_obj(L, sprite->spec()); + return 1; +} + int Sprite_get_selection(lua_State* L) { auto sprite = get_ptr(L, 1); @@ -581,6 +599,7 @@ const Property Sprite_properties[] = { { "width", Sprite_get_width, Sprite_set_width }, { "height", Sprite_get_height, Sprite_set_height }, { "colorMode", Sprite_get_colorMode, nullptr }, + { "spec", Sprite_get_spec, nullptr }, { "selection", Sprite_get_selection, nullptr }, { "frames", Sprite_get_frames, nullptr }, { "palettes", Sprite_get_palettes, nullptr }, diff --git a/src/doc/image_spec.h b/src/doc/image_spec.h index 8f75fe9dd..c73447f5d 100644 --- a/src/doc/image_spec.h +++ b/src/doc/image_spec.h @@ -9,6 +9,7 @@ #pragma once #include "base/debug.h" +#include "doc/color.h" #include "doc/color_mode.h" #include "gfx/rect.h" #include "gfx/size.h"