/
View.h
1073 lines (876 loc) · 30 KB
/
View.h
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
// More traditional UI framework than ui/ui.h.
// Still very simple to use.
// Works very similarly to Android, there's a Measure pass and a Layout pass which you don't
// really need to care about if you just use the standard containers and widgets.
#include <cmath>
#include <cstdio>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "Common/Render/TextureAtlas.h"
#include "Common/Math/lin/matrix4x4.h"
#include "Common/Math/math_util.h"
#include "Common/Math/geom2d.h"
#include "Common/Input/KeyCodes.h"
#include "Common/Common.h"
#undef small
struct KeyInput;
struct TouchInput;
struct AxisInput;
struct ImageID;
class DrawBuffer;
class Texture;
class UIContext;
namespace Draw {
class DrawContext;
class Texture;
}
// I don't generally like namespaces but I think we do need one for UI, so many potentially-clashing names.
namespace UI {
class View;
enum DrawableType {
DRAW_NOTHING,
DRAW_SOLID_COLOR,
DRAW_4GRID,
DRAW_STRETCH_IMAGE,
};
enum Visibility {
V_VISIBLE,
V_INVISIBLE, // Keeps position, not drawn or interacted with
V_GONE, // Does not participate in layout
};
struct Drawable {
Drawable() : type(DRAW_NOTHING), image(ImageID::invalid()), color(0xFFFFFFFF) {}
explicit Drawable(uint32_t col) : type(DRAW_SOLID_COLOR), image(ImageID::invalid()), color(col) {}
Drawable(DrawableType t, ImageID img, uint32_t col = 0xFFFFFFFF) : type(t), image(img), color(col) {}
DrawableType type;
ImageID image;
uint32_t color;
};
struct Style {
Style() : fgColor(0xFFFFFFFF), background(0xFF303030), image(ImageID::invalid()) {}
uint32_t fgColor;
Drawable background;
ImageID image; // where applicable.
};
struct FontStyle {
FontStyle() {}
FontStyle(FontID atlasFnt, const char *name, int size) : atlasFont(atlasFnt), fontName(name), sizePts(size) {}
FontID atlasFont{ nullptr };
// For native fonts:
std::string fontName;
int sizePts = 0;
int flags = 0;
};
// To use with an UI atlas.
struct Theme {
FontStyle uiFont;
FontStyle uiFontSmall;
FontStyle uiFontSmaller;
ImageID checkOn;
ImageID checkOff;
ImageID sliderKnob;
ImageID whiteImage;
ImageID dropShadow4Grid;
Style itemStyle;
Style itemDownStyle;
Style itemFocusedStyle;
Style itemDisabledStyle;
Style headerStyle;
Style infoStyle;
Style popupStyle;
uint32_t backgroundColor;
};
// The four cardinal directions should be enough, plus Prev/Next in "element order".
enum FocusDirection {
FOCUS_UP,
FOCUS_DOWN,
FOCUS_LEFT,
FOCUS_RIGHT,
FOCUS_NEXT,
FOCUS_PREV,
FOCUS_FIRST,
FOCUS_LAST,
FOCUS_PREV_PAGE,
FOCUS_NEXT_PAGE,
};
typedef float Size; // can also be WRAP_CONTENT or FILL_PARENT.
static constexpr Size WRAP_CONTENT = -1.0f;
static constexpr Size FILL_PARENT = -2.0f;
// Gravity
enum Gravity {
G_LEFT = 0,
G_RIGHT = 1,
G_HCENTER = 2,
G_HORIZMASK = 3,
G_TOP = 0,
G_BOTTOM = 4,
G_VCENTER = 8,
G_TOPLEFT = G_TOP | G_LEFT,
G_TOPRIGHT = G_TOP | G_RIGHT,
G_BOTTOMLEFT = G_BOTTOM | G_LEFT,
G_BOTTOMRIGHT = G_BOTTOM | G_RIGHT,
G_CENTER = G_HCENTER | G_VCENTER,
G_VERTMASK = 3 << 2,
};
enum Borders {
BORDER_NONE = 0,
BORDER_TOP = 0x0001,
BORDER_LEFT = 0x0002,
BORDER_BOTTOM = 0x0004,
BORDER_RIGHT = 0x0008,
BORDER_HORIZ = BORDER_LEFT | BORDER_RIGHT,
BORDER_VERT = BORDER_TOP | BORDER_BOTTOM,
BORDER_ALL = BORDER_TOP | BORDER_LEFT | BORDER_BOTTOM | BORDER_RIGHT,
};
enum class BorderStyle {
HEADER_FG,
ITEM_DOWN_BG,
};
enum Orientation {
ORIENT_HORIZONTAL,
ORIENT_VERTICAL,
};
inline Orientation Opposite(Orientation o) {
if (o == ORIENT_HORIZONTAL) return ORIENT_VERTICAL; else return ORIENT_HORIZONTAL;
}
inline FocusDirection Opposite(FocusDirection d) {
switch (d) {
case FOCUS_UP: return FOCUS_DOWN;
case FOCUS_DOWN: return FOCUS_UP;
case FOCUS_LEFT: return FOCUS_RIGHT;
case FOCUS_RIGHT: return FOCUS_LEFT;
case FOCUS_PREV: return FOCUS_NEXT;
case FOCUS_NEXT: return FOCUS_PREV;
case FOCUS_FIRST: return FOCUS_LAST;
case FOCUS_LAST: return FOCUS_FIRST;
case FOCUS_PREV_PAGE: return FOCUS_NEXT_PAGE;
case FOCUS_NEXT_PAGE: return FOCUS_PREV_PAGE;
}
return d;
}
enum MeasureSpecType {
UNSPECIFIED,
EXACTLY,
AT_MOST,
};
// I hope I can find a way to simplify this one day.
enum EventReturn {
EVENT_DONE, // Return this when no other view may process this event, for example if you changed the view hierarchy
EVENT_SKIPPED, // Return this if you ignored an event
EVENT_CONTINUE, // Return this if it's safe to send this event to further listeners. This should normally be the default choice but often EVENT_DONE is necessary.
};
enum FocusFlags {
FF_LOSTFOCUS = 1,
FF_GOTFOCUS = 2
};
enum PersistStatus {
PERSIST_SAVE,
PERSIST_RESTORE,
};
typedef std::vector<int> PersistBuffer;
typedef std::map<std::string, UI::PersistBuffer> PersistMap;
class ViewGroup;
struct MeasureSpec {
MeasureSpec(MeasureSpecType t, float s = 0.0f) : type(t), size(s) {}
MeasureSpec() : type(UNSPECIFIED), size(0) {}
MeasureSpec operator -(float amount) {
// TODO: Check type
return MeasureSpec(type, size - amount);
}
MeasureSpecType type;
float size;
};
// Should cover all bases.
struct EventParams {
View *v;
uint32_t a, b, x, y;
float f;
std::string s;
};
struct HandlerRegistration {
std::function<EventReturn(EventParams&)> func;
};
class Event {
public:
Event() {}
~Event();
// Call this from input thread or whatever, it doesn't matter
void Trigger(EventParams &e);
// Call this from UI thread
EventReturn Dispatch(EventParams &e);
// This is suggested for use in most cases. Autobinds, allowing for neat syntax.
template<class T>
T *Handle(T *thiz, EventReturn (T::* theCallback)(EventParams &e)) {
Add(std::bind(theCallback, thiz, std::placeholders::_1));
return thiz;
}
// Sometimes you have an already-bound function<>, just use this then.
void Add(std::function<EventReturn(EventParams&)> func);
private:
std::vector<HandlerRegistration> handlers_;
DISALLOW_COPY_AND_ASSIGN(Event);
};
struct Margins {
Margins() : top(0), bottom(0), left(0), right(0) {}
explicit Margins(int8_t all) : top(all), bottom(all), left(all), right(all) {}
Margins(int8_t horiz, int8_t vert) : top(vert), bottom(vert), left(horiz), right(horiz) {}
Margins(int8_t l, int8_t t, int8_t r, int8_t b) : top(t), bottom(b), left(l), right(r) {}
int horiz() const {
return left + right;
}
int vert() const {
return top + bottom;
}
void SetAll(float f) {
int8_t i = (int)f;
top = i;
bottom = i;
left = i;
right = i;
}
int8_t top;
int8_t bottom;
int8_t left;
int8_t right;
};
struct Padding {
Padding() : top(0), bottom(0), left(0), right(0) {}
explicit Padding(float all) : top(all), bottom(all), left(all), right(all) {}
Padding(float horiz, float vert) : top(vert), bottom(vert), left(horiz), right(horiz) {}
Padding(float l, float t, float r, float b) : top(t), bottom(b), left(l), right(r) {}
float horiz() const {
return left + right;
}
float vert() const {
return top + bottom;
}
float top;
float bottom;
float left;
float right;
};
enum LayoutParamsType {
LP_PLAIN = 0,
LP_LINEAR = 1,
LP_ANCHOR = 2,
LP_GRID = 3,
};
// Need a virtual destructor so vtables are created, otherwise RTTI can't work
class LayoutParams {
public:
LayoutParams(LayoutParamsType type = LP_PLAIN)
: width(WRAP_CONTENT), height(WRAP_CONTENT), type_(type) {}
LayoutParams(Size w, Size h, LayoutParamsType type = LP_PLAIN)
: width(w), height(h), type_(type) {}
virtual ~LayoutParams() {}
Size width;
Size height;
// Fake RTTI
bool Is(LayoutParamsType type) const { return type_ == type; }
template <typename T>
T *As() {
if (Is(T::StaticType())) {
return static_cast<T *>(this);
}
return nullptr;
}
template <typename T>
const T *As() const {
if (Is(T::StaticType())) {
return static_cast<const T *>(this);
}
return nullptr;
}
static LayoutParamsType StaticType() {
return LP_PLAIN;
}
private:
LayoutParamsType type_;
};
View *GetFocusedView();
class Tween;
class CallbackColorTween;
class View {
public:
View(LayoutParams *layoutParams = 0) : layoutParams_(layoutParams) {
if (!layoutParams)
layoutParams_.reset(new LayoutParams());
}
virtual ~View();
// Please note that Touch is called ENTIRELY asynchronously from drawing!
// Can even be called on a different thread! This is to really minimize latency, and decouple
// touch response from the frame rate. Same with Key and Axis.
virtual bool Key(const KeyInput &input) { return false; }
virtual bool Touch(const TouchInput &input) { return true; }
virtual void Axis(const AxisInput &input) {}
virtual void Update();
virtual void DeviceLost() {}
virtual void DeviceRestored(Draw::DrawContext *draw) {}
// If this view covers these coordinates, it should add itself and its children to the list.
virtual void Query(float x, float y, std::vector<View *> &list);
virtual std::string DescribeLog() const;
// Accessible/searchable description.
virtual std::string DescribeText() const { return ""; }
virtual void FocusChanged(int focusFlags) {}
virtual void PersistData(PersistStatus status, std::string anonId, PersistMap &storage);
void Move(Bounds bounds) {
bounds_ = bounds;
}
// Views don't do anything here in Layout, only containers implement this.
virtual void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert);
virtual void Layout() {}
virtual void Draw(UIContext &dc) {}
virtual float GetMeasuredWidth() const { return measuredWidth_; }
virtual float GetMeasuredHeight() const { return measuredHeight_; }
// Override this for easy standard behaviour. No need to override Measure.
virtual void GetContentDimensions(const UIContext &dc, float &w, float &h) const;
virtual void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const;
// Called when the layout is done.
void SetBounds(Bounds bounds) { bounds_ = bounds; }
virtual const LayoutParams *GetLayoutParams() const { return layoutParams_.get(); }
virtual void ReplaceLayoutParams(LayoutParams *newLayoutParams) { layoutParams_.reset(newLayoutParams); }
const Bounds &GetBounds() const { return bounds_; }
virtual bool SetFocus();
virtual bool CanBeFocused() const { return true; }
virtual bool SubviewFocused(View *view) { return false; }
bool HasFocus() const {
return GetFocusedView() == this;
}
void SetEnabled(bool enabled) {
enabledFunc_ = nullptr;
enabledPtr_ = nullptr;
enabled_ = enabled;
enabledMeansDisabled_ = false;
}
bool IsEnabled() const {
if (enabledFunc_)
return enabledFunc_() != enabledMeansDisabled_;
if (enabledPtr_)
return *enabledPtr_ != enabledMeansDisabled_;
return enabled_ != enabledMeansDisabled_;
}
void SetEnabledFunc(std::function<bool()> func) {
enabledFunc_ = func;
enabledPtr_ = nullptr;
enabledMeansDisabled_ = false;
}
void SetEnabledPtr(bool *enabled) {
enabledFunc_ = nullptr;
enabledPtr_ = enabled;
enabledMeansDisabled_ = false;
}
void SetDisabledPtr(bool *disabled) {
enabledFunc_ = nullptr;
enabledPtr_ = disabled;
enabledMeansDisabled_ = true;
}
virtual void SetVisibility(Visibility visibility) { visibility_ = visibility; }
Visibility GetVisibility() const { return visibility_; }
const std::string &Tag() const { return tag_; }
void SetTag(const std::string &str) { tag_ = str; }
// Fake RTTI
virtual bool IsViewGroup() const { return false; }
virtual bool ContainsSubview(const View *view) const { return false; }
Point GetFocusPosition(FocusDirection dir) const;
template <class T>
T *AddTween(T *t) {
tweens_.push_back(t);
return t;
}
protected:
// Inputs to layout
std::unique_ptr<LayoutParams> layoutParams_;
std::string tag_;
Visibility visibility_ = V_VISIBLE;
// Results of measure pass. Set these in Measure.
float measuredWidth_ = 0.0f;
float measuredHeight_ = 0.0f;
// Outputs of layout. X/Y are absolute screen coordinates, hierarchy is "gone" here.
Bounds bounds_{};
std::vector<Tween *> tweens_;
private:
std::function<bool()> enabledFunc_;
bool *enabledPtr_ = nullptr;
bool enabled_ = true;
bool enabledMeansDisabled_ = false;
DISALLOW_COPY_AND_ASSIGN(View);
};
// These don't do anything when touched.
class InertView : public View {
public:
InertView(LayoutParams *layoutParams)
: View(layoutParams) {}
bool Key(const KeyInput &input) override { return false; }
bool Touch(const TouchInput &input) override { return false; }
bool CanBeFocused() const override { return false; }
};
// All these light up their background when touched, or have focus.
class Clickable : public View {
public:
Clickable(LayoutParams *layoutParams);
bool Key(const KeyInput &input) override;
bool Touch(const TouchInput &input) override;
void FocusChanged(int focusFlags) override;
Event OnClick;
protected:
// Internal method that fires on a click. Default behaviour is to trigger
// the event.
// Use it for checking/unchecking checkboxes, etc.
virtual void Click();
void DrawBG(UIContext &dc, const Style &style);
CallbackColorTween *bgColor_ = nullptr;
float bgColorLast_ = 0.0f;
int downCountDown_ = 0;
bool dragging_ = false;
bool down_ = false;
};
// TODO: Very similar to Choice, should probably merge them.
// Right now more flexible image support though.
class Button : public Clickable {
public:
Button(const std::string &text, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), text_(text), imageID_(ImageID::invalid()) {}
Button(const std::string &text, ImageID imageID, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), text_(text), imageID_(imageID) {}
void Click() override;
void Draw(UIContext &dc) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
const std::string &GetText() const { return text_; }
std::string DescribeText() const override;
void SetPadding(int w, int h) {
paddingW_ = w;
paddingH_ = h;
}
void SetImageID(ImageID imageID) {
imageID_ = imageID;
}
void SetIgnoreText(bool ignore) {
ignoreText_ = ignore;
}
// Needed an extra small button...
void SetScale(float f) {
scale_ = f;
}
private:
Style style_;
std::string text_;
ImageID imageID_;
int paddingW_ = 16;
int paddingH_ = 8;
float scale_ = 1.0f;
bool ignoreText_ = false;
};
class RadioButton : public Clickable {
public:
RadioButton(int *value, int thisButtonValue, const std::string &text, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), value_(value), thisButtonValue_(thisButtonValue), text_(text) {}
void Click() override;
void Draw(UIContext &dc) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
std::string DescribeText() const override;
private:
int *value_;
int thisButtonValue_;
std::string text_;
const float paddingW_ = 8;
const float paddingH_ = 4;
const float radioRadius_ = 16.0f;
const float radioInnerRadius_ = 8.0f;
};
class Slider : public Clickable {
public:
Slider(int *value, int minValue, int maxValue, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), value_(value), showPercent_(false), minValue_(minValue), maxValue_(maxValue), paddingLeft_(5), paddingRight_(70), step_(1), repeat_(-1) {}
Slider(int *value, int minValue, int maxValue, int step = 1, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), value_(value), showPercent_(false), minValue_(minValue), maxValue_(maxValue), paddingLeft_(5), paddingRight_(70), repeat_(-1) {
step_ = step <= 0 ? 1 : step;
}
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
bool Key(const KeyInput &input) override;
bool Touch(const TouchInput &input) override;
void Update() override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
void SetShowPercent(bool s) { showPercent_ = s; }
// OK to call this from the outside after having modified *value_
void Clamp();
Event OnChange;
private:
bool ApplyKey(InputKeyCode keyCode);
int *value_;
bool showPercent_;
int minValue_;
int maxValue_;
float paddingLeft_;
float paddingRight_;
int step_;
int repeat_ = 0;
InputKeyCode repeatCode_ = NKCODE_UNKNOWN;
};
class SliderFloat : public Clickable {
public:
SliderFloat(float *value, float minValue, float maxValue, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), paddingLeft_(5), paddingRight_(70), repeat_(-1) {}
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
bool Key(const KeyInput &input) override;
bool Touch(const TouchInput &input) override;
void Update() override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
// OK to call this from the outside after having modified *value_
void Clamp();
Event OnChange;
private:
bool ApplyKey(InputKeyCode keyCode);
float *value_;
float minValue_;
float maxValue_;
float paddingLeft_;
float paddingRight_;
int repeat_;
InputKeyCode repeatCode_ = NKCODE_UNKNOWN;
};
// Basic button that modifies a bitfield based on the pressed status. Supports multitouch.
// Suitable for controller simulation (ABXY etc).
class TriggerButton : public View {
public:
TriggerButton(uint32_t *bitField, uint32_t bit, ImageID imageBackground, ImageID imageForeground, LayoutParams *layoutParams)
: View(layoutParams), down_(0.0), bitField_(bitField), bit_(bit), imageBackground_(imageBackground), imageForeground_(imageForeground) {}
bool Touch(const TouchInput &input) override;
void Draw(UIContext &dc) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
private:
int down_; // bitfield of pressed fingers, translates into bitField
uint32_t *bitField_;
uint32_t bit_;
ImageID imageBackground_;
ImageID imageForeground_;
};
// The following classes are mostly suitable as items in ListView which
// really is just a LinearLayout in a ScrollView, possibly with some special optimizations.
class Item : public InertView {
public:
Item(LayoutParams *layoutParams);
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
};
class ClickableItem : public Clickable {
public:
ClickableItem(LayoutParams *layoutParams);
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
// Draws the item background.
void Draw(UIContext &dc) override;
};
// Use to trigger something or open a submenu screen.
class Choice : public ClickableItem {
public:
Choice(const std::string &text, LayoutParams *layoutParams = nullptr)
: Choice(text, std::string(), false, layoutParams) {}
Choice(const std::string &text, ImageID image, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), text_(text), image_(image) {}
Choice(const std::string &text, const std::string &smallText, bool selected = false, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), text_(text), smallText_(smallText), image_(ImageID::invalid()) {}
Choice(ImageID image, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), image_(image), rightIconImage_(ImageID::invalid()) {}
Choice(ImageID image, float imgScale, float imgRot, bool imgFlipH = false, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), image_(image), rightIconImage_(ImageID::invalid()), imgScale_(imgScale), imgRot_(imgRot), imgFlipH_(imgFlipH) {}
void Click() override;
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
void SetCentered(bool c) {
centered_ = c;
}
void SetDrawTextFlags(u32 flags) {
drawTextFlags_ = flags;
}
void SetIcon(ImageID iconImage, float scale = 1.0f, float rot = 0.0f, bool flipH = false, bool keepColor = true) {
rightIconKeepColor_ = keepColor;
rightIconScale_ = scale;
rightIconRot_ = rot;
rightIconFlipH_ = flipH;
rightIconImage_ = iconImage;
}
protected:
// hackery
virtual bool IsSticky() const { return false; }
std::string text_;
std::string smallText_;
ImageID image_; // Centered if no text, on the left if text.
ImageID rightIconImage_ = ImageID::invalid(); // Shows in the right.
float rightIconScale_ = 0.0f;
float rightIconRot_ = 0.0f;
bool rightIconFlipH_ = false;
bool rightIconKeepColor_ = false;
Padding textPadding_;
bool centered_ = false;
float imgScale_ = 1.0f;
float imgRot_ = 0.0f;
bool imgFlipH_ = false;
u32 drawTextFlags_ = 0;
private:
bool selected_ = false;
};
// Different key handling.
class StickyChoice : public Choice {
public:
StickyChoice(const std::string &text, const std::string &smallText = "", LayoutParams *layoutParams = 0)
: Choice(text, smallText, false, layoutParams) {}
StickyChoice(ImageID buttonImage, LayoutParams *layoutParams = 0)
: Choice(buttonImage, layoutParams) {}
bool Key(const KeyInput &key) override;
bool Touch(const TouchInput &touch) override;
void FocusChanged(int focusFlags) override;
void Press() { down_ = true; dragging_ = false; }
void Release() { down_ = false; dragging_ = false; }
bool IsDown() { return down_; }
protected:
// hackery
bool IsSticky() const override { return true; }
};
class InfoItem : public Item {
public:
InfoItem(const std::string &text, const std::string &rightText, LayoutParams *layoutParams = nullptr);
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
// These are focusable so that long lists of them can be keyboard scrolled.
bool CanBeFocused() const override { return true; }
void SetText(const std::string &text) {
text_ = text;
}
const std::string &GetText() const {
return text_;
}
void SetRightText(const std::string &text) {
rightText_ = text;
}
void SetChoiceStyle(bool choiceStyle) {
choiceStyle_ = choiceStyle;
}
private:
CallbackColorTween *bgColor_ = nullptr;
CallbackColorTween *fgColor_ = nullptr;
std::string text_;
std::string rightText_;
bool choiceStyle_ = false;
};
class ItemHeader : public Item {
public:
ItemHeader(const std::string &text, LayoutParams *layoutParams = 0);
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;
private:
std::string text_;
};
class PopupHeader : public Item {
public:
PopupHeader(const std::string &text, LayoutParams *layoutParams = 0)
: Item(layoutParams), text_(text) {
layoutParams_->width = FILL_PARENT;
layoutParams_->height = 64;
}
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
private:
std::string text_;
};
class CheckBox : public ClickableItem {
public:
CheckBox(bool *toggle, const std::string &text, const std::string &smallText = "", LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), toggle_(toggle), text_(text), smallText_(smallText) {
OnClick.Handle(this, &CheckBox::OnClicked);
}
// Image-only "checkbox", lights up instead of showing a checkmark.
CheckBox(bool *toggle, ImageID imageID, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), toggle_(toggle), imageID_(imageID) {
OnClick.Handle(this, &CheckBox::OnClicked);
}
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
EventReturn OnClicked(EventParams &e);
//allow external agents to toggle the checkbox
virtual void Toggle();
virtual bool Toggled() const;
protected:
bool *toggle_;
std::string text_;
std::string smallText_;
ImageID imageID_;
};
class CollapsibleHeader : public CheckBox {
public:
CollapsibleHeader(bool *toggle, const std::string &text, LayoutParams *layoutParams = nullptr);
void Draw(UIContext &dc) override;
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
void SetHasSubitems(bool hasSubItems) { hasSubItems_ = hasSubItems; }
private:
bool hasSubItems_ = true;
};
class BitCheckBox : public CheckBox {
public:
BitCheckBox(uint32_t *bitfield, uint32_t bit, const std::string &text, const std::string &smallText = "", LayoutParams *layoutParams = nullptr)
: CheckBox(nullptr, text, smallText, layoutParams), bitfield_(bitfield), bit_(bit) {
}
BitCheckBox(int *bitfield, int bit, const std::string &text, const std::string &smallText = "", LayoutParams *layoutParams = nullptr) : BitCheckBox((uint32_t *)bitfield, (uint32_t)bit, text, smallText, layoutParams) {}
void Toggle() override;
bool Toggled() const override;
private:
uint32_t *bitfield_;
uint32_t bit_;
};
// These are for generic use.
class Spacer : public InertView {
public:
Spacer(LayoutParams *layoutParams = 0)
: InertView(layoutParams), size_(0.0f) {}
Spacer(float size, LayoutParams *layoutParams = 0)
: InertView(layoutParams), size_(size) {}
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override {
w = size_; h = size_;
}
void Draw(UIContext &dc) override {}
std::string DescribeText() const override { return ""; }
private:
float size_ = 0.0f;
};
class BorderView : public InertView {
public:
BorderView(Borders borderFlags, BorderStyle style, float size = 2.0f, LayoutParams *layoutParams = nullptr)
: InertView(layoutParams), borderFlags_(borderFlags), style_(style), size_(size) {
}
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;
void Draw(UIContext &dc) override;
std::string DescribeText() const override { return ""; }
private:
Borders borderFlags_;
BorderStyle style_;
float size_;
};
class TextView : public InertView {
public:
TextView(std::string_view text, LayoutParams *layoutParams = 0)
: InertView(layoutParams), text_(text), textAlign_(0), textColor_(0xFFFFFFFF), small_(false), shadow_(false), focusable_(false), clip_(true) {}
TextView(std::string_view text, int textAlign, bool small, LayoutParams *layoutParams = 0)
: InertView(layoutParams), text_(text), textAlign_(textAlign), textColor_(0xFFFFFFFF), small_(small), shadow_(false), focusable_(false), clip_(true) {}
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;
void Draw(UIContext &dc) override;
void SetText(const std::string &text) { text_ = text; }
const std::string &GetText() const { return text_; }
std::string DescribeText() const override { return GetText(); }
void SetSmall(bool small) { small_ = small; }
void SetTextColor(uint32_t color) { textColor_ = color; hasTextColor_ = true; }
void SetShadow(bool shadow) { shadow_ = shadow; }
void SetFocusable(bool focusable) { focusable_ = focusable; }
void SetClip(bool clip) { clip_ = clip; }
void SetBullet(bool bullet) { bullet_ = bullet; }
void SetPadding(float pad) { pad_ = pad; }
bool CanBeFocused() const override { return focusable_; }
private:
std::string text_;
int textAlign_;
uint32_t textColor_;
bool hasTextColor_ = false;
bool small_;
bool shadow_;
bool focusable_;
bool clip_;
bool bullet_ = false;
float pad_ = 0.0f;
};
class TextEdit : public View {
public:
TextEdit(const std::string &text, const std::string &title, const std::string &placeholderText, LayoutParams *layoutParams = nullptr);
void SetText(const std::string &text) { text_ = text; scrollPos_ = 0; caret_ = (int)text_.size(); }
void SetTextColor(uint32_t color) { textColor_ = color; hasTextColor_ = true; }
const std::string &GetText() const { return text_; }
void SetMaxLen(size_t maxLen) { maxLen_ = maxLen; }
void SetTextAlign(int align) { align_ = align; } // Only really useful for setting FLAG_DYNAMIC_ASCII
void FocusChanged(int focusFlags) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
bool Key(const KeyInput &key) override;
bool Touch(const TouchInput &touch) override;
Event OnTextChange;
Event OnEnter;
private:
void InsertAtCaret(const char *text);
std::string text_;
std::string title_;
std::string undo_;
std::string placeholderText_;
uint32_t textColor_;
bool hasTextColor_ = false;
int caret_;
int scrollPos_ = 0;
size_t maxLen_;
bool ctrlDown_ = false; // TODO: Make some global mechanism for this.
int align_ = 0;
// TODO: Selections
};