forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenWithMenuElements.cpp
444 lines (388 loc) · 11.7 KB
/
ScreenWithMenuElements.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
#include "global.h"
#include "ScreenWithMenuElements.h"
#include "MenuTimer.h"
#include "RageLog.h"
#include "ThemeManager.h"
#include "GameState.h"
#include "PrefsManager.h"
#include "ScreenManager.h"
#include "GameSoundManager.h"
#include "MemoryCardDisplay.h"
#include "InputEventPlus.h"
#define TIMER_STEALTH THEME->GetMetricB(m_sName,"TimerStealth")
#define SHOW_STAGE_DISPLAY THEME->GetMetricB(m_sName,"ShowStageDisplay")
#define MEMORY_CARD_ICONS THEME->GetMetricB(m_sName,"MemoryCardIcons")
#define FORCE_TIMER THEME->GetMetricB(m_sName,"ForceTimer")
#define STOP_MUSIC_ON_BACK THEME->GetMetricB(m_sName,"StopMusicOnBack")
#define WAIT_FOR_CHILDREN_BEFORE_TWEENING_OUT THEME->GetMetricB(m_sName,"WaitForChildrenBeforeTweeningOut")
REGISTER_SCREEN_CLASS( ScreenWithMenuElements );
ScreenWithMenuElements::ScreenWithMenuElements()
{
m_MenuTimer = nullptr;
FOREACH_PlayerNumber( p )
{
m_MemoryCardDisplay[p] = nullptr;
}
m_MenuTimer = nullptr;
m_bShouldAllowLateJoin= false;
}
void ScreenWithMenuElements::Init()
{
PLAY_MUSIC.Load( m_sName, "PlayMusic" );
MUSIC_ALIGN_BEAT.Load( m_sName, "MusicAlignBeat" );
DELAY_MUSIC_SECONDS.Load( m_sName, "DelayMusicSeconds" );
CANCEL_TRANSITIONS_OUT.Load( m_sName, "CancelTransitionsOut" );
TIMER_SECONDS.Load( m_sName, "TimerSeconds" );
TIMER_METRICS_GROUP.Load( m_sName, "TimerMetricsGroup" );
Screen::Init();
ASSERT( this->m_SubActors.empty() ); // don't call Init twice!
if( MEMORY_CARD_ICONS )
{
FOREACH_PlayerNumber( p )
{
ASSERT( m_MemoryCardDisplay[p] == nullptr );
m_MemoryCardDisplay[p] = new MemoryCardDisplay;
m_MemoryCardDisplay[p]->Load( p );
m_MemoryCardDisplay[p]->SetName( fmt::sprintf("MemoryCardDisplayP%d",p+1) );
LOAD_ALL_COMMANDS_AND_SET_XY( m_MemoryCardDisplay[p] );
this->AddChild( m_MemoryCardDisplay[p] );
}
}
if( TIMER_SECONDS != -1 )
{
ASSERT( m_MenuTimer == nullptr ); // don't load twice
m_MenuTimer = new MenuTimer;
m_MenuTimer->Load( TIMER_METRICS_GROUP.GetValue() );
m_MenuTimer->SetName( "Timer" );
if( TIMER_STEALTH )
m_MenuTimer->EnableStealth( true );
LOAD_ALL_COMMANDS_AND_SET_XY( m_MenuTimer );
ResetTimer();
this->AddChild( m_MenuTimer );
}
m_sprUnderlay.Load( THEME->GetPathB(m_sName,"underlay") );
m_sprUnderlay->SetName("Underlay");
m_sprUnderlay->SetDrawOrder( DRAW_ORDER_UNDERLAY );
this->AddChild( m_sprUnderlay );
LOAD_ALL_COMMANDS( m_sprUnderlay );
m_sprOverlay.Load( THEME->GetPathB(m_sName,"overlay") );
m_sprOverlay->SetName("Overlay");
m_sprOverlay->SetDrawOrder( DRAW_ORDER_OVERLAY );
this->AddChild( m_sprOverlay );
LOAD_ALL_COMMANDS( m_sprOverlay );
// decorations
{
AutoActor decorations;
decorations.LoadB( m_sName, "decorations" );
decorations->SetName("Decorations");
decorations->SetDrawOrder( DRAW_ORDER_DECORATIONS );
ActorFrame *pFrame = dynamic_cast<ActorFrame*>(static_cast<Actor*>(decorations));
if( pFrame )
{
m_vDecorations = pFrame->GetChildren();
for (auto *child: m_vDecorations)
{
this->AddChild( child );
}
pFrame->RemoveAllChildren();
}
}
m_In.SetName( "In" );
m_In.Load( THEME->GetPathB(m_sName,"in") );
m_In.SetDrawOrder( DRAW_ORDER_TRANSITIONS );
this->AddChild( &m_In );
LOAD_ALL_COMMANDS( m_In );
m_Out.SetName( "Out" );
m_Out.Load( THEME->GetPathB(m_sName,"out") );
m_Out.SetDrawOrder( DRAW_ORDER_TRANSITIONS );
this->AddChild( &m_Out );
LOAD_ALL_COMMANDS( m_Out );
m_Cancel.SetName( "Cancel" );
m_Cancel.Load( THEME->GetPathB(m_sName,"cancel") );
m_Cancel.SetDrawOrder( DRAW_ORDER_TRANSITIONS );
this->AddChild( &m_Cancel );
LOAD_ALL_COMMANDS( m_Cancel );
// Grab the music path here; don't GetPath during BeginScreen.
if( PLAY_MUSIC )
m_sPathToMusic = THEME->GetPathS( m_sName, "music" );
}
void ScreenWithMenuElements::BeginScreen()
{
Screen::BeginScreen();
m_In.Reset();
m_Out.Reset();
m_Cancel.Reset();
TweenOnScreen();
this->SortByDrawOrder();
m_In.StartTransitioning( SM_DoneFadingIn );
SOUND->PlayOnceFromAnnouncer( m_sName+" intro" );
StartPlayingMusic();
/* Evaluate FirstUpdateCommand. */
this->PlayCommand( "FirstUpdate" );
/* If AutoJoin and a player is already joined, then try to join a player. (If no players
* are joined, they'll join on the first JoinInput.) */
if( GAMESTATE->GetCoinMode() == CoinMode_Pay && GAMESTATE->m_bAutoJoin.Get() )
{
if( GAMESTATE->GetNumSidesJoined() > 0 && GAMESTATE->JoinPlayers() )
SCREENMAN->PlayStartSound();
}
}
void ScreenWithMenuElements::HandleScreenMessage( const ScreenMessage SM )
{
if( SM == SM_MenuTimer )
{
FOREACH_HumanPlayer(p)
{
InputEventPlus iep;
iep.pn = p;
MenuStart( iep );
}
}
Screen::HandleScreenMessage( SM );
}
ScreenWithMenuElements::~ScreenWithMenuElements()
{
Rage::safe_delete( m_MenuTimer );
FOREACH_PlayerNumber( p )
{
if( m_MemoryCardDisplay[p] != nullptr )
Rage::safe_delete( m_MemoryCardDisplay[p] );
}
for (auto *actor: m_vDecorations)
{
delete actor;
}
}
void ScreenWithMenuElements::SetHelpText( std::string s )
{
Message msg("SetHelpText");
msg.SetParam( "Text", s );
this->HandleMessage( msg );
}
std::string ScreenWithMenuElements::HandleLuaMusicFile(std::string const& path)
{
FileType ft= ActorUtil::GetFileType(path);
std::string ret= path;
if(ft == FT_Lua)
{
std::string script;
std::string error= "Lua runtime error: ";
if(GetFileContents(path, script))
{
Lua* L= LUA->Get();
if(!LuaHelpers::RunScript(L, script, "@"+path, error, 0, 1, true))
{
ret= "";
}
else
{
// there are two possible ways to load a music file via Lua.
// 1) return the path to the sound
// (themer has to use THEME:GetPathS())
std::string music_path_from_lua;
LuaHelpers::Pop(L, music_path_from_lua);
if(!music_path_from_lua.empty())
{
ret= music_path_from_lua;
}
else
{
// 2) perhaps it's a table with some params? unsure if I want to support
// this just yet. -aj
LOG->Trace("Lua music script did not return a path to a sound.");
ret= "";
}
}
LUA->Release(L);
}
else
{
LOG->Trace("run script failed hardcore, lol");
ret= "";
}
}
else if(ft != FT_Sound)
{
LuaHelpers::ReportScriptErrorFmt("Music file %s is not a sound file.", path.c_str());
ret= "";
}
return ret;
}
void ScreenWithMenuElements::StartPlayingMusic()
{
/* Some screens should leave the music alone (eg. ScreenPlayerOptions music
* sample left over from ScreenSelectMusic). */
if( PLAY_MUSIC )
{
GameSoundManager::PlayMusicParams pmp;
pmp.sFile= HandleLuaMusicFile(m_sPathToMusic);
if(!pmp.sFile.empty() && pmp.sFile != SOUND->GetMusicPath())
{
pmp.bAlignBeat = MUSIC_ALIGN_BEAT;
if(DELAY_MUSIC_SECONDS > 0.0f)
{
pmp.fStartSecond = -DELAY_MUSIC_SECONDS;
}
SOUND->PlayMusic(pmp);
}
}
}
void ScreenWithMenuElements::Update( float fDeltaTime )
{
Screen::Update( fDeltaTime );
}
void ScreenWithMenuElements::ResetTimer()
{
if( m_MenuTimer == nullptr )
return;
if( TIMER_SECONDS > 0.0f && (PREFSMAN->m_bMenuTimer || FORCE_TIMER) )
{
m_MenuTimer->SetSeconds( TIMER_SECONDS );
m_MenuTimer->Start();
}
else
{
m_MenuTimer->Disable();
}
}
void ScreenWithMenuElements::StartTransitioningScreen( ScreenMessage smSendWhenDone )
{
using std::max;
TweenOffScreen();
m_Out.StartTransitioning( smSendWhenDone );
if( WAIT_FOR_CHILDREN_BEFORE_TWEENING_OUT )
{
// Time the transition so that it finishes exactly when all actors have
// finished tweening.
float fSecondsUntilFinished = GetTweenTimeLeft();
float fSecondsUntilBeginOff = max( fSecondsUntilFinished - m_Out.GetTweenTimeLeft(), 0.f );
m_Out.SetHibernate( fSecondsUntilBeginOff );
}
}
void ScreenWithMenuElements::TweenOnScreen()
{
this->PlayCommand( "On" );
}
void ScreenWithMenuElements::TweenOffScreen()
{
if( m_MenuTimer )
{
m_MenuTimer->SetSeconds( 0 );
m_MenuTimer->Stop();
}
this->PlayCommand( "Off" );
// If we're a stacked screen, then there's someone else between us and the
// background, so don't tween it off.
if( !SCREENMAN->IsStackedScreen(this) )
SCREENMAN->PlaySharedBackgroundOffCommand();
}
void ScreenWithMenuElements::Cancel( ScreenMessage smSendWhenDone )
{
m_sNextScreen = GetPrevScreen();
if( CANCEL_TRANSITIONS_OUT )
{
StartTransitioningScreen( smSendWhenDone );
COMMAND( m_Out, "Cancel" );
return;
}
this->PlayCommand( "Cancel" );
if( m_Cancel.IsTransitioning() )
return; // ignore
if( STOP_MUSIC_ON_BACK )
SOUND->StopMusic();
if( m_MenuTimer )
m_MenuTimer->Stop();
m_Cancel.StartTransitioning( smSendWhenDone );
COMMAND( m_Cancel, "Cancel" );
}
bool ScreenWithMenuElements::IsTransitioning()
{
return m_In.IsTransitioning() || m_Out.IsTransitioning() || m_Cancel.IsTransitioning();
}
void ScreenWithMenuElements::StopTimer()
{
if( m_MenuTimer )
m_MenuTimer->Stop();
}
REGISTER_SCREEN_CLASS( ScreenWithMenuElementsSimple );
bool ScreenWithMenuElementsSimple::MenuStart( const InputEventPlus &input )
{
if( IsTransitioning() )
return false;
if( m_fLockInputSecs > 0 )
return false;
StartTransitioningScreen( SM_GoToNextScreen );
return true;
}
bool ScreenWithMenuElementsSimple::MenuBack( const InputEventPlus &input )
{
if( IsTransitioning() )
return false;
if( m_fLockInputSecs > 0 )
return false;
Cancel( SM_GoToPrevScreen );
return true;
}
// lua start
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the ScreenWithMenuElements. */
class LunaScreenWithMenuElements: public Luna<ScreenWithMenuElements>
{
public:
static int Cancel( T* p, lua_State *L ) { p->Cancel( SM_GoToPrevScreen ); COMMON_RETURN_SELF; }
static int IsTransitioning( T* p, lua_State *L ) { lua_pushboolean( L, p->IsTransitioning() ); return 1; }
static int SetAllowLateJoin( T* p, lua_State *L )
{
p->m_bShouldAllowLateJoin= BArg(1);
COMMON_RETURN_SELF;
}
static int StartTransitioningScreen( T* p, lua_State *L )
{
std::string sMessage = SArg(1);
ScreenMessage SM = ScreenMessageHelpers::ToScreenMessage( sMessage );
p->StartTransitioningScreen( SM );
COMMON_RETURN_SELF;
}
LunaScreenWithMenuElements()
{
ADD_METHOD( Cancel );
ADD_METHOD( IsTransitioning );
ADD_METHOD( SetAllowLateJoin );
ADD_METHOD( StartTransitioningScreen );
}
};
LUA_REGISTER_DERIVED_CLASS( ScreenWithMenuElements, Screen )
/** @brief Allow Lua to have access to the ScreenWithMenuElementsSimple. */
class LunaScreenWithMenuElementsSimple: public Luna<ScreenWithMenuElementsSimple>
{
public:
LunaScreenWithMenuElementsSimple()
{
}
};
LUA_REGISTER_DERIVED_CLASS( ScreenWithMenuElementsSimple, ScreenWithMenuElements )
// lua end
/*
* (c) 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.
*/