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/engine.h" |
14 | #include "app/script/luacpp.h" |
15 | #include "doc/slice.h" |
16 | #include "doc/sprite.h" |
17 | |
18 | namespace app { |
19 | namespace script { |
20 | |
21 | namespace { |
22 | |
23 | struct SlicesObj { |
24 | ObjectId spriteId; |
25 | SlicesObj(Sprite* sprite) |
26 | : spriteId(sprite->id()) { |
27 | } |
28 | SlicesObj(const SlicesObj&) = delete; |
29 | SlicesObj& operator=(const SlicesObj&) = delete; |
30 | |
31 | Sprite* sprite(lua_State* L) { return check_docobj(L, doc::get<Sprite>(spriteId)); } |
32 | }; |
33 | |
34 | int Slices_gc(lua_State* L) |
35 | { |
36 | get_obj<SlicesObj>(L, 1)->~SlicesObj(); |
37 | return 0; |
38 | } |
39 | |
40 | int Slices_len(lua_State* L) |
41 | { |
42 | auto obj = get_obj<SlicesObj>(L, 1); |
43 | lua_pushinteger(L, obj->sprite(L)->slices().size()); |
44 | return 1; |
45 | } |
46 | |
47 | int Slices_index(lua_State* L) |
48 | { |
49 | auto obj = get_obj<SlicesObj>(L, 1); |
50 | auto& slices = obj->sprite(L)->slices(); |
51 | const int i = lua_tonumber(L, 2); |
52 | if (i >= 1 && i <= int(slices.size())) |
53 | push_docobj(L, *(slices.begin()+i-1)); |
54 | else |
55 | lua_pushnil(L); |
56 | return 1; |
57 | } |
58 | |
59 | const luaL_Reg Slices_methods[] = { |
60 | { "__gc" , Slices_gc }, |
61 | { "__len" , Slices_len }, |
62 | { "__index" , Slices_index }, |
63 | { nullptr, nullptr } |
64 | }; |
65 | |
66 | } // anonymous namespace |
67 | |
68 | DEF_MTNAME(SlicesObj); |
69 | |
70 | void register_slices_class(lua_State* L) |
71 | { |
72 | using Slices = SlicesObj; |
73 | REG_CLASS(L, Slices); |
74 | } |
75 | |
76 | void push_sprite_slices(lua_State* L, Sprite* sprite) |
77 | { |
78 | push_new<SlicesObj>(L, sprite); |
79 | } |
80 | |
81 | } // namespace script |
82 | } // namespace app |
83 | |