lua: Add functions to modify the selection (Sprite.selection and Selection.origin)

This commit is contained in:
David Capello 2019-02-15 17:19:23 -03:00
parent 7594ebf25b
commit 798d0e9a08
3 changed files with 61 additions and 2 deletions

View File

@ -34,6 +34,7 @@ namespace doc {
class FrameTag;
class Image;
class Layer;
class Mask;
class Palette;
class Sprite;
class WithUserData;
@ -134,6 +135,7 @@ namespace app {
doc::Image* get_image_from_arg(lua_State* L, int index);
doc::Cel* get_image_cel_from_arg(lua_State* L, int index);
doc::frame_t get_frame_number_from_arg(lua_State* L, int index);
const doc::Mask* get_mask_from_arg(lua_State* L, int index);
} // namespace script
} // namespace app

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2015-2018 David Capello
//
// This program is distributed under the terms of
@ -12,6 +12,7 @@
#include "app/cmd/deselect_mask.h"
#include "app/cmd/set_mask.h"
#include "app/doc.h"
#include "app/doc_api.h"
#include "app/script/docobj.h"
#include "app/script/engine.h"
#include "app/script/luacpp.h"
@ -261,6 +262,44 @@ int Selection_get_bounds(lua_State* L)
return 1;
}
int Selection_get_origin(lua_State* L)
{
const auto obj = get_obj<SelectionObj>(L, 1);
if (auto sprite = obj->sprite(L)) {
Doc* doc = static_cast<Doc*>(sprite->document());
if (doc->isMaskVisible()) {
push_obj(L, doc->mask()->bounds().origin());
}
else {
push_new<gfx::Point>(L, 0, 0);
}
}
else {
const auto mask = obj->mask(L);
push_obj(L, mask->bounds().origin());
}
return 1;
}
int Selection_set_origin(lua_State* L)
{
auto obj = get_obj<SelectionObj>(L, 1);
const auto pt = convert_args_into_point(L, 2);
if (auto sprite = obj->sprite(L)) {
Doc* doc = static_cast<Doc*>(sprite->document());
if (doc->isMaskVisible()) {
Tx tx;
doc->getApi(tx).setMaskPosition(pt.x, pt.y);
tx.commit();
}
}
else {
const auto mask = obj->mask(L);
mask->setOrigin(pt.x, pt.y);
}
return 0;
}
int Selection_get_isEmpty(lua_State* L)
{
const auto obj = get_obj<SelectionObj>(L, 1);
@ -284,6 +323,7 @@ const luaL_Reg Selection_methods[] = {
const Property Selection_properties[] = {
{ "bounds", Selection_get_bounds, nullptr },
{ "origin", Selection_get_origin, Selection_set_origin },
{ "isEmpty", Selection_get_isEmpty, nullptr },
{ nullptr, nullptr, nullptr }
};
@ -306,5 +346,10 @@ void push_sprite_selection(lua_State* L, Sprite* sprite)
push_new<SelectionObj>(L, nullptr, sprite);
}
const doc::Mask* get_mask_from_arg(lua_State* L, int index)
{
return get_obj<SelectionObj>(L, index)->mask(L);
}
} // namespace script
} // namespace app

View File

@ -17,6 +17,7 @@
#include "app/cmd/remove_frame_tag.h"
#include "app/cmd/remove_layer.h"
#include "app/cmd/remove_slice.h"
#include "app/cmd/set_mask.h"
#include "app/cmd/set_sprite_size.h"
#include "app/cmd/set_transparent_color.h"
#include "app/color_spaces.h"
@ -639,6 +640,17 @@ int Sprite_set_height(lua_State* L)
return 0;
}
int Sprite_set_selection(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
const auto mask = get_mask_from_arg(L, 2);
Doc* doc = static_cast<Doc*>(sprite->document());
Tx tx;
tx(new cmd::SetMask(doc, mask));
tx.commit();
return 0;
}
int Sprite_get_bounds(lua_State* L)
{
const auto sprite = get_docobj<Sprite>(L, 1);
@ -684,7 +696,7 @@ const Property Sprite_properties[] = {
{ "colorMode", Sprite_get_colorMode, nullptr },
{ "colorSpace", Sprite_get_colorSpace, Sprite_assignColorSpace },
{ "spec", Sprite_get_spec, nullptr },
{ "selection", Sprite_get_selection, nullptr },
{ "selection", Sprite_get_selection, Sprite_set_selection },
{ "frames", Sprite_get_frames, nullptr },
{ "palettes", Sprite_get_palettes, nullptr },
{ "layers", Sprite_get_layers, nullptr },