示例#1
0
//--
// EventManager::AddScriptListener						- Chapter 11, page 336
//
// Creates a script-side event listener, given an appropriate Lua function.
bool cEventManager::AddScriptListener( char const * const pEventName, LuaObject callbackFunction )
{
	//Ensure this event type exists.
	const EventType testEventType( pEventName );
	const EventTypeSet::const_iterator typeIter = m_typeList.find( testEventType );
	if ( m_typeList.end() == typeIter ){
		printf("event not registered and listened!\n");
		return false;
	}

	const unsigned int eventID = testEventType.getHashValue();

	//OK, valid event type.  Make sure this isn't a duplicate.
	ScriptEventListenerMap::const_iterator mapIter = m_ScriptEventListenerMap.find( eventID );
	while ( m_ScriptEventListenerMap.end() != mapIter ){
		//Iterate through and ensure no duplicates.
		const ScriptEventListenerPtr evtListener = mapIter->second;
		const LuaObject & evtObj = evtListener->GetHandlerFunction();
		if ( evtObj == callbackFunction )
		{
			assert( 0 && "Attempted to listen to the same event handler twice!" );
			return false;
		}
		++mapIter;
	}

	//Now let's rez up a new script listener.
	ScriptEventListenerPtr listener( new ScriptEventListener( callbackFunction ) );

	m_ScriptEventListenerMap.insert( std::make_pair( eventID, listener ) );

	const bool bSuccess = VAddListener( listener, testEventType );
	return bSuccess;
}
示例#2
0
	// /////////////////////////////////////////////////////////////////
	// 
	// /////////////////////////////////////////////////////////////////
	bool EventManager::AddScriptActorListener(char const * const pEventName, LuaObject callbackFunction, const I32 actorID)
	{
		//Ensure this event type exists.
		const EventType testEventType( pEventName );
		const EventTypeSet::const_iterator typeIter = m_typeList.find( testEventType );
		if ( m_typeList.end() == typeIter )
		{
			assert( 0 && "Attempted to listen to an event type that wasn't registered!" );
			return false;
		}

		const U64 eventID = testEventType.getHashValue();

		//OK, valid event type.  Make sure this isn't a duplicate.
		ScriptActorEventListenerMap::const_iterator mapIter = m_ScriptActorEventListenerMap.find( eventID );
		while ( m_ScriptActorEventListenerMap.end() != mapIter )
		{
			//Iterate through and ensure no duplicates.
			const ScriptActorEventListenerPtr evtListener = mapIter->second;
			const LuaObject & evtObj = evtListener->GetHandlerFunction();
			if ( ( evtObj == callbackFunction ) && ( actorID == evtListener->GetActorID() ) )
			{
				assert( 0 && "Attempted to listen to the same event handler twice for a specific actor!" );
				return false;
			}
			++mapIter;
		}

		//Now let's rez up a new script listener.
		ScriptActorEventListenerPtr listener( GCC_NEW ScriptActorEventListener( callbackFunction, actorID ) );

		m_ScriptActorEventListenerMap.insert( std::make_pair( eventID, listener ) );

		const bool bSuccess = VAddListener( listener, testEventType );
		return bSuccess;
	}