Beispiel #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;
}
Beispiel #2
0
	// /////////////////////////////////////////////////////////////////
	// 
	// /////////////////////////////////////////////////////////////////
	bool EventManager::RemoveScriptListener(const char *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 )
		{
			assert( 0 && "Attempted to remove a listener for an event type that doesn't exist!" );
			return false;
		}

		const U64 eventID = testEventType.getHashValue();

		//OK, now ensure it exists in the multimap.
		bool bFound = false;
		ScriptEventListenerMap::iterator mapIter = m_ScriptEventListenerMap.find( eventID );
		while ( m_ScriptEventListenerMap.end() != mapIter )
		{
			const ScriptEventListenerPtr evtListener = mapIter->second;
			const LuaObject & evtObj = evtListener->GetHandlerFunction();
			if ( evtObj == callbackFunction )
			{
				bFound = true;
				break;
			}
			++mapIter;
		}

		if ( false == bFound )
		{
			assert( 0 && "Attempted to remove a script listener for an event it was never listening for!" );
			return false;
		}

		//Remove this listener from the map.
		const ScriptEventListenerPtr listener = mapIter->second;
		m_ScriptEventListenerMap.erase( mapIter );

		//Now remove from the "main" listener set.
		const bool bSuccess = VDelListener( listener, testEventType );
		return bSuccess;
	}