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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
#include "StarWidget.hpp"
#include "StarPane.hpp"
#include "StarLabelWidget.hpp"
#include "StarFlowLayout.hpp"
namespace Star {
Widget::Widget() {
m_parent = nullptr;
m_context = GuiContext::singletonPtr();
m_visible = true;
m_focus = false;
m_doScissor = true;
m_container = false;
m_mouseTransparent = false;
}
Widget::~Widget() {
removeAllChildren();
}
void Widget::update(float dt) {
for (auto& widget : m_members)
widget->update(dt);
}
GuiContext* Widget::context() const {
return m_context;
}
void Widget::render(RectI const& region) {
if (!m_visible)
return;
if (!setupDrawRegion(region))
return;
renderImpl();
drawChildren();
}
void Widget::renderImpl() {}
void Widget::drawChildren() {
for (auto child : m_members)
child->render(m_drawingArea);
}
Vec2I Widget::position() const {
return m_position + m_drawingOffset;
}
Vec2I Widget::relativePosition() const {
return m_position;
}
bool Widget::setupDrawRegion(RectI const& region) {
RectI scissorRect;
if (m_doScissor) {
scissorRect = getScissorRect();
} else {
scissorRect = noScissor();
}
m_drawingArea = scissorRect.limited(region);
if (m_drawingArea.isEmpty())
return false;
m_context->setInterfaceScissorRect(m_drawingArea);
return true;
}
Vec2I Widget::drawingOffset() const {
return m_drawingOffset;
}
void Widget::setDrawingOffset(Vec2I const& offset) {
m_drawingOffset = offset;
}
RectI Widget::getScissorRect() const {
return screenBoundRect();
}
RectI Widget::noScissor() const {
return RectI::inf();
}
void Widget::disableScissoring() {
m_doScissor = false;
}
void Widget::enableScissoring() {
m_doScissor = true;
}
void Widget::setPosition(Vec2I const& position) {
m_position = position;
}
Vec2I Widget::size() const {
return m_size;
}
void Widget::setSize(Vec2I const& size) {
m_size = size;
}
RectI Widget::relativeBoundRect() const {
return RectI::withSize(relativePosition(), size());
}
RectI Widget::screenBoundRect() const {
return relativeBoundRect().translated(screenPosition() - relativePosition());
}
void Widget::determineSizeFromChildren() {
Vec2I max;
for (auto& child : m_members) {
Vec2I childMax = child->position() + child->size();
max = max.piecewiseMax(childMax);
}
setSize(max);
}
void Widget::markAsContainer() {
m_container = true;
}
KeyboardCaptureMode Widget::keyboardCaptured() const {
if (active()) {
for (auto const& member : m_members) {
auto mode = member->keyboardCaptured();
if (mode != KeyboardCaptureMode::None)
return mode;
}
}
return KeyboardCaptureMode::None;
}
void Widget::setData(Json const& data) {
m_data = data;
}
Json const& Widget::data() {
return m_data;
}
Vec2I Widget::screenPosition() const {
if (m_parent) {
return m_parent->screenPosition() + position();
} else {
return position();
}
}
bool Widget::inMember(Vec2I const& position) const {
if (!m_visible)
return false;
if (m_mouseTransparent)
return false;
if (!m_drawingArea.isNull() && !m_drawingArea.contains(Vec2I::floor(position)))
return false;
if (m_container) {
for (auto child : m_members)
if (child->inMember(position))
return true;
} else {
return screenBoundRect().contains(position);
}
return false;
}
bool Widget::sendEvent(InputEvent const& event) {
if (!m_visible)
return false;
for (auto child : reverseIterate(m_members)) {
if (child->sendEvent(event))
return true;
}
return false;
}
void Widget::mouseOver() {}
void Widget::mouseOut() {}
void Widget::mouseReturnStillDown() {}
void Widget::setMouseTransparent(bool transparent) {
m_mouseTransparent = transparent;
}
bool Widget::mouseTransparent() {
return m_mouseTransparent;
}
Widget* Widget::parent() const {
return m_parent;
}
void Widget::setParent(Widget* parent) {
m_parent = parent;
}
void Widget::show() {
m_visible = true;
}
void Widget::hide() {
m_visible = false;
}
bool Widget::visibility() const {
return m_visible;
}
void Widget::toggleVisibility() {
m_visible = !m_visible;
}
void Widget::setVisibility(bool visibility) {
if (visibility)
show();
else
hide();
}
bool Widget::active() const {
return m_visible;
}
bool Widget::interactive() const {
return true;
}
bool Widget::hasFocus() const {
return m_focus;
}
void Widget::focus() {
m_focus = true;
if (auto w = window())
w->setFocus(this);
}
void Widget::blur() {
m_focus = false;
if (window())
window()->removeFocus(this);
}
unsigned Widget::windowHeight() const {
return context()->windowHeight();
}
unsigned Widget::windowWidth() const {
return context()->windowWidth();
}
Vec2I Widget::windowSize() const {
return Vec2I(context()->windowSize());
}
Pane* Widget::window() {
if (m_parent)
return m_parent->window();
return nullptr;
}
Pane const* Widget::window() const {
if (m_parent)
return m_parent->window();
return nullptr;
}
void Widget::addChild(String const& name, WidgetPtr member) {
member->setName(name);
m_members.push_back(member);
m_memberHash[member->name()] = member;
member->setParent(this);
}
void Widget::addChildAt(String const& name, WidgetPtr member, size_t at) {
if (at > m_members.size())
throw GuiException("Attempted to insert item after the end of the list.");
m_members.insert(m_members.begin() + at, member);
m_memberHash[name] = member;
member->setName(name);
member->setParent(this);
}
bool Widget::removeChild(Widget* member) {
m_memberHash.erase(member->name());
for (auto child = m_members.begin(); child != m_members.end(); ++child) {
if (child->get() == member) {
(*child)->setParent(nullptr);
m_members.erase(child);
return true;
} else {
if ((*child)->removeChild(member))
return true;
}
}
return false;
}
bool Widget::removeChild(String const& name) {
m_memberHash.erase(name);
for (auto child = m_members.begin(); child != m_members.end(); ++child) {
auto ptr = *child;
if (ptr->name() == name) {
m_members.erase(child);
ptr->setParent(nullptr);
return true;
}
}
return false;
}
bool Widget::removeChildAt(size_t at) {
if (at >= m_members.size())
return false;
m_memberHash.erase(m_members.at(at)->name());
m_members.at(at)->setParent(nullptr);
m_members.erase(m_members.begin() + at);
return true;
}
void Widget::removeAllChildren() {
for (auto child : m_members)
child->setParent(nullptr);
m_members.clear();
m_memberHash.clear();
}
bool Widget::containsChild(String const& name) {
if (fetchChild(name))
return true;
return false;
}
WidgetPtr Widget::fetchChild(String const& name) {
WidgetPtr res;
if (name.contains(".")) {
StringList nameList = name.split(".", 1);
if (auto child = m_memberHash.value(nameList[0], {}))
return child->fetchChild(nameList[1]);
} else {
if (auto child = m_memberHash.value(name, {}))
return child;
}
return {};
}
WidgetPtr Widget::findChild(String const& name) {
if (auto found = fetchChild(name))
return found;
for (auto const& child : m_members) {
if (auto found = child->findChild(name))
return found;
}
return {};
}
WidgetPtr Widget::childPtr(Widget const* child) const {
for (auto const& m : m_members) {
if (m.get() == child)
return m;
if (auto c = m->childPtr(child))
return c;
}
return {};
}
WidgetPtr Widget::getChildAt(Vec2I const& pos) {
for (auto child : reverseIterate(m_members)) {
if (child->inMember(pos)) {
auto res = child->getChildAt(pos);
if (res) {
return res;
}
return child;
}
}
return {};
}
size_t Widget::numChildren() const {
return m_members.size();
}
WidgetPtr Widget::getChildNum(size_t num) const {
return m_members.at(num);
}
String const& Widget::name() const {
return m_name;
}
void Widget::setName(String const& name) {
m_name = name;
}
String Widget::fullName() const {
if (m_parent) {
return m_parent->fullName() + "." + name();
}
return name();
}
String Widget::toStringImpl(int indentLevel) const {
auto leader = String(" ") * indentLevel;
String childrenString;
for (auto child : m_members) {
childrenString.append(child->toStringImpl(indentLevel + 4));
}
String output = strf(R"OUTPUT({}{} : {
{} address : %p,
{} visible : {},
{} position : {},
{} size : {},
{} children : {
{}
{} }
{}}
)OUTPUT",
leader,
m_name,
leader,
(void*)this,
leader,
m_visible ? "true" : "false",
leader,
m_position,
leader,
m_size,
leader,
childrenString,
leader,
leader);
return output;
}
bool Widget::setLabel(String const& name, String const& value) {
if (containsChild(name)) {
auto child = fetchChild(name);
if (auto label = as<LabelWidget>(child)) {
label->setText(value);
return true;
}
}
return false;
}
std::ostream& operator<<(std::ostream& os, Widget const& widget) {
os << widget.toStringImpl(0);
return os;
}
}
|