forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActorScroller.cpp
417 lines (359 loc) · 13.8 KB
/
ActorScroller.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
#include "global.h"
#include "ActorScroller.h"
#include "RageMath.hpp"
#include "RageUtil.h"
#include "XmlFile.h"
#include "arch/Dialog/Dialog.h"
#include "RageLog.h"
#include "ActorUtil.h"
#include "LuaBinding.h"
#include <limits>
using std::vector;
/* Tricky: We need ActorFrames created in Lua to auto delete their children.
* We don't want classes that derive from ActorFrame to auto delete their
* children. The name "ActorFrame" is widely used in Lua, so we'll have
* that string instead create an ActorFrameAutoDeleteChildren object. */
//REGISTER_ACTOR_CLASS( ActorScroller );
REGISTER_ACTOR_CLASS_WITH_NAME( ActorScrollerAutoDeleteChildren, ActorScroller );
ActorScroller *ActorScroller::Copy() const { return new ActorScroller(*this); }
ActorScroller::ActorScroller()
{
m_iNumItems = 0;
m_fCurrentItem = 0;
m_fDestinationItem = 0;
m_fSecondsPerItem = 1;
m_fSecondsPauseBetweenItems = 0;
m_fNumItemsToDraw = 7;
m_iFirstSubActorIndex = 0;
m_bLoop = false;
m_bWrap = false;
m_bFastCatchup = false;
m_bFunctionDependsOnPositionOffset = true;
m_bFunctionDependsOnItemIndex = true;
m_fPauseCountdownSeconds = 0;
m_fQuantizePixels = 0;
m_quadMask.SetBlendMode( BLEND_NO_EFFECT ); // don't change color values
m_quadMask.SetUseZBuffer( true ); // we want to write to the Zbuffer
m_fMaskWidth = 0;
m_fMaskHeight = 0;
DisableMask();
}
void ActorScroller::Load2()
{
m_iNumItems = m_SubActors.size();
Lua *L = LUA->Get();
for( unsigned i = 0; i < m_SubActors.size(); ++i )
{
lua_pushnumber( L, i );
this->m_SubActors[i]->m_pLuaInstance->Set( L, "ItemIndex" );
}
LUA->Release( L );
}
void ActorScroller::SetTransformFromReference( const LuaReference &ref )
{
m_exprTransformFunction.SetFromReference( ref );
// Probe to find which of the parameters are used.
#define GP(a,b) m_exprTransformFunction.GetTransformCached( a, b, 2 )
m_bFunctionDependsOnPositionOffset = (GP(0,0) != GP(1,0)) && (GP(0,1) != GP(1,1));
m_bFunctionDependsOnItemIndex = (GP(0,0) != GP(0,1)) && (GP(1,0) != GP(1,1));
m_exprTransformFunction.ClearCache();
}
void ActorScroller::SetTransformFromExpression( const std::string &sTransformFunction )
{
LuaReference ref;
ref.SetFromExpression( sTransformFunction );
SetTransformFromReference( ref );
}
void ActorScroller::SetTransformFromWidth( float fItemWidth )
{
SetTransformFromExpression( fmt::sprintf("function(self,offset,itemIndex,numItems) self:x(%f*offset) end",fItemWidth) );
}
void ActorScroller::SetTransformFromHeight( float fItemHeight )
{
SetTransformFromExpression( fmt::sprintf("function(self,offset,itemIndex,numItems) self:y(%f*offset) end",fItemHeight) );
}
void ActorScroller::EnableMask( float fWidth, float fHeight )
{
m_quadMask.SetVisible( fWidth != 0 && fHeight != 0 );
m_quadMask.SetWidth( fWidth );
m_fMaskWidth = fWidth;
m_quadMask.SetHeight( fHeight );
m_fMaskHeight = fHeight;
}
void ActorScroller::DisableMask()
{
m_quadMask.SetVisible( false );
}
void ActorScroller::ScrollThroughAllItems()
{
m_fCurrentItem = ( m_bLoop || m_bWrap )? +m_fNumItemsToDraw/2.0f : -(m_fNumItemsToDraw/2.0f)-1;
m_fDestinationItem = (float)(m_iNumItems+m_fNumItemsToDraw/2.0f+1);
}
void ActorScroller::ScrollWithPadding( float fItemPaddingStart, float fItemPaddingEnd )
{
m_fCurrentItem = -fItemPaddingStart;
m_fDestinationItem = m_iNumItems-1+fItemPaddingEnd;
}
float ActorScroller::GetSecondsForCompleteScrollThrough() const
{
float fTotalItems = m_fNumItemsToDraw + m_iNumItems;
return fTotalItems * (m_fSecondsPerItem + m_fSecondsPauseBetweenItems );
}
float ActorScroller::GetSecondsToDestination() const
{
float fTotalItemsToMove = fabsf(m_fCurrentItem - m_fDestinationItem);
return fTotalItemsToMove * m_fSecondsPerItem;
}
void ActorScroller::LoadFromNode( const XNode *pNode )
{
ActorFrame::LoadFromNode( pNode );
Load2();
float fNumItemsToDraw = 0;
if( pNode->GetAttrValue("NumItemsToDraw", fNumItemsToDraw) )
SetNumItemsToDraw( fNumItemsToDraw );
float fSecondsPerItem = 0;
if( pNode->GetAttrValue("SecondsPerItem", fSecondsPerItem) )
ActorScroller::SetSecondsPerItem( fSecondsPerItem );
Lua *L = LUA->Get();
pNode->PushAttrValue( L, "TransformFunction" );
{
LuaReference ref;
ref.SetFromStack( L );
if( !ref.IsNil() )
SetTransformFromReference( ref );
}
LUA->Release( L );
int iSubdivisions = 1;
if( pNode->GetAttrValue("Subdivisions", iSubdivisions) )
ActorScroller::SetNumSubdivisions( iSubdivisions );
bool bUseMask = false;
pNode->GetAttrValue( "UseMask", bUseMask );
if( bUseMask )
{
pNode->GetAttrValue( "MaskWidth", m_fMaskWidth );
pNode->GetAttrValue( "MaskHeight", m_fMaskHeight );
EnableMask( m_fMaskWidth, m_fMaskHeight );
}
pNode->GetAttrValue( "QuantizePixels", m_fQuantizePixels );
pNode->GetAttrValue( "LoopScroller", m_bLoop );
pNode->GetAttrValue( "WrapScroller", m_bWrap );
}
void ActorScroller::UpdateInternal( float fDeltaTime )
{
ActorFrame::UpdateInternal( fDeltaTime );
// If we have no children, the code below will busy loop.
if( !m_SubActors.size() )
return;
// handle pause
if( fDeltaTime > m_fPauseCountdownSeconds )
{
fDeltaTime -= m_fPauseCountdownSeconds;
m_fPauseCountdownSeconds = 0;
}
else
{
m_fPauseCountdownSeconds -= fDeltaTime;
fDeltaTime = 0;
return;
}
if( m_fCurrentItem == m_fDestinationItem )
return; // done scrolling
float fOldItemAtTop = m_fCurrentItem;
if( m_fSecondsPerItem > 0 )
{
float fApproachSpeed = fDeltaTime/m_fSecondsPerItem;
if( m_bFastCatchup )
{
float fDistanceToMove = fabsf(m_fCurrentItem - m_fDestinationItem);
if( fDistanceToMove > 1 )
fApproachSpeed *= fDistanceToMove*fDistanceToMove;
}
fapproach( m_fCurrentItem, m_fDestinationItem, fApproachSpeed );
}
// if items changed, then pause
if( (int)fOldItemAtTop != (int)m_fCurrentItem )
m_fPauseCountdownSeconds = m_fSecondsPauseBetweenItems;
if( m_bWrap )
{
float Delta = m_fDestinationItem - m_fCurrentItem;
m_fCurrentItem = fmodf( m_fCurrentItem, (float) m_iNumItems );
m_fDestinationItem = m_fCurrentItem + Delta;
}
if( m_bLoop )
{
m_fCurrentItem = fmodf( m_fCurrentItem, (float) m_iNumItems );
}
}
void ActorScroller::DrawPrimitives()
{
PositionItemsAndDrawPrimitives( true );
}
void ActorScroller::PositionItems()
{
PositionItemsAndDrawPrimitives( false );
}
/* Shift m_SubActors forward by iDist. This will place item m_iFirstSubActorIndex
* in m_SubActors[0]. */
void ActorScroller::ShiftSubActors( int iDist )
{
if( iDist != std::numeric_limits<int>::max() )
CircularShift( m_SubActors, iDist );
}
void ActorScroller::PositionItemsAndDrawPrimitives( bool bDrawPrimitives )
{
if( m_SubActors.empty() )
return;
float fNumItemsToDraw = m_fNumItemsToDraw;
if( m_quadMask.GetVisible() )
{
// write to z buffer so that top and bottom are clipped
// Draw an extra item; this is the one that will be masked.
fNumItemsToDraw++;
float fPositionFullyOffScreenTop = -(fNumItemsToDraw)/2.f;
float fPositionFullyOffScreenBottom = (fNumItemsToDraw)/2.f;
m_exprTransformFunction.TransformItemCached( m_quadMask, fPositionFullyOffScreenTop, -1, m_iNumItems );
if( bDrawPrimitives ) m_quadMask.Draw();
m_exprTransformFunction.TransformItemCached( m_quadMask, fPositionFullyOffScreenBottom, m_iNumItems, m_iNumItems );
if( bDrawPrimitives ) m_quadMask.Draw();
}
float fFirstItemToDraw = m_fCurrentItem - fNumItemsToDraw/2.f;
float fLastItemToDraw = m_fCurrentItem + fNumItemsToDraw/2.f;
int iFirstItemToDraw = (int) ceilf( fFirstItemToDraw );
int iLastItemToDraw = (int) ceilf( fLastItemToDraw );
if( !m_bLoop && !m_bWrap )
{
iFirstItemToDraw = Rage::clamp( iFirstItemToDraw, 0, m_iNumItems );
iLastItemToDraw = Rage::clamp( iLastItemToDraw, 0, m_iNumItems );
}
vector<Actor*> subs;
{
// Shift m_SubActors so iFirstItemToDraw is at the beginning.
int iNewFirstIndex = iFirstItemToDraw;
int iDist = iNewFirstIndex - m_iFirstSubActorIndex;
m_iFirstSubActorIndex = iNewFirstIndex;
ShiftSubActors( iDist );
}
int iNumToDraw = iLastItemToDraw - iFirstItemToDraw;
for( int i = 0; i < iNumToDraw; ++i )
{
int iItem = i + iFirstItemToDraw;
float fPosition = iItem - m_fCurrentItem;
int iIndex = i; // index into m_SubActors
if( m_bLoop || m_bWrap )
wrap( iIndex, m_SubActors.size() );
else if( iIndex < 0 || iIndex >= (int)m_SubActors.size() )
continue;
// Optimization: Zero out unused parameters so that they don't create new,
// unnecessary entries in the position cache. On scrollers with lots of
// items, especially with Subdivisions > 1, m_exprTransformFunction uses
// too much memory.
if( !m_bFunctionDependsOnPositionOffset )
fPosition = 0;
if( !m_bFunctionDependsOnItemIndex )
iItem = 0;
m_exprTransformFunction.TransformItemCached( *m_SubActors[iIndex], fPosition, iItem, m_iNumItems );
if( bDrawPrimitives )
{
if( m_bDrawByZPosition )
subs.push_back( m_SubActors[iIndex] );
else
m_SubActors[iIndex]->Draw();
}
}
if( m_bDrawByZPosition )
{
ActorUtil::SortByZPosition( subs );
for (auto *a: subs)
{
a->Draw();
}
}
}
// lua start
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the ActorScroller. */
class LunaActorScroller: public Luna<ActorScroller>
{
public:
static int PositionItems( T* p, lua_State *L ) { p->PositionItems(); COMMON_RETURN_SELF; }
static int SetTransformFromFunction( T* p, lua_State *L )
{
LuaReference ref;
LuaHelpers::FromStack( L, ref, 1 );
p->SetTransformFromReference( ref );
COMMON_RETURN_SELF;
}
static int SetTransformFromHeight( T* p, lua_State *L ) { p->SetTransformFromHeight(FArg(1)); COMMON_RETURN_SELF; }
static int SetTransformFromWidth( T* p, lua_State *L ) { p->SetTransformFromWidth(FArg(1)); COMMON_RETURN_SELF; }
static int SetCurrentAndDestinationItem( T* p, lua_State *L ) { p->SetCurrentAndDestinationItem( FArg(1) ); COMMON_RETURN_SELF; }
static int SetDestinationItem( T* p, lua_State *L ) { p->SetDestinationItem( FArg(1) ); COMMON_RETURN_SELF; }
static int GetSecondsToDestination( T* p, lua_State *L ) { lua_pushnumber( L, p->GetSecondsToDestination() ); return 1; }
static int SetSecondsPerItem( T* p, lua_State *L ) { p->SetSecondsPerItem(FArg(1)); COMMON_RETURN_SELF; }
static int GetSecondsPauseBetweenItems( T* p, lua_State *L ) { lua_pushnumber( L, p->GetSecondsPauseBetweenItems() ); return 1; }
static int SetSecondsPauseBetweenItems( T* p, lua_State *L ) { p->SetSecondsPauseBetweenItems(FArg(1)); COMMON_RETURN_SELF; }
static int SetPauseCountdownSeconds( T* p, lua_State *L ) { p->SetPauseCountdownSeconds(FArg(1)); COMMON_RETURN_SELF; }
static int SetNumSubdivisions( T* p, lua_State *L ) { p->SetNumSubdivisions(IArg(1)); COMMON_RETURN_SELF; }
static int ScrollThroughAllItems( T* p, lua_State *L ) { p->ScrollThroughAllItems(); COMMON_RETURN_SELF; }
static int ScrollWithPadding( T* p, lua_State *L ) { p->ScrollWithPadding(FArg(1),FArg(2)); COMMON_RETURN_SELF; }
static int SetFastCatchup( T* p, lua_State *L ) { p->SetFastCatchup(BArg(1)); COMMON_RETURN_SELF; }
static int SetLoop( T* p, lua_State *L ) { p->SetLoop(BArg(1)); COMMON_RETURN_SELF; }
static int SetWrap( T* p, lua_State *L ) { p->SetWrap(BArg(1)); COMMON_RETURN_SELF; }
static int SetMask( T* p, lua_State *L ) { p->EnableMask(FArg(1), FArg(2)); COMMON_RETURN_SELF; }
static int SetNumItemsToDraw( T* p, lua_State *L ) { p->SetNumItemsToDraw(FArg(1)); COMMON_RETURN_SELF; }
static int GetFullScrollLengthSeconds( T* p, lua_State *L ) { lua_pushnumber( L, p->GetSecondsForCompleteScrollThrough() ); return 1; }
static int GetCurrentItem( T* p, lua_State *L ) { lua_pushnumber( L, p->GetCurrentItem() ); return 1; }
static int GetDestinationItem( T* p, lua_State *L ) { lua_pushnumber( L, p->GetDestinationItem() ); return 1; }
static int GetNumItems( T* p, lua_State *L ) { lua_pushnumber( L, p->GetNumItems() ); return 1; }
LunaActorScroller()
{
ADD_METHOD( PositionItems );
ADD_METHOD( SetTransformFromFunction );
ADD_METHOD( SetTransformFromHeight );
ADD_METHOD( SetTransformFromWidth );
ADD_METHOD( SetCurrentAndDestinationItem );
ADD_METHOD( SetDestinationItem );
ADD_METHOD( GetSecondsToDestination );
ADD_METHOD( SetSecondsPerItem );
ADD_METHOD( SetSecondsPauseBetweenItems );
ADD_METHOD( GetSecondsPauseBetweenItems );
ADD_METHOD( SetPauseCountdownSeconds );
ADD_METHOD( SetNumSubdivisions );
ADD_METHOD( ScrollThroughAllItems );
ADD_METHOD( ScrollWithPadding );
ADD_METHOD( SetFastCatchup );
ADD_METHOD( SetLoop );
ADD_METHOD( SetWrap );
ADD_METHOD( SetMask );
ADD_METHOD( SetNumItemsToDraw );
ADD_METHOD( GetFullScrollLengthSeconds );
ADD_METHOD( GetCurrentItem );
ADD_METHOD( GetDestinationItem );
ADD_METHOD( GetNumItems );
}
};
LUA_REGISTER_DERIVED_CLASS( ActorScroller, ActorFrame )
// lua end
/*
* (c) 2003-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.
*/