- Main Page
- Classes
- Files

- File List
- File Members

# C:/Users/Public/AGS Final Folder/BFLETCHER D4010283/Source/Game/Source/EventManager.cpp

Go to the documentation of this file.

    00001 #include "EventManager.h"
    00002
    00003 #include "HashString.h"
    00004
    00005 #include "SDL.h"
    00006 #include "DebugUtil.h"
    00007
    00008 template<> EventManager* TSingleton<EventManager>::m_pInstance = NULL;
    00009
    00010 EventManager::EventManager()
    00011 {
    00012
    00013 }
    00014
    00015 EventManager::~EventManager()
    00016 {
    00017         for(size_t i = 0; i < m_vEventListenerSets.size(); i++)
    00018                 delete m_vEventListenerSets[i];
    00019         m_vEventListenerSets.clear();
    00020
    00021
    00022         while(m_qEventQueue.size() > 0)
    00023         {
    00024                 delete m_qEventQueue.front();
    00025                 m_qEventQueue.pop();
    00026         }
    00027 }
    00028
    00029 void EventManager::ProcessQueue(unsigned int totalTime /* = 0 */)
    00030 {
    00031         Uint32 startTime = SDL_GetTicks();
    00032         Uint32 lastProcessingTime = 0;
    00033
    00034         while(lastProcessingTime - startTime < totalTime || totalTime == 0)
    00035         {
    00036                 if(m_qEventQueue.size() == 0)
    00037                         return;
    00038
    00039                 IEvent* pEvent = m_qEventQueue.front();
    00040                 m_qEventQueue.pop();
    00041
    00042                 HashString eventType = pEvent->GetEventType();
    00043                 EventListenerSet* pSet = GetListenerSet(eventType);
    00044
    00045                 if(pSet)
    00046                 {
    00047                         std::vector<IEventListener*>::iterator listenerIt = pSet->EventListeners.begin();
    00048                         while(listenerIt != pSet->EventListeners.end())
    00049                         {
    00050                                 if((*listenerIt)->HandleEvent(pEvent)) //If it returns true it has consumed the event
    00051                                         break;
    00052
    00053                                 listenerIt++;
    00054                         }
    00055                 }
    00056
    00057                 delete pEvent;
    00058                 lastProcessingTime = SDL_GetTicks();
    00059         }
    00060 }
    00061
    00062 void EventManager::AddEventListener(const HashString &eventType, IEventListener *pListener)
    00063 {
    00064         EventListenerSet* pSet = GetListenerSet(eventType);
    00065         if(!pSet)
    00066         {
    00067                 pSet = new EventListenerSet();
    00068                 pSet->EventType = eventType;
    00069                 m_vEventListenerSets.push_back(pSet);
    00070         }
    00071
    00072         for(unsigned int i = 0; i < pSet->EventListeners.size(); i++)
    00073         {
    00074                 if(pSet->EventListeners[i] == pListener)
    00075                         return; //Listener already in list.
    00076         }
    00077
    00078         pSet->EventListeners.push_back(pListener);
    00079 }
    00080
    00081 void EventManager::RemoveEventListener(const HashString &eventType, IEventListener *pListener)
    00082 {
    00083         EventListenerSet* pSet = GetListenerSet(eventType);
    00084         if(!pSet)
    00085                 return; //throw error
    00086
    00087         std::vector<IEventListener*>::iterator it = pSet->EventListeners.begin();
    00088         while(it != pSet->EventListeners.end())
    00089         {
    00090                 if((*it) == pListener)
    00091                 {
    00092                         pSet->EventListeners.erase(it);
    00093                         return;
    00094                 }
    00095
    00096                 it++;
    00097         }
    00098 }
    00099
    00100 void EventManager::RemoveEventListenerFromAll(IEventListener *pListener)
    00101 {
    00102         std::vector<EventListenerSet*>::iterator setIter = m_vEventListenerSets.begin();
    00103         while(setIter != m_vEventListenerSets.end())
    00104         {
    00105                 std::vector<IEventListener*>::iterator it = (*setIter)->EventListeners.begin();
    00106                 while(it != (*setIter)->EventListeners.end())
    00107                 {
    00108                         if((*it) == pListener)
    00109                         {
    00110                                 (*setIter)->EventListeners.erase(it);
    00111                                 break;
    00112                         }
    00113
    00114                         it++;
    00115                 }
    00116
    00117                 setIter++;
    00118         }
    00119 }
    00120
    00121 void EventManager::TriggerEvent(IEvent *pEvent)
    00122 {
    00123         DebugUtil::outputDebugText("Event received\n");
    00124         //HashString eventType = pEvent->GetEventType();
    00125         DebugUtil::outputDebugText("Event received: %s\n", pEvent->GetEventType().getStr().c_str());
    00126         EventListenerSet* pSet = GetListenerSet(pEvent->GetEventType());
    00127
    00128         if(!pSet)
    00129                 return; //Throw low priority error
    00130
    00131         std::vector<IEventListener*>::iterator listenerIt = pSet->EventListeners.begin();
    00132         while(listenerIt != pSet->EventListeners.end())
    00133         {
    00134                 if((*listenerIt)->HandleEvent(pEvent)) //If it returns true it has consumed the event
    00135                         return;
    00136
    00137                 listenerIt++;
    00138         }
    00139
    00140         delete pEvent;
    00141 }
    00142
    00143 void EventManager::QueueEvent(IEvent *pEvent)
    00144 {
    00145         if(GetListenerSet(pEvent->GetEventType()))
    00146                 m_qEventQueue.push(pEvent);
    00147 //      else
    00148 //              throw ERROR;
    00149 }
    00150
    00151 EventManager::EventListenerSet* EventManager::GetListenerSet(const HashString &eventType)
    00152 {
    00153         std::vector<EventListenerSet*>::iterator it = m_vEventListenerSets.begin();
    00154         while(it != m_vEventListenerSets.end())
    00155         {
    00156                 if((*it)->EventType == eventType)
    00157                         return (*it);
    00158
    00159                 it++;
    00160         }
    00161
    00162         return 0;
    00163 }