KEYMAPPER: Rewrite the EventMapper API

This commit is contained in:
Tarek Soliman 2012-02-17 17:08:58 -06:00
parent c0b04fdcaa
commit a0ba4eb569
4 changed files with 50 additions and 62 deletions

View File

@ -180,38 +180,35 @@ void Keymapper::popKeymap(const char *name) {
}
bool Keymapper::notifyEvent(const Common::Event &ev) {
bool mapped = false;
List<Event> Keymapper::mapEvent(const Event &ev, EventSource *source) {
if (source && !source->allowMapping()) {
return DefaultEventMapper::mapEvent(ev, source);
}
List<Event> mappedEvents;
if (ev.type == Common::EVENT_KEYDOWN)
mapped = mapKeyDown(ev.kbd);
mappedEvents = mapKeyDown(ev.kbd);
else if (ev.type == Common::EVENT_KEYUP)
mapped = mapKeyUp(ev.kbd);
mappedEvents = mapKeyUp(ev.kbd);
if (mapped)
return true;
if (!mappedEvents.empty())
return mappedEvents;
else
return mapEvent(ev);
return DefaultEventMapper::mapEvent(ev, source);
}
bool Keymapper::mapEvent(const Common::Event &ev) {
// pass through - copy the event
Event evt = ev;
addEvent(evt);
return true;
}
bool Keymapper::mapKeyDown(const KeyState& key) {
List<Event> Keymapper::mapKeyDown(const KeyState& key) {
return mapKey(key, true);
}
bool Keymapper::mapKeyUp(const KeyState& key) {
List<Event> Keymapper::mapKeyUp(const KeyState& key) {
return mapKey(key, false);
}
bool Keymapper::mapKey(const KeyState& key, bool keyDown) {
List<Event> Keymapper::mapKey(const KeyState& key, bool keyDown) {
if (!_enabled || _activeMaps.empty())
return false;
return List<Event>();
Action *action = 0;
@ -238,11 +235,9 @@ bool Keymapper::mapKey(const KeyState& key, bool keyDown) {
}
if (!action)
return false;
return List<Event>();
executeAction(action, keyDown);
return true;
return executeAction(action, keyDown);
}
Action *Keymapper::getAction(const KeyState& key) {
@ -251,7 +246,8 @@ Action *Keymapper::getAction(const KeyState& key) {
return action;
}
void Keymapper::executeAction(const Action *action, bool keyDown) {
List<Event> Keymapper::executeAction(const Action *action, bool keyDown) {
List<Event> mappedEvents;
List<Event>::const_iterator it;
for (it = action->events.begin(); it != action->events.end(); ++it) {
@ -291,8 +287,9 @@ void Keymapper::executeAction(const Action *action, bool keyDown) {
}
evt.mouse = _eventMan->getMousePos();
addEvent(evt);
mappedEvents.push_back(evt);
}
return mappedEvents;
}
const HardwareKey *Keymapper::findHardwareKey(const KeyState& key) {

View File

@ -39,7 +39,7 @@ namespace Common {
const char *const kGuiKeymapName = "gui";
const char *const kGlobalKeymapName = "global";
class Keymapper : public Common::EventMapper, private Common::ArtificialEventSource {
class Keymapper : public Common::DefaultEventMapper {
public:
struct MapRecord {
@ -77,6 +77,9 @@ public:
Keymapper(EventManager *eventMan);
~Keymapper();
// EventMapper interface
virtual List<Event> mapEvent(const Event &ev, EventSource *source);
/**
* Registers a HardwareKeySet with the Keymapper
* @note should only be called once (during backend initialisation)
@ -137,37 +140,27 @@ public:
*/
void popKeymap(const char *name = 0);
// Implementation of the EventMapper interface
bool notifyEvent(const Common::Event &ev);
bool pollEvent(Common::Event &ev) { return Common::ArtificialEventSource::pollEvent(ev); }
/**
* @brief Map a key press event.
* If the active keymap contains a Action mapped to the given key, then
* the Action's events are pushed into the EventManager's event queue.
* @param key key that was pressed
* @param keyDown true for key down, false for key up
* @return true if key was mapped
* @return mapped events
*/
bool mapKey(const KeyState& key, bool keyDown);
List<Event> mapKey(const KeyState& key, bool keyDown);
/**
* @brief Map a key down event.
* @see mapKey
*/
bool mapKeyDown(const KeyState& key);
List<Event> mapKeyDown(const KeyState& key);
/**
* @brief Map a key up event.
* @see mapKey
*/
bool mapKeyUp(const KeyState& key);
/**
* Map non-key incoming events
* @param ev incoming event
*/
bool mapEvent(const Common::Event &ev);
List<Event> mapKeyUp(const KeyState& key);
/**
* Enable/disable the keymapper
@ -195,7 +188,7 @@ private:
void pushKeymap(Keymap *newMap, bool transparent, bool global);
Action *getAction(const KeyState& key);
void executeAction(const Action *act, bool keyDown);
List<Event> executeAction(const Action *act, bool keyDown);
EventManager *_eventMan;

View File

@ -21,7 +21,6 @@
*/
#include "common/events.h"
#include "common/textconsole.h"
namespace Common {
@ -49,26 +48,15 @@ void EventDispatcher::dispatch() {
dispatchPoll();
for (List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) {
const bool allowMapping = i->source->allowMapping();
while (i->source->pollEvent(event)) {
// We only try to process the events via the setup event mapper, when
// we have a setup mapper and when the event source allows mapping.
assert(_mapper);
if (allowMapping) {
bool mapped = _mapper->notifyEvent(event);
// EventMappers must map all events
if (!mapped)
error("Event [%u] was not mapped by the EventMapper!", event.type);
// We allow the event mapper to create multiple events, when
// eating an event.
while (_mapper->pollEvent(event))
dispatchEvent(event);
List<Event> mappedEvents = _mapper->mapEvent(event, i->source);
// Try getting another event from the current EventSource.
continue;
} else {
dispatchEvent(Event(event));
for (List<Event>::iterator j = mappedEvents.begin(); j != mappedEvents.end(); ++j) {
const Event mappedEvent = *j;
dispatchEvent(mappedEvent);
}
}
}

View File

@ -213,15 +213,25 @@ public:
*
* An example for this is the Keymapper.
*/
class EventMapper : public EventSource, public EventObserver {
class EventMapper {
public:
/** For event mappers resulting events should never be mapped */
bool allowMapping() const { return false; }
virtual ~EventMapper() {}
/**
* Map an incoming event to one or more action events
*/
virtual List<Event> mapEvent(const Event &ev, EventSource *source) = 0;
};
class DefaultEventMapper : public EventMapper, private ArtificialEventSource {
bool notifyEvent(const Event &ev) { ArtificialEventSource::addEvent(Event(ev)); return true; }
bool pollEvent(Event &ev) { return ArtificialEventSource::pollEvent(ev); }
class DefaultEventMapper : public EventMapper {
public:
// EventMapper interface
virtual List<Event> mapEvent(const Event &ev, EventSource *source) {
List<Event> events;
// just pass it through
events.push_back(ev);
return events;
}
};
/**