KEYMAPPER: Allow multiple inputs bound to a keymap action as defaults (#4830)

This allows backeds to override a keymap action mapping with lists of mapped hardware inputs instead of just one hardware input id

However, it will still be an overridding, so it will require all hardware input ids for a given keymap action to be added.
In other words, if a backend uses addDefaultBinding() for a given action, it will not ADD to any existing mappings of the action,
other than the ones that the backend itself has added (with addDefaultBinding())
This commit is contained in:
Athanasios Antoniou 2023-03-24 12:28:24 +02:00 committed by GitHub
parent 397a73572c
commit 3565d0e312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 11 deletions

View File

@ -256,19 +256,13 @@ StringArray Keymap::getActionDefaultMappings(Action *action) {
if (_backendDefaultBindings) {
KeymapperDefaultBindings::const_iterator it = _backendDefaultBindings->findDefaultBinding(_id, action->id);
if (it != _backendDefaultBindings->end()) {
if (it->_value.empty()) {
return StringArray();
}
return StringArray(1, it->_value);
return it->_value;
}
// If no keymap-specific default mapping was found, look for a standard action binding
it = _backendDefaultBindings->findDefaultBinding(kStandardActionsKeymapName, action->id);
if (it != _backendDefaultBindings->end()) {
if (it->_value.empty()) {
return StringArray();
}
return StringArray(1, it->_value);
return it->_value;
}
}

View File

@ -29,7 +29,7 @@
namespace Common {
class KeymapperDefaultBindings : public HashMap<String, String> {
class KeymapperDefaultBindings : public HashMap<String, StringArray> {
public:
/**
* This sets a default hwInput for a given Keymap Action
@ -37,12 +37,42 @@ public:
* @param actionId String representing Action id (Action.id)
* @param hwInputId String representing the HardwareInput id (HardwareInput.id)
*/
void setDefaultBinding(String keymapId, String actionId, String hwInputId) { setVal(keymapId + "_" + actionId, hwInputId); }
void setDefaultBinding(String keymapId, String actionId, String hwInputId) {
setVal(keymapId + "_" + actionId, hwInputId.empty() ? StringArray() : StringArray(1, hwInputId));
}
/**
* This adds a default hwInput for a given Keymap Action
* @param keymapId String representing Keymap id (Keymap.name)
* @param actionId String representing Action id (Action.id)
* @param hwInputId String representing the HardwareInput id (HardwareInput.id)
*/
void addDefaultBinding(String keymapId, String actionId, String hwInputId) {
// NOTE: addDefaultBinding() cannot be used to remove bindings;
// use setDefaultBinding() with a nullptr or empty string as hwInputId instead.
if (hwInputId.empty()) {
return;
}
KeymapperDefaultBindings::iterator it = findDefaultBinding(keymapId, actionId);
if (it != end()) {
// Don't allow an input to map to the same action multiple times
StringArray &itv = it->_value;
Array<String>::const_iterator found = Common::find(itv.begin(), itv.end(), hwInputId);
if (found == itv.end()) {
itv.push_back(hwInputId);
}
} else {
setDefaultBinding(keymapId, actionId, hwInputId);
}
}
/**
* This retrieves the assigned default hwKey for a given Keymap Action
* @param keymapId String representing Keymap id (Keymap.name)
* @param actionId String representing Action id (Action.id)
* @return String representing the HardwareInput id (HardwareInput.id)
* @return StringArray representing the list of HardwareInput ids (HardwareInput.id) that are mapped to Keymap Action
*/
const_iterator findDefaultBinding(String keymapId, String actionId) const {
return find(keymapId + "_" + actionId);