1 | // Aseprite |
2 | // Copyright (C) 2018-2020 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 | #ifndef APP_SCRIPT_USERDATA_HELPER_H_INCLUDED |
9 | #define APP_SCRIPT_USERDATA_HELPER_H_INCLUDED |
10 | #pragma once |
11 | |
12 | #include "app/cmd/set_user_data.h" |
13 | #include "app/color.h" |
14 | #include "app/color_utils.h" |
15 | #include "app/script/luacpp.h" |
16 | #include "app/tx.h" |
17 | #include "doc/cel.h" |
18 | #include "doc/with_user_data.h" |
19 | |
20 | namespace app { |
21 | namespace script { |
22 | |
23 | template<typename T> |
24 | inline doc::WithUserData* get_WithUserData(T* obj) { |
25 | return static_cast<doc::WithUserData*>(obj); |
26 | } |
27 | |
28 | template<> |
29 | inline doc::WithUserData* get_WithUserData<doc::Cel>(doc::Cel* obj) { |
30 | return obj->data(); |
31 | } |
32 | |
33 | template<typename T> |
34 | int UserData_get_text(lua_State* L) { |
35 | auto obj = get_docobj<T>(L, 1); |
36 | lua_pushstring(L, get_WithUserData<T>(obj)->userData().text().c_str()); |
37 | return 1; |
38 | } |
39 | |
40 | template<typename T> |
41 | int UserData_get_color(lua_State* L) { |
42 | auto obj = get_docobj<T>(L, 1); |
43 | doc::color_t docColor = get_WithUserData<T>(obj)->userData().color(); |
44 | app::Color appColor = app::Color::fromRgb(doc::rgba_getr(docColor), |
45 | doc::rgba_getg(docColor), |
46 | doc::rgba_getb(docColor), |
47 | doc::rgba_geta(docColor)); |
48 | if (appColor.getAlpha() == 0) |
49 | appColor = app::Color::fromMask(); |
50 | push_obj<app::Color>(L, appColor); |
51 | return 1; |
52 | } |
53 | |
54 | template<typename T> |
55 | int UserData_set_text(lua_State* L) { |
56 | auto obj = get_docobj<T>(L, 1); |
57 | auto spr = obj->sprite(); |
58 | const char* text = lua_tostring(L, 2); |
59 | auto wud = get_WithUserData<T>(obj); |
60 | UserData ud = wud->userData(); |
61 | ud.setText(text); |
62 | if (spr) { |
63 | Tx tx; |
64 | tx(new cmd::SetUserData(wud, ud, static_cast<Doc*>(spr->document()))); |
65 | tx.commit(); |
66 | } |
67 | else { |
68 | wud->setUserData(ud); |
69 | } |
70 | return 0; |
71 | } |
72 | |
73 | template<typename T> |
74 | int UserData_set_color(lua_State* L) { |
75 | auto obj = get_docobj<T>(L, 1); |
76 | auto spr = obj->sprite(); |
77 | doc::color_t docColor = convert_args_into_pixel_color(L, 2, doc::IMAGE_RGB); |
78 | auto wud = get_WithUserData<T>(obj); |
79 | UserData ud = wud->userData(); |
80 | ud.setColor(docColor); |
81 | if (spr) { |
82 | Tx tx; |
83 | tx(new cmd::SetUserData(wud, ud, static_cast<Doc*>(spr->document()))); |
84 | tx.commit(); |
85 | } |
86 | else { |
87 | wud->setUserData(ud); |
88 | } |
89 | return 0; |
90 | } |
91 | |
92 | } // namespace script |
93 | } // namespace app |
94 | |
95 | #endif |
96 | |