| 1 | // Aseprite |
| 2 | // Copyright (C) 2019-2020 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/tileset.h" |
| 15 | |
| 16 | namespace app { |
| 17 | namespace script { |
| 18 | |
| 19 | using namespace doc; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | int Tileset_eq(lua_State* L) |
| 24 | { |
| 25 | const auto a = get_docobj<Tileset>(L, 1); |
| 26 | const auto b = get_docobj<Tileset>(L, 2); |
| 27 | lua_pushboolean(L, a->id() == b->id()); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | int Tileset_len(lua_State* L) |
| 32 | { |
| 33 | auto tileset = get_docobj<Tileset>(L, 1); |
| 34 | lua_pushinteger(L, tileset->size()); |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | int Tileset_getTile(lua_State* L) |
| 39 | { |
| 40 | auto tileset = get_docobj<Tileset>(L, 1); |
| 41 | tile_index i = lua_tointeger(L, 2); |
| 42 | ImageRef image = tileset->get(i); |
| 43 | if (image) |
| 44 | push_tileset_image(L, tileset, image.get()); |
| 45 | else |
| 46 | lua_pushnil(L); |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | int Tileset_get_name(lua_State* L) |
| 51 | { |
| 52 | auto tileset = get_docobj<Tileset>(L, 1); |
| 53 | lua_pushstring(L, tileset->name().c_str()); |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | int Tileset_set_name(lua_State* L) |
| 58 | { |
| 59 | auto tileset = get_docobj<Tileset>(L, 1); |
| 60 | if (const char* newName = lua_tostring(L, 2)) |
| 61 | tileset->setName(newName); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int Tileset_get_grid(lua_State* L) |
| 66 | { |
| 67 | auto tileset = get_docobj<Tileset>(L, 1); |
| 68 | push_obj(L, tileset->grid()); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | int Tileset_get_baseIndex(lua_State* L) |
| 73 | { |
| 74 | auto tileset = get_docobj<Tileset>(L, 1); |
| 75 | lua_pushinteger(L, tileset->baseIndex()); |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | int Tileset_set_baseIndex(lua_State* L) |
| 80 | { |
| 81 | auto tileset = get_docobj<Tileset>(L, 1); |
| 82 | int i = lua_tointeger(L, 2); |
| 83 | tileset->setBaseIndex(i); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | const luaL_Reg Tileset_methods[] = { |
| 88 | { "__eq" , Tileset_eq }, |
| 89 | { "__len" , Tileset_len }, |
| 90 | { "getTile" , Tileset_getTile }, |
| 91 | // TODO |
| 92 | // { "setTile", Tileset_setTile }, |
| 93 | { nullptr, nullptr } |
| 94 | }; |
| 95 | |
| 96 | const Property Tileset_properties[] = { |
| 97 | { "name" , Tileset_get_name, Tileset_set_name }, |
| 98 | { "grid" , Tileset_get_grid, nullptr }, |
| 99 | { "baseIndex" , Tileset_get_baseIndex, Tileset_set_baseIndex }, |
| 100 | { nullptr, nullptr, nullptr } |
| 101 | }; |
| 102 | |
| 103 | } // anonymous namespace |
| 104 | |
| 105 | DEF_MTNAME(Tileset); |
| 106 | |
| 107 | void register_tileset_class(lua_State* L) |
| 108 | { |
| 109 | using Tileset = doc::Tileset; |
| 110 | REG_CLASS(L, Tileset); |
| 111 | REG_CLASS_PROPERTIES(L, Tileset); |
| 112 | } |
| 113 | |
| 114 | void push_tileset(lua_State* L, Tileset* tileset) |
| 115 | { |
| 116 | push_docobj(L, tileset); |
| 117 | } |
| 118 | |
| 119 | } // namespace script |
| 120 | } // namespace app |
| 121 | |