1 | // Aseprite |
2 | // Copyright (C) 2018-2022 Igara Studio S.A. |
3 | // Copyright (C) 2018 David Capello |
4 | // |
5 | // This program is distributed under the terms of |
6 | // the End-User License Agreement for Aseprite. |
7 | |
8 | #ifdef HAVE_CONFIG_H |
9 | #include "config.h" |
10 | #endif |
11 | |
12 | #include "app/cmd/set_slice_key.h" |
13 | #include "app/cmd/set_slice_name.h" |
14 | #include "app/script/docobj.h" |
15 | #include "app/script/engine.h" |
16 | #include "app/script/luacpp.h" |
17 | #include "app/script/userdata.h" |
18 | #include "app/tx.h" |
19 | #include "doc/slice.h" |
20 | #include "doc/sprite.h" |
21 | |
22 | namespace app { |
23 | namespace script { |
24 | |
25 | using namespace doc; |
26 | |
27 | namespace { |
28 | |
29 | int Slice_eq(lua_State* L) |
30 | { |
31 | const auto a = may_get_docobj<Slice>(L, 1); |
32 | const auto b = may_get_docobj<Slice>(L, 2); |
33 | lua_pushboolean(L, (!a && !b) || (a && b && a->id() == b->id())); |
34 | return 1; |
35 | } |
36 | |
37 | int Slice_get_sprite(lua_State* L) |
38 | { |
39 | auto slice = get_docobj<Slice>(L, 1); |
40 | push_docobj(L, slice->owner()->sprite()); |
41 | return 1; |
42 | } |
43 | |
44 | int Slice_get_name(lua_State* L) |
45 | { |
46 | auto slice = get_docobj<Slice>(L, 1); |
47 | lua_pushstring(L, slice->name().c_str()); |
48 | return 1; |
49 | } |
50 | |
51 | int Slice_get_bounds(lua_State* L) |
52 | { |
53 | auto slice = get_docobj<Slice>(L, 1); |
54 | if (!slice->empty()) |
55 | push_new<gfx::Rect>(L, slice->begin()->value()->bounds()); |
56 | else |
57 | lua_pushnil(L); |
58 | return 1; |
59 | } |
60 | |
61 | int Slice_get_center(lua_State* L) |
62 | { |
63 | auto slice = get_docobj<Slice>(L, 1); |
64 | if (!slice->empty() && slice->begin()->value()->hasCenter()) |
65 | push_new<gfx::Rect>(L, slice->begin()->value()->center()); |
66 | else |
67 | lua_pushnil(L); |
68 | return 1; |
69 | } |
70 | |
71 | int Slice_get_pivot(lua_State* L) |
72 | { |
73 | auto slice = get_docobj<Slice>(L, 1); |
74 | if (!slice->empty() && slice->begin()->value()->hasPivot()) |
75 | push_new<gfx::Point>(L, slice->begin()->value()->pivot()); |
76 | else |
77 | lua_pushnil(L); |
78 | return 1; |
79 | } |
80 | |
81 | int Slice_set_name(lua_State* L) |
82 | { |
83 | auto slice = get_docobj<Slice>(L, 1); |
84 | const char* name = lua_tostring(L, 2); |
85 | if (name) { |
86 | Tx tx; |
87 | tx(new cmd::SetSliceName(slice, name)); |
88 | tx.commit(); |
89 | } |
90 | return 0; |
91 | } |
92 | |
93 | int Slice_set_bounds(lua_State* L) |
94 | { |
95 | auto slice = get_docobj<Slice>(L, 1); |
96 | gfx::Rect bounds = convert_args_into_rect(L, 2); |
97 | SliceKey key; |
98 | if (const SliceKey* srcKey = slice->getByFrame(0)) |
99 | key = *srcKey; |
100 | key.setBounds(bounds); |
101 | Tx tx; |
102 | tx(new cmd::SetSliceKey(slice, 0, key)); |
103 | tx.commit(); |
104 | return 0; |
105 | } |
106 | |
107 | int Slice_set_center(lua_State* L) |
108 | { |
109 | auto slice = get_docobj<Slice>(L, 1); |
110 | gfx::Rect center = convert_args_into_rect(L, 2); |
111 | SliceKey key; |
112 | if (const SliceKey* srcKey = slice->getByFrame(0)) |
113 | key = *srcKey; |
114 | key.setCenter(center); |
115 | Tx tx; |
116 | tx(new cmd::SetSliceKey(slice, 0, key)); |
117 | tx.commit(); |
118 | return 0; |
119 | } |
120 | |
121 | int Slice_set_pivot(lua_State* L) |
122 | { |
123 | auto slice = get_docobj<Slice>(L, 1); |
124 | gfx::Point pivot = convert_args_into_point(L, 2); |
125 | SliceKey key; |
126 | if (const SliceKey* srcKey = slice->getByFrame(0)) |
127 | key = *srcKey; |
128 | key.setPivot(pivot); |
129 | Tx tx; |
130 | tx(new cmd::SetSliceKey(slice, 0, key)); |
131 | tx.commit(); |
132 | return 0; |
133 | } |
134 | |
135 | const luaL_Reg Slice_methods[] = { |
136 | { "__eq" , Slice_eq }, |
137 | { nullptr, nullptr } |
138 | }; |
139 | |
140 | const Property Slice_properties[] = { |
141 | { "sprite" , Slice_get_sprite, nullptr }, |
142 | { "name" , Slice_get_name, Slice_set_name }, |
143 | { "bounds" , Slice_get_bounds, Slice_set_bounds }, |
144 | { "center" , Slice_get_center, Slice_set_center }, |
145 | { "pivot" , Slice_get_pivot, Slice_set_pivot }, |
146 | { "color" , UserData_get_color<Slice>, UserData_set_color<Slice> }, |
147 | { "data" , UserData_get_text<Slice>, UserData_set_text<Slice> }, |
148 | { nullptr, nullptr, nullptr } |
149 | }; |
150 | |
151 | } // anonymous namespace |
152 | |
153 | DEF_MTNAME(Slice); |
154 | |
155 | void register_slice_class(lua_State* L) |
156 | { |
157 | using doc::Slice; |
158 | REG_CLASS(L, Slice); |
159 | REG_CLASS_PROPERTIES(L, Slice); |
160 | } |
161 | |
162 | } // namespace script |
163 | } // namespace app |
164 | |