work on allowing parameters to messages: pass a Message
struct, not just a string
This commit is contained in:
+12
-6
@@ -1003,7 +1003,7 @@ void Actor::AddRotationR( float rot )
|
||||
RageQuatMultiply( &DestTweenState().quat, DestTweenState().quat, RageQuatFromR(rot) );
|
||||
}
|
||||
|
||||
void Actor::RunCommands( const LuaReference& cmds )
|
||||
void Actor::RunCommands( const LuaReference& cmds, const LuaReference *pParamTable )
|
||||
{
|
||||
Lua *L = LUA->Get();
|
||||
|
||||
@@ -1014,8 +1014,14 @@ void Actor::RunCommands( const LuaReference& cmds )
|
||||
// 1st parameter
|
||||
this->PushSelf( L );
|
||||
|
||||
// call function with 1 argument and 0 results
|
||||
lua_call( L, 1, 0 );
|
||||
// 2nd parameter
|
||||
if( pParamTable == NULL )
|
||||
lua_pushnil( L );
|
||||
else
|
||||
pParamTable->PushSelf( L );
|
||||
|
||||
// call function with 2 arguments and 0 results
|
||||
lua_call( L, 2, 0 );
|
||||
|
||||
LUA->Release(L);
|
||||
}
|
||||
@@ -1184,11 +1190,11 @@ const apActorCommands *Actor::GetCommand( const RString &sCommandName ) const
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
void Actor::PlayCommand( const RString &sCommandName )
|
||||
void Actor::PlayCommand( const RString &sCommandName, const LuaReference *pParamTable )
|
||||
{
|
||||
const apActorCommands *pCmd = GetCommand( sCommandName );
|
||||
if( pCmd != NULL )
|
||||
RunCommands( *pCmd );
|
||||
RunCommands( *pCmd, pParamTable );
|
||||
}
|
||||
|
||||
void Actor::PushContext( lua_State *L )
|
||||
@@ -1217,7 +1223,7 @@ void Actor::SetParent( Actor *pParent )
|
||||
|
||||
void Actor::HandleMessage( const Message &msg )
|
||||
{
|
||||
PlayCommand( msg.GetName() );
|
||||
PlayCommand( msg.GetName(), &msg.GetParamTable() );
|
||||
}
|
||||
|
||||
Actor::TweenInfo::TweenInfo()
|
||||
|
||||
@@ -356,11 +356,11 @@ public:
|
||||
void AddCommand( const RString &sCmdName, apActorCommands apac );
|
||||
bool HasCommand( const RString &sCmdName );
|
||||
const apActorCommands *GetCommand( const RString &sCommandName ) const;
|
||||
virtual void PlayCommand( const RString &sCommandName );
|
||||
virtual void RunCommands( const LuaReference& cmds );
|
||||
void RunCommands( const apActorCommands& cmds ) { this->RunCommands( *cmds ); } // convenience
|
||||
virtual void PlayCommand( const RString &sCommandName, const LuaReference *pParamTable = NULL );
|
||||
virtual void RunCommands( const LuaReference& cmds, const LuaReference *pParamTable = NULL );
|
||||
void RunCommands( const apActorCommands& cmds, const LuaReference *pParamTable = NULL ) { this->RunCommands( *cmds, pParamTable ); } // convenience
|
||||
// If we're a leaf, then execute this command.
|
||||
virtual void RunCommandsOnLeaves( const LuaReference& cmds ) { RunCommands(cmds); }
|
||||
virtual void RunCommandsOnLeaves( const LuaReference& cmds, const LuaReference *pParamTable = NULL ) { RunCommands(cmds, pParamTable); }
|
||||
|
||||
//
|
||||
// Messages
|
||||
|
||||
@@ -236,30 +236,30 @@ void ActorFrame::EndDraw()
|
||||
Actor::EndDraw();
|
||||
}
|
||||
|
||||
void ActorFrame::PlayCommandOnChildren( const RString &sCommandName )
|
||||
void ActorFrame::PlayCommandOnChildren( const RString &sCommandName, const LuaReference *pParamTable )
|
||||
{
|
||||
const apActorCommands *pCmd = GetCommand( sCommandName );
|
||||
if( pCmd != NULL )
|
||||
RunCommandsOnChildren( *pCmd );
|
||||
RunCommandsOnChildren( *pCmd, pParamTable );
|
||||
}
|
||||
|
||||
void ActorFrame::PlayCommandOnLeaves( const RString &sCommandName )
|
||||
void ActorFrame::PlayCommandOnLeaves( const RString &sCommandName, const LuaReference *pParamTable )
|
||||
{
|
||||
const apActorCommands *pCmd = GetCommand( sCommandName );
|
||||
if( pCmd != NULL )
|
||||
RunCommandsOnLeaves( **pCmd );
|
||||
RunCommandsOnLeaves( **pCmd, pParamTable );
|
||||
}
|
||||
|
||||
void ActorFrame::RunCommandsOnChildren( const LuaReference& cmds )
|
||||
void ActorFrame::RunCommandsOnChildren( const LuaReference& cmds, const LuaReference *pParamTable )
|
||||
{
|
||||
for( unsigned i=0; i<m_SubActors.size(); i++ )
|
||||
m_SubActors[i]->RunCommands( cmds );
|
||||
m_SubActors[i]->RunCommands( cmds, pParamTable );
|
||||
}
|
||||
|
||||
void ActorFrame::RunCommandsOnLeaves( const LuaReference& cmds )
|
||||
void ActorFrame::RunCommandsOnLeaves( const LuaReference& cmds, const LuaReference *pParamTable )
|
||||
{
|
||||
for( unsigned i=0; i<m_SubActors.size(); i++ )
|
||||
m_SubActors[i]->RunCommandsOnLeaves( cmds );
|
||||
m_SubActors[i]->RunCommandsOnLeaves( cmds, pParamTable );
|
||||
}
|
||||
|
||||
void ActorFrame::UpdateInternal( float fDeltaTime )
|
||||
@@ -351,12 +351,12 @@ void ActorFrame::DeleteAllChildren()
|
||||
m_SubActors.clear();
|
||||
}
|
||||
|
||||
void ActorFrame::RunCommands( const LuaReference& cmds )
|
||||
void ActorFrame::RunCommands( const LuaReference& cmds, const LuaReference *pParamTable )
|
||||
{
|
||||
if( m_bPropagateCommands )
|
||||
RunCommandsOnChildren( cmds );
|
||||
RunCommandsOnChildren( cmds, pParamTable );
|
||||
else
|
||||
Actor::RunCommands( cmds );
|
||||
Actor::RunCommands( cmds, pParamTable );
|
||||
}
|
||||
|
||||
void ActorFrame::SetPropagateCommands( bool b )
|
||||
@@ -364,9 +364,9 @@ void ActorFrame::SetPropagateCommands( bool b )
|
||||
m_bPropagateCommands = b;
|
||||
}
|
||||
|
||||
void ActorFrame::PlayCommand( const RString &sCommandName )
|
||||
void ActorFrame::PlayCommand( const RString &sCommandName, const LuaReference *pParamTable )
|
||||
{
|
||||
Actor::PlayCommand( sCommandName );
|
||||
Actor::PlayCommand( sCommandName, pParamTable );
|
||||
|
||||
// HACK: Don't propogate Init. It gets called once for every Actor when the
|
||||
// Actor is loaded, and we don't want to call it again.
|
||||
@@ -376,7 +376,7 @@ void ActorFrame::PlayCommand( const RString &sCommandName )
|
||||
for( unsigned i=0; i<m_SubActors.size(); i++ )
|
||||
{
|
||||
Actor* pActor = m_SubActors[i];
|
||||
pActor->PlayCommand( sCommandName );
|
||||
pActor->PlayCommand( sCommandName, pParamTable );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,11 +35,11 @@ public:
|
||||
// Commands
|
||||
//
|
||||
virtual void PushSelf( lua_State *L );
|
||||
void PlayCommandOnChildren( const RString &sCommandName );
|
||||
void PlayCommandOnLeaves( const RString &sCommandName );
|
||||
virtual void RunCommandsOnChildren( const LuaReference& cmds ); /* but not on self */
|
||||
void RunCommandsOnChildren( const apActorCommands& cmds ) { this->RunCommandsOnChildren( *cmds ); } // convenience
|
||||
virtual void RunCommandsOnLeaves( const LuaReference& cmds ); /* but not on self */
|
||||
void PlayCommandOnChildren( const RString &sCommandName, const LuaReference *pParamTable = NULL );
|
||||
void PlayCommandOnLeaves( const RString &sCommandName, const LuaReference *pParamTable = NULL );
|
||||
virtual void RunCommandsOnChildren( const LuaReference& cmds, const LuaReference *pParamTable = NULL ); /* but not on self */
|
||||
void RunCommandsOnChildren( const apActorCommands& cmds, const LuaReference *pParamTable = NULL ) { this->RunCommandsOnChildren( *cmds, pParamTable ); } // convenience
|
||||
virtual void RunCommandsOnLeaves( const LuaReference& cmds, const LuaReference *pParamTable = NULL ); /* but not on self */
|
||||
|
||||
virtual void UpdateInternal( float fDeltaTime );
|
||||
virtual void ProcessMessages( float fDeltaTime );
|
||||
@@ -65,9 +65,9 @@ public:
|
||||
/* Amount of time until all tweens (and all children's tweens) have stopped: */
|
||||
virtual float GetTweenTimeLeft() const;
|
||||
|
||||
virtual void PlayCommand( const RString &sCommandName );
|
||||
virtual void RunCommands( const LuaReference& cmds );
|
||||
void RunCommands( const apActorCommands& cmds ) { this->RunCommands( *cmds ); } // convenience
|
||||
virtual void PlayCommand( const RString &sCommandName, const LuaReference *pParamTable = NULL );
|
||||
virtual void RunCommands( const LuaReference& cmds, const LuaReference *pParamTable = NULL );
|
||||
void RunCommands( const apActorCommands& cmds, const LuaReference *pParamTable = NULL ) { this->RunCommands( *cmds, pParamTable ); } // convenience
|
||||
|
||||
protected:
|
||||
void LoadChildrenFromNode( const XNode* pNode );
|
||||
|
||||
@@ -155,13 +155,22 @@ Message::~Message()
|
||||
void Message::PushParamTable( lua_State *L )
|
||||
{
|
||||
m_pParams->PushSelf( L );
|
||||
}
|
||||
|
||||
const LuaReference &Message::GetParamTable() const
|
||||
{
|
||||
return *m_pParams;
|
||||
}
|
||||
|
||||
void Message::PushParam( lua_State *L, RString sName )
|
||||
{
|
||||
}
|
||||
|
||||
void Message::SetParamFromStack( lua_State *L, const RString &sName )
|
||||
{
|
||||
m_pParams->Set( L, sName );
|
||||
}
|
||||
|
||||
MessageManager::MessageManager()
|
||||
{
|
||||
// Register with Lua.
|
||||
@@ -212,23 +221,27 @@ void MessageManager::Unsubscribe( IMessageSubscriber* pSubscriber, MessageID m )
|
||||
Unsubscribe( pSubscriber, MessageToString(m) );
|
||||
}
|
||||
|
||||
void MessageManager::Broadcast( const RString& sMessage ) const
|
||||
void MessageManager::Broadcast( const Message &msg ) const
|
||||
{
|
||||
ASSERT( !sMessage.empty() );
|
||||
|
||||
LockMut(g_Mutex);
|
||||
|
||||
map<RString,SubscribersSet>::const_iterator iter = g_MessageToSubscribers.find( sMessage );
|
||||
map<RString,SubscribersSet>::const_iterator iter = g_MessageToSubscribers.find( msg.GetName() );
|
||||
if( iter == g_MessageToSubscribers.end() )
|
||||
return;
|
||||
|
||||
FOREACHS_CONST( IMessageSubscriber*, iter->second, p )
|
||||
{
|
||||
IMessageSubscriber *pSub = *p;
|
||||
pSub->HandleMessage( Message(sMessage) );
|
||||
pSub->HandleMessage( msg );
|
||||
}
|
||||
}
|
||||
|
||||
void MessageManager::Broadcast( const RString& sMessage ) const
|
||||
{
|
||||
ASSERT( !sMessage.empty() );
|
||||
Broadcast( Message(sMessage) );
|
||||
}
|
||||
|
||||
void MessageManager::Broadcast( MessageID m ) const
|
||||
{
|
||||
Broadcast( MessageToString(m) );
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "LuaManager.h"
|
||||
struct lua_State;
|
||||
class LuaTable;
|
||||
class LuaReference;
|
||||
|
||||
enum MessageID
|
||||
{
|
||||
@@ -142,8 +143,20 @@ struct Message
|
||||
RString GetName() const { return m_sName; }
|
||||
|
||||
void PushParamTable( lua_State *L );
|
||||
const LuaReference &GetParamTable() const;
|
||||
void PushParam( lua_State *L, RString sName );
|
||||
|
||||
void SetParamFromStack( lua_State *L, const RString &sName );
|
||||
|
||||
template<typename T>
|
||||
void SetParam( const RString &sName, const T &val )
|
||||
{
|
||||
Lua *L = LUA->Get();
|
||||
LuaHelpers::Push( L, val );
|
||||
SetParamFromStack( L, sName );
|
||||
LUA->Release( L );
|
||||
}
|
||||
|
||||
bool operator==( const RString &s ) const { return m_sName == s; }
|
||||
bool operator==( MessageID id ) const { return MessageIDToString(id) == m_sName; }
|
||||
|
||||
@@ -193,6 +206,7 @@ public:
|
||||
void Subscribe( IMessageSubscriber* pSubscriber, MessageID m );
|
||||
void Unsubscribe( IMessageSubscriber* pSubscriber, const RString& sMessage );
|
||||
void Unsubscribe( IMessageSubscriber* pSubscriber, MessageID m );
|
||||
void Broadcast( const Message &msg ) const;
|
||||
void Broadcast( const RString& sMessage ) const;
|
||||
void Broadcast( MessageID m ) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user