2018-09-05 03:10:32 +08:00
|
|
|
// Aseprite
|
2018-11-21 05:12:43 +08:00
|
|
|
// Copyright (C) 2018 Igara Studio S.A.
|
2018-09-05 03:10:32 +08:00
|
|
|
// 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
|
|
|
|
|
2018-09-11 02:11:59 +08:00
|
|
|
#include "app/cmd/set_layer_blend_mode.h"
|
2018-09-05 03:10:32 +08:00
|
|
|
#include "app/cmd/set_layer_name.h"
|
|
|
|
#include "app/cmd/set_layer_opacity.h"
|
2018-11-21 05:12:43 +08:00
|
|
|
#include "app/script/docobj.h"
|
2018-09-05 03:10:32 +08:00
|
|
|
#include "app/script/engine.h"
|
|
|
|
#include "app/script/luacpp.h"
|
2018-09-05 20:22:59 +08:00
|
|
|
#include "app/script/userdata.h"
|
2018-09-05 03:10:32 +08:00
|
|
|
#include "app/tx.h"
|
|
|
|
#include "doc/layer.h"
|
|
|
|
#include "doc/sprite.h"
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
namespace script {
|
|
|
|
|
|
|
|
using namespace doc;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-09-05 20:41:44 +08:00
|
|
|
int Layer_eq(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
const auto a = get_docobj<Layer>(L, 1);
|
|
|
|
const auto b = get_docobj<Layer>(L, 2);
|
2018-09-05 20:41:44 +08:00
|
|
|
lua_pushboolean(L, a->id() == b->id());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-05 03:10:32 +08:00
|
|
|
int Layer_get_sprite(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
|
|
|
push_docobj<Sprite>(L, layer->sprite());
|
2018-09-05 03:10:32 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-18 20:36:42 +08:00
|
|
|
int Layer_get_parent(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 20:36:42 +08:00
|
|
|
if (layer->parent() == layer->sprite()->root())
|
2018-11-21 05:12:43 +08:00
|
|
|
push_docobj<Sprite>(L, layer->sprite());
|
2018-09-18 20:36:42 +08:00
|
|
|
else
|
2018-11-21 05:12:43 +08:00
|
|
|
push_docobj<Layer>(L, layer->parent());
|
2018-09-18 20:36:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_previous(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 20:36:42 +08:00
|
|
|
if (auto previous = layer->getPrevious())
|
2018-11-21 05:12:43 +08:00
|
|
|
push_docobj<Layer>(L, previous);
|
2018-09-18 20:36:42 +08:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_next(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 20:36:42 +08:00
|
|
|
if (auto next = layer->getNext())
|
2018-11-21 05:12:43 +08:00
|
|
|
push_docobj<Layer>(L, next);
|
2018-09-18 20:36:42 +08:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-05 03:10:32 +08:00
|
|
|
int Layer_get_name(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-05 03:10:32 +08:00
|
|
|
lua_pushstring(L, layer->name().c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_opacity(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-05 03:10:32 +08:00
|
|
|
if (layer->isImage()) {
|
|
|
|
lua_pushinteger(L, static_cast<LayerImage*>(layer)->opacity());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-11 02:11:59 +08:00
|
|
|
int Layer_get_blendMode(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-11 02:11:59 +08:00
|
|
|
if (layer->isImage()) {
|
|
|
|
lua_pushinteger(L, (int)static_cast<LayerImage*>(layer)->blendMode());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-05 04:30:18 +08:00
|
|
|
int Layer_get_isImage(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-05 04:30:18 +08:00
|
|
|
lua_pushboolean(L, layer->isImage());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_isGroup(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-05 04:30:18 +08:00
|
|
|
lua_pushboolean(L, layer->isGroup());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-07 00:02:50 +08:00
|
|
|
int Layer_get_isTransparent(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-07 00:02:50 +08:00
|
|
|
lua_pushboolean(L, layer->isTransparent());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_isBackground(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-07 00:02:50 +08:00
|
|
|
lua_pushboolean(L, layer->isBackground());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:21:40 +08:00
|
|
|
int Layer_get_isEditable(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
lua_pushboolean(L, layer->isEditable());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_isVisible(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
lua_pushboolean(L, layer->isVisible());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_isContinuous(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
lua_pushboolean(L, layer->isContinuous());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_isCollapsed(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
lua_pushboolean(L, layer->isCollapsed());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_get_isExpanded(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
lua_pushboolean(L, layer->isExpanded());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-12 05:29:15 +08:00
|
|
|
int Layer_get_cels(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-12 05:29:15 +08:00
|
|
|
push_layer_cels(L, layer);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-05 03:10:32 +08:00
|
|
|
int Layer_set_name(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-05 03:10:32 +08:00
|
|
|
const char* name = lua_tostring(L, 2);
|
|
|
|
if (name) {
|
|
|
|
Tx tx;
|
|
|
|
tx(new cmd::SetLayerName(layer, name));
|
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_set_opacity(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-05 03:10:32 +08:00
|
|
|
const int opacity = lua_tointeger(L, 2);
|
|
|
|
if (layer->isImage()) {
|
|
|
|
Tx tx;
|
|
|
|
tx(new cmd::SetLayerOpacity(static_cast<LayerImage*>(layer), opacity));
|
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-11 02:11:59 +08:00
|
|
|
int Layer_set_blendMode(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-11 02:11:59 +08:00
|
|
|
const int blendMode = lua_tointeger(L, 2);
|
|
|
|
if (layer->isImage()) {
|
|
|
|
Tx tx;
|
|
|
|
tx(new cmd::SetLayerBlendMode(static_cast<LayerImage*>(layer),
|
|
|
|
(doc::BlendMode)blendMode));
|
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:21:40 +08:00
|
|
|
int Layer_set_isEditable(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
layer->setEditable(lua_toboolean(L, 2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_set_isVisible(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
layer->setVisible(lua_toboolean(L, 2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_set_isContinuous(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
layer->setContinuous(lua_toboolean(L, 2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_set_isCollapsed(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
layer->setCollapsed(lua_toboolean(L, 2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layer_set_isExpanded(lua_State* L)
|
|
|
|
{
|
2018-11-21 05:12:43 +08:00
|
|
|
auto layer = get_docobj<Layer>(L, 1);
|
2018-09-18 11:21:40 +08:00
|
|
|
layer->setCollapsed(!lua_toboolean(L, 2));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-05 03:10:32 +08:00
|
|
|
const luaL_Reg Layer_methods[] = {
|
2018-09-05 20:41:44 +08:00
|
|
|
{ "__eq", Layer_eq },
|
2018-09-05 03:10:32 +08:00
|
|
|
{ nullptr, nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
const Property Layer_properties[] = {
|
|
|
|
{ "sprite", Layer_get_sprite, nullptr },
|
2018-09-18 20:36:42 +08:00
|
|
|
{ "parent", Layer_get_parent, nullptr },
|
|
|
|
{ "previous", Layer_get_previous, nullptr },
|
|
|
|
{ "next", Layer_get_next, nullptr },
|
2018-09-05 03:10:32 +08:00
|
|
|
{ "name", Layer_get_name, Layer_set_name },
|
|
|
|
{ "opacity", Layer_get_opacity, Layer_set_opacity },
|
2018-09-11 02:11:59 +08:00
|
|
|
{ "blendMode", Layer_get_blendMode, Layer_set_blendMode },
|
2018-09-05 04:30:18 +08:00
|
|
|
{ "isImage", Layer_get_isImage, nullptr },
|
|
|
|
{ "isGroup", Layer_get_isGroup, nullptr },
|
2018-09-07 00:02:50 +08:00
|
|
|
{ "isTransparent", Layer_get_isTransparent, nullptr },
|
|
|
|
{ "isBackground", Layer_get_isBackground, nullptr },
|
2018-09-18 11:21:40 +08:00
|
|
|
{ "isEditable", Layer_get_isEditable, Layer_set_isEditable },
|
|
|
|
{ "isVisible", Layer_get_isVisible, Layer_set_isVisible },
|
|
|
|
{ "isContinuous", Layer_get_isContinuous, Layer_set_isContinuous },
|
|
|
|
{ "isCollapsed", Layer_get_isCollapsed, Layer_set_isCollapsed },
|
|
|
|
{ "isExpanded", Layer_get_isExpanded, Layer_set_isExpanded },
|
2018-09-12 05:29:15 +08:00
|
|
|
{ "cels", Layer_get_cels, nullptr },
|
2018-09-05 20:22:59 +08:00
|
|
|
{ "color", UserData_get_color<Layer>, UserData_set_color<Layer> },
|
|
|
|
{ "data", UserData_get_text<Layer>, UserData_set_text<Layer> },
|
2018-09-05 03:10:32 +08:00
|
|
|
{ nullptr, nullptr, nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
DEF_MTNAME(Layer);
|
|
|
|
|
|
|
|
void register_layer_class(lua_State* L)
|
|
|
|
{
|
|
|
|
using Layer = doc::Layer;
|
|
|
|
REG_CLASS(L, Layer);
|
|
|
|
REG_CLASS_PROPERTIES(L, Layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace script
|
|
|
|
} // namespace app
|