ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

EventDispatcher.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/events.h"
00024 
00025 namespace Common {
00026 
00027 EventDispatcher::EventDispatcher() : _autoFreeMapper(false), _mapper(nullptr) {
00028 }
00029 
00030 EventDispatcher::~EventDispatcher() {
00031     for (List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) {
00032         if (i->autoFree)
00033             delete i->source;
00034     }
00035 
00036     for (List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) {
00037         if (i->autoFree)
00038             delete i->observer;
00039     }
00040 
00041     if (_autoFreeMapper) {
00042         delete _mapper;
00043     }
00044     _mapper = nullptr;
00045 }
00046 
00047 void EventDispatcher::dispatch() {
00048     Event event;
00049 
00050     dispatchPoll();
00051 
00052     for (List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) {
00053         while (i->source->pollEvent(event)) {
00054             // We only try to process the events via the setup event mapper, when
00055             // we have a setup mapper and when the event source allows mapping.
00056             assert(_mapper);
00057             List<Event> mappedEvents = _mapper->mapEvent(event, i->source);
00058 
00059             for (List<Event>::iterator j = mappedEvents.begin(); j != mappedEvents.end(); ++j) {
00060                 const Event mappedEvent = *j;
00061                 dispatchEvent(mappedEvent);
00062             }
00063         }
00064     }
00065 
00066     List<Event> delayedEvents = _mapper->getDelayedEvents();
00067     for (List<Event>::iterator k = delayedEvents.begin(); k != delayedEvents.end(); ++k) {
00068         const Event delayedEvent = *k;
00069         dispatchEvent(delayedEvent);
00070     }
00071 }
00072 
00073 void EventDispatcher::clearEvents() {
00074     Event event;
00075 
00076     for (List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) {
00077         while (i->source->pollEvent(event)) {}
00078     }
00079 }
00080 
00081 
00082 void EventDispatcher::registerMapper(EventMapper *mapper, bool autoFree) {
00083     if (_autoFreeMapper) {
00084         delete _mapper;
00085     }
00086     _mapper = mapper;
00087     _autoFreeMapper = autoFree;
00088 }
00089 
00090 
00091 void EventDispatcher::registerSource(EventSource *source, bool autoFree) {
00092     SourceEntry newEntry;
00093 
00094     newEntry.source = source;
00095     newEntry.autoFree = autoFree;
00096 
00097     _sources.push_back(newEntry);
00098 }
00099 
00100 void EventDispatcher::unregisterSource(EventSource *source) {
00101     for (List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) {
00102         if (i->source == source) {
00103             if (i->autoFree)
00104                 delete source;
00105 
00106             _sources.erase(i);
00107             return;
00108         }
00109     }
00110 }
00111 
00112 void EventDispatcher::registerObserver(EventObserver *obs, uint priority, bool autoFree, bool notifyPoll) {
00113     ObserverEntry newEntry;
00114 
00115     newEntry.observer = obs;
00116     newEntry.priority = priority;
00117     newEntry.autoFree = autoFree;
00118     newEntry.poll = notifyPoll;
00119 
00120     for (List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) {
00121         if (i->priority < priority) {
00122             _observers.insert(i, newEntry);
00123             return;
00124         }
00125     }
00126 
00127     _observers.push_back(newEntry);
00128 }
00129 
00130 void EventDispatcher::unregisterObserver(EventObserver *obs) {
00131     for (List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) {
00132         if (i->observer == obs) {
00133             if (i->autoFree)
00134                 delete obs;
00135 
00136             _observers.erase(i);
00137             return;
00138         }
00139     }
00140 }
00141 
00142 void EventDispatcher::dispatchEvent(const Event &event) {
00143     for (List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) {
00144         if (i->observer->notifyEvent(event))
00145             break;
00146     }
00147 }
00148 
00149 void EventDispatcher::dispatchPoll() {
00150     for (List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) {
00151         if (i->poll == true)
00152             if (i->observer->notifyPoll())
00153                 break;
00154     }
00155 }
00156 
00157 } // End of namespace Common


Generated on Sat May 18 2019 05:01:01 for ResidualVM by doxygen 1.7.1
curved edge   curved edge