1// Aseprite
2// Copyright (C) 2019 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/grid.h"
15
16namespace app {
17namespace script {
18
19using namespace doc;
20
21namespace {
22
23doc::Grid Grid_new(lua_State* L, int index)
24{
25 doc::Grid grid;
26 // Copy other size
27 if (auto grid2 = may_get_obj<doc::Grid>(L, index)) {
28 grid = *grid2;
29 }
30 // Convert Rectangle into a Grid
31 else if (lua_istable(L, index)) {
32 gfx::Rect rect = convert_args_into_rect(L, index);
33 doc::Grid grid(rect.size());
34 grid.origin(rect.origin());
35 }
36 return grid;
37}
38
39int Grid_new(lua_State* L)
40{
41 push_obj(L, Grid_new(L, 1));
42 return 1;
43}
44
45int Grid_gc(lua_State* L)
46{
47 get_obj<doc::Grid>(L, 1)->~Grid();
48 return 0;
49}
50
51int Grid_get_tileSize(lua_State* L)
52{
53 auto grid = get_obj<doc::Grid>(L, 1);
54 push_obj(L, grid->tileSize());
55 return 1;
56}
57
58int Grid_get_origin(lua_State* L)
59{
60 auto grid = get_obj<doc::Grid>(L, 1);
61 push_obj(L, grid->origin());
62 return 1;
63}
64
65const luaL_Reg Grid_methods[] = {
66 { "__gc", Grid_gc },
67 { nullptr, nullptr }
68};
69
70const Property Grid_properties[] = {
71 { "tileSize", Grid_get_tileSize, nullptr },
72 { "origin", Grid_get_origin, nullptr },
73 { nullptr, nullptr, nullptr }
74};
75
76} // anonymous namespace
77
78DEF_MTNAME(Grid);
79
80void register_grid_class(lua_State* L)
81{
82 using Grid = doc::Grid;
83 REG_CLASS(L, Grid);
84 REG_CLASS_NEW(L, Grid);
85 REG_CLASS_PROPERTIES(L, Grid);
86}
87
88doc::Grid convert_args_into_grid(lua_State* L, int index)
89{
90 return Grid_new(L, index);
91}
92
93} // namespace script
94} // namespace app
95