-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiGUI.h
More file actions
1112 lines (968 loc) · 36.8 KB
/
Copy pathwiGUI.h
File metadata and controls
1112 lines (968 loc) · 36.8 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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "CommonInclude.h"
#include "wiGraphicsDevice.h"
#include "wiPrimitive.h"
#include "wiCanvas.h"
#include "wiVector.h"
#include "wiColor.h"
#include "wiScene.h"
#include "wiSprite.h"
#include "wiSpriteFont.h"
#include "wiLocalization.h"
#include <string>
#include <functional>
namespace wi::gui
{
struct EventArgs
{
XMFLOAT2 clickPos = {}; // mouse click coordinate
XMFLOAT2 startPos = {}; // mouse start position of operation (drag)
XMFLOAT2 deltaPos = {}; // mouse delta position of operation since last update (drag)
XMFLOAT2 endPos = {}; // mouse end position of operation (drag)
float fValue = 0; // generic float value of operation
bool bValue = false; // generic boolean value of operation
int iValue = 0; // generic integer value of operation
int iValue2 = 0; // secondary generic integer value of operation
wi::Color color; // color value of color picker operation
std::string sValue; // generic string value of operation
uint64_t userdata = 0; // this will provide the userdata value that was set to a widget (or part of a widget)
};
enum WIDGETSTATE
{
IDLE, // widget is doing nothing
FOCUS, // widget got pointer dragged on or selected
ACTIVE, // widget is interacted with right now
DEACTIVATING, // widget has last been active but no more interactions are occuring
WIDGETSTATE_COUNT,
};
// These can be used to target a setting for a specific widget control and state:
enum WIDGET_ID
{
// IDs for normal widget states:
WIDGET_ID_IDLE = IDLE,
WIDGET_ID_FOCUS = FOCUS,
WIDGET_ID_ACTIVE = ACTIVE,
WIDGET_ID_DEACTIVATING = DEACTIVATING,
// IDs for special widget states:
// TextInputField:
WIDGET_ID_TEXTINPUTFIELD_BEGIN, // do not use!
WIDGET_ID_TEXTINPUTFIELD_IDLE,
WIDGET_ID_TEXTINPUTFIELD_FOCUS,
WIDGET_ID_TEXTINPUTFIELD_ACTIVE,
WIDGET_ID_TEXTINPUTFIELD_DEACTIVATING,
WIDGET_ID_TEXTINPUTFIELD_END, // do not use!
// Slider:
WIDGET_ID_SLIDER_BEGIN, // do not use!
WIDGET_ID_SLIDER_BASE_IDLE,
WIDGET_ID_SLIDER_BASE_FOCUS,
WIDGET_ID_SLIDER_BASE_ACTIVE,
WIDGET_ID_SLIDER_BASE_DEACTIVATING,
WIDGET_ID_SLIDER_KNOB_IDLE,
WIDGET_ID_SLIDER_KNOB_FOCUS,
WIDGET_ID_SLIDER_KNOB_ACTIVE,
WIDGET_ID_SLIDER_KNOB_DEACTIVATING,
WIDGET_ID_SLIDER_END, // do not use!
// Scrollbar:
WIDGET_ID_SCROLLBAR_BEGIN, // do not use!
WIDGET_ID_SCROLLBAR_BASE_IDLE,
WIDGET_ID_SCROLLBAR_BASE_FOCUS,
WIDGET_ID_SCROLLBAR_BASE_ACTIVE,
WIDGET_ID_SCROLLBAR_BASE_DEACTIVATING,
WIDGET_ID_SCROLLBAR_KNOB_INACTIVE,
WIDGET_ID_SCROLLBAR_KNOB_HOVER,
WIDGET_ID_SCROLLBAR_KNOB_GRABBED,
WIDGET_ID_SCROLLBAR_END, // do not use!
// Combo box:
WIDGET_ID_COMBO_DROPDOWN,
// Window:
WIDGET_ID_WINDOW_BASE,
// Colorpicker:
WIDGET_ID_COLORPICKER_BASE,
// other user-defined widget states can be specified after this (but in user's own enum):
// And you will of course need to handle it yourself in a SetColor() override for example
WIDGET_ID_USER,
};
enum class LocalizationEnabled
{
None = 0,
Text = 1 << 0,
Tooltip = 1 << 1,
Items = 1 << 2, // ComboBox items
Children = 1 << 3, // Window children
All = Text | Tooltip | Items | Children,
};
struct Theme
{
// Reduced version of wi::image::Params, excluding position, alignment, etc.
struct Image
{
inline static const wi::image::Params params; // prototype for default values
XMFLOAT4 color = params.color;
wi::enums::BLENDMODE blendFlag = params.blendFlag;
wi::image::SAMPLEMODE sampleFlag = params.sampleFlag;
wi::image::QUALITY quality = params.quality;
bool background = params.isBackgroundEnabled();
bool corner_rounding = params.isCornerRoundingEnabled();
wi::image::Params::Rounding corners_rounding[arraysize(params.corners_rounding)];
bool highlight = false;
XMFLOAT3 highlight_color = XMFLOAT3(1, 1, 1);
float highlight_spread = 1;
float border_soften = 0;
void Apply(wi::image::Params& params) const
{
params.color = color;
params.blendFlag = blendFlag;
params.sampleFlag = sampleFlag;
params.quality = quality;
if (background)
{
params.enableBackground();
}
else
{
params.disableBackground();
}
if (corner_rounding)
{
params.enableCornerRounding();
}
else
{
params.disableCornerRounding();
}
if (highlight)
{
params.enableHighlight();
}
else
{
params.disableHighlight();
}
params.highlight_color = highlight_color;
params.highlight_spread = highlight_spread;
std::memcpy(params.corners_rounding, corners_rounding, sizeof(corners_rounding));
}
void CopyFrom(const wi::image::Params& params)
{
color = params.color;
blendFlag = params.blendFlag;
sampleFlag = params.sampleFlag;
quality = params.quality;
background = params.isBackgroundEnabled();
corner_rounding = params.isCornerRoundingEnabled();
highlight = params.isHighlightEnabled();
highlight_color = params.highlight_color;
highlight_spread = params.highlight_spread;
std::memcpy(corners_rounding, params.corners_rounding, sizeof(corners_rounding));
}
} image;
// Reduced version of wi::font::Params, excluding position, alignment, etc.
struct Font
{
inline static const wi::font::Params params; // prototype for default values
wi::Color color = params.color;
wi::Color shadow_color = params.shadowColor;
int style = params.style;
float softness = params.softness;
float bolden = params.bolden;
float shadow_softness = params.shadow_softness;
float shadow_bolden = params.shadow_bolden;
float shadow_offset_x = params.shadow_offset_x;
float shadow_offset_y = params.shadow_offset_y;
void Apply(wi::font::Params& params) const
{
params.color = color;
params.shadowColor = shadow_color;
params.style = style;
params.softness = softness;
params.bolden = bolden;
params.shadow_softness = shadow_softness;
params.shadow_bolden = shadow_bolden;
params.shadow_offset_x = shadow_offset_x;
params.shadow_offset_y = shadow_offset_y;
}
void CopyFrom(const wi::font::Params& params)
{
color = params.color;
shadow_color = params.shadowColor;
style = params.style;
softness = params.softness;
bolden = params.bolden;
shadow_softness = params.shadow_softness;
shadow_bolden = params.shadow_bolden;
shadow_offset_x = params.shadow_offset_x;
shadow_offset_y = params.shadow_offset_y;
}
} font;
float shadow = -1; // shadow radius, if less than 0, it won't be used to override
wi::Color shadow_color = wi::Color::Shadow(); // shadow color for whole widget
bool shadow_highlight = false;
XMFLOAT3 shadow_highlight_color = XMFLOAT3(1, 1, 1);
float shadow_highlight_spread = 1;
Image tooltipImage;
Font tooltipFont;
float tooltip_shadow = -1; // shadow radius, if less than 0, it won't be used to override
wi::Color tooltip_shadow_color = wi::Color::Shadow();
};
class Widget;
class ComboBox;
class GUI
{
private:
wi::vector<Widget*> widgets;
bool focus = false;
bool visible = true;
public:
void Update(const wi::Canvas& canvas, float dt);
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const;
void AddWidget(Widget* widget);
void RemoveWidget(Widget* widget);
Widget* GetWidget(const std::string& name);
// returns true if any gui element has the focus
bool HasFocus() const;
// returns true if text input is happening
bool IsTyping() const;
void SetVisible(bool value) { visible = value; }
bool IsVisible() const { return visible; }
void SetColor(wi::Color color, int id = -1);
void SetImage(const wi::Resource& resource, int id = -1);
void SetShadowColor(wi::Color color);
void SetTheme(const Theme& theme, int id = -1);
void ExportLocalization(wi::Localization& localization) const;
void ImportLocalization(const wi::Localization& localization);
};
class Widget : public wi::scene::TransformComponent
{
friend class ComboBox;
private:
int tooltipTimer = 0;
protected:
std::string name;
bool enabled = true;
bool visible = true;
LocalizationEnabled localization_enabled = LocalizationEnabled::All;
float shadow = 1; // shadow radius
wi::Color shadow_color = wi::Color::Shadow();
bool shadow_highlight = false;
XMFLOAT3 shadow_highlight_color = XMFLOAT3(1, 1, 1);
float shadow_highlight_spread = 1;
WIDGETSTATE state = IDLE;
float tooltip_shadow = 1; // shadow radius
wi::Color tooltip_shadow_color = wi::Color::Shadow();
mutable wi::Sprite tooltipSprite;
mutable wi::SpriteFont tooltipFont;
mutable wi::SpriteFont scripttipFont;
float angular_highlight_width = 0;
float angular_highlight_timer = 0;
XMFLOAT4 angular_highlight_color = XMFLOAT4(1, 1, 1, 1);
float left_text_width = 0;
float right_text_width = 0;
public:
Widget();
virtual ~Widget() = default;
// Delete copy/move to keep internal references stable.
Widget(const Widget &) = delete;
const std::string& GetName() const;
void SetName(const std::string& value);
std::string GetText() const;
std::string GetTooltip() const;
void SetText(const char* value);
void SetText(const std::string& value);
void SetText(std::string&& value);
void SetTooltip(const std::string& value);
void SetTooltip(std::string&& value);
void SetScriptTip(const std::string& value);
void SetScriptTip(std::string&& value);
void SetPos(const XMFLOAT2& value);
void SetSize(const XMFLOAT2& value);
XMFLOAT2 GetPos() const;
virtual XMFLOAT2 GetSize() const;
virtual WIDGETSTATE GetState() const;
virtual void SetEnabled(bool val);
bool IsEnabled() const;
virtual void SetVisible(bool val);
bool IsVisible() const;
wi::Color GetColor() const;
float GetShadowRadius() const { return shadow; }
void SetShadowRadius(float value) { shadow = value; }
bool IsShadowHighlightEnabled() { return shadow_highlight; }
void SetShadowHighlightEnabled(bool value) { shadow_highlight = value; }
XMFLOAT3 GetShadowHighlightColor() const { return shadow_highlight_color; }
void SetShadowHighlightColor(const XMFLOAT3& value) { shadow_highlight_color = value; }
float GetShadowHighlightSpread() const { return shadow_highlight_spread; }
void SetShadowHighlightSpread(float value) { shadow_highlight_spread = value; }
virtual void ResizeLayout() {};
virtual void Update(const wi::Canvas& canvas, float dt);
virtual void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const;
virtual void RenderTooltip(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const;
// last param default: set color for all states
// you can specify a WIDGET_ID here, or your own custom ID if you use your own widget type
virtual void SetColor(wi::Color color, int id = -1);
virtual void SetShadowColor(wi::Color color);
virtual void SetImage(wi::Resource textureResource, int id = -1);
virtual void SetTheme(const Theme& theme, int id = -1);
virtual const char* GetWidgetTypeName() const { return "Widget"; }
wi::Sprite sprites[WIDGETSTATE_COUNT];
wi::SpriteFont font;
XMFLOAT3 translation = XMFLOAT3(0, 0, 0);
XMFLOAT3 scale = XMFLOAT3(1, 1, 1);
wi::primitive::Hitbox2D hitBox;
wi::graphics::Rect scissorRect;
Widget* parent = nullptr;
void AttachTo(Widget* parent);
void Detach();
virtual void Activate();
virtual void Deactivate();
void ApplyScissor(const wi::Canvas& canvas, const wi::graphics::Rect rect, wi::graphics::CommandList cmd, bool constrain_to_parent = true) const;
wi::primitive::Hitbox2D GetPointerHitbox(bool constrained = true) const;
XMFLOAT2 GetPointerHighlightPos(const wi::Canvas& canvas) const;
wi::primitive::Hitbox2D active_area; // Pointer hitbox constrain area
void HitboxConstrain(wi::primitive::Hitbox2D& hb) const;
bool priority_change = true;
uint32_t priority = 0;
bool force_disable = false;
bool IsLocalizationEnabled() const { return localization_enabled != LocalizationEnabled::None; }
LocalizationEnabled GetLocalizationEnabled() const { return localization_enabled; }
void SetLocalizationEnabled(LocalizationEnabled value) { localization_enabled = value; }
void SetLocalizationEnabled(bool value) { localization_enabled = value ? LocalizationEnabled::All : LocalizationEnabled::None; }
virtual void ExportLocalization(wi::Localization& localization) const;
virtual void ImportLocalization(const wi::Localization& localization);
void SetAngularHighlightWidth(float value) { angular_highlight_width = value; };
float GetAngularHighlightWidth() const { return angular_highlight_width; };
void SetAngularHighlightColor(const XMFLOAT4& value) { angular_highlight_color = value; };
XMFLOAT4 GetAngularHighlightColor() const { return angular_highlight_color; };
constexpr float GetLeftTextWidth() const { return left_text_width; }
constexpr float GetRightTextWidth() const { return right_text_width; }
};
// Clickable, draggable box
class Button : public Widget
{
protected:
std::function<void(const EventArgs& args)> onClick;
std::function<void(const EventArgs& args)> onRightClick;
std::function<void(const EventArgs& args)> onDragStart;
std::function<void(const EventArgs& args)> onDrag;
std::function<void(const EventArgs& args)> onDragEnd;
XMFLOAT2 dragStart = XMFLOAT2(0, 0);
XMFLOAT2 prevPos = XMFLOAT2(0, 0);
bool disableClicking = false;
public:
void Create(const std::string& name);
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void SetTheme(const Theme& theme, int id = -1) override;
const char* GetWidgetTypeName() const override { return "Button"; }
void OnClick(std::function<void(const EventArgs& args)> func);
void OnRightClick(std::function<void(const EventArgs& args)> func);
void OnDragStart(std::function<void(const EventArgs& args)> func);
void OnDrag(std::function<void(const EventArgs& args)> func);
void OnDragEnd(std::function<void(const EventArgs& args)> func);
wi::SpriteFont font_description;
void SetDescription(const std::string& desc) { font_description.SetText(desc); }
const std::string GetDescription() const { return font_description.GetTextA(); }
void DisableClickForCurrentDragOperation() { disableClicking = true; }
};
// Image display widget
class Image : public Widget
{
public:
void Create(const std::string& name);
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
const char* GetWidgetTypeName() const override { return "Image"; }
};
// Generic scroll bar
class ScrollBar : public Widget
{
protected:
float scrollbar_delta = 0;
float scrollbar_length = 0;
float scrollbar_value = 0;
float scrollbar_granularity = 1;
float list_length = 0;
float list_offset = 0;
float overscroll = 0;
bool vertical = true;
float safe_area = 0;
XMFLOAT2 grab_pos = {};
float grab_delta = 0;
public:
// Set the list's length that will be scrollable and moving
void SetListLength(float size) { list_length = size; }
// The scrolling offset that should be applied to the list items
float GetOffset() const { return list_offset; }
void SetOffset(float value);
// This can be called by user for extra scrolling on top of base functionality
// scroll sensitivity based on how much content is hidden (higher granularity = more amplification)
void Scroll(const float amount)
{
if (scrollbar_granularity < 1.0f)
{
scrollbar_delta -= amount * scrollbar_granularity / (1.0f - scrollbar_granularity);
}
}
// How much the max scrolling will offset the list even further than it would be necessary for fitting
// this value is in percent of a full scrollbar's worth of extra offset
// 0: no extra offset
// 1: full extra offset
void SetOverScroll(float amount) { overscroll = amount; }
// Check whether the scrollbar is required (when the items don't fit and scrolling could be used)
bool IsScrollbarRequired() const { return scrollbar_granularity < 1; }
// Check whether the scrollbar is at the beginning
bool IsScrolledToBegin() const { return scrollbar_delta <= 0; }
void SetSafeArea(float value) { safe_area = value; }
enum SCROLLBAR_STATE
{
SCROLLBAR_INACTIVE,
SCROLLBAR_HOVER,
SCROLLBAR_GRABBED,
SCROLLBAR_STATE_COUNT,
} scrollbar_state = SCROLLBAR_INACTIVE;
wi::Sprite sprites_knob[SCROLLBAR_STATE_COUNT];
XMFLOAT2 knob_inset_border = {};
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void SetColor(wi::Color color, int id = -1) override;
void SetTheme(const Theme& theme, int id = -1) override;
const char* GetWidgetTypeName() const override { return "ScrollBar"; }
void SetVertical(bool value) { vertical = value; }
bool IsVertical() const { return vertical; }
};
// Static box that holds text
class Label : public Widget
{
protected:
bool wrap_enabled = true;
bool fittext_enabled = false;
public:
void Create(const std::string& name);
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void SetColor(wi::Color color, int id = -1) override;
void SetTheme(const Theme& theme, int id = -1) override;
const char* GetWidgetTypeName() const override { return "Label"; }
float scrollbar_width = 18;
ScrollBar scrollbar;
void SetWrapEnabled(bool value) { wrap_enabled = value; }
void SetFitTextEnabled(bool value) { fittext_enabled = value; }
float margin_left = 0;
float margin_right = 0;
float margin_top = 0;
float margin_bottom = 0;
};
// Text input box
class TextInputField : public Widget
{
protected:
std::function<void(const EventArgs& args)> onInputAccepted;
std::function<void(const EventArgs& args)> onInput;
static wi::SpriteFont font_input;
bool cancel_input_enabled = true;
int float_precision = -1;
public:
void Create(const std::string& name);
void SetValue(const std::string& newValue);
void SetValue(int newValue);
void SetValue(float newValue);
const std::string GetValue();
const std::string GetCurrentInputValue();
// Set whether incomplete input will be removed on lost activation state (default: true)
void SetCancelInputEnabled(bool value) { cancel_input_enabled = value; }
bool IsCancelInputEnabled() const { return cancel_input_enabled; }
// There can only be ONE active text input field, so these methods modify the active one
static void AddInput(const wchar_t inputChar);
static void AddInput(const char inputChar);
static void DeleteFromInput(int direction = -1);
// Sets this text input as currently active in typing state
// selectall params: selects the current input, so typing will immediately overwrite it
void SetAsActive(bool selectall = false);
void Activate() override;
void Deactivate() override;
void SetEnabled(bool val) override;
void SetVisible(bool val) override;
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void SetColor(wi::Color color, int id = -1) override;
void SetTheme(const Theme& theme, int id = -1) override;
const char* GetWidgetTypeName() const override { return "TextInputField"; }
// Called when input was accepted with ENTER key:
void OnInputAccepted(std::function<void(const EventArgs& args)> func);
// Called when input was updated with new character:
void OnInput(std::function<void(const EventArgs& args)> func);
wi::SpriteFont font_description;
void SetDescription(const std::string& desc) { font_description.SetText(desc); }
const std::string GetDescription() const { return font_description.GetTextA(); }
// Set number of fractional float digits (-1 = default)
constexpr void SetFloatPrecision(int value) { float_precision = value; }
};
// Define an interval and slide the control along it
class Slider : public Widget
{
protected:
std::function<void(const EventArgs& args)> onSlide;
float start = 0, end = 1;
float step = 1000;
float value = 0;
public:
// start : slider minimum value
// end : slider maximum value
// defaultValue : slider default Value
// step : slider step size
void Create(float start, float end, float defaultValue, float step, const std::string& name);
wi::Sprite sprites_knob[WIDGETSTATE_COUNT];
void SetValue(float value);
void SetValue(int value);
float GetValue() const;
void SetRange(float start, float end);
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void RenderTooltip(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void SetColor(wi::Color color, int id = -1) override;
void SetTheme(const Theme& theme, int id = -1) override;
const char* GetWidgetTypeName() const override { return "Slider"; }
WIDGETSTATE GetState() const override { return std::max(state, valueInputField.GetState()); };
void OnSlide(std::function<void(const EventArgs& args)> func);
TextInputField valueInputField;
};
// Two-state clickable box
class CheckBox :public Widget
{
protected:
std::function<void(const EventArgs& args)> onClick;
bool checked = false;
std::wstring check_text;
std::wstring uncheck_text;
public:
void Create(const std::string& name);
void SetCheck(bool value);
bool GetCheck() const;
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
const char* GetWidgetTypeName() const override { return "CheckBox"; }
void OnClick(std::function<void(const EventArgs& args)> func);
static void SetCheckTextGlobal(const std::string& text);
void SetCheckText(const std::string& text);
void SetUnCheckText(const std::string& text);
};
// Drop-down list
class ComboBox :public Widget
{
protected:
std::function<void(const EventArgs& args)> onSelect;
int selected = -1;
int maxVisibleItemCount = 8;
int firstItemVisible = 0;
bool drop_arrow = true;
float fixed_drop_width = 0; // 0 = not fixed, takes width from base scale
// While the widget is active (rolled down) these are the inner states that control behaviour
enum COMBOSTATE
{
COMBOSTATE_INACTIVE, // When the list is just being dropped down, or the widget is not active
COMBOSTATE_HOVER, // The widget is in drop-down state with the last item hovered highlited
COMBOSTATE_SELECTING, // The hovered item is clicked
COMBOSTATE_SCROLLBAR_HOVER, // scrollbar is to be selected
COMBOSTATE_SCROLLBAR_GRABBED, // scrollbar is moved
COMBOSTATE_FILTER_INTERACT, // interaction with filter sub-widget
COMBOSTATE_COUNT,
} combostate = COMBOSTATE_INACTIVE;
int hovered = -1;
float scrollbar_delta = 0;
struct Item
{
std::string name;
uint64_t userdata = 0;
};
wi::vector<Item> items;
wi::Color drop_color = wi::Color::Ghost();
std::wstring invalid_selection_text;
TextInputField filter;
std::string filterText;
int filteredItemCount = 0;
float GetDropOffset(const wi::Canvas& canvas) const;
float GetDropX(const wi::Canvas& canvas) const;
float GetItemOffset(const wi::Canvas& canvas, int index) const;
public:
void Create(const std::string& name);
void AddItem(const std::string& name, uint64_t userdata = 0);
void RemoveItem(int index);
void ClearItems();
void SetMaxVisibleItemCount(int value);
bool HasScrollbar() const;
void SetSelected(int index);
void SetSelectedWithoutCallback(int index); // SetSelected() but the OnSelect callback will not be executed
void SetSelectedByUserdata(uint64_t userdata);
void SetSelectedByUserdataWithoutCallback(uint64_t userdata); // SetSelectedByUserdata() but the OnSelect callback will not be executed
int GetSelected() const;
uint64_t GetSelectedUserdata() const;
void SetItemText(int index, const std::string& text);
void SetItemUserdata(int index, uint64_t userdata);
std::string GetItemText(int index) const;
uint64_t GetItemUserData(int index) const;
size_t GetItemCount() const { return items.size(); }
void SetInvalidSelectionText(const std::string& text);
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void SetColor(wi::Color color, int id = -1) override;
void SetTheme(const Theme& theme, int id = -1) override;
const char* GetWidgetTypeName() const override { return "ComboBox"; }
void OnSelect(std::function<void(const EventArgs& args)> func);
wi::SpriteFont selected_font;
void ExportLocalization(wi::Localization& localization) const override;
void ImportLocalization(const wi::Localization& localization) override;
void SetDropArrowEnabled(bool value) { drop_arrow = value; }
bool IsDropArrowEnabled() const { return drop_arrow; }
void SetFixedDropWidth(float value) { fixed_drop_width = value; }
float GetFixedDropWidth() const { return fixed_drop_width; }
};
// Widget container
class Window :public Widget
{
protected:
wi::vector<Widget*> widgets;
bool minimized = false;
bool has_titlebar = false;
bool right_aligned_image = false;
Widget scrollable_area;
float control_size = 20;
std::function<void(const EventArgs& args)> onClose;
std::function<void(const EventArgs& args)> onCollapse;
std::function<void()> onResize;
float resizehitboxwidth = 6;
enum RESIZE_STATE
{
RESIZE_STATE_NONE,
RESIZE_STATE_LEFT,
RESIZE_STATE_TOP,
RESIZE_STATE_RIGHT,
RESIZE_STATE_BOTTOM,
RESIZE_STATE_TOPLEFT,
RESIZE_STATE_TOPRIGHT,
RESIZE_STATE_BOTTOMRIGHT,
RESIZE_STATE_BOTTOMLEFT,
} resize_state = RESIZE_STATE_NONE;
XMFLOAT2 resize_begin = XMFLOAT2(0, 0);
float resize_blink_timer = 0;
struct Layout
{
float padding = 2;
float width = 0;
float height = 0;
float y = 0;
float x = 0;
float margin_left = 160;
// Reset layout state:
void reset(Window& window)
{
const XMFLOAT2 widgetareasize = window.GetWidgetAreaSize();
width = widgetareasize.x;
height = widgetareasize.y - window.GetControlSize();
y = padding;
}
// Jump over an empty space to visually separate widgets a bit:
void jump(float amount = 20)
{
y += amount;
}
// Add one widget in a row:
void add(wi::gui::Widget& widget)
{
if (!widget.IsVisible())
return;
const XMFLOAT2 size = widget.GetSize();
x = margin_left;
y += widget.GetShadowRadius();
widget.SetPos(XMFLOAT2(x, y));
widget.SetSize(XMFLOAT2(width - x - padding - widget.GetShadowRadius(), size.y));
y += size.y;
y += widget.GetShadowRadius();
y += padding;
}
// Add one widget to the right side:
void add_right(wi::gui::Widget& widget)
{
if (!widget.IsVisible())
return;
const XMFLOAT2 size = widget.GetSize();
x = width - padding - size.x;
x -= widget.GetRightTextWidth();
x -= widget.GetShadowRadius();
y += widget.GetShadowRadius();
widget.SetPos(XMFLOAT2(x, y));
if (widget.GetLeftTextWidth() > 0)
{
x -= widget.GetLeftTextWidth() + padding * 4;
}
x -= padding;
x -= widget.GetShadowRadius();
y += size.y;
y += widget.GetShadowRadius();
y += padding;
}
// Add one widget to fill the whole width:
void add_fullwidth(wi::gui::Widget& widget)
{
if (!widget.IsVisible())
return;
const XMFLOAT2 size = widget.GetSize();
x = padding;
x += widget.GetLeftTextWidth();
x += widget.GetShadowRadius();
y += widget.GetShadowRadius();
widget.SetPos(XMFLOAT2(x, y));
widget.SetSize(XMFLOAT2(width - x - padding - widget.GetShadowRadius(), size.y));
y += size.y;
y += widget.GetShadowRadius();
y += padding;
}
// Add one widget to fill the whole width and keep aspect ratio:
void add_fullwidth_aspect(wi::gui::Widget& widget)
{
if (!widget.IsVisible())
return;
x = padding;
x += widget.GetLeftTextWidth();
x += widget.GetShadowRadius();
y += widget.GetShadowRadius();
widget.SetPos(XMFLOAT2(margin_left, y));
float h_aspect = widget.GetSize().y / widget.GetSize().x;
const wi::Resource& res = widget.sprites[widget.GetState()].textureResource;
if (res.IsValid())
{
const wi::graphics::Texture& tex = res.GetTexture();
if (has_flag(tex.desc.misc_flags, wi::graphics::ResourceMiscFlag::TEXTURECUBE))
{
// fixed aspect for cubemap cross:
h_aspect = 3.0f / 4.0f;
}
else
{
h_aspect = (float)tex.desc.height / (float)tex.desc.width;
}
}
const float w = width - x - padding - widget.GetShadowRadius();
widget.SetSize(XMFLOAT2(w, w * h_aspect));
widget.SetPos(XMFLOAT2(x, y));
y += widget.GetSize().y;
y += widget.GetShadowRadius();
y += padding;
}
// Add multiple widgets in a row aligned to the right side:
template<typename... Args>
void add_right(wi::gui::Widget& widget, Args&&... args)
{
add_right(std::forward<Args>(args)...);
if (!widget.IsVisible())
return;
const XMFLOAT2 size = widget.GetSize();
x -= size.x;
x -= widget.GetRightTextWidth();
x -= widget.GetShadowRadius();
widget.SetPos(XMFLOAT2(x, y - size.y - padding - widget.GetShadowRadius()));
if (widget.GetLeftTextWidth() > 0)
{
x -= widget.GetLeftTextWidth() + padding * 4;
}
x -= padding;
x -= widget.GetShadowRadius();
}
void helper_fitx(float sizx, wi::gui::Widget& widget)
{
float left = widget.GetLeftTextWidth();
if (left > 0)
left += padding;
widget.SetSize(XMFLOAT2(sizx - left, widget.GetSize().y));
}
template<typename... Args>
void helper_fitx(float sizx, wi::gui::Widget& widget, Args&&... args)
{
helper_fitx(sizx, widget);
helper_fitx(sizx, std::forward<Args>(args)...);
}
// Add multiple widgets in a row, equally sized to fit
template<typename... Args>
void add(wi::gui::Widget& widget, Args&&... args)
{
constexpr size_t total_count = 1 + sizeof...(Args);
const float onewidth = (width - padding) / total_count - (padding * (total_count - 1));
helper_fitx(onewidth, widget, std::forward<Args>(args)...);
add_right(widget, std::forward<Args>(args)...);
}
} layout;
public:
enum class WindowControls
{
NONE = 0,
RESIZE_LEFT = 1 << 0,
RESIZE_TOP = 1 << 1,
RESIZE_RIGHT = 1 << 2,
RESIZE_BOTTOM = 1 << 3,
RESIZE_TOPLEFT = 1 << 4,
RESIZE_TOPRIGHT = 1 << 5,
RESIZE_BOTTOMLEFT = 1 << 6,
RESIZE_BOTTOMRIGHT = 1 << 7,
MOVE = 1 << 8,
CLOSE = 1 << 9,
COLLAPSE = 1 << 10,
DISABLE_TITLE_BAR = 1 << 11,
FIT_ALL_WIDGETS_VERTICAL = 1 << 12, // auto resize window vertically to fit all widgets that are in it
RESIZE = RESIZE_LEFT | RESIZE_TOP | RESIZE_RIGHT | RESIZE_BOTTOM | RESIZE_TOPLEFT | RESIZE_TOPRIGHT | RESIZE_BOTTOMLEFT | RESIZE_BOTTOMRIGHT,
CLOSE_AND_COLLAPSE = CLOSE | COLLAPSE,
ALL = RESIZE | MOVE | CLOSE | COLLAPSE,
};
void Create(const std::string& name, WindowControls window_controls = WindowControls::ALL);
enum class AttachmentOptions
{
NONE = 0,
SCROLLABLE = 1 << 0,
};
void AddWidget(Widget* widget, AttachmentOptions options = AttachmentOptions::SCROLLABLE);
void RemoveWidget(Widget* widget);
void RemoveWidgets();
void ResizeLayout() override;
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void RenderTooltip(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void SetColor(wi::Color color, int id = -1) override;
void SetImage(wi::Resource resource, int id = -1) override;
void SetShadowColor(wi::Color color) override;
void SetTheme(const Theme& theme, int id = -1) override;
const char* GetWidgetTypeName() const override { return "Window"; }
void SetVisible(bool value) override;
void SetEnabled(bool value) override;
void SetCollapsed(bool value);
bool IsCollapsed() const;
void SetMinimized(bool value); // Same as SetCollapsed()
bool IsMinimized() const; // Same as IsCollapsed()
void SetControlSize(float value);
float GetControlSize() const;
XMFLOAT2 GetSize() const override; // For the window, the returned size can be modified by collapsed state
XMFLOAT2 GetWidgetAreaSize() const;
void OnClose(std::function<void(const EventArgs& args)> func);
void OnCollapse(std::function<void(const EventArgs& args)> func);
void OnResize(std::function<void()> func);
Button closeButton;
Button collapseButton;
Button moveDragger;
Label label;
ScrollBar scrollbar_vertical;
ScrollBar scrollbar_horizontal;
WindowControls controls;
// Set whether the background image should be aligned to the right side while keeping aspect ratio
void SetRightAlignedImage(bool value) { right_aligned_image = value; }
void ExportLocalization(wi::Localization& localization) const override;
void ImportLocalization(const wi::Localization& localization) override;
wi::graphics::Texture background_overlay;
};
// HSV-Color Picker
class ColorPicker : public Window
{
protected:
std::function<void(const EventArgs& args)> onColorChanged;
enum COLORPICKERSTATE
{
CPS_IDLE,
CPS_HUE,
CPS_SATURATION,
} colorpickerstate = CPS_IDLE;
float hue = 0.0f; // [0, 360] degrees
float saturation = 0.0f; // [0, 1]
float luminance = 1.0f; // [0, 1]
void FireEvents();
public:
void Create(const std::string& name, WindowControls window_controls = WindowControls::ALL);
void Update(const wi::Canvas& canvas, float dt) override;
void Render(const wi::Canvas& canvas, wi::graphics::CommandList cmd) const override;
void ResizeLayout() override;
void SetColor(wi::Color color, int id = -1) override;
void SetImage(wi::Resource resource, int id = -1) override;