-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathwidget.cpp
More file actions
451 lines (392 loc) · 12.4 KB
/
widget.cpp
File metadata and controls
451 lines (392 loc) · 12.4 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "components/widget.h"
#include "components/recttransform.h"
#include "components/layout.h"
#include "stylesheet.h"
#include "utils/stringutil.h"
#include "uisystem.h"
#include <components/actor.h>
#include <commandbuffer.h>
#include <gizmos.h>
Widget *Widget::m_focusWidget = nullptr;
/*!
\module Gui
\title Graphical User Interface for Thunder Engine SDK
\brief Contains classes related to Graphical User Interfaces.
*/
/*!
\class Widget
\brief The Widget class is the base class of all user interface objects.
\inmodule Gui
The Widget class serves as the base class for all user interface objects, providing basic functionality for handling updates, drawing, and interaction.
Internal methods are marked as internal and are intended for use within the framework rather than by external code.
*/
Widget::Widget() :
m_parent(nullptr),
m_transform(nullptr) {
}
Widget::~Widget() {
if(m_transform) {
m_transform->unsubscribe(this);
}
static_cast<UiSystem *>(system())->removeWidget(this);
}
/*!
Sets a textual description of widget style.
*/
std::string Widget::style() const {
std::string result;
for(auto &it : m_styleRules) {
if(it.second.first == 1000) {
result += it.first + ": " + it.second.second + ";";
}
}
return result;
}
/*!
Returns a list of stylesheet class names attached to this widget.
*/
const std::list<std::string> &Widget::classes() const {
return m_classes;
}
/*!
Adds a stylesheet class \a name attached to this widget.
*/
void Widget::addClass(const std::string &name) {
m_classes.push_back(name);
}
/*!
\internal
Internal method called to draw the widget using the provided command buffer.
*/
void Widget::draw(CommandBuffer &buffer) {
A_UNUSED(buffer);
}
/*!
Lowers the widget to the bottom of the widget's stack.
\sa raise()
*/
void Widget::lower() {
UiSystem *render = static_cast<UiSystem *>(system());
for(auto it : childWidgets()) {
it->lower();
}
auto &widgets = render->widgets();
widgets.remove(this);
widgets.push_front(this);
}
/*!
Raises this widget to the top of the widget's stack.
\sa lower()
*/
void Widget::raise() {
UiSystem *render = static_cast<UiSystem *>(system());
auto &widgets = render->widgets();
widgets.remove(this);
widgets.push_back(this);
for(auto it : childWidgets()) {
it->raise();
}
}
/*!
Callback to respond to changes in the widget's \a size.
*/
void Widget::boundChanged(const Vector2 &size) {
A_UNUSED(size);
}
/*!
Applies style settings assigned to widget.
*/
void Widget::applyStyle() {
// Size
bool pixels;
Vector2 size = m_transform->size();
size = styleBlock2Length("-uikit-size", size, pixels);
size.x = styleLength("width", size.x, pixels);
size.y = styleLength("height", size.y, pixels);
m_transform->setSize(size);
// Pivot point
Vector2 pivot = m_transform->pivot();
pivot = styleBlock2Length("-uikit-pivot", pivot, pixels);
m_transform->setPivot(pivot);
// Anchors
Vector2 minAnchors = m_transform->minAnchors();
minAnchors = styleBlock2Length("-uikit-min-anchors", minAnchors, pixels);
m_transform->setMinAnchors(minAnchors);
Vector2 maxAnchors = m_transform->maxAnchors();
maxAnchors = styleBlock2Length("-uikit-max-anchors", maxAnchors, pixels);
m_transform->setMaxAnchors(maxAnchors);
// Border width
Vector4 border(m_transform->border());
border = styleBlock4Length("border-width", border, pixels);
border.x = styleLength("border-top-width", border.x, pixels);
border.y = styleLength("border-right-width", border.y, pixels);
border.z = styleLength("border-bottom-width", border.z, pixels);
border.w = styleLength("border-left-width", border.w, pixels);
m_transform->setBorder(border);
// Margins
Vector4 margin(m_transform->margin());
margin = styleBlock4Length("margin", margin, pixels);
margin.x = styleLength("margin-top", margin.x, pixels);
margin.y = styleLength("margin-right", margin.y, pixels);
margin.z = styleLength("margin-bottom", margin.z, pixels);
margin.w = styleLength("margin-left", margin.w, pixels);
m_transform->setMargin(margin);
// Padding
Vector4 padding(m_transform->padding());
padding = styleBlock4Length("padding", padding, pixels);
padding.x = styleLength("padding-top", padding.x, pixels);
padding.y = styleLength("padding-right", padding.y, pixels);
padding.z = styleLength("padding-bottom", padding.z, pixels);
padding.w = styleLength("padding-left", padding.w, pixels);
m_transform->setPadding(padding);
// Display
Layout *layout = m_transform->layout();
auto it = m_styleRules.find("display");
if(it != m_styleRules.end()) {
std::string layoutMode = it->second.second;
if(layoutMode == "none") {
actor()->setEnabled(false);
} else {
layout = new Layout;
if(layoutMode == "block") {
layout->setDirection(Layout::Vertical);
} else if(layoutMode == "inline") {
layout->setDirection(Layout::Horizontal);
}
m_transform->setLayout(layout);
}
}
// Child widgets
for(auto it : childWidgets()) {
if(layout) {
layout->addTransform(it->rectTransform());
}
it->applyStyle();
}
}
/*!
Returns the parent Widget.
*/
Widget *Widget::parentWidget() const {
return m_parent;
}
/*!
Returns a list of child widgets;
*/
std::list<Widget *> Widget::childWidgets() const {
std::list<Widget *> result;
for(auto it : actor()->componentsInChild("Widget")) {
result.push_back(static_cast<Widget *>(it));
}
return result;
}
/*!
Returns RectTransform component attached to parent Actor.
*/
RectTransform *Widget::rectTransform() const {
return m_transform;
}
/*!
Returns true if provided \a widget is a part of complex widget; otherwise returns false.
*/
bool Widget::isSubWidget(Widget *widget) const {
for(auto it : m_subWidgets) {
if(it.second == widget) {
return true;
}
}
return false;
}
/*!
Returns the application widget that has the keyboard input focus, or nullptr if no widget in this application has the focus.
*/
Widget *Widget::focusWidget() {
return m_focusWidget;
}
Widget *Widget::subWidget(const std::string &name) const {
auto it = m_subWidgets.find(name);
if(it != m_subWidgets.end()) {
return it->second;
}
return nullptr;
}
void Widget::setSubWidget(const std::string &name, Widget *widget) {
Widget *current = subWidget(name);
if(current != widget) {
disconnect(current, _SIGNAL(destroyed()), this, _SLOT(onReferenceDestroyed()));
m_subWidgets[name] = widget;
if(widget) {
connect(widget, _SIGNAL(destroyed()), this, _SLOT(onReferenceDestroyed()));
}
}
}
/*!
\internal
Internal method to set the widget that has the keyboard input focus.
*/
void Widget::setFocusWidget(Widget *widget) {
m_focusWidget = widget;
}
/*!
\internal
Internal method to set the RectTransform component for the widget.
*/
void Widget::setRectTransform(RectTransform *transform) {
if(m_transform) {
m_transform->unsubscribe(this);
}
m_transform = transform;
if(m_transform) {
m_transform->subscribe(this);
}
}
/*!
\internal
Internal method to set the parent of the widget and handle changes.
*/
void Widget::setParent(Object *parent, int32_t position, bool force) {
NativeBehaviour::setParent(parent, position, force);
actorParentChanged();
}
/*!
\internal
Internal slot method called when a referenced object is destroyed.
*/
void Widget::onReferenceDestroyed() {
Object *object = sender();
for(auto it : m_subWidgets) {
if(it.second == object) {
m_subWidgets.erase(it.first);
break;
}
}
}
/*!
\internal
Internal method called when the parent actor of the widget changes.
*/
void Widget::actorParentChanged() {
Actor *object = actor();
if(object) {
setRectTransform(dynamic_cast<RectTransform *>(object->transform()));
object = dynamic_cast<Actor *>(object->parent());
if(object) {
m_parent = static_cast<Widget *>(object->component("Widget"));
}
}
}
/*!
\internal
Internal method to compose the widget component, creating and setting the RectTransform.
*/
void Widget::composeComponent() {
Actor *a = actor();
if(a) {
Transform *transform = a->transform();
RectTransform *rect = dynamic_cast<RectTransform *>(transform);
if(rect == nullptr) {
if(transform) {
delete transform;
}
rect = Engine::objectCreate<RectTransform>("RectTransform", a);
a->setTransform(rect);
setRectTransform(rect);
}
}
}
/*!
\internal
Internal method to set the system for the widget, adding it to the render system.
*/
void Widget::setSystem(ObjectSystem *system) {
Object::setSystem(system);
UiSystem *render = static_cast<UiSystem *>(system);
render->addWidget(this);
}
/*!
\internal
Applies a new stylesheet \a rules to the widget.
A \a wieght parameter required to select rules between new one and existant.
*/
void Widget::addStyleRules(const std::map<std::string, std::string> &rules, uint32_t weight) {
for(auto rule : rules) {
auto it = m_styleRules.find(rule.first);
if(it == m_styleRules.end() || it->second.first <= weight) {
m_styleRules[rule.first] = make_pair(weight, rule.second);
}
}
for(auto it : m_subWidgets) {
it.second->addStyleRules(rules, weight);
}
}
/*!
\internal
Returns length for the stylesheet \a property.
Default \a value will be used in case of property will not be found.
Parameter \a pixels contains a definition of unit of measurement.
*/
float Widget::styleLength(const std::string &property, float value, bool &pixels) {
auto it = m_styleRules.find(property);
if(it != m_styleRules.end()) {
return StyleSheet::toLength(it->second.second, pixels);
} else {
pixels = true;
}
return value;
}
/*!
\internal
Returns length block for the stylesheet \a property.
Default \a value will be used in case of property will not be found.
Parameter \a pixels contains a definition of unit of measurement.
*/
Vector2 Widget::styleBlock2Length(const std::string &property, const Vector2 &value, bool &pixels) {
Vector2 result(value);
auto it = m_styleRules.find(property);
if(it != m_styleRules.end()) {
auto list = StringUtil::split(it->second.second, ' ');
Vector2 value;
if(list.size() == 1) {
result.x = value.y = stof(list[0]);
} else {
result.x = stof(list[0]);
result.y = stof(list[1]);
}
}
return result;
}
/*!
\internal
Returns length block for the stylesheet \a property.
Default \a value will be used in case of property will not be found.
Parameter \a pixels contains a definition of unit of measurement.
*/
Vector4 Widget::styleBlock4Length(const std::string &property, const Vector4 &value, bool &pixels) {
Vector4 result(value);
auto it = m_styleRules.find(property);
if(it != m_styleRules.end()) {
auto array = StringUtil::split(it->second.second, ' ');
switch(array.size()) {
case 1: {
result = Vector4(StyleSheet::toLength(array[0], pixels));
} break;
case 2: {
result.x = result.z = StyleSheet::toLength(array[0], pixels);
result.y = result.w = StyleSheet::toLength(array[1], pixels);
} break;
case 3: {
result.y = StyleSheet::toLength(array[0], pixels);
result.z = result.x = StyleSheet::toLength(array[1], pixels);
result.w = StyleSheet::toLength(array[2], pixels);
} break;
case 4: {
result.x = StyleSheet::toLength(array[0], pixels);
result.y = StyleSheet::toLength(array[1], pixels);
result.z = StyleSheet::toLength(array[2], pixels);
result.w = StyleSheet::toLength(array[3], pixels);
} break;
default: break;
}
}
return result;
}