diff options
-rw-r--r-- | backends/common/action.cpp (renamed from backends/common/user-action.cpp) | 10 | ||||
-rw-r--r-- | backends/common/action.h (renamed from backends/common/user-action.h) | 64 | ||||
-rw-r--r-- | backends/common/hardware-key.h | 18 | ||||
-rw-r--r-- | backends/common/keymap-manager.cpp | 65 | ||||
-rw-r--r-- | backends/common/keymap.cpp | 50 | ||||
-rw-r--r-- | backends/common/keymap.h | 64 | ||||
-rw-r--r-- | backends/common/keymapper.cpp | 2 | ||||
-rw-r--r-- | backends/common/keymapper.h | 4 | ||||
-rw-r--r-- | backends/module.mk | 2 | ||||
-rw-r--r-- | dists/msvc8/scummvm.vcproj | 16 |
10 files changed, 176 insertions, 119 deletions
diff --git a/backends/common/user-action.cpp b/backends/common/action.cpp index 588d5dd45b..d6cb820b73 100644 --- a/backends/common/user-action.cpp +++ b/backends/common/action.cpp @@ -23,12 +23,12 @@ * */ -#include "backends/common/user-action.h" +#include "backends/common/action.h" #include "backends/common/keymap.h" namespace Common { -UserAction::UserAction(String des, UserActionCategory cat, UserActionType ty, +Action::Action(String des, ActionCategory cat, ActionType ty, int pr, int gr, int fl) { description = des; category = cat; @@ -40,18 +40,18 @@ UserAction::UserAction(String des, UserActionCategory cat, UserActionType ty, _parent = 0; } -void UserAction::setParent(Keymap *parent) { +void Action::setParent(Keymap *parent) { _parent = parent; } -void UserAction::mapKey(const HardwareKey *key) { +void Action::mapKey(const HardwareKey *key) { assert(_parent); if (_hwKey) _parent->unregisterMapping(this); _hwKey = key; if (_hwKey) _parent->registerMapping(this, key); } -const HardwareKey *UserAction::getMappedKey() const { +const HardwareKey *Action::getMappedKey() const { return _hwKey; } diff --git a/backends/common/user-action.h b/backends/common/action.h index 3fd7f7ae0a..a9e9356af8 100644 --- a/backends/common/user-action.h +++ b/backends/common/action.h @@ -23,10 +23,11 @@ * */ -#ifndef COMMON_USERACTION -#define COMMON_USERACTION +#ifndef COMMON_ACTION +#define COMMON_ACTION #include "common/events.h" +#include "common/func.h" #include "common/list.h" #include "common/str.h" @@ -36,32 +37,34 @@ struct HardwareKey; class Keymap; -enum UserActionType { - kGenericUserActionType, +enum ActionType { + kGenericActionType, // common actions - kDirectionUpUserAction, - kDirectionDownUserAction, - kDirectionLeftUserAction, - kDirectionRightUserAction, - kLeftClickUserAction, - kRightClickUserAction, - kSaveUserAction, - kMenuUserAction, - - kUserActionTypeMax + kDirectionUpAction, + kDirectionDownAction, + kDirectionLeftAction, + kDirectionRightAction, + kLeftClickAction, + kRightClickAction, + kSaveAction, + kMenuAction, + kVirtualKeyboardAction, + kRemapKeysAction, + + kActionTypeMax }; -enum UserActionCategory { - kGenericUserActionCategory, +enum ActionCategory { + kGenericActionCategory, // classes of action - probably need to be slightly more specific than this - kInGameUserAction, // effects the actual gameplay - kSystemUserAction, //show a menu / change volume / etc + kInGameAction, // effects the actual gameplay + kSystemAction, //show a menu / change volume / etc - kUserActionCategoryMax + kActionCategoryMax }; -struct UserAction { +struct Action { /** unique id used for saving/loading to config */ int32 id; /** Human readable description */ @@ -69,21 +72,21 @@ struct UserAction { /** Events to be sent when mapped key is pressed */ List<Event> events; - UserActionCategory category; - UserActionType type; + ActionCategory category; + ActionType type; int priority; int group; int flags; private: - /** Hardware key that is mapped to this UserAction */ + /** Hardware key that is mapped to this Action */ const HardwareKey *_hwKey; Keymap *_parent; public: - UserAction( String des = "", - UserActionCategory cat = kGenericUserActionCategory, - UserActionType ty = kGenericUserActionType, + Action( String des = "", + ActionCategory cat = kGenericActionCategory, + ActionType ty = kGenericActionType, int pr = 0, int gr = 0, int fl = 0 ); void setParent(Keymap *parent); @@ -93,6 +96,15 @@ public: const HardwareKey *getMappedKey() const; }; +struct ActionPriorityComp : public BinaryFunction<Action, Action, bool> { + bool operator()(const Action *x, const Action *y) const { + return x->priority > y->priority; + } + bool operator()(const Action &x, const Action &y) const { + return x.priority > y.priority; + } +}; + } // end of namespace Common #endif diff --git a/backends/common/hardware-key.h b/backends/common/hardware-key.h index 2059ff6a34..4aba5280bf 100644 --- a/backends/common/hardware-key.h +++ b/backends/common/hardware-key.h @@ -26,7 +26,7 @@ #ifndef COMMON_HARDWAREKEY #define COMMON_HARDWAREKEY -#include "backends/common/user-action.h" +#include "backends/common/action.h" namespace Common { @@ -44,13 +44,13 @@ struct HardwareKey { */ KeyState key; - UserActionCategory preferredCategory; - UserActionType preferredType; + ActionCategory preferredCategory; + ActionType preferredType; int16 group; HardwareKey(KeyState ks = KeyState(), String des = "", - UserActionCategory cat = kGenericUserActionCategory, - UserActionType ty = kGenericUserActionType, int gr = 0) { + ActionCategory cat = kGenericActionCategory, + ActionType ty = kGenericActionType, int gr = 0) { key = ks; description = des; preferredCategory = cat; @@ -98,6 +98,14 @@ public: return 0; } + List<HardwareKey*> getHardwareKeys() const { + return _keys; + } + + uint count() const { + return _keys.size(); + } + private: diff --git a/backends/common/keymap-manager.cpp b/backends/common/keymap-manager.cpp index 77712f331a..7af74f4edc 100644 --- a/backends/common/keymap-manager.cpp +++ b/backends/common/keymap-manager.cpp @@ -24,6 +24,7 @@ */ #include "backends/common/keymap-manager.h" +#include "common/algorithm.h" namespace Common { @@ -112,17 +113,17 @@ bool KeymapManager::loadKeymap(ConfigManager::Domain *domain, if (!key.hasPrefix(prefix.c_str())) continue; - // parse UserAction ID + // parse Action ID const char *actionIdStart = key.c_str() + prefix.size(); char *err; int32 actionId = (int32) strtol(actionIdStart, &err, 0); if (err == actionIdStart) { - warning("'%s' is not a valid UserAction ID", err); + warning("'%s' is not a valid Action ID", err); continue; } - UserAction *ua = map->getUserAction(actionId); + Action *ua = map->getAction(actionId); if (!ua) { - warning("'%s' keymap does not contain UserAction with ID %d", + warning("'%s' keymap does not contain Action with ID %d", name.c_str(), (int)actionId); continue; } @@ -145,29 +146,71 @@ bool KeymapManager::loadKeymap(ConfigManager::Domain *domain, } bool KeymapManager::isMapComplete(const Keymap *map) { - return false; + const List<Action*>& actions = map->getActions(); + List<Action*>::const_iterator it; + bool allMapped = true; + uint numberMapped = 0; + for (it = actions.begin(); it != actions.end(); it++) { + if ((*it)->getMappedKey()) { + numberMapped++; + } else { + allMapped = false; + break; + } + } + return (allMapped || numberMapped == _hardwareKeys->count()); } void KeymapManager::saveKeymap(ConfigManager::Domain *domain, const String& name, const Keymap *map) { - const Array<UserAction>& actions = map->getUserActions(); - Array<UserAction>::const_iterator it; + const List<Action*>& actions = map->getActions(); + List<Action*>::const_iterator it; char buf[11]; for (it = actions.begin(); it != actions.end(); it++) { String key("km_"); - sprintf(buf, "%d", it->id); + sprintf(buf, "%d", (*it)->id); key += name + "_" + buf; - if (it->getMappedKey()) - sprintf(buf, "%d", it->getMappedKey()->id); + if ((*it)->getMappedKey()) + sprintf(buf, "%d", (*it)->getMappedKey()->id); else strcpy(buf, ""); domain->setVal(key, buf); } } - void KeymapManager::automaticMap(Keymap *map) { + List<Action*> actions(map->getActions()), unmapped; + List<Action*>::iterator actIt; + List<HardwareKey*> keys = _hardwareKeys->getHardwareKeys(); + List<HardwareKey*>::iterator keyIt, selectedKey; + + // sort by priority + ActionPriorityComp priorityComp; + sort(actions.begin(), actions.end(), priorityComp); + + for (actIt = actions.begin(); actIt != actions.end(); actIt++) { + selectedKey = keys.end(); + Action *act = *actIt; + for (keyIt = keys.begin(); keyIt != keys.end(); keyIt++) { + if ((*keyIt)->preferredType == act->type) { + selectedKey = keyIt; + break; + } else if ((*keyIt)->preferredCategory == act->category) { + selectedKey = keyIt; + } + } + if (selectedKey != keys.end()) { + act->mapKey(*selectedKey); + keys.erase(selectedKey); + } else + unmapped.push_back(act); + } + + actIt = unmapped.begin(); + keyIt = keys.begin(); + while (actIt != unmapped.end() && keyIt != keys.end()) + (*actIt)->mapKey(*keyIt); } diff --git a/backends/common/keymap.cpp b/backends/common/keymap.cpp index 509f50e4eb..09ccc14c1d 100644 --- a/backends/common/keymap.cpp +++ b/backends/common/keymap.cpp @@ -29,28 +29,24 @@ namespace Common { Keymap::Keymap(const Keymap& km) : _actions(km._actions), _keymap() { - init(); - for (uint i = 0; i < _actions.size(); i++) { - const HardwareKey *hwKey = _actions[i].getMappedKey(); + List<Action*>::iterator it; + for (it = _actions.begin(); it != _actions.end(); it++) { + const HardwareKey *hwKey = (*it)->getMappedKey(); if (hwKey) { - _keymap[hwKey->key] = &_actions[i]; + _keymap[hwKey->key] = *it; } } } -void Keymap::init() { - _actions.reserve(20); -} - -void Keymap::addAction(UserAction& action) { - if (findUserAction(action.id)) - error("UserAction with id %d already in KeyMap!", action.id); - action.setParent(this); +void Keymap::addAction(Action *action) { + if (findAction(action->id)) + error("Action with id %d already in KeyMap!", action->id); + action->setParent(this); _actions.push_back(action); } -void Keymap::registerMapping(UserAction *action, const HardwareKey *hwKey) { - HashMap<KeyState, UserAction*>::iterator it; +void Keymap::registerMapping(Action *action, const HardwareKey *hwKey) { + HashMap<KeyState, Action*>::iterator it; it = _keymap.find(hwKey->key); // if key is already mapped to an action then un-map it if (it != _keymap.end()) @@ -59,36 +55,36 @@ void Keymap::registerMapping(UserAction *action, const HardwareKey *hwKey) { _keymap[hwKey->key] = action; } -void Keymap::unregisterMapping(UserAction *action) { +void Keymap::unregisterMapping(Action *action) { const HardwareKey *hwKey = action->getMappedKey(); if (hwKey) _keymap[hwKey->key] = 0; } -UserAction *Keymap::getUserAction(int32 id) { - return findUserAction(id); +Action *Keymap::getAction(int32 id) { + return findAction(id); } -UserAction *Keymap::findUserAction(int32 id) { - Array<UserAction>::iterator it; +Action *Keymap::findAction(int32 id) { + List<Action*>::iterator it; for (it = _actions.begin(); it != _actions.end(); it++) { - if (it->id == id) - return &*it; + if ((*it)->id == id) + return *it; } return 0; } -const UserAction *Keymap::findUserAction(int32 id) const { - Array<UserAction>::const_iterator it; +const Action *Keymap::findAction(int32 id) const { + List<Action*>::const_iterator it; for (it = _actions.begin(); it != _actions.end(); it++) { - if (it->id == id) - return &*it; + if ((*it)->id == id) + return *it; } return 0; } -UserAction *Keymap::getMappedAction(const KeyState& ks) const { - HashMap<KeyState, UserAction*>::iterator it; +Action *Keymap::getMappedAction(const KeyState& ks) const { + HashMap<KeyState, Action*>::iterator it; it = _keymap.find(ks); if (it == _keymap.end()) return 0; diff --git a/backends/common/keymap.h b/backends/common/keymap.h index eb7b227e52..c8b46cfd78 100644 --- a/backends/common/keymap.h +++ b/backends/common/keymap.h @@ -26,11 +26,11 @@ #ifndef COMMON_KEYMAP #define COMMON_KEYMAP -#include "common/array.h" -#include "common/keyboard.h" #include "common/func.h" #include "common/hashmap.h" -#include "backends/common/user-action.h" +#include "common/keyboard.h" +#include "common/list.h" +#include "backends/common/action.h" namespace Common { @@ -49,62 +49,60 @@ template<> struct Hash<KeyState> class Keymap { public: - Keymap() { init(); } + Keymap() {} Keymap(const Keymap& km); -private: - void init(); public: /** - * Adds a new UserAction to this Map, + * Adds a new Action to this Map, * adding it at the back of the internal array - * @param action the UserAction to add + * @param action the Action to add */ - void addAction(UserAction& action); + void addAction(Action *action); /** - * Retrieves the UserAction with the given id - * @param id id of UserAction to retrieve - * @return Pointer to the UserAction or 0 if not found + * Retrieves the Action with the given id + * @param id id of Action to retrieve + * @return Pointer to the Action or 0 if not found */ - UserAction *getUserAction(int32 id); + Action *getAction(int32 id); /** - * Get a read-only array of all the UserActions contained in this Keymap + * Get a read-only array of all the Actions contained in this Keymap */ - const Array<UserAction>& getUserActions() const { return _actions; } + const List<Action*>& getActions() const { return _actions; } /** - * Find the UserAction that a key is mapped to - * @param key the key that is mapped to the required UserAction - * @return a pointer to the UserAction or 0 if no + * Find the Action that a key is mapped to + * @param key the key that is mapped to the required Action + * @return a pointer to the Action or 0 if no */ - UserAction *getMappedAction(const KeyState& ks) const; + Action *getMappedAction(const KeyState& ks) const; private: - friend struct UserAction; + friend struct Action; /** - * Registers a HardwareKey to the given UserAction - * @param action UserAction in this Keymap + * Registers a HardwareKey to the given Action + * @param action Action in this Keymap * @param key pointer to HardwareKey to map - * @see UserAction::mapKey + * @see Action::mapKey */ - void registerMapping(UserAction *action, const HardwareKey *key); + void registerMapping(Action *action, const HardwareKey *key); /** - * Unregisters a HardwareKey from the given UserAction (if one is mapped) - * @param action UserAction in this Keymap - * @see UserAction::mapKey + * Unregisters a HardwareKey from the given Action (if one is mapped) + * @param action Action in this Keymap + * @see Action::mapKey */ - void unregisterMapping(UserAction *action); + void unregisterMapping(Action *action); - UserAction *findUserAction(int32 id); - const UserAction *findUserAction(int32 id) const; + Action *findAction(int32 id); + const Action *findAction(int32 id) const; - void internalMapKey(UserAction *action, HardwareKey *hwKey); + void internalMapKey(Action *action, HardwareKey *hwKey); - Array<UserAction> _actions; - HashMap<KeyState, UserAction*> _keymap; + List<Action*> _actions; + HashMap<KeyState, Action*> _keymap; }; diff --git a/backends/common/keymapper.cpp b/backends/common/keymapper.cpp index 1d06a6dbd7..b31333aebb 100644 --- a/backends/common/keymapper.cpp +++ b/backends/common/keymapper.cpp @@ -86,7 +86,7 @@ bool Keymapper::mapKeyUp(const KeyState& key) { bool Keymapper::mapKey(const KeyState& key, bool isKeyDown) { if (!_currentMap) return false; - UserAction *action = _currentMap->getMappedAction(key); + Action *action = _currentMap->getMappedAction(key); if (!action) return false; List<Event>::iterator it; for (it = action->events.begin(); it != action->events.end(); it++) { diff --git a/backends/common/keymapper.h b/backends/common/keymapper.h index 8d3e421625..a7d5386171 100644 --- a/backends/common/keymapper.h +++ b/backends/common/keymapper.h @@ -81,8 +81,8 @@ public: /** * @brief Map a key press event. - * If the active keymap contains a UserAction mapped to the given key, then - * the UserAction's events are pushed into the EventManager's event queue. + * 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 isKeyDown true for key down, false for key up * @return true if key was mapped diff --git a/backends/module.mk b/backends/module.mk index b690012042..758dff13b2 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -34,7 +34,7 @@ MODULE_OBJS := \ common/keymap.o \ common/keymap-manager.o \ common/keymapper.o \ - common/user-action.o \ + common/action.o \ # Include common rules include $(srcdir)/rules.mk diff --git a/dists/msvc8/scummvm.vcproj b/dists/msvc8/scummvm.vcproj index 736873fb47..36a72ac5d7 100644 --- a/dists/msvc8/scummvm.vcproj +++ b/dists/msvc8/scummvm.vcproj @@ -1057,6 +1057,14 @@ Name="common" > <File + RelativePath="..\..\backends\common\action.cpp" + > + </File> + <File + RelativePath="..\..\backends\common\action.h" + > + </File> + <File RelativePath="..\..\backends\common\hardware-key.h" > </File> @@ -1085,14 +1093,6 @@ > </File> <File - RelativePath="..\..\backends\common\user-action.cpp" - > - </File> - <File - RelativePath="..\..\backends\common\user-action.h" - > - </File> - <File RelativePath="..\..\backends\common\virtual-keyboard-gui.cpp" > </File> |