/
View.cpp
1660 lines (1445 loc) · 46.3 KB
/
View.cpp
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
#include <algorithm>
#include <mutex>
#include "Common/Input/InputState.h"
#include "Common/Input/KeyCodes.h"
#include "Common/Render/DrawBuffer.h"
#include "Common/Render/TextureAtlas.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Data/Text/I18n.h"
#include "Common/UI/UI.h"
#include "Common/UI/View.h"
#include "Common/UI/Context.h"
#include "Common/UI/Tween.h"
#include "Common/UI/Root.h"
#include "Common/GPU/thin3d.h"
#include "Common/System/Request.h"
#include "Common/System/System.h"
#include "Common/TimeUtil.h"
#include "Common/StringUtils.h"
#include "Common/Log.h"
namespace UI {
static constexpr Size ITEM_HEIGHT = 64.f;
static constexpr float MIN_TEXT_SCALE = 0.8f;
static constexpr float MAX_ITEM_SIZE = 65535.0f;
void MeasureBySpec(Size sz, float contentDim, MeasureSpec spec, float *measured) {
if (sz == WRAP_CONTENT) {
if (spec.type == UNSPECIFIED)
*measured = contentDim;
else if (spec.type == AT_MOST)
*measured = contentDim < spec.size ? contentDim : spec.size;
else if (spec.type == EXACTLY)
*measured = spec.size;
} else if (sz == FILL_PARENT) {
// UNSPECIFIED may have a minimum size of the parent. Let's use it to fill.
if (spec.type == UNSPECIFIED)
*measured = std::max(spec.size, contentDim);
else
*measured = spec.size;
} else if (spec.type == EXACTLY || (spec.type == AT_MOST && *measured > spec.size)) {
*measured = spec.size;
} else {
*measured = sz;
}
}
static void ApplyBoundBySpec(float &bound, MeasureSpec spec) {
switch (spec.type) {
case AT_MOST:
bound = bound < spec.size ? bound : spec.size;
break;
case EXACTLY:
bound = spec.size;
break;
case UNSPECIFIED:
break;
}
}
void ApplyBoundsBySpec(Bounds &bounds, MeasureSpec horiz, MeasureSpec vert) {
ApplyBoundBySpec(bounds.w, horiz);
ApplyBoundBySpec(bounds.h, vert);
}
void Event::Add(std::function<EventReturn(EventParams&)> func) {
HandlerRegistration reg;
reg.func = func;
handlers_.push_back(reg);
}
// Call this from input thread or whatever, it doesn't matter
void Event::Trigger(EventParams &e) {
EventTriggered(this, e);
}
// Call this from UI thread
EventReturn Event::Dispatch(EventParams &e) {
for (auto iter = handlers_.begin(); iter != handlers_.end(); ++iter) {
if ((iter->func)(e) == UI::EVENT_DONE) {
// Event is handled, stop looping immediately. This event might even have gotten deleted.
return UI::EVENT_DONE;
}
}
return UI::EVENT_SKIPPED;
}
Event::~Event() {
handlers_.clear();
RemoveQueuedEventsByEvent(this);
}
View::~View() {
if (HasFocus())
SetFocusedView(0);
RemoveQueuedEventsByView(this);
// Could use unique_ptr, but then we have to include tween everywhere.
for (auto &tween : tweens_)
delete tween;
}
void View::Update() {
for (size_t i = 0; i < tweens_.size(); ++i) {
Tween *tween = tweens_[i];
if (!tween->Finished()) {
tween->Apply(this);
} else if (!tween->Persists()) {
tweens_.erase(tweens_.begin() + i);
i--;
delete tween;
}
}
}
void View::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) {
float contentW = 0.0f, contentH = 0.0f;
GetContentDimensionsBySpec(dc, horiz, vert, contentW, contentH);
MeasureBySpec(layoutParams_->width, contentW, horiz, &measuredWidth_);
MeasureBySpec(layoutParams_->height, contentH, vert, &measuredHeight_);
}
void View::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
// Default values
w = 10.0f;
h = 10.0f;
}
void View::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const {
GetContentDimensions(dc, w, h);
}
void View::Query(float x, float y, std::vector<View *> &list) {
if (bounds_.Contains(x, y)) {
list.push_back(this);
}
}
std::string View::DescribeLog() const {
return StringFromFormat("%0.1f,%0.1f %0.1fx%0.1f", bounds_.x, bounds_.y, bounds_.w, bounds_.h);
}
void View::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) {
// Remember if this view was a focused view.
std::string tag = Tag();
if (tag.empty()) {
tag = anonId;
}
const std::string focusedKey = "ViewFocused::" + tag;
switch (status) {
case UI::PERSIST_SAVE:
if (HasFocus()) {
storage[focusedKey].resize(1);
}
break;
case UI::PERSIST_RESTORE:
if (storage.find(focusedKey) != storage.end()) {
SetFocus();
}
break;
}
for (int i = 0; i < (int)tweens_.size(); ++i) {
tweens_[i]->PersistData(status, tag + "/" + StringFromInt(i), storage);
}
}
Point View::GetFocusPosition(FocusDirection dir) const {
// The +2/-2 is some extra fudge factor to cover for views sitting right next to each other.
// Distance zero yields strange results otherwise.
switch (dir) {
case FOCUS_LEFT: return Point(bounds_.x + 2, bounds_.centerY());
case FOCUS_RIGHT: return Point(bounds_.x2() - 2, bounds_.centerY());
case FOCUS_UP: return Point(bounds_.centerX(), bounds_.y + 2);
case FOCUS_DOWN: return Point(bounds_.centerX(), bounds_.y2() - 2);
default:
return bounds_.Center();
}
}
Point CollapsibleHeader::GetFocusPosition(FocusDirection dir) const {
// Bias the focus position to the left.
switch (dir) {
case FOCUS_UP: return Point(bounds_.x + 50, bounds_.y + 2);
case FOCUS_DOWN: return Point(bounds_.x + 50, bounds_.y2() - 2);
default:
return View::GetFocusPosition(dir);
}
}
bool View::SetFocus() {
if (IsFocusMovementEnabled()) {
if (CanBeFocused()) {
SetFocusedView(this);
return true;
}
}
return false;
}
Clickable::Clickable(LayoutParams *layoutParams)
: View(layoutParams) {
// We set the colors later once we have a UIContext.
bgColor_ = AddTween(new CallbackColorTween(0.1f));
bgColor_->Persist();
}
void Clickable::DrawBG(UIContext &dc, const Style &style) {
if (style.background.type == DRAW_SOLID_COLOR) {
if (time_now_d() - bgColorLast_ >= 0.25f) {
bgColor_->Reset(style.background.color);
} else if (bgColor_->CurrentValue() != style.background.color) {
bgColor_->Divert(style.background.color, down_ ? 0.05f : 0.1f);
}
bgColorLast_ = time_now_d();
dc.FillRect(Drawable(bgColor_->CurrentValue()), bounds_);
} else {
dc.FillRect(style.background, bounds_);
}
}
void Clickable::Click() {
UI::EventParams e{};
e.v = this;
OnClick.Trigger(e);
};
void Clickable::FocusChanged(int focusFlags) {
if (focusFlags & FF_LOSTFOCUS) {
down_ = false;
dragging_ = false;
}
}
bool Clickable::Touch(const TouchInput &input) {
bool contains = bounds_.Contains(input.x, input.y);
if (!IsEnabled()) {
dragging_ = false;
down_ = false;
return contains;
}
if (input.flags & TOUCH_DOWN) {
if (bounds_.Contains(input.x, input.y)) {
if (IsFocusMovementEnabled())
SetFocusedView(this);
dragging_ = true;
down_ = true;
} else {
down_ = false;
dragging_ = false;
}
} else if (input.flags & TOUCH_MOVE) {
if (dragging_)
down_ = bounds_.Contains(input.x, input.y);
}
if (input.flags & TOUCH_UP) {
if ((input.flags & TOUCH_CANCEL) == 0 && dragging_ && bounds_.Contains(input.x, input.y)) {
Click();
}
down_ = false;
downCountDown_ = 0;
dragging_ = false;
}
return contains;
}
static bool MatchesKeyDef(const std::vector<InputMapping> &defs, const KeyInput &key) {
// In addition to the actual search, we need to do another search where we replace the device ID with "ANY".
return
std::find(defs.begin(), defs.end(), InputMapping(key.deviceId, key.keyCode)) != defs.end() ||
std::find(defs.begin(), defs.end(), InputMapping(DEVICE_ID_ANY, key.keyCode)) != defs.end();
}
// TODO: O/X confirm preference for xperia play?
bool IsDPadKey(const KeyInput &key) {
if (dpadKeys.empty()) {
return key.keyCode >= NKCODE_DPAD_UP && key.keyCode <= NKCODE_DPAD_RIGHT;
} else {
return MatchesKeyDef(dpadKeys, key);
}
}
bool IsAcceptKey(const KeyInput &key) {
if (confirmKeys.empty()) {
// This path is pretty much not used, confirmKeys should be set.
// TODO: Get rid of this stuff?
if (key.deviceId == DEVICE_ID_KEYBOARD) {
return key.keyCode == NKCODE_SPACE || key.keyCode == NKCODE_ENTER || key.keyCode == NKCODE_Z || key.keyCode == NKCODE_NUMPAD_ENTER;
} else {
return key.keyCode == NKCODE_BUTTON_A || key.keyCode == NKCODE_BUTTON_CROSS || key.keyCode == NKCODE_BUTTON_1 || key.keyCode == NKCODE_DPAD_CENTER;
}
} else {
return MatchesKeyDef(confirmKeys, key);
}
}
bool IsEscapeKey(const KeyInput &key) {
if (cancelKeys.empty()) {
// This path is pretty much not used, cancelKeys should be set.
// TODO: Get rid of this stuff?
if (key.deviceId == DEVICE_ID_KEYBOARD) {
return key.keyCode == NKCODE_ESCAPE || key.keyCode == NKCODE_BACK;
} else {
return key.keyCode == NKCODE_BUTTON_CIRCLE || key.keyCode == NKCODE_BUTTON_B || key.keyCode == NKCODE_BUTTON_2;
}
} else {
return MatchesKeyDef(cancelKeys, key);
}
}
// Corresponds to Triangle
bool IsInfoKey(const KeyInput &key) {
if (infoKeys.empty()) {
// This path is pretty much not used, infoKeys should be set.
// TODO: Get rid of this stuff?
if (key.deviceId == DEVICE_ID_KEYBOARD) {
return key.keyCode == NKCODE_S || key.keyCode == NKCODE_NUMPAD_ADD;
} else {
return key.keyCode == NKCODE_BUTTON_Y || key.keyCode == NKCODE_BUTTON_3;
}
} else {
return MatchesKeyDef(infoKeys, key);
}
}
bool IsTabLeftKey(const KeyInput &key) {
if (tabLeftKeys.empty()) {
// This path is pretty much not used, tabLeftKeys should be set.
// TODO: Get rid of this stuff?
return key.keyCode == NKCODE_BUTTON_L1;
} else {
return MatchesKeyDef(tabLeftKeys, key);
}
}
bool IsTabRightKey(const KeyInput &key) {
if (tabRightKeys.empty()) {
// This path is pretty much not used, tabRightKeys should be set.
// TODO: Get rid of this stuff?
return key.keyCode == NKCODE_BUTTON_R1;
} else {
return MatchesKeyDef(tabRightKeys, key);
}
}
bool Clickable::Key(const KeyInput &key) {
if (!HasFocus() && key.deviceId != DEVICE_ID_MOUSE) {
down_ = false;
return false;
}
// TODO: Replace most of Update with this.
bool ret = false;
if (key.flags & KEY_DOWN) {
if (IsAcceptKey(key)) {
down_ = true;
ret = true;
}
}
if (key.flags & KEY_UP) {
if (IsAcceptKey(key)) {
if (down_) {
Click();
down_ = false;
ret = true;
}
} else if (down_ && IsEscapeKey(key)) {
down_ = false;
}
}
return ret;
}
bool StickyChoice::Touch(const TouchInput &touch) {
bool contains = bounds_.Contains(touch.x, touch.y);
dragging_ = false;
if (!IsEnabled()) {
down_ = false;
return contains;
}
if (touch.flags & TOUCH_DOWN) {
if (contains) {
if (IsFocusMovementEnabled())
SetFocusedView(this);
down_ = true;
Click();
return true;
}
}
return false;
}
bool StickyChoice::Key(const KeyInput &key) {
if (!HasFocus()) {
return false;
}
// TODO: Replace most of Update with this.
if (key.flags & KEY_DOWN) {
if (IsAcceptKey(key)) {
down_ = true;
UI::PlayUISound(UI::UISound::TOGGLE_ON);
Click();
return true;
}
}
return false;
}
void StickyChoice::FocusChanged(int focusFlags) {
// Override Clickable's FocusChanged to do nothing.
}
Item::Item(LayoutParams *layoutParams) : InertView(layoutParams) {
if (!layoutParams) {
layoutParams_->width = FILL_PARENT;
layoutParams_->height = ITEM_HEIGHT;
}
}
void Item::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
w = 0.0f;
h = 0.0f;
}
void ClickableItem::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
w = 0.0f;
h = ITEM_HEIGHT;
}
ClickableItem::ClickableItem(LayoutParams *layoutParams) : Clickable(layoutParams) {
if (!layoutParams) {
// The default LayoutParams assigned by View::View defaults to WRAP_CONTENT/WRAP_CONTENT.
if (layoutParams_->width == WRAP_CONTENT)
layoutParams_->width = FILL_PARENT;
}
}
void ClickableItem::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
if (!IsEnabled()) {
style = dc.theme->itemDisabledStyle;
}
if (HasFocus()) {
style = dc.theme->itemFocusedStyle;
}
if (down_) {
style = dc.theme->itemDownStyle;
}
DrawBG(dc, style);
}
void Choice::Click() {
ClickableItem::Click();
UI::PlayUISound(UI::UISound::CONFIRM);
}
void Choice::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const {
float totalW = 0.0f;
float totalH = 0.0f;
if (image_.isValid()) {
dc.Draw()->GetAtlas()->measureImage(image_, &w, &h);
totalW = w + 6;
totalH = h;
}
if (!text_.empty()) {
const int paddingX = 12;
float availWidth = horiz.size - paddingX * 2 - textPadding_.horiz() - totalW;
if (availWidth < 0.0f) {
// Let it have as much space as it needs.
availWidth = MAX_ITEM_SIZE;
}
if (horiz.type != EXACTLY && layoutParams_->width > 0.0f && availWidth > layoutParams_->width)
availWidth = layoutParams_->width;
float scale = dc.CalculateTextScale(text_.c_str(), availWidth, bounds_.h);
Bounds availBounds(0, 0, availWidth, vert.size);
float textW = 0.0f, textH = 0.0f;
dc.MeasureTextRect(dc.theme->uiFont, scale, scale, text_.c_str(), (int)text_.size(), availBounds, &textW, &textH, FLAG_WRAP_TEXT);
totalH = std::max(totalH, textH);
totalW += textW;
}
w = totalW + 24;
h = totalH + 16;
h = std::max(h, ITEM_HEIGHT);
}
void Choice::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
if (HasFocus()) style = dc.theme->itemFocusedStyle;
if (down_) style = dc.theme->itemDownStyle;
if (!IsEnabled()) style = dc.theme->itemDisabledStyle;
DrawBG(dc, style);
if (image_.isValid() && text_.empty()) {
dc.Draw()->DrawImageRotated(image_, bounds_.centerX(), bounds_.centerY(), imgScale_, imgRot_, style.fgColor, imgFlipH_);
} else if (!text_.empty()) {
dc.SetFontStyle(dc.theme->uiFont);
int paddingX = 12;
float availWidth = bounds_.w - paddingX * 2 - textPadding_.horiz();
if (image_.isValid()) {
const AtlasImage *image = dc.Draw()->GetAtlas()->getImage(image_);
_dbg_assert_(image);
paddingX += image->w + 6;
availWidth -= image->w + 6;
// TODO: Use scale rotation and flip here as well (DrawImageRotated is always ALIGN_CENTER for now)
dc.Draw()->DrawImage(image_, bounds_.x + 6, bounds_.centerY(), 1.0f, style.fgColor, ALIGN_LEFT | ALIGN_VCENTER);
}
if (centered_) {
dc.DrawTextRectSqueeze(text_.c_str(), bounds_, style.fgColor, ALIGN_CENTER | FLAG_WRAP_TEXT | drawTextFlags_);
} else {
if (rightIconImage_.isValid()) {
uint32_t col = rightIconKeepColor_ ? 0xffffffff : style.fgColor; // Don't apply theme to gold icon
dc.Draw()->DrawImageRotated(rightIconImage_, bounds_.x2() - 32 - paddingX, bounds_.centerY(), rightIconScale_, rightIconRot_, col, rightIconFlipH_);
}
Bounds textBounds(bounds_.x + paddingX + textPadding_.left, bounds_.y, availWidth, bounds_.h);
dc.DrawTextRectSqueeze(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT | drawTextFlags_);
}
dc.SetFontScale(1.0f, 1.0f);
}
if (selected_) {
dc.Draw()->DrawImage(dc.theme->checkOn, bounds_.x2() - 40, bounds_.centerY(), 1.0f, style.fgColor, ALIGN_CENTER);
}
}
std::string Choice::DescribeText() const {
auto u = GetI18NCategory(I18NCat::UI_ELEMENTS);
return ApplySafeSubstitutions(u->T("%1 choice"), text_);
}
InfoItem::InfoItem(const std::string &text, const std::string &rightText, LayoutParams *layoutParams)
: Item(layoutParams), text_(text), rightText_(rightText) {
// We set the colors later once we have a UIContext.
bgColor_ = AddTween(new CallbackColorTween(0.1f));
bgColor_->Persist();
fgColor_ = AddTween(new CallbackColorTween(0.1f));
fgColor_->Persist();
}
void InfoItem::Draw(UIContext &dc) {
Item::Draw(dc);
UI::Style style = HasFocus() ? dc.theme->itemFocusedStyle : dc.theme->infoStyle;
if (choiceStyle_) {
style = HasFocus() ? dc.theme->itemFocusedStyle : dc.theme->itemStyle;
}
if (style.background.type == DRAW_SOLID_COLOR) {
// For a smoother fade, using the same color with 0 alpha.
if ((style.background.color & 0xFF000000) == 0)
style.background.color = dc.theme->itemFocusedStyle.background.color & 0x00FFFFFF;
bgColor_->Divert(style.background.color & 0x7fffffff);
style.background.color = bgColor_->CurrentValue();
}
fgColor_->Divert(style.fgColor);
style.fgColor = fgColor_->CurrentValue();
dc.FillRect(style.background, bounds_);
int paddingX = 12;
Bounds padBounds = bounds_.Expand(-paddingX, 0);
float leftWidth, leftHeight;
dc.MeasureTextRect(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), (int)text_.size(), padBounds, &leftWidth, &leftHeight, ALIGN_VCENTER);
dc.SetFontStyle(dc.theme->uiFont);
dc.DrawTextRect(text_.c_str(), padBounds, style.fgColor, ALIGN_VCENTER);
Bounds rightBounds(padBounds.x + leftWidth, padBounds.y, padBounds.w - leftWidth, padBounds.h);
dc.DrawTextRect(rightText_.c_str(), rightBounds, style.fgColor, ALIGN_VCENTER | ALIGN_RIGHT | FLAG_WRAP_TEXT);
}
std::string InfoItem::DescribeText() const {
auto u = GetI18NCategory(I18NCat::UI_ELEMENTS);
return ApplySafeSubstitutions(u->T("%1: %2"), text_, rightText_);
}
ItemHeader::ItemHeader(const std::string &text, LayoutParams *layoutParams)
: Item(layoutParams), text_(text) {
layoutParams_->width = FILL_PARENT;
layoutParams_->height = 40;
}
void ItemHeader::Draw(UIContext &dc) {
dc.SetFontStyle(dc.theme->uiFontSmall);
dc.DrawText(text_.c_str(), bounds_.x + 4, bounds_.centerY(), dc.theme->headerStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER);
dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y2()-2, bounds_.x2(), bounds_.y2(), dc.theme->headerStyle.fgColor);
}
void ItemHeader::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const {
Bounds bounds(0, 0, layoutParams_->width, layoutParams_->height);
if (bounds.w < 0) {
// If there's no size, let's grow as big as we want.
bounds.w = horiz.size == 0 ? MAX_ITEM_SIZE : horiz.size;
}
if (bounds.h < 0) {
bounds.h = vert.size == 0 ? MAX_ITEM_SIZE : vert.size;
}
ApplyBoundsBySpec(bounds, horiz, vert);
dc.MeasureTextRect(dc.theme->uiFontSmall, 1.0f, 1.0f, text_.c_str(), (int)text_.length(), bounds, &w, &h, ALIGN_LEFT | ALIGN_VCENTER);
}
std::string ItemHeader::DescribeText() const {
auto u = GetI18NCategory(I18NCat::UI_ELEMENTS);
return ApplySafeSubstitutions(u->T("%1 heading"), text_);
}
CollapsibleHeader::CollapsibleHeader(bool *toggle, const std::string &text, LayoutParams *layoutParams)
: CheckBox(toggle, text, "", layoutParams) {
layoutParams_->width = FILL_PARENT;
layoutParams_->height = 40;
}
void CollapsibleHeader::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
if (HasFocus()) style = dc.theme->itemFocusedStyle;
if (down_) style = dc.theme->itemDownStyle;
if (!IsEnabled()) style = dc.theme->itemDisabledStyle;
DrawBG(dc, style);
float xoff = 37.0f;
dc.SetFontStyle(dc.theme->uiFontSmall);
dc.DrawText(text_.c_str(), bounds_.x + 4 + xoff, bounds_.centerY(), dc.theme->headerStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER);
dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y2() - 2, bounds_.x2(), bounds_.y2(), dc.theme->headerStyle.fgColor);
if (hasSubItems_) {
dc.Draw()->DrawImageRotated(ImageID("I_ARROW"), bounds_.x + 20.0f, bounds_.y + 20.0f, 1.0f, *toggle_ ? -M_PI / 2 : M_PI);
}
}
void CollapsibleHeader::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const {
Bounds bounds(0, 0, layoutParams_->width, layoutParams_->height);
if (bounds.w < 0) {
// If there's no size, let's grow as big as we want.
bounds.w = horiz.size == 0 ? MAX_ITEM_SIZE : horiz.size;
}
if (bounds.h < 0) {
bounds.h = vert.size == 0 ? MAX_ITEM_SIZE : vert.size;
}
ApplyBoundsBySpec(bounds, horiz, vert);
dc.MeasureTextRect(dc.theme->uiFontSmall, 1.0f, 1.0f, text_.c_str(), (int)text_.length(), bounds, &w, &h, ALIGN_LEFT | ALIGN_VCENTER);
}
void CollapsibleHeader::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
View::GetContentDimensions(dc, w, h);
}
void BorderView::Draw(UIContext &dc) {
Color color = 0xFFFFFFFF;
if (style_ == BorderStyle::HEADER_FG)
color = dc.theme->headerStyle.fgColor;
else if (style_ == BorderStyle::ITEM_DOWN_BG)
color = dc.theme->itemDownStyle.background.color;
if (borderFlags_ & BORDER_TOP)
dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y + size_, color);
if (borderFlags_ & BORDER_LEFT)
dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y, bounds_.x + size_, bounds_.y2(), color);
if (borderFlags_ & BORDER_BOTTOM)
dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y2() - size_, bounds_.x2(), bounds_.y2(), color);
if (borderFlags_ & BORDER_RIGHT)
dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x2() - size_, bounds_.y, bounds_.x2(), bounds_.y2(), color);
}
void BorderView::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const {
Bounds bounds(0, 0, layoutParams_->width, layoutParams_->height);
if (bounds.w < 0) {
// If there's no size, let's grow as big as we want.
bounds.w = horiz.size == 0 ? MAX_ITEM_SIZE : horiz.size;
}
if (bounds.h < 0) {
bounds.h = vert.size == 0 ? MAX_ITEM_SIZE : vert.size;
}
ApplyBoundsBySpec(bounds, horiz, vert);
// If we have vertical borders, grow to width so they're spaced apart.
w = (borderFlags_ & BORDER_VERT) != 0 ? bounds.w : 0;
h = (borderFlags_ & BORDER_HORIZ) != 0 ? bounds.h : 0;
}
void PopupHeader::Draw(UIContext &dc) {
const float paddingHorizontal = 12;
const float availableWidth = bounds_.w - paddingHorizontal * 2;
float tw, th;
dc.SetFontStyle(dc.theme->uiFont);
dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, text_.c_str(), &tw, &th, 0);
float sineWidth = std::max(0.0f, (tw - availableWidth)) / 2.0f;
float tx = paddingHorizontal;
if (availableWidth < tw) {
float overageRatio = 1.5f * availableWidth * 1.0f / tw;
tx -= (1.0f + sin(time_now_d() * overageRatio)) * sineWidth;
Bounds tb = bounds_;
tb.x = bounds_.x + paddingHorizontal;
tb.w = bounds_.w - paddingHorizontal * 2;
dc.PushScissor(tb);
}
dc.DrawText(text_.c_str(), bounds_.x + tx, bounds_.centerY(), dc.theme->itemStyle.fgColor, ALIGN_LEFT | ALIGN_VCENTER);
dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y2()-2, bounds_.x2(), bounds_.y2(), dc.theme->itemStyle.fgColor);
if (availableWidth < tw) {
dc.PopScissor();
}
}
std::string PopupHeader::DescribeText() const {
auto u = GetI18NCategory(I18NCat::UI_ELEMENTS);
return ApplySafeSubstitutions(u->T("%1 heading"), text_);
}
void CheckBox::Toggle() {
if (toggle_) {
*toggle_ = !(*toggle_);
UI::PlayUISound(*toggle_ ? UI::UISound::TOGGLE_ON : UI::UISound::TOGGLE_OFF);
}
}
bool CheckBox::Toggled() const {
if (toggle_)
return *toggle_;
return false;
}
EventReturn CheckBox::OnClicked(EventParams &e) {
Toggle();
return EVENT_CONTINUE; // It's safe to keep processing events.
}
void CheckBox::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
if (!IsEnabled()) {
style = dc.theme->itemDisabledStyle;
}
ImageID image = Toggled() ? dc.theme->checkOn : dc.theme->checkOff;
// In image mode, light up instead of showing a checkbox.
if (imageID_.isValid()) {
image = imageID_;
if (Toggled()) {
if (HasFocus()) {
style = dc.theme->itemDownStyle;
} else {
style = dc.theme->itemFocusedStyle;
}
} else {
if (HasFocus()) {
style = dc.theme->itemDownStyle;
} else {
style = dc.theme->itemStyle;
}
}
if (down_) {
style.background.color = lightenColor(style.background.color);
}
} else {
if (HasFocus()) {
style = dc.theme->itemFocusedStyle;
}
if (down_) {
style = dc.theme->itemDownStyle;
}
}
dc.SetFontStyle(dc.theme->uiFont);
DrawBG(dc, style);
float imageW, imageH;
dc.Draw()->MeasureImage(image, &imageW, &imageH);
const int paddingX = 12;
// Padding right of the checkbox image too.
const float availWidth = bounds_.w - paddingX * 2 - imageW - paddingX;
if (!text_.empty()) {
Bounds textBounds(bounds_.x + paddingX, bounds_.y, availWidth, bounds_.h);
dc.DrawTextRectSqueeze(text_.c_str(), textBounds, style.fgColor, ALIGN_VCENTER | FLAG_WRAP_TEXT);
}
dc.Draw()->DrawImage(image, bounds_.x2() - paddingX, bounds_.centerY(), 1.0f, style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
dc.SetFontScale(1.0f, 1.0f);
}
std::string CheckBox::DescribeText() const {
auto u = GetI18NCategory(I18NCat::UI_ELEMENTS);
std::string text = ApplySafeSubstitutions(u->T("%1 checkbox"), text_);
if (!smallText_.empty()) {
text += "\n" + smallText_;
}
return text;
}
void CheckBox::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
ImageID image = Toggled() ? dc.theme->checkOn : dc.theme->checkOff;
if (imageID_.isValid()) {
image = imageID_;
}
float imageW, imageH;
dc.Draw()->MeasureImage(image, &imageW, &imageH);
const int paddingX = 12;
if (imageID_.isValid()) {
w = imageW + paddingX * 2;
h = std::max(imageH, ITEM_HEIGHT);
return;
}
// The below code is kinda wacky, we shouldn't involve bounds_ here.
// Padding right of the checkbox image too.
float availWidth = bounds_.w - paddingX * 2 - imageW - paddingX;
if (availWidth < 0.0f) {
// Let it have as much space as it needs.
availWidth = MAX_ITEM_SIZE;
}
if (!text_.empty()) {
float scale = dc.CalculateTextScale(text_.c_str(), availWidth, bounds_.h);
float actualWidth, actualHeight;
Bounds availBounds(0, 0, availWidth, bounds_.h);
dc.MeasureTextRect(dc.theme->uiFont, scale, scale, text_.c_str(), (int)text_.size(), availBounds, &actualWidth, &actualHeight, ALIGN_VCENTER | FLAG_WRAP_TEXT);
h = std::max(actualHeight, ITEM_HEIGHT);
} else {
h = std::max(imageH, ITEM_HEIGHT);
}
w = bounds_.w;
}
void BitCheckBox::Toggle() {
if (bitfield_) {
*bitfield_ = *bitfield_ ^ bit_;
if (*bitfield_ & bit_) {
UI::PlayUISound(UI::UISound::TOGGLE_ON);
} else {
UI::PlayUISound(UI::UISound::TOGGLE_OFF);
}
}
}
bool BitCheckBox::Toggled() const {
if (bitfield_)
return (bit_ & *bitfield_) == bit_;
return false;
}
void Button::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
if (imageID_.isValid()) {
dc.Draw()->GetAtlas()->measureImage(imageID_, &w, &h);
} else {
w = 0.0f;
h = 0.0f;
}
if (!text_.empty() && !ignoreText_) {
float width = 0.0f;
float height = 0.0f;
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &width, &height);
w += width;
if (imageID_.isValid()) {
w += paddingW_;
}
h = std::max(h, height);
}
// Add some internal padding to not look totally ugly
w += paddingW_;
h += paddingH_;
w *= scale_;
h *= scale_;
}
std::string Button::DescribeText() const {
auto u = GetI18NCategory(I18NCat::UI_ELEMENTS);
return ApplySafeSubstitutions(u->T("%1 button"), GetText());
}
void Button::Click() {
Clickable::Click();
UI::PlayUISound(UI::UISound::CONFIRM);
}
void Button::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
if (HasFocus()) style = dc.theme->itemFocusedStyle;
if (down_) style = dc.theme->itemDownStyle;
if (!IsEnabled()) style = dc.theme->itemDisabledStyle;
// dc.Draw()->DrawImage4Grid(style.image, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y2(), style.bgColor);
DrawBG(dc, style);
float tw, th;
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &tw, &th);
tw *= scale_;
th *= scale_;
if (tw > bounds_.w || imageID_.isValid()) {
dc.PushScissor(bounds_);
}
dc.SetFontStyle(dc.theme->uiFont);
dc.SetFontScale(scale_, scale_);
if (imageID_.isValid() && (ignoreText_ || text_.empty())) {
dc.Draw()->DrawImage(imageID_, bounds_.centerX(), bounds_.centerY(), scale_, style.fgColor, ALIGN_CENTER);
} else if (!text_.empty()) {
float textX = bounds_.centerX();
if (imageID_.isValid()) {
const AtlasImage *img = dc.Draw()->GetAtlas()->getImage(imageID_);
if (img) {
dc.Draw()->DrawImage(imageID_, bounds_.centerX() - tw / 2 - 5, bounds_.centerY(), 1.0f, style.fgColor, ALIGN_CENTER);
textX += img->w / 2.0f;
}
}
dc.DrawText(text_.c_str(), textX, bounds_.centerY(), style.fgColor, ALIGN_CENTER);
}
dc.SetFontScale(1.0f, 1.0f);
if (tw > bounds_.w || imageID_.isValid()) {
dc.PopScissor();
}
}
void RadioButton::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
w = 0.0f;
h = 0.0f;
if (!text_.empty()) {
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &w, &h);
}
// Add some internal padding to not look totally ugly
w += paddingW_ * 3.0f + radioRadius_ * 2.0f;
h = std::max(h, radioRadius_ * 2) + paddingH_ * 2;
}
std::string RadioButton::DescribeText() const {
auto u = GetI18NCategory(I18NCat::UI_ELEMENTS);
return ApplySafeSubstitutions(u->T("%1 radio button"), text_);
}
void RadioButton::Click() {
Clickable::Click();
UI::PlayUISound(UI::UISound::CONFIRM);
*value_ = thisButtonValue_;
}
void RadioButton::Draw(UIContext &dc) {
Style style = dc.theme->itemStyle;
bool checked = *value_ == thisButtonValue_;
if (HasFocus()) style = dc.theme->itemFocusedStyle;
if (down_) style = dc.theme->itemDownStyle;
if (!IsEnabled()) style = dc.theme->itemDisabledStyle;
DrawBG(dc, style);
dc.Flush();
dc.BeginNoTex();
dc.Draw()->Circle(bounds_.x + paddingW_ + radioRadius_, bounds_.centerY(), radioRadius_, 2.5f, 36, 0, style.fgColor, 1.0f);
if (checked) {
dc.Draw()->FillCircle(bounds_.x + paddingW_ + radioRadius_, bounds_.centerY(), radioInnerRadius_, 36, style.fgColor);
}
dc.Flush();
dc.Begin();
float tw, th;
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), &tw, &th);
if (tw > bounds_.w) {
dc.PushScissor(bounds_);
}
dc.SetFontStyle(dc.theme->uiFont);
if (!text_.empty()) {