1 | // Aseprite |
2 | // Copyright (C) 2018-2021 Igara Studio S.A. |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include "app/script/docobj.h" |
12 | #include "app/script/engine.h" |
13 | #include "app/script/luacpp.h" |
14 | #include "doc/cel.h" |
15 | #include "doc/object_ids.h" |
16 | |
17 | namespace app { |
18 | namespace script { |
19 | |
20 | using namespace doc; |
21 | |
22 | namespace { |
23 | |
24 | struct ImagesObj { |
25 | ObjectIds cels; |
26 | |
27 | ImagesObj(const ObjectIds& cels) |
28 | : cels(cels) { |
29 | } |
30 | |
31 | ImagesObj(const ImagesObj&) = delete; |
32 | ImagesObj& operator=(const ImagesObj&) = delete; |
33 | }; |
34 | |
35 | int Images_gc(lua_State* L) |
36 | { |
37 | get_obj<ImagesObj>(L, 1)->~ImagesObj(); |
38 | return 0; |
39 | } |
40 | |
41 | int Images_len(lua_State* L) |
42 | { |
43 | auto obj = get_obj<ImagesObj>(L, 1); |
44 | lua_pushinteger(L, obj->cels.size()); |
45 | return 1; |
46 | } |
47 | |
48 | int Images_index(lua_State* L) |
49 | { |
50 | auto obj = get_obj<ImagesObj>(L, 1); |
51 | const int i = lua_tointeger(L, 2); |
52 | if (i >= 1 && i <= obj->cels.size()) { |
53 | if (auto cel = doc::get<doc::Cel>(obj->cels[i-1])) { |
54 | push_cel_image(L, cel); |
55 | return 1; |
56 | } |
57 | } |
58 | lua_pushnil(L); |
59 | return 1; |
60 | } |
61 | |
62 | const luaL_Reg Images_methods[] = { |
63 | { "__gc" , Images_gc }, |
64 | { "__len" , Images_len }, |
65 | { "__index" , Images_index }, |
66 | { nullptr, nullptr } |
67 | }; |
68 | |
69 | } // anonymous namespace |
70 | |
71 | DEF_MTNAME(ImagesObj); |
72 | |
73 | void register_images_class(lua_State* L) |
74 | { |
75 | using Images = ImagesObj; |
76 | REG_CLASS(L, Images); |
77 | } |
78 | |
79 | void push_cel_images(lua_State* L, const ObjectIds& cels) |
80 | { |
81 | push_new<ImagesObj>(L, cels); |
82 | } |
83 | |
84 | } // namespace script |
85 | } // namespace app |
86 | |