1// Aseprite
2// Copyright (C) 2018-2022 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/cmd/set_tag_anidir.h"
13#include "app/cmd/set_tag_color.h"
14#include "app/cmd/set_tag_name.h"
15#include "app/cmd/set_tag_range.h"
16#include "app/script/docobj.h"
17#include "app/script/engine.h"
18#include "app/script/luacpp.h"
19#include "app/script/userdata.h"
20#include "app/tx.h"
21#include "doc/sprite.h"
22#include "doc/tag.h"
23
24namespace app {
25namespace script {
26
27using namespace doc;
28
29namespace {
30
31int Tag_eq(lua_State* L)
32{
33 const auto a = may_get_docobj<Tag>(L, 1);
34 const auto b = may_get_docobj<Tag>(L, 2);
35 lua_pushboolean(L, (!a && !b) || (a && b && a->id() == b->id()));
36 return 1;
37}
38
39int Tag_get_sprite(lua_State* L)
40{
41 auto tag = get_docobj<Tag>(L, 1);
42 push_docobj(L, tag->owner()->sprite());
43 return 1;
44}
45
46int Tag_get_fromFrame(lua_State* L)
47{
48 auto tag = get_docobj<Tag>(L, 1);
49 if (tag->owner()->sprite())
50 push_sprite_frame(L, tag->owner()->sprite(), tag->fromFrame());
51 else
52 lua_pushnil(L);
53 return 1;
54}
55
56int Tag_get_toFrame(lua_State* L)
57{
58 auto tag = get_docobj<Tag>(L, 1);
59 if (tag->owner()->sprite())
60 push_sprite_frame(L, tag->owner()->sprite(), tag->toFrame());
61 else
62 lua_pushnil(L);
63 return 1;
64}
65
66int Tag_get_frames(lua_State* L)
67{
68 auto tag = get_docobj<Tag>(L, 1);
69 lua_pushinteger(L, tag->frames());
70 return 1;
71}
72
73int Tag_get_name(lua_State* L)
74{
75 auto tag = get_docobj<Tag>(L, 1);
76 lua_pushstring(L, tag->name().c_str());
77 return 1;
78}
79
80int Tag_get_aniDir(lua_State* L)
81{
82 auto tag = get_docobj<Tag>(L, 1);
83 lua_pushinteger(L, (int)tag->aniDir());
84 return 1;
85}
86
87int Tag_set_fromFrame(lua_State* L)
88{
89 auto tag = get_docobj<Tag>(L, 1);
90 const auto fromFrame = get_frame_number_from_arg(L, 2);
91 Tx tx;
92 tx(new cmd::SetTagRange(tag, fromFrame,
93 std::max(fromFrame, tag->toFrame())));
94 tx.commit();
95 return 0;
96}
97
98int Tag_set_toFrame(lua_State* L)
99{
100 auto tag = get_docobj<Tag>(L, 1);
101 const auto toFrame = get_frame_number_from_arg(L, 2);
102 Tx tx;
103 tx(new cmd::SetTagRange(tag,
104 std::min(tag->fromFrame(), toFrame),
105 toFrame));
106 tx.commit();
107 return 0;
108}
109
110int Tag_set_name(lua_State* L)
111{
112 auto tag = get_docobj<Tag>(L, 1);
113 const char* name = lua_tostring(L, 2);
114 if (name) {
115 Tx tx;
116 tx(new cmd::SetTagName(tag, name));
117 tx.commit();
118 }
119 return 0;
120}
121
122int Tag_set_aniDir(lua_State* L)
123{
124 auto tag = get_docobj<Tag>(L, 1);
125 const int aniDir = lua_tointeger(L, 2);
126 Tx tx;
127 tx(new cmd::SetTagAniDir(tag, (doc::AniDir)aniDir));
128 tx.commit();
129 return 0;
130}
131
132const luaL_Reg Tag_methods[] = {
133 { "__eq", Tag_eq },
134 { nullptr, nullptr }
135};
136
137const Property Tag_properties[] = {
138 { "sprite", Tag_get_sprite, nullptr },
139 { "fromFrame", Tag_get_fromFrame, Tag_set_fromFrame },
140 { "toFrame", Tag_get_toFrame, Tag_set_toFrame },
141 { "frames", Tag_get_frames, nullptr },
142 { "name", Tag_get_name, Tag_set_name },
143 { "aniDir", Tag_get_aniDir, Tag_set_aniDir },
144 { "color", UserData_get_color<Tag>, UserData_set_color<Tag> },
145 { "data", UserData_get_text<Tag>, UserData_set_text<Tag> },
146 { nullptr, nullptr, nullptr }
147};
148
149} // anonymous namespace
150
151DEF_MTNAME(Tag);
152
153void register_tag_class(lua_State* L)
154{
155 using Tag = doc::Tag;
156 REG_CLASS(L, Tag);
157 REG_CLASS_PROPERTIES(L, Tag);
158}
159
160} // namespace script
161} // namespace app
162