1 | // Aseprite |
2 | // Copyright (C) 2018 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/script/docobj.h" |
13 | #include "app/script/luacpp.h" |
14 | #include "doc/cel.h" |
15 | #include "doc/cels_range.h" |
16 | #include "doc/layer.h" |
17 | #include "doc/object_ids.h" |
18 | #include "doc/sprite.h" |
19 | |
20 | #include <algorithm> |
21 | #include <iterator> |
22 | |
23 | namespace app { |
24 | namespace script { |
25 | |
26 | using namespace doc; |
27 | |
28 | namespace { |
29 | |
30 | struct CelsObj { |
31 | ObjectIds cels; |
32 | CelsObj(CelsRange& range) { |
33 | for (const Cel* cel : range) |
34 | cels.push_back(cel->id()); |
35 | } |
36 | CelsObj(CelList& list) { |
37 | for (Cel* cel : list) |
38 | cels.push_back(cel->id()); |
39 | } |
40 | CelsObj(const ObjectIds& cels) |
41 | : cels(cels) { |
42 | } |
43 | CelsObj(const CelsObj&) = delete; |
44 | CelsObj& operator=(const CelsObj&) = delete; |
45 | }; |
46 | |
47 | int Cels_gc(lua_State* L) |
48 | { |
49 | get_obj<CelsObj>(L, 1)->~CelsObj(); |
50 | return 0; |
51 | } |
52 | |
53 | int Cels_len(lua_State* L) |
54 | { |
55 | auto obj = get_obj<CelsObj>(L, 1); |
56 | lua_pushinteger(L, obj->cels.size()); |
57 | return 1; |
58 | } |
59 | |
60 | int Cels_index(lua_State* L) |
61 | { |
62 | auto obj = get_obj<CelsObj>(L, 1); |
63 | const int i = lua_tointeger(L, 2); |
64 | if (i >= 1 && i <= obj->cels.size()) |
65 | push_docobj<Cel>(L, obj->cels[i-1]); |
66 | else |
67 | lua_pushnil(L); |
68 | return 1; |
69 | } |
70 | |
71 | const luaL_Reg Cels_methods[] = { |
72 | { "__gc" , Cels_gc }, |
73 | { "__len" , Cels_len }, |
74 | { "__index" , Cels_index }, |
75 | { nullptr, nullptr } |
76 | }; |
77 | |
78 | } // anonymous namespace |
79 | |
80 | DEF_MTNAME(CelsObj); |
81 | |
82 | void register_cels_class(lua_State* L) |
83 | { |
84 | using Cels = CelsObj; |
85 | REG_CLASS(L, Cels); |
86 | } |
87 | |
88 | void push_cels(lua_State* L, Sprite* sprite) |
89 | { |
90 | CelsRange cels = sprite->cels(); |
91 | push_new<CelsObj>(L, cels); |
92 | } |
93 | |
94 | void push_cels(lua_State* L, Layer* layer) |
95 | { |
96 | CelList cels; |
97 | if (layer->isImage()) |
98 | static_cast<LayerImage*>(layer)->getCels(cels); |
99 | push_new<CelsObj>(L, cels); |
100 | } |
101 | |
102 | void push_cels(lua_State* L, const ObjectIds& cels) |
103 | { |
104 | push_new<CelsObj>(L, cels); |
105 | } |
106 | |
107 | } // namespace script |
108 | } // namespace app |
109 | |