forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionRow.cpp
1081 lines (922 loc) · 29.5 KB
/
OptionRow.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 "global.h"
#include "OptionRow.h"
#include "RageUtil.h"
#include "RageLog.h"
#include "OptionRowHandler.h"
#include "CommonMetrics.h"
#include "GameState.h"
#include "Song.h"
#include "Course.h"
#include "Style.h"
#include "ActorUtil.h"
using std::vector;
const std::string NEXT_ROW_NAME = "NextRow";
const std::string EXIT_NAME = "Exit";
std::string OptionRow::GetThemedItemText( int iChoice ) const
{
std::string s = m_pHand->GetThemedItemText( iChoice );
// HACK: Always theme the NEXT_ROW and EXIT items.
if( m_bFirstItemGoesDown && iChoice == 0 )
s = CommonMetrics::LocalizeOptionItem( NEXT_ROW_NAME, false );
else if( m_RowType == OptionRow::RowType_Exit )
s = CommonMetrics::LocalizeOptionItem( EXIT_NAME, false );
return s;
}
std::string ITEMS_LONG_ROW_X_NAME( size_t p ) { return fmt::sprintf("ItemsLongRowP%dX",int(p+1)); }
std::string MOD_ICON_X_NAME( size_t p ) { return fmt::sprintf("ModIconP%dX",int(p+1)); }
OptionRow::OptionRow( const OptionRowType *pSource )
{
m_pParentType = pSource;
m_pHand = nullptr;
m_textTitle = nullptr;
ZERO( m_ModIcons );
Clear();
this->AddChild( &m_Frame );
m_tsDestination.Init();
}
OptionRow::~OptionRow()
{
Clear();
}
void OptionRow::Clear()
{
ActorFrame::RemoveAllChildren();
FOREACH_PlayerNumber( p )
{
m_vbSelected[p].clear();
}
m_Frame.DeleteAllChildren();
m_textItems.clear();
FOREACH_PlayerNumber( p )
{
m_Underline[p].clear();
}
if( m_pHand != nullptr )
{
for (auto const &m: m_pHand->m_vsReloadRowMessages)
{
MESSAGEMAN->Unsubscribe( this, m );
}
}
Rage::safe_delete( m_pHand );
m_bFirstItemGoesDown = false;
ZERO( m_bRowHasFocus );
ZERO( m_iChoiceInRowWithFocus );
}
void OptionRowType::Load( const std::string &sMetricsGroup, Actor *pParent )
{
m_sMetricsGroup = sMetricsGroup;
ITEMS_START_X .Load(sMetricsGroup,"ItemsStartX");
ITEMS_END_X .Load(sMetricsGroup,"ItemsEndX");
ITEMS_GAP_X .Load(sMetricsGroup,"ItemsGapX");
ITEMS_MIN_BASE_ZOOM .Load(sMetricsGroup,"ItemsMinBaseZoom");
ITEMS_LONG_ROW_X .Load(sMetricsGroup,ITEMS_LONG_ROW_X_NAME,NUM_PLAYERS);
ITEMS_LONG_ROW_SHARED_X .Load(sMetricsGroup,"ItemsLongRowSharedX");
MOD_ICON_X .Load(sMetricsGroup,MOD_ICON_X_NAME,NUM_PLAYERS);
COLOR_SELECTED .Load(sMetricsGroup,"ColorSelected");
COLOR_NOT_SELECTED .Load(sMetricsGroup,"ColorNotSelected");
COLOR_DISABLED .Load(sMetricsGroup,"ColorDisabled");
TWEEN_SECONDS .Load(sMetricsGroup,"TweenSeconds");
SHOW_BPM_IN_SPEED_TITLE .Load(sMetricsGroup,"ShowBpmInSpeedTitle");
SHOW_MOD_ICONS .Load(sMetricsGroup,"ShowModIcons");
SHOW_UNDERLINES .Load(sMetricsGroup,"ShowUnderlines");
MOD_ICON_METRICS_GROUP .Load(sMetricsGroup,"ModIconMetricsGroup");
m_textItem.LoadFromFont( THEME->GetPathF(sMetricsGroup,"Item") );
m_textItem.SetName( "Item" );
ActorUtil::LoadAllCommands( m_textItem, sMetricsGroup );
if( SHOW_UNDERLINES )
{
FOREACH_PlayerNumber( p )
{
m_Underline[p].Load( "OptionsUnderline" + PlayerNumberToString(p), false );
}
}
m_textTitle.LoadFromFont( THEME->GetPathF(sMetricsGroup,"title") );
m_textTitle.SetName( "Title" );
ActorUtil::LoadAllCommandsAndSetXY( m_textTitle, sMetricsGroup );
Actor *pActor = ActorUtil::MakeActor( THEME->GetPathG(sMetricsGroup,"Frame"), pParent );
if( pActor == nullptr )
pActor = new Actor;
m_sprFrame.Load( pActor );
m_sprFrame->SetName( "Frame" );
ActorUtil::LoadAllCommandsAndSetXY( m_sprFrame, sMetricsGroup );
if( SHOW_MOD_ICONS )
{
m_ModIcon.Load( MOD_ICON_METRICS_GROUP.GetValue() );
m_ModIcon.SetName( "ModIcon" );
ActorUtil::LoadAllCommands( m_ModIcon, sMetricsGroup );
}
}
void OptionRow::LoadNormal( OptionRowHandler *pHand, bool bFirstItemGoesDown )
{
m_RowType = OptionRow::RowType_Normal;
m_pHand = pHand;
m_bFirstItemGoesDown = bFirstItemGoesDown;
for (auto const &m: m_pHand->m_vsReloadRowMessages)
{
MESSAGEMAN->Subscribe( this, m );
}
ChoicesChanged( RowType_Normal );
}
void OptionRow::LoadExit()
{
m_RowType = OptionRow::RowType_Exit;
OptionRowHandler *pHand = OptionRowHandlerUtil::MakeNull();
pHand->m_Def.m_selectType = SELECT_NONE;
pHand->m_Def.m_sName = EXIT_NAME;
pHand->m_Def.m_vsChoices.push_back( EXIT_NAME );
pHand->m_Def.m_layoutType = LAYOUT_SHOW_ONE_IN_ROW;
pHand->m_Def.m_bOneChoiceForAllPlayers = true;
m_pHand = pHand;
ChoicesChanged( RowType_Exit );
}
void OptionRow::ChoicesChanged( RowType type, bool reset_focus )
{
ASSERT_M( !m_pHand->m_Def.m_vsChoices.empty(), m_pHand->m_Def.m_sName + " has no choices" );
// Remove the NextRow marker before reloading choices
if( m_pHand->m_Def.m_vsChoices[0] == NEXT_ROW_NAME )
{
m_pHand->m_Def.m_vsChoices.erase( m_pHand->m_Def.m_vsChoices.begin() );
FOREACH_PlayerNumber( p )
{
m_vbSelected[p].erase( m_vbSelected[p].begin() );
}
}
FOREACH_PlayerNumber( p )
{
vector<bool> &vbSelected = m_vbSelected[p];
vbSelected.resize( 0 );
vbSelected.resize( m_pHand->m_Def.m_vsChoices.size(), false );
// set select the first item if a SELECT_ONE row
if( vbSelected.size() && m_pHand->m_Def.m_selectType == SELECT_ONE )
vbSelected[0] = true;
}
// TRICKY: Insert a down arrow as the first choice in the row.
if( m_bFirstItemGoesDown )
{
m_pHand->m_Def.m_vsChoices.insert( m_pHand->m_Def.m_vsChoices.begin(), NEXT_ROW_NAME );
FOREACH_PlayerNumber( p )
{
m_vbSelected[p].insert( m_vbSelected[p].begin(), false );
}
}
InitText( type );
// Lua can change the choices now, and when it does, we don't want to change focus.
if(reset_focus)
{
// When choices change, the old focus position is meaningless; reset it.
FOREACH_PlayerNumber( p )
{
SetChoiceInRowWithFocus( p, 0 );
}
}
m_textTitle->SetText( GetRowTitle() );
}
std::string OptionRow::GetRowTitle() const
{
std::string sTitle = m_pHand->OptionTitle();
// HACK: tack the BPM onto the name of the speed line
Rage::ci_ascii_string speed{ "speed" };
if( speed == m_pHand->m_Def.m_sName )
{
bool bShowBpmInSpeedTitle = m_pParentType->SHOW_BPM_IN_SPEED_TITLE;
if( GAMESTATE->m_pCurCourse )
{
const Trail* pTrail = GAMESTATE->m_pCurTrail[GAMESTATE->GetMasterPlayerNumber()];
ASSERT( pTrail != nullptr );
const int iNumCourseEntries = pTrail->m_vEntries.size();
if( iNumCourseEntries > CommonMetrics::MAX_COURSE_ENTRIES_BEFORE_VARIOUS )
bShowBpmInSpeedTitle = false;
}
if( bShowBpmInSpeedTitle )
{
DisplayBpms bpms;
if( GAMESTATE->get_curr_song() )
{
const Song* pSong = GAMESTATE->get_curr_song();
pSong->GetDisplayBpms( bpms );
}
else if( GAMESTATE->m_pCurCourse )
{
const Course *pCourse = GAMESTATE->m_pCurCourse;
StepsType st = GAMESTATE->GetCurrentStyle(GAMESTATE->GetMasterPlayerNumber())->m_StepsType;
const Trail* pTrail = pCourse->GetTrail( st );
ASSERT( pTrail != nullptr );
pTrail->GetDisplayBpms( bpms );
}
if( bpms.IsSecret() )
sTitle += fmt::sprintf( " (??" "?)" ); // split so gcc doesn't think this is a trigraph
else if( bpms.BpmIsConstant() )
sTitle += fmt::sprintf( " (%.0f)", bpms.GetMin() );
else
sTitle += fmt::sprintf( " (%.0f-%.0f)", bpms.GetMin(), bpms.GetMax() );
}
}
return sTitle;
}
/* Set up text, underlines and titles for options. This can be called as soon as
* m_pHand->m_Def is available. */
void OptionRow::InitText(RowType)
{
/* If we have elements already, we're being updated from a new set of options.
* Delete the old ones. */
m_Frame.DeleteAllChildren();
m_textItems.clear();
FOREACH_PlayerNumber( p )
{
m_Underline[p].clear();
}
m_textTitle = new BitmapText( m_pParentType->m_textTitle );
m_Frame.AddChild( m_textTitle );
m_sprFrame = m_pParentType->m_sprFrame->Copy();
m_sprFrame->SetDrawOrder(-1); // under title
m_Frame.AddChild( m_sprFrame );
if( m_pParentType->SHOW_MOD_ICONS )
{
switch( m_RowType )
{
case RowType_Normal:
FOREACH_PlayerNumber( p )
{
m_ModIcons[p] = new ModIcon( m_pParentType->m_ModIcon );
m_ModIcons[p]->SetDrawOrder(-1); // under title
m_ModIcons[p]->PlayCommand( "On" );
m_Frame.AddChild( m_ModIcons[p] );
GameCommand gc;
SetModIcon( p, "", gc );
}
break;
case RowType_Exit:
break;
}
}
// If the items will go off the edge of the screen, then force LAYOUT_SHOW_ONE_IN_ROW.
float fBaseZoom = 1.0f;
{
BitmapText bt( m_pParentType->m_textItem );
bt.PlayCommand( "On" );
// Figure out the width of the row.
float fWidth = 0;
for( unsigned c=0; c<m_pHand->m_Def.m_vsChoices.size(); c++ )
{
std::string sText = GetThemedItemText( c );
bt.SetText( sText );
fWidth += bt.GetZoomedWidth();
if( c != m_pHand->m_Def.m_vsChoices.size()-1 )
fWidth += m_pParentType->ITEMS_GAP_X;
}
// Try to fit everything on one line.
float fTotalWidth = m_pParentType->ITEMS_END_X - m_pParentType->ITEMS_START_X;
if( fWidth > fTotalWidth )
{
float fPossibleBaseZoom = fTotalWidth / fWidth;
if( fPossibleBaseZoom >= m_pParentType->ITEMS_MIN_BASE_ZOOM )
fBaseZoom = fPossibleBaseZoom;
else
m_pHand->m_Def.m_layoutType = LAYOUT_SHOW_ONE_IN_ROW;
}
}
// load m_textItems
switch( m_pHand->m_Def.m_layoutType )
{
case LAYOUT_SHOW_ONE_IN_ROW:
// init text
FOREACH_PlayerNumber( p )
{
BitmapText *pText = new BitmapText( m_pParentType->m_textItem );
m_textItems.push_back( pText );
pText->PlayCommand( "On" );
if( m_pHand->m_Def.m_bOneChoiceForAllPlayers )
{
pText->SetX( m_pParentType->ITEMS_LONG_ROW_SHARED_X );
break; // only initialize one item since it's shared
}
else
{
pText->SetX( m_pParentType->ITEMS_LONG_ROW_X.GetValue(p) );
}
// Set the text now, so SetWidthXY below is correct.
UpdateText( p );
// init underlines
if( m_pParentType->SHOW_UNDERLINES && GetRowType() != OptionRow::RowType_Exit )
{
OptionsCursor *pCursor = new OptionsCursor( m_pParentType->m_Underline[p] );
m_Underline[p].push_back( pCursor );
int iWidth, iX, iY;
GetWidthXY( p, 0, iWidth, iX, iY );
pCursor->SetX( float(iX) );
pCursor->SetBarWidth( iWidth );
}
}
break;
case LAYOUT_SHOW_ALL_IN_ROW:
{
float fX = m_pParentType->ITEMS_START_X;
for( unsigned c=0; c<m_pHand->m_Def.m_vsChoices.size(); c++ )
{
// init text
BitmapText *bt = new BitmapText( m_pParentType->m_textItem );
m_textItems.push_back( bt );
bt->SetBaseZoomX( fBaseZoom );
bt->PlayCommand( "On" );
// Set text after running OnCommand so e.g. uppercase,true works -aj
std::string sText = GetThemedItemText( c );
bt->SetText( sText );
// set the X position of each item in the line
float fItemWidth = bt->GetZoomedWidth();
fX += fItemWidth/2;
bt->SetX( fX );
// init underlines
if( m_pParentType->SHOW_UNDERLINES )
{
FOREACH_PlayerNumber( p )
{
OptionsCursor *ul = new OptionsCursor( m_pParentType->m_Underline[p] );
m_Underline[p].push_back( ul );
ul->SetX( fX );
ul->SetBarWidth( int(fItemWidth) );
}
}
fX += fItemWidth/2 + m_pParentType->ITEMS_GAP_X * fBaseZoom;
}
}
break;
default:
FAIL_M(fmt::sprintf("Invalid option row layout: %i", m_pHand->m_Def.m_layoutType));
}
for (auto *item: m_textItems)
{
m_Frame.AddChild( item );
}
FOREACH_PlayerNumber( p )
{
for (auto *item: m_Underline[p])
{
m_Frame.AddChild( item );
}
}
// This is set in OptionRow::AfterImportOptions, so if we're reused with a
// different song selected, SHOW_BPM_IN_SPEED_TITLE will show the new BPM.
//m_textTitle->SetText( GetRowTitle() );
m_textTitle->PlayCommand( "On" );
m_sprFrame->PlayCommand( "On" );
m_Frame.SortByDrawOrder();
this->SortByDrawOrder();
}
// After importing options, choose which item is focused.
void OptionRow::AfterImportOptions( PlayerNumber pn )
{
/* We load items for both players on start, since we don't know which players
* will be joined when we're displayed. Hide items for inactive players. */
if( m_pHand->m_Def.m_layoutType == LAYOUT_SHOW_ONE_IN_ROW &&
!m_pHand->m_Def.m_bOneChoiceForAllPlayers )
m_textItems[pn]->SetVisible( GAMESTATE->IsHumanPlayer(pn) );
// Hide underlines for disabled players.
if( !GAMESTATE->IsHumanPlayer(pn) )
{
for (auto *item: m_Underline[pn])
{
item->SetVisible( false );
}
}
// Make all selections the same if bOneChoiceForAllPlayers.
// Hack: we only import active players, so if only player 2 is imported,
// we need to copy p2 to p1, not p1 to p2.
if( m_pHand->m_Def.m_bOneChoiceForAllPlayers )
{
PlayerNumber pnCopyFrom = GAMESTATE->GetMasterPlayerNumber();
if( GAMESTATE->GetMasterPlayerNumber() == PLAYER_INVALID )
pnCopyFrom = PLAYER_1;
FOREACH_PlayerNumber( p )
{
m_vbSelected[p] = m_vbSelected[pnCopyFrom];
}
}
switch( m_pHand->m_Def.m_selectType )
{
case SELECT_ONE:
{
// Make sure the row actually has a selection.
int iSelection = GetOneSelection(pn, true);
if( iSelection == -1 )
{
ASSERT( !m_vbSelected[pn].empty() );
m_vbSelected[pn][0] = true;
}
}
default:
break;
}
ResetFocusFromSelection( pn );
PositionUnderlines( pn );
}
void OptionRow::PositionUnderlines( PlayerNumber pn )
{
vector<OptionsCursor*> &vpUnderlines = m_Underline[pn];
if( vpUnderlines.empty() )
return;
PlayerNumber pnTakeSelectedFrom = m_pHand->m_Def.m_bOneChoiceForAllPlayers ? PLAYER_1 : pn;
for( int i=0; i<(int)vpUnderlines.size(); i++ )
{
OptionsCursor& ul = *vpUnderlines[i];
int iChoiceWithFocus = (m_pHand->m_Def.m_layoutType == LAYOUT_SHOW_ONE_IN_ROW) ? GetChoiceInRowWithFocus(pn) : i;
float fAlpha = 1.0f;
if( m_pHand->m_Def.m_layoutType == LAYOUT_SHOW_ONE_IN_ROW )
{
bool bRowEnabled = m_pHand->m_Def.m_vEnabledForPlayers.find(pn) != m_pHand->m_Def.m_vEnabledForPlayers.end();
if( !m_pHand->m_Def.m_bOneChoiceForAllPlayers )
{
if( m_bRowHasFocus[pn] ) fAlpha = m_pParentType->COLOR_SELECTED.GetValue().a;
else if( bRowEnabled ) fAlpha = m_pParentType->COLOR_NOT_SELECTED.GetValue().a;
else fAlpha = m_pParentType->COLOR_DISABLED.GetValue().a;
}
}
// Don't tween X movement and color changes.
ul.StopTweening();
int iWidth, iX, iY;
GetWidthXY( pn, iChoiceWithFocus, iWidth, iX, iY );
ul.SetX( (float)iX );
// only set alpha, in case a theme tries to color underlines. -aj
ul.SetDiffuseAlpha( fAlpha );
ASSERT( m_vbSelected[pnTakeSelectedFrom].size() == m_pHand->m_Def.m_vsChoices.size() );
bool bSelected = (iChoiceWithFocus==-1) ? false : m_vbSelected[pnTakeSelectedFrom][ iChoiceWithFocus ];
bool bVisible = bSelected && GAMESTATE->IsHumanPlayer(pn);
ul.BeginTweening( m_pParentType->TWEEN_SECONDS );
ul.SetVisible( bVisible );
ul.SetBarWidth( iWidth );
}
}
void OptionRow::PositionIcons( PlayerNumber pn )
{
ModIcon *pIcon = m_ModIcons[pn];
if( pIcon == nullptr )
return;
pIcon->SetX( m_pParentType->MOD_ICON_X.GetValue(pn) );
}
// This is called when the focus changes, to update "long row" text.
void OptionRow::UpdateText( PlayerNumber p )
{
using std::min;
switch( m_pHand->m_Def.m_layoutType )
{
case LAYOUT_SHOW_ONE_IN_ROW:
{
unsigned pn = m_pHand->m_Def.m_bOneChoiceForAllPlayers ? 0 : p;
int iChoiceWithFocus = m_iChoiceInRowWithFocus[pn];
if( iChoiceWithFocus == -1 )
break;
std::string sText = GetThemedItemText( iChoiceWithFocus );
// If player_no is 2 and there is no player 1:
int index = min( pn, static_cast<unsigned int>(m_textItems.size()-1) );
// TODO: Always have one textItem for each player
m_textItems[index]->SetText( sText );
}
default: break;
}
}
void OptionRow::SetRowHasFocus( PlayerNumber pn, bool bRowHasFocus )
{
m_bRowHasFocus[pn] = bRowHasFocus;
}
void OptionRow::SetDestination( Actor::TweenState &ts, bool bTween )
{
if( m_Frame.DestTweenState() != ts )
{
m_Frame.StopTweening();
if( bTween && m_pParentType->TWEEN_SECONDS != 0 )
m_Frame.BeginTweening( m_pParentType->TWEEN_SECONDS );
m_Frame.DestTweenState() = ts;
}
}
void OptionRow::UpdateEnabledDisabled()
{
using std::min;
bool bThisRowHasFocusByAny = false;
FOREACH_HumanPlayer( p )
{
bThisRowHasFocusByAny |= m_bRowHasFocus[p];
}
bool bThisRowHasFocusByAll = true;
FOREACH_HumanPlayer( p )
{
bThisRowHasFocusByAll &= m_bRowHasFocus[p];
}
bool bRowEnabled = !m_pHand->m_Def.m_vEnabledForPlayers.empty();
// Don't tween selection colors at all.
std::string sCmdName;
if( bThisRowHasFocusByAny ) sCmdName = "GainFocus";
else if( bRowEnabled ) sCmdName = "LoseFocus";
else sCmdName = "Disabled";
Rage::Color color;
if( bThisRowHasFocusByAny ) color = m_pParentType->COLOR_SELECTED;
else if( bRowEnabled ) color = m_pParentType->COLOR_NOT_SELECTED;
else color = m_pParentType->COLOR_DISABLED;
m_sprFrame->PlayCommand( sCmdName );
m_textTitle->PlayCommand( sCmdName );
for( unsigned j=0; j<m_textItems.size(); j++ )
{
bool bThisItemHasFocusByAny = false;
FOREACH_HumanPlayer( p )
{
if( m_bRowHasFocus[p] )
{
if( (int)j == GetChoiceInRowWithFocus(p) )
{
bThisItemHasFocusByAny = true;
break;
}
}
}
if( bThisItemHasFocusByAny )
m_textItems[j]->PlayCommand( "GainFocus" );
else
m_textItems[j]->PlayCommand( "LoseFocus" );
}
switch( m_pHand->m_Def.m_layoutType )
{
case LAYOUT_SHOW_ALL_IN_ROW:
for (auto *item: m_textItems)
{
if( item->DestTweenState().diffuse[0] == color )
continue;
item->StopTweening();
item->BeginTweening( m_pParentType->TWEEN_SECONDS );
item->SetDiffuse( color );
}
break;
case LAYOUT_SHOW_ONE_IN_ROW:
FOREACH_HumanPlayer( pn )
{
bRowEnabled = m_pHand->m_Def.m_vEnabledForPlayers.find(pn) != m_pHand->m_Def.m_vEnabledForPlayers.end();
if( !m_pHand->m_Def.m_bOneChoiceForAllPlayers )
{
if( m_bRowHasFocus[pn] ) color = m_pParentType->COLOR_SELECTED;
else if( bRowEnabled ) color = m_pParentType->COLOR_NOT_SELECTED;
else color = m_pParentType->COLOR_DISABLED;
}
unsigned item_no = m_pHand->m_Def.m_bOneChoiceForAllPlayers ? 0 : pn;
// If player_no is 2 and there is no player 1:
item_no = min( item_no, static_cast<unsigned int>(m_textItems.size()-1) );
BitmapText &bt = *m_textItems[item_no];
if( bt.DestTweenState().diffuse[0] != color )
{
bt.StopTweening();
bt.BeginTweening( m_pParentType->TWEEN_SECONDS );
bt.SetDiffuse( color );
}
}
break;
default:
FAIL_M(fmt::sprintf("Invalid option row layout: %i", m_pHand->m_Def.m_layoutType));
}
}
void OptionRow::SetModIcon( PlayerNumber pn, const std::string &sText, GameCommand &gc )
{
// update row frame
Message msg( "Refresh" );
msg.SetParam( "GameCommand", &gc );
msg.SetParam( "Text", sText );
m_sprFrame->HandleMessage( msg );
if( m_ModIcons[pn] != nullptr )
m_ModIcons[pn]->Set( sText );
}
const BitmapText &OptionRow::GetTextItemForRow( PlayerNumber pn, int iChoiceOnRow ) const
{
bool bOneChoice = m_pHand->m_Def.m_bOneChoiceForAllPlayers;
int index = -1;
switch( m_pHand->m_Def.m_layoutType )
{
case LAYOUT_SHOW_ONE_IN_ROW:
index = bOneChoice ? 0 : pn;
// If only P2 is enabled, his selections will be in index 0.
if( m_textItems.size() == 1 )
index = 0;
break;
case LAYOUT_SHOW_ALL_IN_ROW:
index = iChoiceOnRow;
break;
default:
FAIL_M(fmt::sprintf("Invalid option row layout: %i", m_pHand->m_Def.m_layoutType));
}
ASSERT_M( index < (int)m_textItems.size(), fmt::sprintf("%i < %i", index, (int)m_textItems.size() ) );
return *m_textItems[index];
}
void OptionRow::GetWidthXY( PlayerNumber pn, int iChoiceOnRow, int &iWidthOut, int &iXOut, int &iYOut ) const
{
const BitmapText &text = GetTextItemForRow( pn, iChoiceOnRow );
iWidthOut = std::lrint( text.GetZoomedWidth() );
iXOut = std::lrint( text.GetDestX() );
iYOut = std::lrint( m_Frame.GetDestY() );
}
int OptionRow::GetOneSelection( PlayerNumber pn, bool bAllowFail ) const
{
for( unsigned i=0; i<m_vbSelected[pn].size(); i++ )
if( m_vbSelected[pn][i] )
return i;
ASSERT( bAllowFail ); // shouldn't call this if not expecting one to be selected
return -1;
}
int OptionRow::GetOneSharedSelection( bool bAllowFail ) const
{
return GetOneSelection( PLAYER_1, bAllowFail );
}
void OptionRow::SetOneSelection( PlayerNumber pn, int iChoice )
{
vector<bool> &vb = m_vbSelected[pn];
if( vb.empty() )
return;
std::fill(vb.begin(), vb.end(), false);
vb[iChoice] = true;
NotifyHandlerOfSelection(pn, iChoice);
}
void OptionRow::SetOneSharedSelection( int iChoice )
{
FOREACH_PlayerNumber( pn )
{
SetOneSelection( pn, iChoice );
}
}
void OptionRow::SetOneSharedSelectionIfPresent( const std::string &sChoice )
{
for( unsigned i=0; i<m_pHand->m_Def.m_vsChoices.size(); i++ )
{
if( sChoice == m_pHand->m_Def.m_vsChoices[i] )
{
SetOneSharedSelection( i );
break;
}
}
}
int OptionRow::GetChoiceInRowWithFocus( PlayerNumber pn ) const
{
if( m_pHand->m_Def.m_bOneChoiceForAllPlayers )
pn = PLAYER_1;
if( m_pHand->m_Def.m_vsChoices.empty() )
return -1;
int iChoice = m_iChoiceInRowWithFocus[pn];
return iChoice;
}
int OptionRow::GetChoiceInRowWithFocusShared() const
{
return GetChoiceInRowWithFocus( PLAYER_1 );
}
void OptionRow::SetChoiceInRowWithFocus( PlayerNumber pn, int iChoice )
{
if( m_pHand->m_Def.m_bOneChoiceForAllPlayers )
pn = PLAYER_1;
ASSERT(iChoice >= 0 && iChoice < (int)m_pHand->m_Def.m_vsChoices.size());
m_iChoiceInRowWithFocus[pn] = iChoice;
UpdateText( pn );
//PositionUnderlines( pn );
}
void OptionRow::ResetFocusFromSelection( PlayerNumber pn )
{
int iSelection = -1;
switch( m_pHand->m_Def.m_selectType )
{
case SELECT_ONE:
// Import the focus from the selected option.
iSelection = GetOneSelection( pn, true );
default:
break;
}
// HACK: Set focus to one item in the row, which is "go down"
if( m_bFirstItemGoesDown )
iSelection = 0;
if( iSelection != -1 )
SetChoiceInRowWithFocus( pn, iSelection );
}
bool OptionRow::GetSelected( PlayerNumber pn, int iChoice ) const
{
if( m_pHand->m_Def.m_bOneChoiceForAllPlayers )
pn = PLAYER_1;
return m_vbSelected[pn][iChoice];
}
const OptionRowDefinition &OptionRow::GetRowDef() const
{
return m_pHand->m_Def;
}
OptionRowDefinition &OptionRow::GetRowDef()
{
return m_pHand->m_Def;
}
bool OptionRow::SetSelected( PlayerNumber pn, int iChoice, bool b )
{
if( m_pHand->m_Def.m_bOneChoiceForAllPlayers )
pn = PLAYER_1;
m_vbSelected[pn][iChoice] = b;
return NotifyHandlerOfSelection(pn, iChoice);
}
bool OptionRow::NotifyHandlerOfSelection(PlayerNumber pn, int choice)
{
bool changed= m_pHand->NotifyOfSelection(pn, choice - m_bFirstItemGoesDown);
if(changed)
{
ChoicesChanged(m_RowType, false);
vector<PlayerNumber> vpns;
FOREACH_HumanPlayer( p )
{
vpns.push_back( p );
}
ImportOptions(vpns);
FOREACH_PlayerNumber(p)
{
PositionUnderlines(p);
}
UpdateEnabledDisabled();
}
return changed;
}
bool OptionRow::GoToFirstOnStart()
{
return m_pHand->GoToFirstOnStart();
}
void OptionRow::SetExitText( std::string sExitText )
{
BitmapText *bt = m_textItems.back();
bt->SetText( sExitText );
}
void OptionRow::Reload()
{
// TODO: Nothing uses this yet and it causes skips when changing options.
/*
if( m_pHand->m_Def.m_bExportOnChange )
{
bool bRowHasFocus[NUM_PLAYERS];
ZERO( bRowHasFocus );
ExportOptions( vpns, bRowHasFocus );
}
*/
switch( m_pHand->Reload() )
{
case RELOAD_CHANGED_NONE:
break;
case RELOAD_CHANGED_ALL:
{
ChoicesChanged( m_RowType );
vector<PlayerNumber> vpns;
FOREACH_HumanPlayer( p )
{
vpns.push_back( p );
}
ImportOptions( vpns );
FOREACH_HumanPlayer( p )
{
AfterImportOptions( p );
}
// fall through
}
case RELOAD_CHANGED_ENABLED:
UpdateEnabledDisabled();
FOREACH_HumanPlayer( pn )
{
PositionUnderlines( pn );
}
break;
default:
break;
}
// TODO: Nothing uses this yet and it causes skips when changing options.
/*
if( m_pHand->m_Def.m_bExportOnChange )
{
bool bRowHasFocus[NUM_PLAYERS];
ZERO( bRowHasFocus );
ExportOptions( vpns, bRowHasFocus );
}
*/
}
void OptionRow::HandleMessage( const Message &msg )
{
auto shouldReload = [&msg](std::string const &m) {
return m == msg.GetName();
};
auto &messages = m_pHand->m_vsReloadRowMessages;
bool bReload = std::any_of(messages.begin(), messages.end(), shouldReload);
if( bReload )
Reload();
ActorFrame::HandleMessage( msg );
}
/* Hack: the NextRow entry is never set, and should be transparent.
* Remove it, and readd it below. */
#define ERASE_ONE_BOOL_AT_FRONT_IF_NEEDED( vbSelected ) \
if( GetFirstItemGoesDown() ) \
vbSelected.erase( vbSelected.begin() );
#define INSERT_ONE_BOOL_AT_FRONT_IF_NEEDED( vbSelected ) \
if( GetFirstItemGoesDown() ) \
vbSelected.insert( vbSelected.begin(), false );
void OptionRow::ImportOptions( const vector<PlayerNumber> &vpns )
{
ASSERT( m_pHand->m_Def.m_vsChoices.size() > 0 );
for (auto const &p: vpns)
{
std::fill(m_vbSelected[p].begin(), m_vbSelected[p].end(), false);
ASSERT( m_vbSelected[p].size() == m_pHand->m_Def.m_vsChoices.size() );
ERASE_ONE_BOOL_AT_FRONT_IF_NEEDED( m_vbSelected[p] );
}
m_pHand->ImportOption( this, vpns, m_vbSelected );
for (auto const &p: vpns)
{
INSERT_ONE_BOOL_AT_FRONT_IF_NEEDED( m_vbSelected[p] );
VerifySelected( m_pHand->m_Def.m_selectType, m_vbSelected[p], m_pHand->m_Def.m_sName );
}
}
int OptionRow::ExportOptions( const vector<PlayerNumber> &vpns, bool bRowHasFocus[NUM_PLAYERS] )
{
ASSERT( m_pHand->m_Def.m_vsChoices.size() > 0 );
int iChangeMask = 0;
for (auto const &p: vpns)
{
bool bFocus = bRowHasFocus[p];
VerifySelected( m_pHand->m_Def.m_selectType, m_vbSelected[p], m_pHand->m_Def.m_sName );
ASSERT( m_vbSelected[p].size() == m_pHand->m_Def.m_vsChoices.size() );
ERASE_ONE_BOOL_AT_FRONT_IF_NEEDED( m_vbSelected[p] );
// SELECT_NONE rows get exported if they have focus when the user presses Start.
int iChoice = GetChoiceInRowWithFocus( p );
if( m_pHand->m_Def.m_selectType == SELECT_NONE && bFocus )
m_vbSelected[p][iChoice] = true;
}
iChangeMask |= m_pHand->ExportOption( vpns, m_vbSelected );
for (auto const &p: vpns)
{
bool bFocus = bRowHasFocus[p];
int iChoice = GetChoiceInRowWithFocus( p );
if( m_pHand->m_Def.m_selectType == SELECT_NONE && bFocus )
m_vbSelected[p][iChoice] = false;
INSERT_ONE_BOOL_AT_FRONT_IF_NEEDED( m_vbSelected[p] );
}