forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme_class.cpp
247 lines (231 loc) · 6.93 KB
/
theme_class.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Aseprite Code Generator
// Copyright (c) 2015-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "gen/theme_class.h"
#include "base/string.h"
#include "gen/common.h"
#include <cstring>
#include <iostream>
#include <vector>
void gen_theme_class(TiXmlDocument* doc, const std::string& inputFn)
{
std::vector<std::string> dimensions;
std::vector<std::string> colors;
std::vector<std::string> parts;
std::vector<std::string> cursors;
std::vector<std::string> styles;
TiXmlHandle handle(doc);
TiXmlElement* elem = handle
.FirstChild("theme")
.FirstChild("dimensions")
.FirstChild("dim").ToElement();
while (elem) {
const char* id = elem->Attribute("id");
dimensions.push_back(id);
elem = elem->NextSiblingElement();
}
elem = handle
.FirstChild("theme")
.FirstChild("colors")
.FirstChild("color").ToElement();
while (elem) {
const char* id = elem->Attribute("id");
colors.push_back(id);
elem = elem->NextSiblingElement();
}
elem = handle
.FirstChild("theme")
.FirstChild("parts")
.FirstChild("part").ToElement();
while (elem) {
const char* id = elem->Attribute("id");
if (std::strncmp(id, "cursor_", 7) == 0) {
cursors.push_back(std::string(id).substr(7));
}
else if (!std::strchr(id, ':'))
parts.push_back(id);
elem = elem->NextSiblingElement();
}
elem = handle
.FirstChild("theme")
.FirstChild("styles")
.FirstChild("style").ToElement();
while (elem) {
const char* id = elem->Attribute("id");
styles.push_back(id);
elem = elem->NextSiblingElement();
}
std::cout
<< "// Don't modify, generated file from " << inputFn << "\n"
<< "\n"
<< "#ifndef GENERATED_THEME_H_INCLUDED\n"
<< "#define GENERATED_THEME_H_INCLUDED\n"
<< "#pragma once\n"
<< "\n"
<< "namespace app {\n"
<< "namespace gen {\n"
<< "\n"
<< " template<typename T>\n"
<< " class ThemeFile {\n"
<< " public:\n"
<< "\n";
// Dimensions sub class
std::cout
<< " class Dimensions {\n"
<< " template<typename> friend class ThemeFile;\n"
<< " public:\n";
for (auto dimension : dimensions) {
std::string id = convert_xmlid_to_cppid(dimension, false);
std::cout
<< " int " << id << "() const { return m_" << id << "; }\n";
}
std::cout
<< " private:\n";
for (auto dimension : dimensions) {
std::string id = convert_xmlid_to_cppid(dimension, false);
std::cout
<< " int m_" << id << ";\n";
}
std::cout
<< " };\n";
// Colors sub class
std::cout
<< " class Colors {\n"
<< " template<typename> friend class ThemeFile;\n"
<< " public:\n";
for (auto color : colors) {
std::string id = convert_xmlid_to_cppid(color, false);
std::cout
<< " gfx::Color " << id << "() const { return m_" << id << "; }\n";
}
std::cout
<< " private:\n";
for (auto color : colors) {
std::string id = convert_xmlid_to_cppid(color, false);
std::cout
<< " gfx::Color m_" << id << ";\n";
}
std::cout
<< " };\n";
// Parts sub class
std::cout
<< " class Parts {\n"
<< " template<typename> friend class ThemeFile;\n"
<< " public:\n";
for (auto part : parts) {
std::string id = convert_xmlid_to_cppid(part, false);
std::cout
<< " const skin::SkinPartPtr& " << id << "() const { return m_" << id << "; }\n";
}
std::cout
<< " private:\n";
for (auto part : parts) {
std::string id = convert_xmlid_to_cppid(part, false);
std::cout
<< " skin::SkinPartPtr m_" << id << ";\n";
}
std::cout
<< " };\n";
// Cursors sub class
std::cout
<< " class Cursors {\n"
<< " template<typename> friend class ThemeFile;\n"
<< " public:\n";
for (auto cursor : cursors) {
std::string id = convert_xmlid_to_cppid(cursor, false);
std::cout
<< " const ui::Cursor* " << id << "() const { return m_" << id << "; }\n";
}
std::cout
<< " private:\n";
for (auto cursor : cursors) {
std::string id = convert_xmlid_to_cppid(cursor, false);
std::cout
<< " ui::Cursor* m_" << id << ";\n";
}
std::cout
<< " };\n";
// Styles sub class
std::cout
<< "\n"
<< " class Styles {\n"
<< " template<typename> friend class ThemeFile;\n"
<< " public:\n";
for (auto style : styles) {
std::string id = convert_xmlid_to_cppid(style, false);
std::cout
<< " ui::Style* " << id << "() const { return m_" << id << "; }\n";
}
std::cout
<< " private:\n";
for (auto style : styles) {
std::string id = convert_xmlid_to_cppid(style, false);
std::cout
<< " ui::Style* m_" << id << ";\n";
}
std::cout
<< " };\n";
std::cout
<< "\n"
<< " Dimensions dimensions;\n"
<< " Colors colors;\n"
<< " Parts parts;\n"
<< " Cursors cursors;\n"
<< " Styles styles;\n"
<< "\n"
<< " protected:\n"
<< " void updateInternals() {\n";
for (auto dimension : dimensions) {
std::string id = convert_xmlid_to_cppid(dimension, false);
std::cout << " byId(dimensions.m_" << id
<< ", \"" << dimension << "\");\n";
}
for (auto color : colors) {
std::string id = convert_xmlid_to_cppid(color, false);
std::cout << " byId(colors.m_" << id
<< ", \"" << color << "\");\n";
}
for (auto part : parts) {
std::string id = convert_xmlid_to_cppid(part, false);
std::cout << " byId(parts.m_" << id
<< ", \"" << part << "\");\n";
}
for (auto cursor : cursors) {
std::string id = convert_xmlid_to_cppid(cursor, false);
std::cout << " byId(cursors.m_" << id
<< ", \"" << cursor << "\");\n";
}
for (auto style : styles) {
std::string id = convert_xmlid_to_cppid(style, false);
std::cout << " byId(styles.m_" << id
<< ", \"" << style << "\");\n";
}
std::cout
<< " }\n"
<< "\n"
<< " private:\n"
<< " void byId(int& dimension, const std::string& id) {\n"
<< " dimension = static_cast<T*>(this)->getDimensionById(id);\n"
<< " }\n"
<< " void byId(gfx::Color& color, const std::string& id) {\n"
<< " color = static_cast<T*>(this)->getColorById(id);\n"
<< " }\n"
<< " void byId(skin::SkinPartPtr& part, const std::string& id) {\n"
<< " part = static_cast<T*>(this)->getPartById(id);\n"
<< " }\n"
<< " void byId(ui::Cursor*& cursor, const std::string& id) {\n"
<< " cursor = static_cast<T*>(this)->getCursorById(id);\n"
<< " }\n"
<< " void byId(ui::Style*& style, const std::string& id) {\n"
<< " style = static_cast<T*>(this)->getStyleById(id);\n"
<< " }\n";
std::cout
<< " };\n"
<< "\n"
<< "} // namespace gen\n"
<< "} // namespace app\n"
<< "\n"
<< "#endif\n";
}