From c0a215282d12872cf32fb24f9067216c0f869b96 Mon Sep 17 00:00:00 2001 From: Tarek Soliman Date: Thu, 1 Mar 2012 06:29:44 -0600 Subject: KEYMAPPER: Add delays for *UP events coming from non-keys Delayed entries are in a queue where each entry stores how many milliseconds should pass based on the last entry. --- common/EventDispatcher.cpp | 6 ++++++ common/EventMapper.cpp | 28 ++++++++++++++++++++++++++++ common/events.h | 15 +++++++++++++++ 3 files changed, 49 insertions(+) (limited to 'common') diff --git a/common/EventDispatcher.cpp b/common/EventDispatcher.cpp index 4c7286bbb5..012a2dfce5 100644 --- a/common/EventDispatcher.cpp +++ b/common/EventDispatcher.cpp @@ -60,6 +60,12 @@ void EventDispatcher::dispatch() { } } } + + List delayedEvents = _mapper->getDelayedEvents(); + for (List::iterator k = delayedEvents.begin(); k != delayedEvents.end(); ++k) { + const Event delayedEvent = *k; + dispatchEvent(delayedEvent); + } } void EventDispatcher::registerMapper(EventMapper *mapper) { diff --git a/common/EventMapper.cpp b/common/EventMapper.cpp index 0771ecdd89..47db61e472 100644 --- a/common/EventMapper.cpp +++ b/common/EventMapper.cpp @@ -21,6 +21,8 @@ */ #include "common/events.h" + +#include "common/system.h" #include "common/textconsole.h" namespace Common { @@ -61,4 +63,30 @@ List DefaultEventMapper::mapEvent(const Event &ev, EventSource *source) { } +void DefaultEventMapper::addDelayedEvent(uint32 millis, Event ev) { + if (_delayedEvents.empty()) { + _delayedEffectiveTime = g_system->getMillis() + millis; + millis = 0; + } + DelayedEventsEntry entry = DelayedEventsEntry(millis, ev); + _delayedEvents.push(entry); +} + +List DefaultEventMapper::getDelayedEvents() { + List events; + + if (_delayedEvents.empty()) + return events; + + uint32 now = g_system->getMillis(); + + while (!_delayedEvents.empty() && now >= _delayedEffectiveTime) { + DelayedEventsEntry entry = _delayedEvents.pop(); + if (!_delayedEvents.empty()) + _delayedEffectiveTime += _delayedEvents.front().timerOffset; + events.push_back(entry.event); + } + return events; +} + } // namespace Common diff --git a/common/events.h b/common/events.h index 7e411ecce5..7366c51d36 100644 --- a/common/events.h +++ b/common/events.h @@ -232,12 +232,27 @@ public: * Map an incoming event to one or more action events */ virtual List mapEvent(const Event &ev, EventSource *source) = 0; + + virtual List getDelayedEvents() = 0; }; class DefaultEventMapper : public EventMapper { public: + DefaultEventMapper() : _delayedEvents(), _delayedEffectiveTime(0) {} // EventMapper interface virtual List mapEvent(const Event &ev, EventSource *source); + virtual List getDelayedEvents(); +protected: + virtual void addDelayedEvent(uint32 millis, Event ev); + + struct DelayedEventsEntry { + const uint32 timerOffset; + const Event event; + DelayedEventsEntry(const uint32 offset, const Event ev) : timerOffset(offset), event(ev) { } + }; + + Queue _delayedEvents; + uint32 _delayedEffectiveTime; }; /** -- cgit v1.2.3