forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercentageDisplay.cpp
259 lines (218 loc) · 8.33 KB
/
PercentageDisplay.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
#include "global.h"
#include "PercentageDisplay.h"
#include "GameState.h"
#include "ThemeManager.h"
#include "PrefsManager.h"
#include "ActorUtil.h"
#include "RageLog.h"
#include "StageStats.h"
#include "PlayerState.h"
#include "XmlFile.h"
#include "Course.h"
REGISTER_ACTOR_CLASS( PercentageDisplay );
PercentageDisplay::PercentageDisplay()
{
m_pPlayerState = nullptr;
m_pPlayerStageStats = nullptr;
m_Last = -1;
m_LastMax = -1;
m_iDancePointsDigits = 0;
m_bUseRemainder = false;
m_bAutoRefresh = false;
m_FormatPercentScore.SetFromExpression( "FormatPercentScore" );
}
void PercentageDisplay::LoadFromNode( const XNode* pNode )
{
pNode->GetAttrValue( "DancePointsDigits", m_iDancePointsDigits );
pNode->GetAttrValue( "AutoRefresh", m_bAutoRefresh );
{
Lua *L = LUA->Get();
if(pNode->PushAttrValue(L, "FormatPercentScore"))
{
m_FormatPercentScore.SetFromStack( L );
if(m_FormatPercentScore.GetLuaType() != LUA_TFUNCTION)
{
// Not reported as an error because _fallback and default provided bad
// examples in their [LifeMeterBattery Percent]:Format metric and nobody
// realized it was supposed to be set to a function. -Kyz
LOG->Trace("Format attribute for PercentageDisplay named '%s' is not a function. Defaulting to 'FormatPercentScore'.", GetName().c_str());
m_FormatPercentScore.SetFromExpression("FormatPercentScore");
}
}
else
{
lua_pop(L, 1);
}
LUA->Release(L);
}
const XNode *pChild = pNode->GetChild( "Percent" );
if( pChild == nullptr )
{
LuaHelpers::ReportScriptError(ActorUtil::GetWhere(pNode) + ": PercentageDisplay: missing the node \"Percent\"");
// Make a BitmapText just so we don't crash.
m_textPercent.LoadFromFont(THEME->GetPathF("", "Common Normal"));
}
else
{
m_textPercent.LoadFromNode( pChild );
}
this->AddChild( &m_textPercent );
pChild = pNode->GetChild( "PercentRemainder" );
if( !ShowDancePointsNotPercentage() && pChild != nullptr )
{
m_bUseRemainder = true;
m_textPercentRemainder.LoadFromNode( pChild );
this->AddChild( &m_textPercentRemainder );
}
// only run the Init command after we load Fonts.
ActorFrame::LoadFromNode( pNode );
}
void PercentageDisplay::Load( const PlayerState *pPlayerState, const PlayerStageStats *pPlayerStageStats )
{
m_pPlayerState = pPlayerState;
m_pPlayerStageStats = pPlayerStageStats;
Refresh();
}
void PercentageDisplay::Load( const PlayerState *pPlayerState, const PlayerStageStats *pPlayerStageStats, const std::string &sMetricsGroup, bool bAutoRefresh )
{
m_pPlayerState = pPlayerState;
m_pPlayerStageStats = pPlayerStageStats;
m_bAutoRefresh = bAutoRefresh;
m_iDancePointsDigits = THEME->GetMetricI( sMetricsGroup, "DancePointsDigits" );
m_bUseRemainder = THEME->GetMetricB( sMetricsGroup, "PercentUseRemainder" );
m_FormatPercentScore = THEME->GetMetricR( sMetricsGroup, "Format" );
m_sPercentFormat = THEME->GetMetric( sMetricsGroup, "PercentFormat" );
m_sRemainderFormat = THEME->GetMetric( sMetricsGroup, "RemainderFormat" );
if(m_FormatPercentScore.GetLuaType() != LUA_TFUNCTION)
{
// Not reported as an error because _fallback and default provided bad
// examples in their [LifeMeterBattery Percent]:Format metric and nobody
// realized it was supposed to be set to a function. -Kyz
LOG->Trace("Format metric is not a function in [%s]. Defaulting to 'FormatPercentScore'.", sMetricsGroup.c_str());
m_FormatPercentScore.SetFromExpression( "FormatPercentScore" );
}
if( ShowDancePointsNotPercentage() )
m_textPercent.SetName( "DancePoints" + PlayerNumberToString(m_pPlayerState->m_PlayerNumber) );
else
m_textPercent.SetName( "Percent" + PlayerNumberToString(m_pPlayerState->m_PlayerNumber) );
m_textPercent.LoadFromFont( THEME->GetPathF(sMetricsGroup,"text") );
ActorUtil::SetXY( m_textPercent, sMetricsGroup );
ActorUtil::LoadAllCommands( m_textPercent, sMetricsGroup );
this->AddChild( &m_textPercent );
if( !ShowDancePointsNotPercentage() && m_bUseRemainder )
{
m_textPercentRemainder.SetName( "PercentRemainder" + PlayerNumberToString(m_pPlayerState->m_PlayerNumber) );
m_textPercentRemainder.LoadFromFont( THEME->GetPathF(sMetricsGroup,"remainder") );
ActorUtil::SetXY( m_textPercentRemainder, sMetricsGroup );
ActorUtil::LoadAllCommands( m_textPercentRemainder, sMetricsGroup );
ASSERT( m_textPercentRemainder.HasCommand("Off") );
m_textPercentRemainder.SetText( "456" );
this->AddChild( &m_textPercentRemainder );
}
Refresh();
}
void PercentageDisplay::Update( float fDeltaTime )
{
ActorFrame::Update( fDeltaTime );
if( m_bAutoRefresh )
Refresh();
}
void PercentageDisplay::Refresh()
{
using std::max;
const int iActualDancePoints = m_pPlayerStageStats->m_iActualDancePoints;
const int iCurPossibleDancePoints = m_pPlayerStageStats->m_iCurPossibleDancePoints;
if( iActualDancePoints == m_Last && iCurPossibleDancePoints == m_LastMax )
return;
m_Last = iActualDancePoints;
m_LastMax = iCurPossibleDancePoints;
std::string sNumToDisplay;
if( ShowDancePointsNotPercentage() )
{
sNumToDisplay = fmt::sprintf( "%*d", m_iDancePointsDigits, max( 0, iActualDancePoints ) );
}
else
{
float fPercentDancePoints = m_pPlayerStageStats->GetPercentDancePoints();
// clamp percentage - feedback is that negative numbers look weird here.
fPercentDancePoints = Rage::clamp( fPercentDancePoints, 0.f, 1.f );
if( m_bUseRemainder )
{
int iPercentWhole = int(fPercentDancePoints*100);
int iPercentRemainder = int( (fPercentDancePoints*100 - int(fPercentDancePoints*100)) * 10 );
sNumToDisplay = fmt::sprintf( m_sPercentFormat, iPercentWhole );
m_textPercentRemainder.SetText( fmt::sprintf(m_sRemainderFormat, iPercentRemainder) );
}
else
{
if(m_FormatPercentScore.GetLuaType() == LUA_TFUNCTION)
{
Lua *L = LUA->Get();
m_FormatPercentScore.PushSelf( L );
LuaHelpers::Push( L, fPercentDancePoints );
std::string Error= "Error running FormatPercentScore: ";
LuaHelpers::RunScriptOnStack(L, Error, 1, 1, true); // 1 arg, 1 result
LuaHelpers::Pop( L, sNumToDisplay );
LUA->Release(L);
}
// HACK: Use the last frame in the numbers texture as '-'
Rage::replace(sNumToDisplay, '-', 'x');
}
}
m_textPercent.SetText( sNumToDisplay );
}
bool PercentageDisplay::ShowDancePointsNotPercentage() const
{
// Use straight dance points in workout because the percentage denominator isn't accurate - we don't know when the players are going to stop.
if( GAMESTATE->m_pCurCourse )
{
if( GAMESTATE->m_pCurCourse->m_fGoalSeconds > 0 )
return true;
}
if( PREFSMAN->m_bDancePointsForOni )
return true;
return false;
}
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the PercentageDisplay. */
class LunaPercentageDisplay: public Luna<PercentageDisplay>
{
public:
static int LoadFromStats( T* p, lua_State *L )
{
const PlayerState *pStageStats = Luna<PlayerState>::check( L, 1 );
const PlayerStageStats *pPlayerStageStats = Luna<PlayerStageStats>::check( L, 2 );
p->Load( pStageStats, pPlayerStageStats );
COMMON_RETURN_SELF;
}
LunaPercentageDisplay()
{
ADD_METHOD( LoadFromStats );
}
};
LUA_REGISTER_DERIVED_CLASS( PercentageDisplay, ActorFrame )
// lua end
/*
* (c) 2001-2003 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.
*/