forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenOptionsManageProfiles.cpp
524 lines (461 loc) · 16.3 KB
/
ScreenOptionsManageProfiles.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
#include "global.h"
#include "ScreenDimensions.h"
#include "ScreenOptionsManageProfiles.h"
#include "ScreenManager.h"
#include "RageLog.h"
#include "GameState.h"
#include "CommonMetrics.h"
#include "ScreenTextEntry.h"
#include "ScreenPrompt.h"
#include "ScreenMiniMenu.h"
#include "ProfileManager.h"
#include "Profile.h"
#include "OptionRowHandler.h"
#include "LocalizedString.h"
#include "RageFmtWrap.h"
using std::vector;
static LocalizedString NEW_PROFILE_DEFAULT_NAME( "ScreenOptionsManageProfiles", "NewProfileDefaultName" );
#define SHOW_CREATE_NEW (!PROFILEMAN->FixedProfiles())
AutoScreenMessage( SM_BackFromEnterNameForNew );
AutoScreenMessage( SM_BackFromRename );
AutoScreenMessage( SM_BackFromDeleteConfirm );
AutoScreenMessage( SM_BackFromClearConfirm );
AutoScreenMessage( SM_BackFromContextMenu );
enum ProfileAction
{
ProfileAction_SetDefaultP1,
ProfileAction_SetDefaultP2,
ProfileAction_Edit,
ProfileAction_Rename,
ProfileAction_Delete,
ProfileAction_Clear,
ProfileAction_MergeToMachine,
ProfileAction_MergeToMachineSkipTotal,
ProfileAction_MergeToP1,
ProfileAction_MergeToP2,
ProfileAction_ChangeToGuest,
ProfileAction_ChangeToNormal,
ProfileAction_ChangeToTest,
ProfileAction_MoveUp,
ProfileAction_MoveDown,
NUM_ProfileAction
};
static const char *ProfileActionNames[] = {
"SetDefaultP1",
"SetDefaultP2",
"Edit",
"Rename",
"Delete",
"Clear",
"MergeToMachine",
"MergeToMachineSkipTotal",
"MergeToP1",
"MergeToP2",
"ChangeToGuest",
"ChangeToNormal",
"ChangeToTest",
"MoveUp",
"MoveDown",
};
XToString( ProfileAction );
XToLocalizedString( ProfileAction );
/** @brief Loop through each ProfileAction. */
#define FOREACH_ProfileAction( i ) FOREACH_ENUM( ProfileAction, i )
static MenuDef g_TempMenu(
"ScreenMiniMenuContext"
);
static LocalizedString PROFILE_NAME_BLANK ( "ScreenOptionsManageProfiles", "profile_name_blank" );
static LocalizedString PROFILE_NAME_CONFLICTS ( "ScreenOptionsManageProfiles", "profile_name_conflicts" );
static bool ValidateLocalProfileName( const std::string &sAnswer, std::string &sErrorOut )
{
if( sAnswer == "" )
{
sErrorOut = PROFILE_NAME_BLANK.GetValue();
return false;
}
Profile *pProfile = PROFILEMAN->GetLocalProfile( GAMESTATE->m_sEditLocalProfileID );
if( pProfile != nullptr && sAnswer == pProfile->m_sDisplayName )
return true; // unchanged
vector<std::string> vsProfileNames;
PROFILEMAN->GetLocalProfileDisplayNames( vsProfileNames );
bool bAlreadyAProfileWithThisName = find( vsProfileNames.begin(), vsProfileNames.end(), sAnswer ) != vsProfileNames.end();
if( bAlreadyAProfileWithThisName )
{
sErrorOut = PROFILE_NAME_CONFLICTS.GetValue();
return false;
}
return true;
}
REGISTER_SCREEN_CLASS( ScreenOptionsManageProfiles );
void ScreenOptionsManageProfiles::Init()
{
ScreenOptions::Init();
SetNavigation( NAV_THREE_KEY_MENU );
SetInputMode( INPUTMODE_SHARE_CURSOR );
}
void ScreenOptionsManageProfiles::BeginScreen()
{
// FIXME
// int iIndex = 0;
vector<OptionRowHandler*> OptionRowHandlers;
if( SHOW_CREATE_NEW )
{
OptionRowHandler *pHand = OptionRowHandlerUtil::Make( ParseCommands(fmt::sprintf("gamecommand;screen,%s;name,dummy",m_sName.c_str())) );
OptionRowDefinition &def = pHand->m_Def;
def.m_layoutType = LAYOUT_SHOW_ALL_IN_ROW;
def.m_bAllowThemeTitle = true;
def.m_bAllowThemeItems = false;
def.m_sName = "Create New Profile";
def.m_sExplanationName = "Create New Profile";
OptionRowHandlers.push_back( pHand );
// FIXME
// gc.Load( iIndex++, );
}
PROFILEMAN->GetLocalProfileIDs( m_vsLocalProfileID );
for (auto &s: m_vsLocalProfileID)
{
Profile *pProfile = PROFILEMAN->GetLocalProfile( s );
ASSERT( pProfile != nullptr );
std::string sCommand = fmt::sprintf( "gamecommand;screen,ScreenOptionsCustomizeProfile;profileid,%s;name,dummy", s.c_str() );
OptionRowHandler *pHand = OptionRowHandlerUtil::Make( ParseCommands(sCommand) );
OptionRowDefinition &def = pHand->m_Def;
def.m_layoutType = LAYOUT_SHOW_ALL_IN_ROW;
def.m_bAllowThemeTitle = false;
def.m_bAllowThemeItems = false;
def.m_sName = pProfile->m_sDisplayName;
def.m_sExplanationName = "Select Profile";
PlayerNumber pn = PLAYER_INVALID;
FOREACH_PlayerNumber( p )
{
if( s == ProfileManager::m_sDefaultLocalProfileID[p].Get() )
pn = p;
}
if( pn != PLAYER_INVALID )
def.m_vsChoices.push_back( PlayerNumberToLocalizedString(pn) );
OptionRowHandlers.push_back( pHand );
// FIXME
// gc.Load( iIndex++, );
}
ScreenOptions::InitMenu( OptionRowHandlers );
// Save sEditLocalProfileID before calling ScreenOptions::BeginScreen, because it will get clobbered.
std::string sEditLocalProfileID = GAMESTATE->m_sEditLocalProfileID;
ScreenOptions::BeginScreen();
// select the last chosen profile
if( !sEditLocalProfileID.empty() )
{
vector<std::string>::const_iterator iter = find( m_vsLocalProfileID.begin(), m_vsLocalProfileID.end(), sEditLocalProfileID );
if( iter != m_vsLocalProfileID.end() )
{
int iIndex = iter - m_vsLocalProfileID.begin();
this->MoveRowAbsolute( PLAYER_1, 1 + iIndex );
}
}
else if( !m_vsLocalProfileID.empty() )
{
// select the first item below "create new"
this->MoveRowAbsolute( PLAYER_1, 1 );
}
AfterChangeRow( PLAYER_1 );
}
static LocalizedString CONFIRM_DELETE_PROFILE ( "ScreenOptionsManageProfiles", "Are you sure you want to delete the profile '%s'?" );
static LocalizedString CONFIRM_CLEAR_PROFILE ( "ScreenOptionsManageProfiles", "Are you sure you want to clear all data in the profile '%s'?" );
static LocalizedString ENTER_PROFILE_NAME ( "ScreenOptionsManageProfiles", "Enter a name for the profile." );
void ScreenOptionsManageProfiles::HandleScreenMessage( const ScreenMessage SM )
{
if( SM == SM_GoToNextScreen )
{
int iCurRow = m_iCurrentRow[GAMESTATE->GetMasterPlayerNumber()];
OptionRow &row = *m_pRows[iCurRow];
if( row.GetRowType() == OptionRow::RowType_Exit )
{
this->HandleScreenMessage( SM_GoToPrevScreen );
return; // don't call base
}
}
else if( SM == SM_BackFromEnterNameForNew )
{
if( !ScreenTextEntry::s_bCancelledLast )
{
ASSERT( ScreenTextEntry::s_sLastAnswer != "" ); // validate should have assured this
std::string sNewName = ScreenTextEntry::s_sLastAnswer;
ASSERT( GAMESTATE->m_sEditLocalProfileID.Get().empty() );
int iNumProfiles = PROFILEMAN->GetNumLocalProfiles();
// create
std::string sProfileID;
// is this the correct way to go about checking the return value? -aj
bool bCreateProfile = PROFILEMAN->CreateLocalProfile( ScreenTextEntry::s_sLastAnswer, sProfileID );
ASSERT(bCreateProfile);
GAMESTATE->m_sEditLocalProfileID.Set( sProfileID );
if( iNumProfiles < NUM_PLAYERS )
{
int iFirstUnused = -1;
auto &profiles = PROFILEMAN->m_sDefaultLocalProfileID.m_v;
for (auto i = profiles.begin(); i != profiles.end(); ++i)
{
std::string sLocalProfileID = (*i)->Get();
if( sLocalProfileID.empty() )
{
iFirstUnused = i - PROFILEMAN->m_sDefaultLocalProfileID.m_v.begin();
break;
}
}
if( iFirstUnused != -1 )
{
PROFILEMAN->m_sDefaultLocalProfileID.m_v[iFirstUnused]->Set( sProfileID );
}
}
SCREENMAN->SetNewScreen( this->m_sName ); // reload
}
}
else if( SM == SM_BackFromRename )
{
if( !ScreenTextEntry::s_bCancelledLast )
{
ASSERT( ScreenTextEntry::s_sLastAnswer != "" ); // validate should have assured this
std::string sNewName = ScreenTextEntry::s_sLastAnswer;
PROFILEMAN->RenameLocalProfile( GAMESTATE->m_sEditLocalProfileID, sNewName );
SCREENMAN->SetNewScreen( this->m_sName ); // reload
}
}
else if( SM == SM_BackFromDeleteConfirm )
{
if( ScreenPrompt::s_LastAnswer == ANSWER_YES )
{
// Select the profile nearest to the one that was just deleted.
int iIndex = -1;
vector<std::string>::const_iterator iter = find( m_vsLocalProfileID.begin(), m_vsLocalProfileID.end(), GAMESTATE->m_sEditLocalProfileID.Get() );
if( iter != m_vsLocalProfileID.end() )
iIndex = iter - m_vsLocalProfileID.begin();
iIndex = Rage::clamp( iIndex, 0, static_cast<int>(m_vsLocalProfileID.size())-1 );
GAMESTATE->m_sEditLocalProfileID.Set( m_vsLocalProfileID[iIndex] );
PROFILEMAN->DeleteLocalProfile( GetLocalProfileIDWithFocus() );
SCREENMAN->SetNewScreen( this->m_sName ); // reload
}
}
else if( SM == SM_BackFromClearConfirm )
{
if( ScreenPrompt::s_LastAnswer == ANSWER_YES )
{
GAMESTATE->GetEditLocalProfile()->InitAll();
SCREENMAN->SetNewScreen( this->m_sName ); // reload
}
}
else if( SM == SM_BackFromContextMenu )
{
if( !ScreenMiniMenu::s_bCancelled )
{
Profile *pProfile = PROFILEMAN->GetLocalProfile( GAMESTATE->m_sEditLocalProfileID );
ASSERT( pProfile != nullptr );
switch( ScreenMiniMenu::s_iLastRowCode )
{
default:
FAIL_M(fmt::sprintf("Last row code not a valid ProfileAction: %i", ScreenMiniMenu::s_iLastRowCode));
case ProfileAction_SetDefaultP1:
case ProfileAction_SetDefaultP2:
{
FOREACH_PlayerNumber( p )
if( ProfileManager::m_sDefaultLocalProfileID[p].Get() == GetLocalProfileIDWithFocus() )
ProfileManager::m_sDefaultLocalProfileID[p].Set("");
PlayerNumber pn = (PlayerNumber)(ScreenMiniMenu::s_iLastRowCode - ProfileAction_SetDefaultP1);
ProfileManager::m_sDefaultLocalProfileID[pn].Set( GetLocalProfileIDWithFocus() );
SCREENMAN->SetNewScreen( this->m_sName ); // reload
}
break;
case ProfileAction_Edit:
{
GAMESTATE->m_sEditLocalProfileID.Set( GetLocalProfileIDWithFocus() );
ScreenOptions::BeginFadingOut();
}
break;
case ProfileAction_Rename:
{
ScreenTextEntry::TextEntry(
SM_BackFromRename,
ENTER_PROFILE_NAME.GetValue(),
pProfile->m_sDisplayName,
PROFILE_MAX_DISPLAY_NAME_LENGTH,
ValidateLocalProfileName );
}
break;
case ProfileAction_Delete:
{
std::string sTitle = pProfile->m_sDisplayName;
std::string sMessage = rage_fmt_wrapper(CONFIRM_DELETE_PROFILE, sTitle.c_str() );
ScreenPrompt::Prompt( SM_BackFromDeleteConfirm, sMessage, PROMPT_YES_NO );
}
break;
case ProfileAction_Clear:
{
std::string sTitle = pProfile->m_sDisplayName;
std::string sMessage = rage_fmt_wrapper(CONFIRM_CLEAR_PROFILE, sTitle.c_str() );
ScreenPrompt::Prompt( SM_BackFromClearConfirm, sMessage, PROMPT_YES_NO );
}
break;
case ProfileAction_MergeToMachine:
PROFILEMAN->MergeLocalProfileIntoMachine(
GetLocalProfileIDWithFocus(), false);
break;
case ProfileAction_MergeToMachineSkipTotal:
PROFILEMAN->MergeLocalProfileIntoMachine(
GetLocalProfileIDWithFocus(), true);
break;
case ProfileAction_MergeToP1:
PROFILEMAN->MergeLocalProfiles(GetLocalProfileIDWithFocus(),
ProfileManager::m_sDefaultLocalProfileID[PLAYER_1].Get());
break;
case ProfileAction_MergeToP2:
PROFILEMAN->MergeLocalProfiles(GetLocalProfileIDWithFocus(),
ProfileManager::m_sDefaultLocalProfileID[PLAYER_2].Get());
break;
case ProfileAction_ChangeToGuest:
PROFILEMAN->ChangeProfileType(GetLocalProfileIndexWithFocus(),
ProfileType_Guest);
SCREENMAN->SetNewScreen(this->m_sName); // reload
break;
case ProfileAction_ChangeToNormal:
PROFILEMAN->ChangeProfileType(GetLocalProfileIndexWithFocus(),
ProfileType_Normal);
SCREENMAN->SetNewScreen(this->m_sName); // reload
break;
case ProfileAction_ChangeToTest:
PROFILEMAN->ChangeProfileType(GetLocalProfileIndexWithFocus(),
ProfileType_Test);
SCREENMAN->SetNewScreen(this->m_sName); // reload
break;
case ProfileAction_MoveUp:
PROFILEMAN->MoveProfilePriority(GetLocalProfileIndexWithFocus(), true);
SCREENMAN->SetNewScreen(this->m_sName); // reload
break;
case ProfileAction_MoveDown:
PROFILEMAN->MoveProfilePriority(GetLocalProfileIndexWithFocus(), false);
SCREENMAN->SetNewScreen(this->m_sName); // reload
break;
}
}
}
else if( SM == SM_LoseFocus )
{
this->PlayCommand( "ScreenLoseFocus" );
}
else if( SM == SM_GainFocus )
{
this->PlayCommand( "ScreenGainFocus" );
}
ScreenOptions::HandleScreenMessage( SM );
}
void ScreenOptionsManageProfiles::AfterChangeRow( PlayerNumber pn )
{
GAMESTATE->m_sEditLocalProfileID.Set( GetLocalProfileIDWithFocus() );
ScreenOptions::AfterChangeRow( pn );
}
void ScreenOptionsManageProfiles::ProcessMenuStart( const InputEventPlus & )
{
if( IsTransitioning() )
return;
int iCurRow = m_iCurrentRow[GAMESTATE->GetMasterPlayerNumber()];
OptionRow &row = *m_pRows[iCurRow];
if( SHOW_CREATE_NEW && iCurRow == 0 ) // "create new"
{
vector<std::string> vsUsedNames;
PROFILEMAN->GetLocalProfileDisplayNames( vsUsedNames );
std::string sPotentialName;
for( int i=1; i<1000; i++ )
{
sPotentialName = fmt::sprintf( "%s%04d", NEW_PROFILE_DEFAULT_NAME.GetValue().c_str(), i );
bool bNameIsUsed = find( vsUsedNames.begin(), vsUsedNames.end(), sPotentialName ) != vsUsedNames.end();
if( !bNameIsUsed )
break;
}
ScreenTextEntry::TextEntry(
SM_BackFromEnterNameForNew,
ENTER_PROFILE_NAME.GetValue(),
sPotentialName,
PROFILE_MAX_DISPLAY_NAME_LENGTH,
ValidateLocalProfileName );
}
else if( row.GetRowType() == OptionRow::RowType_Exit )
{
SCREENMAN->PlayStartSound();
this->BeginFadingOut();
}
else // a profile
{
g_TempMenu.rows.clear();
#define ADD_ACTION( i ) \
g_TempMenu.rows.push_back( MenuRowDef( i, ProfileActionToLocalizedString(i), true, EditMode_Home, false, false, 0, "" ) );
ADD_ACTION( ProfileAction_SetDefaultP1 );
ADD_ACTION( ProfileAction_SetDefaultP2 );
if( PROFILEMAN->FixedProfiles() )
{
ADD_ACTION( ProfileAction_Rename );
ADD_ACTION( ProfileAction_Clear );
}
else
{
ADD_ACTION( ProfileAction_Edit );
ADD_ACTION( ProfileAction_Rename );
ADD_ACTION( ProfileAction_Delete );
ADD_ACTION( ProfileAction_MergeToMachine );
ADD_ACTION( ProfileAction_MergeToMachineSkipTotal );
ADD_ACTION( ProfileAction_MergeToP1 );
ADD_ACTION( ProfileAction_MergeToP2 );
ADD_ACTION( ProfileAction_ChangeToGuest );
ADD_ACTION( ProfileAction_ChangeToNormal );
ADD_ACTION( ProfileAction_ChangeToTest );
ADD_ACTION( ProfileAction_MoveUp );
ADD_ACTION( ProfileAction_MoveDown );
}
int iWidth, iX, iY;
this->GetWidthXY( PLAYER_1, iCurRow, 0, iWidth, iX, iY );
ScreenMiniMenu::MiniMenu( &g_TempMenu, SM_BackFromContextMenu, SM_BackFromContextMenu, (float)iX, SCREEN_CENTER_Y );
}
}
void ScreenOptionsManageProfiles::ImportOptions( int /* iRow */, const vector<PlayerNumber> & /* vpns */ )
{
}
void ScreenOptionsManageProfiles::ExportOptions( int /* iRow */, const vector<PlayerNumber> & /* vpns */ )
{
}
int ScreenOptionsManageProfiles::GetLocalProfileIndexWithFocus() const
{
int iCurRow = m_iCurrentRow[GAMESTATE->GetMasterPlayerNumber()];
OptionRow &row = *m_pRows[iCurRow];
if( SHOW_CREATE_NEW && iCurRow == 0 ) // "create new"
return -1;
else if( row.GetRowType() == OptionRow::RowType_Exit )
return -1;
// a profile
int iIndex = iCurRow + (SHOW_CREATE_NEW ? -1 : 0);
return iIndex;
}
std::string ScreenOptionsManageProfiles::GetLocalProfileIDWithFocus() const
{
int iIndex = GetLocalProfileIndexWithFocus();
if( iIndex == -1 )
return std::string();
return m_vsLocalProfileID[iIndex];
}
/*
* (c) 2002-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/