| 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/app.h" |
| 13 | #include "app/context.h" |
| 14 | #include "app/doc.h" |
| 15 | #include "app/script/docobj.h" |
| 16 | #include "app/script/engine.h" |
| 17 | #include "app/script/luacpp.h" |
| 18 | #include "doc/object_ids.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <iterator> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace app { |
| 25 | namespace script { |
| 26 | |
| 27 | using namespace doc; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | struct SpritesObj { |
| 32 | ObjectIds docs; |
| 33 | |
| 34 | SpritesObj(const Docs& docs) { |
| 35 | for (const Doc* doc : docs) |
| 36 | this->docs.push_back(doc->sprite()->id()); |
| 37 | } |
| 38 | |
| 39 | SpritesObj(const SpritesObj&) = delete; |
| 40 | SpritesObj& operator=(const SpritesObj&) = delete; |
| 41 | }; |
| 42 | |
| 43 | int Sprites_gc(lua_State* L) |
| 44 | { |
| 45 | get_obj<SpritesObj>(L, 1)->~SpritesObj(); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int Sprites_len(lua_State* L) |
| 50 | { |
| 51 | auto obj = get_obj<SpritesObj>(L, 1); |
| 52 | lua_pushinteger(L, obj->docs.size()); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | int Sprites_index(lua_State* L) |
| 57 | { |
| 58 | auto obj = get_obj<SpritesObj>(L, 1); |
| 59 | const int i = lua_tonumber(L, 2); |
| 60 | if (i >= 1 && i <= int(obj->docs.size())) |
| 61 | push_docobj<Sprite>(L, obj->docs[i-1]); |
| 62 | else |
| 63 | lua_pushnil(L); |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | const luaL_Reg Sprites_methods[] = { |
| 68 | { "__gc" , Sprites_gc }, |
| 69 | { "__len" , Sprites_len }, |
| 70 | { "__index" , Sprites_index }, |
| 71 | { nullptr, nullptr } |
| 72 | }; |
| 73 | |
| 74 | } // anonymous namespace |
| 75 | |
| 76 | DEF_MTNAME(SpritesObj); |
| 77 | |
| 78 | void register_sprites_class(lua_State* L) |
| 79 | { |
| 80 | using Sprites = SpritesObj; |
| 81 | REG_CLASS(L, Sprites); |
| 82 | } |
| 83 | |
| 84 | void push_sprites(lua_State* L) |
| 85 | { |
| 86 | app::Context* ctx = App::instance()->context(); |
| 87 | push_new<SpritesObj>(L, ctx->documents()); |
| 88 | } |
| 89 | |
| 90 | } // namespace script |
| 91 | } // namespace app |
| 92 | |