-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.cpp
More file actions
737 lines (609 loc) · 20 KB
/
widgets.cpp
File metadata and controls
737 lines (609 loc) · 20 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
MIT License
Copyright (c) 2022-2026 Kim Kulling
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "widgets.h"
#include "backends/sdl2_renderer.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
#include <cassert>
#include <algorithm>
namespace tinyui {
static Image *findImage(Context &ctx, const char *filename) {
if (filename == nullptr) {
return nullptr;
}
ImageCache::iterator it = ctx.mImageCache.find(filename);
if (it == ctx.mImageCache.end()) {
return nullptr;
}
return it->second;
}
static Image *loadIntoImageCache(Context &ctx, const char *filename) {
if (filename == nullptr) {
return nullptr;
}
Image *image = findImage(ctx, filename);
if (image != nullptr) {
return image;
}
image = new Image;
if (image == nullptr) {
return nullptr;
}
int w, h, bytesPerPixel;
unsigned char *data = stbi_load(filename, &w, &h, &bytesPerPixel, 0);
if (data == nullptr) {
return nullptr;
}
int32_t pitch = w * bytesPerPixel;
pitch = (pitch + 3) & ~3;
image->mSurfaceImpl = Renderer::createSurfaceImpl(data, w, h, bytesPerPixel, pitch);
image->mX = w;
image->mY = h;
image->mComp = bytesPerPixel;
ctx.mImageCache[filename] = image;
return image;
}
static void releaseImageCache(Context &ctx) {
for (auto it = ctx.mImageCache.begin(); it != ctx.mImageCache.end(); ++it) {
Image *image = it->second;
if (image != nullptr) {
Renderer::releaseSurfaceImpl(image->mSurfaceImpl);
delete image;
}
}
ctx.mImageCache.clear();
}
static Widget *getValidRoot(Context &ctx) {
if (ctx.mRoot != nullptr) {
return ctx.mRoot;
}
ctx.mRoot = new Widget;
ctx.mRoot->mType = WidgetType::Container;
return ctx.mRoot;
}
static Widget *setParent(Context &ctx, Widget *child, Id parentId) {
Widget *parent{nullptr};
if (parentId == 0) {
parent = getValidRoot(ctx);
} else {
parent = Widgets::findWidget(parentId, ctx.mRoot);
}
parent->mChildren.emplace_back(child);
parent->mRect.mergeWithRect(child->mRect);
return parent;
}
static Widget *createWidget(Context &ctx, Id id, Id parentId, const Rect &rect, WidgetType type) {
Widget *widget = Widgets::findWidget(id, ctx.mRoot);
if (widget != nullptr) {
if (widget->mType == type) {
return widget;
} else {
ctx.mLogger(LogSeverity::Error, "A widget with the same id but different type already exists.");
return nullptr;
}
}
widget = new Widget;
if (widget == nullptr) {
ctx.mLogger(LogSeverity::Error, "TUI-Widget cannot be created.");
return nullptr;
}
widget->mId = id;
widget->mType = type;
widget->mRect = rect;
widget->mParent = setParent(ctx, widget, parentId);
return widget;
}
void eventDispatcher(Context &ctx, int32_t eventId, EventPayload *eventPayload) {
if (ctx.mFocus == nullptr) {
return;
}
if (eventId == Events::KeyDownEvent || eventId == Events::KeyUpEvent) {
if (eventPayload != nullptr) {
if (ctx.mFocus->mType == WidgetType::InputField) {
ctx.mFocus->mText.append((const char*)eventPayload->payload[0]);
}
}
}
}
ret_code Widgets::container(Id id, Id parentId, const char *text, const Rect &rect) {
auto &ctx = TinyUi::getContext();
if (ctx.mRoot != nullptr) {
return ErrorCode;
}
Widget *widget = createWidget(ctx, id, parentId, rect, WidgetType::Container);
ctx.mRoot = widget;
if (text != nullptr) {
widget->mText.assign(text);
}
return ResultOk;
}
Widget *Widgets::findWidget(Id id, Widget *root) {
if (root == nullptr) {
return nullptr;
}
if (root->mId == id) {
return root;
}
for (size_t i=0; i<root->mChildren.size(); ++i) {
Widget *child = root->mChildren[i];
if (child == nullptr) {
continue;
}
if (child->mId == id) {
return child;
}
Widget *foundWidget = findWidget(id, child);
if (foundWidget != nullptr) {
return foundWidget;
}
}
return nullptr;
}
void Widgets::findSelectedWidget(int x, int y, Widget *currentChild, Widget **found) {
if (found == nullptr) {
return;
}
if (currentChild == nullptr) {
return;
}
if (currentChild->mRect.isIn(x, y)) {
*found = currentChild;
for ( auto &child : currentChild->mChildren) {
if (child->mRect.isIn(x, y)){
findSelectedWidget(x, y, child, found);
}
}
}
}
ret_code Widgets::label(Id id, Id parentId, const char *text, const Rect &rect, Alignment alignment) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return ErrorCode;
}
Widget *widget = createWidget(ctx, id, parentId, rect, WidgetType::Label);
if (widget == nullptr) {
return ErrorCode;
}
widget->mAlignment = alignment;
if (text != nullptr) {
widget->mText.assign(text);
}
return ResultOk;
}
ret_code Widgets::inputText(Id id, Id parentId, const Rect &rect, Alignment alignment) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *widget = createWidget(ctx, id, parentId, rect, WidgetType::InputField);
if (widget == nullptr) {
return ErrorCode;
}
widget->mAlignment = alignment;
return ResultOk;
}
ret_code Widgets::button(Id id, Id parentId, const char *text, const Rect &rect, CallbackI *callback) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Button);
if (child == nullptr) {
return ErrorCode;
}
child->mCallback = callback;
if (text != nullptr) {
child->mText.assign(text);
}
return ResultOk;
}
ret_code Widgets::imageButton(Id id, Id parentId, const char *image, const Rect &rect, CallbackI *callback) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Button);
if (child == nullptr) {
return ErrorCode;
}
child->mCallback = callback;
if (image != nullptr) {
child->mImage = loadIntoImageCache(ctx, image);
}
return ResultOk;
}
ret_code Widgets::box(Id id, Id parentId, const Rect &rect, bool filled) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Box);
if (child == nullptr) {
return ErrorCode;
}
child->mFilledRect = filled;
return ResultOk;
}
ret_code Widgets::imageBox(Id id, Id parentId, const char* image, const Rect& rect, bool filled) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Box);
if (child == nullptr) {
return ErrorCode;
}
child->mFilledRect = filled;
if (image != nullptr) {
child->mImage = loadIntoImageCache(ctx, image);
}
return ResultOk;
}
ret_code Widgets::panel(Id id, Id parentId, const char *title, const Rect &rect, CallbackI *callback) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Panel);
if (child == nullptr) {
return ErrorCode;
}
return ResultOk;
}
static int onTreeViewItemClicked(uint32_t id, void *data) {
Widget *treeView = Widgets::findWidget(id, TinyUi::getContext().mRoot);
if (treeView == nullptr) {
return ErrorCode;
}
for (size_t i = 0; i < treeView->mChildren.size(); ++i ) {
Widget *child = treeView->mChildren[i];
if (child == nullptr) {
continue;
}
child->mEnabled = !child->mEnabled;
}
std::cout << "TreeView item clicked: " << id << std::endl;
return 0;
}
ret_code Widgets::treeView(Id id, Id parentId, const char *title, const Rect &rect) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *widget = createWidget(ctx, id, parentId, rect, WidgetType::TreeView);
if (widget == nullptr) {
return ErrorCode;
}
if (title != nullptr) {
widget->mText.assign(title);
}
CallbackI *callback = new CallbackI(onTreeViewItemClicked, nullptr, Events::MouseButtonDownEvent);
widget->mCallback = callback;
return ResultOk;
}
ret_code Widgets::treeItem(Id id, Id parentItemId, const char *text) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *parentWidget = Widgets::findWidget(parentItemId, ctx.mRoot);
if (parentWidget == nullptr) {
return ErrorCode;
}
auto &parentRect = parentWidget->mRect;
const int32_t margin = ctx.mStyle.mMargin;
const int32_t w = parentRect.width;
const int32_t h = parentRect.height;
size_t numChildren = parentWidget->mChildren.size() + 1;
const Rect rect(parentRect.top.x + margin, parentRect.top.y + numChildren * margin + numChildren * h, w, h);
Widget *child = createWidget(ctx, id, parentItemId, rect, WidgetType::Label);
if (child == nullptr) {
return ErrorCode;
}
child->mIntention = parentWidget->mIntention + 1;
if (text != nullptr) {
child->mText.assign(text);
}
return ResultOk;
}
ret_code Widgets::progressBar(Id id, Id parentId, const Rect &rect, int fillRate, CallbackI *callback) {
auto &ctx = TinyUi::getContext();
if (ctx.mSDLContext.mRenderer == nullptr) {
return InvalidRenderHandle;
}
if (ctx.mRoot == nullptr) {
return InvalidRenderHandle;
}
Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::ProgressBar);
if (child == nullptr) {
return ErrorCode;
}
clamp(0, 100, fillRate);
FilledState state;
state.filledState = fillRate;
child->mContent = new uint8_t[sizeof(EventPayload)];
child->mCallback = callback;
EventPayload payload;
payload.type = EventDataType::FillState;
memcpy(child->mContent, &payload, sizeof(EventPayload));
if (callback != nullptr) {
callback->mInstance = child;
ctx.mUpdateCallbackList.push_back(callback);
}
return ResultOk;
}
static void render(Context &ctx, Widget *currentWidget) {
if (currentWidget == nullptr) {
return;
}
if (!currentWidget->mEnabled) {
return;
}
// Render the widget
const Rect &r = currentWidget->mRect;
switch( currentWidget->mType) {
case WidgetType::Button:
{
Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, true, ctx.mStyle.mFg);
if (currentWidget->mImage != nullptr) {
Renderer::drawImage(ctx, r.top.x, r.top.y, r.width, r.height, currentWidget->mImage);
}
if (!currentWidget->mText.empty()) {
Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg;
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont,
currentWidget->mRect, fg, bg, currentWidget->mAlignment);
}
}
break;
case WidgetType::TreeView:
{
Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, false, ctx.mStyle.mFg);
if (!currentWidget->mText.empty()) {
Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg;
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont,
currentWidget->mRect, fg, bg, currentWidget->mAlignment);
}
}
break;
case WidgetType::Label:
{
if (!currentWidget->mText.empty()) {
Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg;
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont,
currentWidget->mRect, fg, bg, currentWidget->mAlignment);
}
}
break;
case WidgetType::Panel:
{
Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, false, ctx.mStyle.mBorder);
}
break;
case WidgetType::ProgressBar:
{
Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, true, ctx.mStyle.mFg);
auto *payload = reinterpret_cast<EventPayload *>(currentWidget->mContent);
if (payload == nullptr) {
break;
}
FilledState *state = reinterpret_cast<FilledState *>(payload->payload);
if (state == nullptr) {
break;
}
const uint32_t fillRate{ state->filledState };
uint32_t width{ 0 };
if (fillRate != 0) {
width = r.width * fillRate / 100;
}
Renderer::drawRect(ctx, r.top.x, r.top.y, width, r.height, true, ctx.mStyle.mTextColor);
}
break;
case WidgetType::InputField:
{
Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, true, ctx.mStyle.mFg);
Renderer::drawRect(ctx, r.top.x+2, r.top.y+2, r.width-4, r.height-4, true, ctx.mStyle.mBorder);
}
break;
case WidgetType::Container:
case WidgetType::Box:
{
Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, currentWidget->mFilledRect, ctx.mStyle.mBorder);
}
break;
}
for (auto &child : currentWidget->mChildren) {
render(ctx, child);
}
}
void Widgets::renderWidgets() {
auto &ctx = TinyUi::getContext();
if (ctx.mRoot == nullptr) {
return;
}
render(ctx, ctx.mRoot);
}
void Widgets::onMouseButton(int x, int y, int eventType, MouseState state) {
auto &ctx = TinyUi::getContext();
assert(eventType >= 0);
assert(eventType < Events::NumEvents);
if (ctx.mRoot == nullptr) {
return;
}
Widget *found{nullptr};
findSelectedWidget(x, y, ctx.mRoot, &found);
if (found != nullptr) {
#ifdef _DEBUG
if (eventType == Events::MouseButtonDownEvent) {
std::cout << "Clicked " << found->mId << "\n";
} else {
std::cout << "Released " << found->mId << "\n";
}
#endif // _DEBUG
if (found->mCallback != nullptr) {
if (found->mCallback->mfuncCallback[eventType] != nullptr) {
found->mCallback->mfuncCallback[eventType](found->mId, found->mCallback->mInstance);
}
}
}
}
void Widgets::onMouseMove(int x, int y, int eventType, MouseState state) {
assert(eventType >= 0);
assert(eventType < Events::NumEvents);
auto &ctx = TinyUi::getContext();
if (ctx.mRoot == nullptr) {
return;
}
Widget *found{nullptr};
findSelectedWidget(x, y, ctx.mRoot, &found);
if (found == nullptr) {
return;
}
if (found->mCallback != nullptr) {
if (found->mCallback->mfuncCallback[eventType] != nullptr) {
found->mCallback->mfuncCallback[eventType](found->mId, found->mCallback->mInstance);
}
}
}
void Widgets::onKey(const char *key, bool isDown) {
auto &ctx = TinyUi::getContext();
if (key == nullptr) {
return;
}
EventPayload eventPayload;
eventPayload.payload[0] = *key;
int32_t eventId = -1;
if (isDown) {
eventId = Events::KeyDownEvent;
eventPayload.type = EventDataType::KeyDownState;
} else {
eventId = Events::KeyUpEvent;
eventPayload.type = EventDataType::KeyUpState;
}
eventDispatcher(ctx, eventId, &eventPayload);
}
void recursiveClear(Widget *current) {
if (current == nullptr) {
return;
}
if (current->mChildren.empty()) {
return;
}
for (size_t i = 0; i < current->mChildren.size(); ++i) {
recursiveClear(current->mChildren[i]);
}
delete current;
}
void Widgets::clear() {
auto &ctx = TinyUi::getContext();
if (ctx.mRoot == nullptr) {
return;
}
Widget *current{ctx.mRoot};
recursiveClear(current);
releaseImageCache(ctx);
}
bool Widgets::clearItem(Id id, bool recursive) {
auto &ctx = TinyUi::getContext();
Widget *widget = findWidget(id, ctx.mRoot);
if (widget == nullptr) {
return false;
}
if (widget->mParent == nullptr) {
return false;
}
auto &siblings = widget->mParent->mChildren;
auto it = std::find(siblings.begin(), siblings.end(), widget);
bool result{ false };
if (it != siblings.end()) {
siblings.erase(it);
delete widget;
if (recursive) {
recursiveClear(widget);
}
result = true;
}
return result;
}
void Widgets::setEnableState(Id id, bool enabled) {
auto &ctx = TinyUi::getContext();
Widget *widget = findWidget(id, ctx.mRoot);
if (widget != nullptr) {
if (enabled) {
widget->enable();
} else {
widget->disable();
}
}
}
bool Widgets::isEnabled(Id id) {
auto &ctx = TinyUi::getContext();
Widget *widget = findWidget(id, ctx.mRoot);
if (widget != nullptr) {
return widget->isEnabled();
}
return false;
}
ret_code Widgets::setFocus(Id id) {
auto &ctx = TinyUi::getContext();
Widget *widget = findWidget(id, ctx.mRoot);
if (widget == nullptr) {
return ErrorCode;
}
ctx.mFocus = widget;
return ResultOk;
}
Widget *Widgets::getWidgetById(Id id) {
auto &ctx = TinyUi::getContext();
return findWidget(id, ctx.mRoot);
}
bool Widgets::beginChild() {
auto &ctx = TinyUi::getContext();
return true;
}
bool Widgets::endChild() {
auto &ctx = TinyUi::getContext();
return true;
}
} // namespace tinyui