From 8a1fb93003bc00e9aae578276a78b758dcd18023 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Sun, 16 Jan 2005 23:40:21 +0000 Subject: [PATCH] move attribute loading into the class that owns the attribute --- stepmania/src/Actor.cpp | 60 +++++++++++++++++++++++++++------ stepmania/src/Actor.h | 5 +-- stepmania/src/ActorFrame.cpp | 20 +++++++++++ stepmania/src/ActorFrame.h | 3 ++ stepmania/src/ActorScroller.cpp | 6 ++-- stepmania/src/ActorScroller.h | 2 +- stepmania/src/ActorUtil.cpp | 33 ++---------------- stepmania/src/BGAnimation.cpp | 11 +----- 8 files changed, 84 insertions(+), 56 deletions(-) diff --git a/stepmania/src/Actor.cpp b/stepmania/src/Actor.cpp index 71f8c1aa6d..a3cc15bde5 100644 --- a/stepmania/src/Actor.cpp +++ b/stepmania/src/Actor.cpp @@ -8,6 +8,7 @@ #include "RageLog.h" #include "arch/Dialog/Dialog.h" #include "Foreach.h" +#include "XmlFile.h" float Actor::g_fCurrentBGMTime = 0, Actor::g_fCurrentBGMBeat; @@ -62,6 +63,44 @@ Actor::Actor() m_bFirstUpdate = true; } +void Actor::LoadFromNode( const XNode* pNode ) +{ + // Load Name, if any. + pNode->GetAttrValue( "Name", m_sName ); + + + float f; + if( pNode->GetAttrValue( "BaseRotationXDegrees", f ) ) SetBaseRotationX( f ); + if( pNode->GetAttrValue( "BaseRotationYDegrees", f ) ) SetBaseRotationY( f ); + if( pNode->GetAttrValue( "BaseRotationZDegrees", f ) ) SetBaseRotationZ( f ); + if( pNode->GetAttrValue( "BaseZoomX", f ) ) SetBaseZoomX( f ); + if( pNode->GetAttrValue( "BaseZoomY", f ) ) SetBaseZoomY( f ); + if( pNode->GetAttrValue( "BaseZoomZ", f ) ) SetBaseZoomZ( f ); + + + // + // Load commands + // + FOREACH_CONST_Attr( pNode, a ) + { + CString sKeyName = a->m_sName; /* "OnCommand" */ + sKeyName.MakeLower(); + + if( sKeyName.Right(7) != "command" ) + continue; /* not a command */ + + const CString &sCommands = a->m_sValue; + Commands cmds = ParseCommands( sCommands ); + CString sCmdName; + /* Special case: "Command=foo" -> "OnCommand=foo" */ + if( sKeyName.size() == 7 ) + sCmdName="on"; + else + sCmdName = sKeyName.Left( sKeyName.size()-7 ); + m_mapNameToCommands[sCmdName] = cmds; + } +} + void Actor::Draw() { if( m_bHidden ) @@ -624,13 +663,6 @@ void Actor::AddRotationR( float rot ) RageQuatMultiply( &DestTweenState().quat, DestTweenState().quat, RageQuatFromR(rot) ); } -void Actor::AddCommands( const CString sName, const Commands &cmds ) -{ - CString sKey = sName; - sKey.MakeLower(); - m_mapNameToCommands[sKey] = cmds; -} - void Actor::RunCommands( const Commands &cmds ) { FOREACH_CONST( Command, cmds.v, c ) @@ -755,10 +787,18 @@ void Actor::HandleCommand( const Command &command ) * sent to all sub-actors (which aren't necessarily Sprites) on * GainFocus and LoseFocus. So, don't run EndHandleArgs * on these commands. */ - else if( sName=="customtexturerect" || sName=="texcoordvelocity" || sName=="scaletoclipped" || - sName=="stretchtexcoords" || sName=="position" || sName=="loop" || - sName=="rate" || sName=="propagate" ) + else if( + sName=="customtexturerect" || + sName=="texcoordvelocity" || + sName=="scaletoclipped" || + sName=="stretchtexcoords" || + sName=="position" || + sName=="loop" || + sName=="rate" || + sName=="propagate" ) + { return; + } else { CString sError = ssprintf( "Actor::HandleCommand: Unrecognized command name '%s'.", sName.c_str() ); diff --git a/stepmania/src/Actor.h b/stepmania/src/Actor.h index 9eba6c38d4..50c91242af 100644 --- a/stepmania/src/Actor.h +++ b/stepmania/src/Actor.h @@ -7,6 +7,7 @@ #include "Command.h" #include #include +struct XNode; #define DRAW_ORDER_BEFORE_EVERYTHING -100 #define DRAW_ORDER_TRANSITIONS 100 @@ -18,6 +19,7 @@ public: Actor(); virtual ~Actor() {} virtual void Reset(); + void LoadFromNode( const XNode* pNode ); static void SetBGMTime( float fTime, float fBeat ) { g_fCurrentBGMTime = fTime; g_fCurrentBGMBeat = fBeat; } @@ -301,7 +303,7 @@ public: // // Commands // - void AddCommands( const CString sName, const Commands &cmds ); + virtual void PlayCommand( const CString &sCommandName ); void RunCommands( const Commands &cmds ); virtual void HandleCommand( const Command &command ); // derivable static float GetCommandsLengthSeconds( const Commands &cmds ); @@ -319,7 +321,6 @@ public: // virtual void GainFocus( float fRate, bool bRewindMovie, bool bLoop ) {} virtual void LoseFocus() {} - virtual void PlayCommand( const CString &sCommandName ); protected: diff --git a/stepmania/src/ActorFrame.cpp b/stepmania/src/ActorFrame.cpp index 0d037dbae4..810f3e938b 100644 --- a/stepmania/src/ActorFrame.cpp +++ b/stepmania/src/ActorFrame.cpp @@ -2,6 +2,8 @@ #include "ActorFrame.h" #include "arch/Dialog/Dialog.h" #include "RageUtil.h" +#include "XmlFile.h" +#include "ActorUtil.h" ActorFrame::ActorFrame() { @@ -15,6 +17,24 @@ ActorFrame::~ActorFrame() DeleteAllChildren(); } +void ActorFrame::LoadFromNode( const CString &sDir, const XNode* pNode ) +{ + Actor::LoadFromNode( pNode ); + + // + // Load children + // + const XNode* pChildren = pNode->GetChild("children"); + if( pChildren ) + { + FOREACH_CONST_Child( pChildren, pChild ) + { + Actor* pChildActor = LoadFromActorFile( sDir, *pChild ); + AddChild( pChildActor ); + } + } +} + void ActorFrame::AddChild( Actor* pActor ) { #if _DEBUG diff --git a/stepmania/src/ActorFrame.h b/stepmania/src/ActorFrame.h index 9329151764..008d0ecea7 100644 --- a/stepmania/src/ActorFrame.h +++ b/stepmania/src/ActorFrame.h @@ -10,6 +10,9 @@ class ActorFrame : public Actor public: ActorFrame(); virtual ~ActorFrame(); + + void LoadFromNode( const CString &sDir, const XNode* pNode ); + virtual void AddChild( Actor* pActor ); virtual void RemoveChild( Actor* pActor ); virtual void MoveToTail( Actor* pActor ); diff --git a/stepmania/src/ActorScroller.cpp b/stepmania/src/ActorScroller.cpp index 3e977d878f..89594aa8a6 100644 --- a/stepmania/src/ActorScroller.cpp +++ b/stepmania/src/ActorScroller.cpp @@ -39,11 +39,13 @@ void ActorScroller::Load( m_bLoaded = true; } -void ActorScroller::LoadFromNode( const CString &sFile, const XNode *pNode ) +void ActorScroller::LoadFromNode( const CString &sDir, const XNode *pNode ) { + ActorFrame::LoadFromNode( sDir, pNode ); + #define REQUIRED_GET_VALUE( szName, valueOut ) \ if( !pNode->GetAttrValue( szName, valueOut ) ) \ - Dialog::OK( ssprintf("File '%s' is missing the value Scroller::%s", sFile.c_str(), szName) ); + Dialog::OK( ssprintf("Animation in '%s' is missing the value Scroller::%s", sDir.c_str(), szName) ); float fSecondsPerItem = 1; float fNumItemsToDraw = 7; diff --git a/stepmania/src/ActorScroller.h b/stepmania/src/ActorScroller.h index 47a843c9f8..086f934b5d 100644 --- a/stepmania/src/ActorScroller.h +++ b/stepmania/src/ActorScroller.h @@ -22,7 +22,7 @@ public: virtual void Update( float fDelta ); virtual void DrawPrimitives(); // DOES draw - void LoadFromNode( const CString &sFile, const XNode *pNode ); + void LoadFromNode( const CString &sDir, const XNode *pNode ); void SetDestinationItem( int iItem ) { m_fDestinationItem = float(iItem); } void SetCurrentAndDestinationItem( int iItem ) { m_fCurrentItem = m_fDestinationItem = float(iItem); } diff --git a/stepmania/src/ActorUtil.cpp b/stepmania/src/ActorUtil.cpp index 381a2988ec..4c10887163 100644 --- a/stepmania/src/ActorUtil.cpp +++ b/stepmania/src/ActorUtil.cpp @@ -230,37 +230,8 @@ retry: } - float f; - if( layer.GetAttrValue( "BaseRotationXDegrees", f ) ) pActor->SetBaseRotationX( f ); - if( layer.GetAttrValue( "BaseRotationYDegrees", f ) ) pActor->SetBaseRotationY( f ); - if( layer.GetAttrValue( "BaseRotationZDegrees", f ) ) pActor->SetBaseRotationZ( f ); - if( layer.GetAttrValue( "BaseZoomX", f ) ) pActor->SetBaseZoomX( f ); - if( layer.GetAttrValue( "BaseZoomY", f ) ) pActor->SetBaseZoomY( f ); - if( layer.GetAttrValue( "BaseZoomZ", f ) ) pActor->SetBaseZoomZ( f ); - - - // - // Load commands - // - FOREACH_CONST_Attr( &layer, a ) - { - CString KeyName = a->m_sName; /* "OnCommand" */ - KeyName.MakeLower(); - - if( KeyName.Right(7) != "command" ) - continue; /* not a command */ - - const CString &sCommands = a->m_sValue; - Commands cmds = ParseCommands( sCommands ); - CString sCmdName; - /* Special case: "Command=foo" -> "OnCommand=foo" */ - if( KeyName.size() == 7 ) - sCmdName="on"; - else - sCmdName = KeyName.Left( KeyName.size()-7 ); - pActor->AddCommands( sCmdName, cmds ); - } - + // TODO: LoadFromNode should be called when we still have a pointer to the derived type. + pActor->LoadFromNode( &layer ); ASSERT( pActor ); // we should have filled this in above return pActor; diff --git a/stepmania/src/BGAnimation.cpp b/stepmania/src/BGAnimation.cpp index 543bc51319..62efc922c2 100644 --- a/stepmania/src/BGAnimation.cpp +++ b/stepmania/src/BGAnimation.cpp @@ -170,7 +170,6 @@ void BGAnimation::LoadFromNode( const CString &sDir, const XNode& node ) { DEBUG_ASSERT( node.m_sName == "BGAnimation" ); - CString sInitCommand; if( node.GetAttrValue( "InitCommand", sInitCommand ) ) { @@ -180,15 +179,7 @@ void BGAnimation::LoadFromNode( const CString &sDir, const XNode& node ) this->RunCommands( ParseCommands(sInitCommand) ); } - const XNode* pChildren = node.GetChild("children"); - if( pChildren ) - { - FOREACH_CONST_Child( pChildren, pChild ) - { - Actor* pChildActor = LoadFromActorFile( sDir, *pChild ); - AddChild( pChildActor ); - } - } + ActorFrame::LoadFromNode( sDir, &node ); Command cmd; cmd.Load( "PlayCommand,Init" );