// Push event into active queue bool EventManager::VQueueEvent( const IEventPtr& pEvent ) { ENG_ASSERT( m_ActiveQueue >= 0 ); ENG_ASSERT( m_ActiveQueue < EVENTMANAGER_NUM_QUEUES ); auto mapIt = m_EventListeners.find( pEvent->VGetEventType() ); // If no one is listening, abort if( mapIt == m_EventListeners.end() || !mapIt->second.size() ) { return false; } m_EventQueues[m_ActiveQueue].push_back( pEvent ); return true; }
// Make all listeners process the event right now instead of add the event into queue bool EventManager::VTriggerEvent( IEventPtr pEvent ) const { auto mapIt = m_EventListeners.find( pEvent->VGetEventType() ); // corresponding listing list is not exist if( mapIt == m_EventListeners.end() ) { return false; } auto eventListenerList = mapIt->second; bool processed = false; for( auto listIt = eventListenerList.begin(); listIt != eventListenerList.end(); ++listIt ) { EventListenerDelegate listener = (*listIt); // trigger event listener( pEvent ); processed = true; } return processed; }
bool EventManager::VUpdate( unsigned long maxMs ) { unsigned long currMs = (unsigned long) ( GetGlobalTimer()->GetTime() * 1000.0 ); unsigned long targetMs = ( maxMs == kINFINITE )? kINFINITE: currMs + maxMs; // swap active queues and clear the new queue after the swap int queueToProcess = m_ActiveQueue; m_ActiveQueue = (m_ActiveQueue + 1) % EVENTMANAGER_NUM_QUEUES; m_EventQueues[m_ActiveQueue].clear(); std::string s( ( "EventLoop", "Processing Event Queue " + ToStr( queueToProcess ) + "; " + ToStr( ( unsigned long ) m_EventQueues[ queueToProcess ].size() ) + " events to process" ) ); \ Logger::Log( "EventLoop", s, NULL, NULL, 0 ); \ /*ENG_LOG("EventLoop", "Processing Event Queue " + ToStr(queueToProcess) + "; " + ToStr((unsigned long)m_EventQueues[queueToProcess].size()) + " events to process");*/ // Process the queue while ( !m_EventQueues[queueToProcess].empty() ) { // pop the front of the queue IEventPtr pEvent = m_EventQueues[queueToProcess].front(); m_EventQueues[queueToProcess].pop_front(); ENG_LOG( "EventLoop", "\t\tProcessing Event " + std::string( pEvent->GetName() ) ); const EventType& eventType = pEvent->VGetEventType(); // find all the delegate functions registered for this event auto findIt = m_EventListeners.find(eventType); if (findIt != m_EventListeners.end()) { const EventListenerList& eventListeners = findIt->second; ENG_LOG("EventLoop", "\t\tFound " + ToStr((unsigned long)eventListeners.size()) + " delegates"); // call each listener for (auto it = eventListeners.begin(); it != eventListeners.end(); ++it) { EventListenerDelegate listener = (*it); ENG_LOG("EventLoop", "\t\tSending event " + std::string(pEvent->GetName()) + " to delegate"); listener(pEvent); } } // check to see if time ran out currMs = (unsigned long) ( GetGlobalTimer()->GetTime() * 1000.0 ); if ( targetMs != IEventManager::kINFINITE && currMs >= targetMs ) { ENG_LOG("EventLoop", "Aborting event processing; time ran out"); break; } } // If we couldn't process all of the events, push the remaining events to the new active queue. // Note: To preserve sequencing, go back-to-front, inserting them at the head of the active queue bool queueFlushed = ( m_EventQueues[queueToProcess].empty() ); if ( !queueFlushed ) { while (!m_EventQueues[queueToProcess].empty()) { IEventPtr pEvent = m_EventQueues[queueToProcess].back(); m_EventQueues[queueToProcess].pop_back(); m_EventQueues[m_ActiveQueue].push_front(pEvent); } } return queueFlushed; }