Rename KeyDef to InputMapping and give it responsibility for Axis encoding

This commit is contained in:
Henrik Rydgård 2023-03-29 11:59:31 +02:00
parent 3c9e968ca0
commit d00809ae53
13 changed files with 165 additions and 204 deletions

View file

@ -31,21 +31,21 @@ const char *GetDeviceName(int deviceId) {
}
}
std::vector<KeyDef> dpadKeys;
std::vector<KeyDef> confirmKeys;
std::vector<KeyDef> cancelKeys;
std::vector<KeyDef> tabLeftKeys;
std::vector<KeyDef> tabRightKeys;
std::vector<InputMapping> dpadKeys;
std::vector<InputMapping> confirmKeys;
std::vector<InputMapping> cancelKeys;
std::vector<InputMapping> tabLeftKeys;
std::vector<InputMapping> tabRightKeys;
static std::unordered_map<int, int> uiFlipAnalogY;
static void AppendKeys(std::vector<KeyDef> &keys, const std::vector<KeyDef> &newKeys) {
static void AppendKeys(std::vector<InputMapping> &keys, const std::vector<InputMapping> &newKeys) {
for (auto iter = newKeys.begin(); iter != newKeys.end(); ++iter) {
keys.push_back(*iter);
}
}
void SetDPadKeys(const std::vector<KeyDef> &leftKey, const std::vector<KeyDef> &rightKey,
const std::vector<KeyDef> &upKey, const std::vector<KeyDef> &downKey) {
void SetDPadKeys(const std::vector<InputMapping> &leftKey, const std::vector<InputMapping> &rightKey,
const std::vector<InputMapping> &upKey, const std::vector<InputMapping> &downKey) {
dpadKeys.clear();
// Store all directions into one vector for now. In the future it might be
@ -56,12 +56,12 @@ void SetDPadKeys(const std::vector<KeyDef> &leftKey, const std::vector<KeyDef> &
AppendKeys(dpadKeys, downKey);
}
void SetConfirmCancelKeys(const std::vector<KeyDef> &confirm, const std::vector<KeyDef> &cancel) {
void SetConfirmCancelKeys(const std::vector<InputMapping> &confirm, const std::vector<InputMapping> &cancel) {
confirmKeys = confirm;
cancelKeys = cancel;
}
void SetTabLeftRightKeys(const std::vector<KeyDef> &tabLeft, const std::vector<KeyDef> &tabRight) {
void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::vector<InputMapping> &tabRight) {
tabLeftKeys = tabLeft;
tabRightKeys = tabRight;
}

View file

@ -8,6 +8,7 @@
#include "Common/Math/lin/vec3.h"
#include "Common/Input/KeyCodes.h"
#include "Common/Log.h"
// Default device IDs
@ -79,21 +80,51 @@ enum {
#endif
// Represents a single bindable key
class KeyDef {
static const int AXIS_BIND_NKCODE_START = 4000;
inline int TranslateKeyCodeToAxis(int keyCode, int *direction) {
if (keyCode < AXIS_BIND_NKCODE_START)
return 0;
int k = keyCode - AXIS_BIND_NKCODE_START;
// Even/odd for direction.
*direction = k & 1 ? -1 : 1;
return k / 2;
}
class InputMapping {
private:
inline int TranslateKeyCodeFromAxis(int axisId, int direction) {
return AXIS_BIND_NKCODE_START + axisId * 2 + (direction < 0 ? 1 : 0);
}
public:
KeyDef() : deviceId(0), keyCode(0) {}
KeyDef(int devId, int k) : deviceId(devId), keyCode(k) {}
InputMapping() : deviceId(0), keyCode(0) {}
// From a key mapping
InputMapping(int _deviceId, int key) : deviceId(_deviceId), keyCode(key) {}
// From an axis
InputMapping(int _deviceId, int axis, int direction) : deviceId(_deviceId), keyCode(TranslateKeyCodeFromAxis(axis, direction)) {
_dbg_assert_(direction != 0);
}
int deviceId;
int keyCode;
int keyCode; // Can also represent an axis with direction, if encoded properly.
bool IsAxis() const {
return keyCode >= AXIS_BIND_NKCODE_START;
}
int Axis(int *direction) const {
_dbg_assert_(IsAxis());
return TranslateKeyCodeToAxis(keyCode, direction);
}
// If you want to use std::find and match ANY, you need to perform an explicit search for that.
bool operator < (const KeyDef &other) const {
bool operator < (const InputMapping &other) const {
if (deviceId < other.deviceId) return true;
if (deviceId > other.deviceId) return false;
if (keyCode < other.keyCode) return true;
return false;
}
bool operator == (const KeyDef &other) const {
bool operator == (const InputMapping &other) const {
if (deviceId != other.deviceId && deviceId != DEVICE_ID_ANY && other.deviceId != DEVICE_ID_ANY) return false;
if (keyCode != other.keyCode) return false;
return true;
@ -156,15 +187,15 @@ struct AxisInput {
};
// Is there a nicer place for this stuff? It's here to avoid dozens of linking errors in UnitTest..
extern std::vector<KeyDef> dpadKeys;
extern std::vector<KeyDef> confirmKeys;
extern std::vector<KeyDef> cancelKeys;
extern std::vector<KeyDef> tabLeftKeys;
extern std::vector<KeyDef> tabRightKeys;
void SetDPadKeys(const std::vector<KeyDef> &leftKey, const std::vector<KeyDef> &rightKey,
const std::vector<KeyDef> &upKey, const std::vector<KeyDef> &downKey);
void SetConfirmCancelKeys(const std::vector<KeyDef> &confirm, const std::vector<KeyDef> &cancel);
void SetTabLeftRightKeys(const std::vector<KeyDef> &tabLeft, const std::vector<KeyDef> &tabRight);
extern std::vector<InputMapping> dpadKeys;
extern std::vector<InputMapping> confirmKeys;
extern std::vector<InputMapping> cancelKeys;
extern std::vector<InputMapping> tabLeftKeys;
extern std::vector<InputMapping> tabRightKeys;
void SetDPadKeys(const std::vector<InputMapping> &leftKey, const std::vector<InputMapping> &rightKey,
const std::vector<InputMapping> &upKey, const std::vector<InputMapping> &downKey);
void SetConfirmCancelKeys(const std::vector<InputMapping> &confirm, const std::vector<InputMapping> &cancel);
void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::vector<InputMapping> &tabRight);
// 0 means unknown (attempt autodetect), -1 means flip, 1 means original direction.
void SetAnalogFlipY(std::unordered_map<int, int> flipYByDeviceId);

View file

@ -263,11 +263,11 @@ bool Clickable::Touch(const TouchInput &input) {
return contains;
}
static bool MatchesKeyDef(const std::vector<KeyDef> &defs, const KeyInput &key) {
static bool MatchesKeyDef(const std::vector<InputMapping> &defs, const KeyInput &key) {
// In addition to the actual search, we need to do another search where we replace the device ID with "ANY".
return
std::find(defs.begin(), defs.end(), KeyDef(key.deviceId, key.keyCode)) != defs.end() ||
std::find(defs.begin(), defs.end(), KeyDef(DEVICE_ID_ANY, key.keyCode)) != defs.end();
std::find(defs.begin(), defs.end(), InputMapping(key.deviceId, key.keyCode)) != defs.end() ||
std::find(defs.begin(), defs.end(), InputMapping(DEVICE_ID_ANY, key.keyCode)) != defs.end();
}
// TODO: O/X confirm preference for xperia play?

View file

@ -469,7 +469,7 @@ bool UpdateVRKeys(const KeyInput &key) {
std::vector<int> nativeKeys;
bool wasScreenKeyOn = pspKeys[CTRL_SCREEN];
bool wasCameraAdjustOn = pspKeys[VIRTKEY_VR_CAMERA_ADJUST];
if (KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &nativeKeys)) {
if (KeyMap::InputMappingToPspButton(InputMapping(key.deviceId, key.keyCode), &nativeKeys)) {
for (int& nativeKey : nativeKeys) {
pspKeys[nativeKey] = key.flags & KEY_DOWN;
}

View file

@ -95,7 +95,7 @@ void ControlMapper::SetPSPAxis(int device, char axis, float value, int stick) {
bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) {
std::vector<int> pspKeys;
KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &pspKeys);
KeyMap::InputMappingToPspButton(InputMapping(key.deviceId, key.keyCode), &pspKeys);
if (pspKeys.size() && (key.flags & KEY_IS_REPEAT)) {
// Claim that we handled this. Prevents volume key repeats from popping up the volume control on Android.
@ -329,7 +329,7 @@ void ControlMapper::ProcessAxis(const AxisInput &axis, int direction) {
const float scale = virtKeys_[VIRTKEY_ANALOG_LIGHTLY - VIRTKEY_FIRST] ? g_Config.fAnalogLimiterDeadzone : 1.0f;
std::vector<int> results;
KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, direction, &results);
KeyMap::InputMappingToPspButton(InputMapping(axis.deviceId, axis.axisId, direction), &results);
for (int result : results) {
float value = fabs(axis.value) * scale;
@ -367,7 +367,7 @@ void ControlMapper::ProcessAxis(const AxisInput &axis, int direction) {
}
std::vector<int> resultsOpposite;
KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, -direction, &resultsOpposite);
KeyMap::InputMappingToPspButton(InputMapping(axis.deviceId, axis.axisId, -direction), &resultsOpposite);
for (int result : resultsOpposite) {
if (result == VIRTKEY_SPEED_ANALOG)

View file

@ -36,8 +36,6 @@
namespace KeyMap {
KeyDef AxisDef(int deviceId, int axisId, int direction);
KeyMapping g_controllerMap;
// Incremented on modification, so we know when to update menus.
int g_controllerMapGeneration = 0;
@ -47,46 +45,40 @@ std::set<int> g_seenDeviceIds;
bool g_swapped_keys = false;
void KeyCodesFromPspButton(int btn, std::vector<keycode_t> *keycodes) {
for (auto i = g_controllerMap[btn].begin(), end = g_controllerMap[btn].end(); i != end; ++i) {
keycodes->push_back((keycode_t)i->keyCode);
}
}
// TODO: This is such a mess...
void UpdateNativeMenuKeys() {
std::vector<KeyDef> confirmKeys, cancelKeys;
std::vector<KeyDef> tabLeft, tabRight;
std::vector<KeyDef> upKeys, downKeys, leftKeys, rightKeys;
std::vector<InputMapping> confirmKeys, cancelKeys;
std::vector<InputMapping> tabLeft, tabRight;
std::vector<InputMapping> upKeys, downKeys, leftKeys, rightKeys;
int confirmKey = g_Config.iButtonPreference == PSP_SYSTEMPARAM_BUTTON_CROSS ? CTRL_CROSS : CTRL_CIRCLE;
int cancelKey = g_Config.iButtonPreference == PSP_SYSTEMPARAM_BUTTON_CROSS ? CTRL_CIRCLE : CTRL_CROSS;
// Mouse mapping might be problematic in UI, so let's ignore mouse for UI
KeyFromPspButton(confirmKey, &confirmKeys, true);
KeyFromPspButton(cancelKey, &cancelKeys, true);
KeyFromPspButton(CTRL_LTRIGGER, &tabLeft, true);
KeyFromPspButton(CTRL_RTRIGGER, &tabRight, true);
KeyFromPspButton(CTRL_UP, &upKeys, true);
KeyFromPspButton(CTRL_DOWN, &downKeys, true);
KeyFromPspButton(CTRL_LEFT, &leftKeys, true);
KeyFromPspButton(CTRL_RIGHT, &rightKeys, true);
InputMappingsFromPspButton(confirmKey, &confirmKeys, true);
InputMappingsFromPspButton(cancelKey, &cancelKeys, true);
InputMappingsFromPspButton(CTRL_LTRIGGER, &tabLeft, true);
InputMappingsFromPspButton(CTRL_RTRIGGER, &tabRight, true);
InputMappingsFromPspButton(CTRL_UP, &upKeys, true);
InputMappingsFromPspButton(CTRL_DOWN, &downKeys, true);
InputMappingsFromPspButton(CTRL_LEFT, &leftKeys, true);
InputMappingsFromPspButton(CTRL_RIGHT, &rightKeys, true);
#ifdef __ANDROID__
// Hardcode DPAD on Android
upKeys.push_back(KeyDef(DEVICE_ID_ANY, NKCODE_DPAD_UP));
downKeys.push_back(KeyDef(DEVICE_ID_ANY, NKCODE_DPAD_DOWN));
leftKeys.push_back(KeyDef(DEVICE_ID_ANY, NKCODE_DPAD_LEFT));
rightKeys.push_back(KeyDef(DEVICE_ID_ANY, NKCODE_DPAD_RIGHT));
upKeys.push_back(InputMapping(DEVICE_ID_ANY, NKCODE_DPAD_UP));
downKeys.push_back(InputMapping(DEVICE_ID_ANY, NKCODE_DPAD_DOWN));
leftKeys.push_back(InputMapping(DEVICE_ID_ANY, NKCODE_DPAD_LEFT));
rightKeys.push_back(InputMapping(DEVICE_ID_ANY, NKCODE_DPAD_RIGHT));
#endif
// Push several hard-coded keys before submitting to native.
const KeyDef hardcodedConfirmKeys[] = {
KeyDef(DEVICE_ID_KEYBOARD, NKCODE_SPACE),
KeyDef(DEVICE_ID_KEYBOARD, NKCODE_ENTER),
KeyDef(DEVICE_ID_KEYBOARD, NKCODE_NUMPAD_ENTER),
KeyDef(DEVICE_ID_ANY, NKCODE_BUTTON_A),
KeyDef(DEVICE_ID_PAD_0, NKCODE_DPAD_CENTER), // A number of Android devices.
const InputMapping hardcodedConfirmKeys[] = {
InputMapping(DEVICE_ID_KEYBOARD, NKCODE_SPACE),
InputMapping(DEVICE_ID_KEYBOARD, NKCODE_ENTER),
InputMapping(DEVICE_ID_KEYBOARD, NKCODE_NUMPAD_ENTER),
InputMapping(DEVICE_ID_ANY, NKCODE_BUTTON_A),
InputMapping(DEVICE_ID_PAD_0, NKCODE_DPAD_CENTER), // A number of Android devices.
};
// If they're not already bound, add them in.
@ -95,11 +87,11 @@ void UpdateNativeMenuKeys() {
confirmKeys.push_back(hardcodedConfirmKeys[i]);
}
const KeyDef hardcodedCancelKeys[] = {
KeyDef(DEVICE_ID_KEYBOARD, NKCODE_ESCAPE),
KeyDef(DEVICE_ID_ANY, NKCODE_BACK),
KeyDef(DEVICE_ID_ANY, NKCODE_BUTTON_B),
KeyDef(DEVICE_ID_MOUSE, NKCODE_EXT_MOUSEBUTTON_4),
const InputMapping hardcodedCancelKeys[] = {
InputMapping(DEVICE_ID_KEYBOARD, NKCODE_ESCAPE),
InputMapping(DEVICE_ID_ANY, NKCODE_BACK),
InputMapping(DEVICE_ID_ANY, NKCODE_BUTTON_B),
InputMapping(DEVICE_ID_MOUSE, NKCODE_EXT_MOUSEBUTTON_4),
};
for (size_t i = 0; i < ARRAY_SIZE(hardcodedCancelKeys); i++) {
@ -431,8 +423,6 @@ const KeyMap_IntStrPair psp_button_names[] = {
{CTRL_NOTE, "Note"},
};
const int AXIS_BIND_NKCODE_START = 4000;
static std::string FindName(int key, const KeyMap_IntStrPair list[], size_t size) {
for (size_t i = 0; i < size; i++)
if (list[i].key == key)
@ -444,18 +434,19 @@ std::string GetKeyName(int keyCode) {
return FindName(keyCode, key_names, ARRAY_SIZE(key_names));
}
std::string GetKeyOrAxisName(int keyCode) {
if (keyCode >= AXIS_BIND_NKCODE_START) {
std::string GetKeyOrAxisName(const InputMapping &mapping) {
if (mapping.IsAxis()) {
int direction;
int axis = TranslateKeyCodeToAxis(keyCode, direction);
int axis = mapping.Axis(&direction);
std::string temp = GetAxisName(axis);
if (direction == 1)
temp += "+";
else if (direction == -1)
temp += "-";
return temp;
} else {
return FindName(mapping.keyCode, key_names, ARRAY_SIZE(key_names));
}
return FindName(keyCode, key_names, ARRAY_SIZE(key_names));
}
std::string GetAxisName(int axisId) {
@ -481,96 +472,50 @@ std::vector<KeyMap_IntStrPair> GetMappableKeys() {
return temp;
}
int TranslateKeyCodeToAxis(int keyCode, int &direction) {
if (keyCode < AXIS_BIND_NKCODE_START)
return 0;
int v = keyCode - AXIS_BIND_NKCODE_START;
// Even/odd for direction.
direction = v & 1 ? -1 : 1;
return v / 2;
}
int TranslateKeyCodeFromAxis(int axisId, int direction) {
direction = direction < 0 ? 1 : 0;
return AXIS_BIND_NKCODE_START + axisId * 2 + direction;
}
KeyDef AxisDef(int deviceId, int axisId, int direction) {
return KeyDef(deviceId, TranslateKeyCodeFromAxis(axisId, direction));
}
int CheckAxisSwap(int btn) {
if (g_swapped_keys) {
switch (btn) {
case CTRL_UP: btn = VIRTKEY_AXIS_Y_MAX;
break;
case VIRTKEY_AXIS_Y_MAX: btn = CTRL_UP;
break;
case CTRL_DOWN: btn = VIRTKEY_AXIS_Y_MIN;
break;
case VIRTKEY_AXIS_Y_MIN: btn = CTRL_DOWN;
break;
case CTRL_LEFT: btn = VIRTKEY_AXIS_X_MIN;
break;
case VIRTKEY_AXIS_X_MIN: btn = CTRL_LEFT;
break;
case CTRL_RIGHT: btn = VIRTKEY_AXIS_X_MAX;
break;
case VIRTKEY_AXIS_X_MAX: btn = CTRL_RIGHT;
break;
case CTRL_UP: btn = VIRTKEY_AXIS_Y_MAX; break;
case VIRTKEY_AXIS_Y_MAX: btn = CTRL_UP; break;
case CTRL_DOWN: btn = VIRTKEY_AXIS_Y_MIN; break;
case VIRTKEY_AXIS_Y_MIN: btn = CTRL_DOWN; break;
case CTRL_LEFT: btn = VIRTKEY_AXIS_X_MIN; break;
case VIRTKEY_AXIS_X_MIN: btn = CTRL_LEFT; break;
case CTRL_RIGHT: btn = VIRTKEY_AXIS_X_MAX; break;
case VIRTKEY_AXIS_X_MAX: btn = CTRL_RIGHT; break;
}
}
return btn;
}
static bool FindKeyMapping(int deviceId, int key, std::vector<int> *psp_button) {
// Brute force, let's optimize later
static bool FindKeyMapping(const InputMapping &mapping, std::vector<int> *pspButtons) {
for (auto iter = g_controllerMap.begin(); iter != g_controllerMap.end(); ++iter) {
for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2) {
if (*iter2 == KeyDef(deviceId, key)) {
psp_button->push_back(CheckAxisSwap(iter->first));
if (*iter2 == mapping) {
pspButtons->push_back(CheckAxisSwap(iter->first));
}
}
}
return psp_button->size() > 0;
return pspButtons->size() > 0;
}
bool KeyToPspButton(int deviceId, int key, std::vector<int> *pspKeys) {
return FindKeyMapping(deviceId, key, pspKeys);
bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspButtons) {
return FindKeyMapping(mapping, pspButtons);
}
// TODO: vector output
bool KeyFromPspButton(int btn, std::vector<KeyDef> *keys, bool ignoreMouse) {
bool InputMappingsFromPspButton(int btn, std::vector<InputMapping> *mappings, bool ignoreMouse) {
bool mapped = false;
for (auto iter = g_controllerMap.begin(); iter != g_controllerMap.end(); ++iter) {
if (iter->first == btn) {
for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2) {
if (!ignoreMouse || iter2->deviceId != DEVICE_ID_MOUSE)
keys->push_back(*iter2);
if (mappings && (!ignoreMouse || iter2->deviceId != DEVICE_ID_MOUSE)) {
mapped = true;
mappings->push_back(*iter2);
}
}
}
return keys->size() > 0;
}
bool AxisToPspButton(int deviceId, int axisId, int direction, std::vector<int> *pspKeys) {
int key = TranslateKeyCodeFromAxis(axisId, direction);
return KeyToPspButton(deviceId, key, pspKeys);
}
bool AxisFromPspButton(int btn, int *deviceId, int *axisId, int *direction) {
for (auto iter = g_controllerMap.begin(); iter != g_controllerMap.end(); ++iter) {
for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2) {
if (iter->first == btn && iter2->keyCode >= AXIS_BIND_NKCODE_START) {
if (deviceId)
*deviceId = iter2->deviceId;
if (axisId)
*axisId = TranslateKeyCodeToAxis(iter2->keyCode, *direction);
return true;
}
}
}
return false;
return mapped;
}
MappedAnalogAxes MappedAxesForDevice(int deviceId) {
@ -581,7 +526,7 @@ MappedAnalogAxes MappedAxesForDevice(int deviceId) {
MappedAnalogAxis info{ -1 };
for (const auto &key : g_controllerMap[btn]) {
if (key.deviceId == deviceId) {
info.axisId = TranslateKeyCodeToAxis(key.keyCode, info.direction);
info.axisId = TranslateKeyCodeToAxis(key.keyCode, &info.direction);
return info;
}
}
@ -617,7 +562,7 @@ void RemoveButtonMapping(int btn) {
bool IsKeyMapped(int device, int key) {
for (auto &iter : g_controllerMap) {
for (auto &mappedKey : iter.second) {
if (mappedKey == KeyDef(device, key)) {
if (mappedKey == InputMapping(device, key)) {
return true;
}
}
@ -625,7 +570,7 @@ bool IsKeyMapped(int device, int key) {
return false;
}
bool ReplaceSingleKeyMapping(int btn, int index, KeyDef key) {
bool ReplaceSingleKeyMapping(int btn, int index, InputMapping key) {
// Check for duplicate
for (int i = 0; i < (int)g_controllerMap[btn].size(); ++i) {
if (i != index && g_controllerMap[btn][i] == key) {
@ -644,7 +589,7 @@ bool ReplaceSingleKeyMapping(int btn, int index, KeyDef key) {
return true;
}
void SetKeyMapping(int btn, KeyDef key, bool replace) {
void SetInputMapping(int btn, const InputMapping &key, bool replace) {
if (key.keyCode < 0)
return;
if (replace) {
@ -664,11 +609,6 @@ void SetKeyMapping(int btn, KeyDef key, bool replace) {
UpdateNativeMenuKeys();
}
void SetAxisMapping(int btn, int deviceId, int axisId, int direction, bool replace) {
int key = TranslateKeyCodeFromAxis(axisId, direction);
SetKeyMapping(btn, KeyDef(deviceId, key), replace);
}
void RestoreDefault() {
g_controllerMap.clear();
g_controllerMapGeneration++;
@ -732,7 +672,7 @@ void LoadFromIni(IniFile &file) {
int deviceId = atoi(parts[0].c_str());
int keyCode = atoi(parts[1].c_str());
SetKeyMapping(psp_button_names[i].key, KeyDef(deviceId, keyCode), false);
SetInputMapping(psp_button_names[i].key, InputMapping(deviceId, keyCode), false);
g_seenDeviceIds.insert(deviceId);
}
}
@ -744,8 +684,8 @@ void SaveToIni(IniFile &file) {
Section *controls = file.GetOrCreateSection("ControlMapping");
for (size_t i = 0; i < ARRAY_SIZE(psp_button_names); i++) {
std::vector<KeyDef> keys;
KeyFromPspButton(psp_button_names[i].key, &keys, false);
std::vector<InputMapping> keys;
InputMappingsFromPspButton(psp_button_names[i].key, &keys, false);
std::string value;
for (size_t j = 0; j < keys.size(); j++) {
@ -816,8 +756,8 @@ void AutoConfForPad(const std::string &name) {
#endif
// Add a couple of convenient keyboard mappings by default, too.
g_controllerMap[VIRTKEY_PAUSE].push_back(KeyDef(DEVICE_ID_KEYBOARD, NKCODE_ESCAPE));
g_controllerMap[VIRTKEY_FASTFORWARD].push_back(KeyDef(DEVICE_ID_KEYBOARD, NKCODE_TAB));
g_controllerMap[VIRTKEY_PAUSE].push_back(InputMapping(DEVICE_ID_KEYBOARD, NKCODE_ESCAPE));
g_controllerMap[VIRTKEY_FASTFORWARD].push_back(InputMapping(DEVICE_ID_KEYBOARD, NKCODE_TAB));
g_controllerMapGeneration++;
}

View file

@ -22,7 +22,7 @@
#include <vector>
#include <set>
#include "Common/Input/InputState.h" // KeyDef
#include "Common/Input/InputState.h" // InputMapping
#include "Common/Input/KeyCodes.h" // keyboard keys
#include "Core/KeyMapDefaults.h"
@ -76,7 +76,7 @@ enum {
const float AXIS_BIND_THRESHOLD = 0.75f;
const float AXIS_BIND_THRESHOLD_MOUSE = 0.01f;
typedef std::map<int, std::vector<KeyDef>> KeyMapping;
typedef std::map<int, std::vector<InputMapping>> KeyMapping;
struct MappedAnalogAxis {
int axisId;
@ -115,34 +115,24 @@ namespace KeyMap {
// Use if you need to display the textual name
std::string GetKeyName(int keyCode);
std::string GetKeyOrAxisName(int keyCode);
std::string GetKeyOrAxisName(const InputMapping &mapping);
std::string GetAxisName(int axisId);
std::string GetPspButtonName(int btn);
const char* GetPspButtonNameCharPointer(int btn);
std::vector<KeyMap_IntStrPair> GetMappableKeys();
// Use to translate KeyMap Keys to PSP
// buttons. You should have already translated
// your platform's keys to KeyMap keys.
bool KeyToPspButton(int deviceId, int key, std::vector<int> *pspKeys);
bool KeyFromPspButton(int btn, std::vector<KeyDef> *keys, bool ignoreMouse);
// Use to translate input mappings to and from PSP buttons. You should have already translated
// your platform's keys to InputMapping keys.
bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspButtons);
bool InputMappingsFromPspButton(int btn, std::vector<InputMapping> *keys, bool ignoreMouse);
int TranslateKeyCodeToAxis(int keyCode, int &direction);
int TranslateKeyCodeFromAxis(int axisId, int direction);
// Configure the key mapping.
// Configure the key or axis mapping.
// Any configuration will be saved to the Core config.
void SetKeyMapping(int psp_key, KeyDef key, bool replace);
void SetInputMapping(int psp_key, const InputMapping &key, bool replace);
// Return false if bind was a duplicate and got removed
bool ReplaceSingleKeyMapping(int btn, int index, KeyDef key);
bool ReplaceSingleKeyMapping(int btn, int index, InputMapping key);
// Configure an axis mapping, saves the configuration.
// Direction is negative or positive.
void SetAxisMapping(int btn, int deviceId, int axisId, int direction, bool replace);
bool AxisToPspButton(int deviceId, int axisId, int direction, std::vector<int> *pspKeys);
bool AxisFromPspButton(int btn, int *deviceId, int *axisId, int *direction);
MappedAnalogAxes MappedAxesForDevice(int deviceId);
void LoadFromIni(IniFile &iniFile);

View file

@ -13,8 +13,8 @@ namespace KeyMap {
struct DefMappingStruct {
int pspKey;
int key;
int direction;
int keyOrAxis;
int direction; // if 0, it's a key, otherwise an axis.
};
static const DefMappingStruct defaultQwertyKeyboardKeyMap[] = {
@ -341,9 +341,9 @@ static const DefMappingStruct defaultVRRightController[] = {
static void SetDefaultKeyMap(int deviceId, const DefMappingStruct *array, size_t count, bool replace) {
for (size_t i = 0; i < count; i++) {
if (array[i].direction == 0)
SetKeyMapping(array[i].pspKey, KeyDef(deviceId, array[i].key), replace);
SetInputMapping(array[i].pspKey, InputMapping(deviceId, array[i].keyOrAxis), replace);
else
SetAxisMapping(array[i].pspKey, deviceId, array[i].key, array[i].direction, replace);
SetInputMapping(array[i].pspKey, InputMapping(deviceId, array[i].keyOrAxis, array[i].direction), replace);
}
g_seenDeviceIds.insert(deviceId);
}

View file

@ -66,7 +66,7 @@ private:
UI::EventReturn OnReplace(UI::EventParams &params);
UI::EventReturn OnReplaceAll(UI::EventParams &params);
void MappedCallback(KeyDef key);
void MappedCallback(InputMapping key);
enum Action {
NONE,
@ -134,13 +134,13 @@ void SingleControlMapper::Refresh() {
LinearLayout *rightColumn = root->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(rightColumnWidth, WRAP_CONTENT)));
rightColumn->SetSpacing(2.0f);
std::vector<KeyDef> mappings;
KeyMap::KeyFromPspButton(pspKey_, &mappings, false);
std::vector<InputMapping> mappings;
KeyMap::InputMappingsFromPspButton(pspKey_, &mappings, false);
rows_.clear();
for (size_t i = 0; i < mappings.size(); i++) {
std::string deviceName = GetDeviceName(mappings[i].deviceId);
std::string keyName = KeyMap::GetKeyOrAxisName(mappings[i].keyCode);
std::string keyName = KeyMap::GetKeyOrAxisName(mappings[i]);
LinearLayout *row = rightColumn->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
row->SetSpacing(2.0f);
@ -162,14 +162,14 @@ void SingleControlMapper::Refresh() {
}
}
void SingleControlMapper::MappedCallback(KeyDef kdf) {
void SingleControlMapper::MappedCallback(InputMapping kdf) {
switch (action_) {
case ADD:
KeyMap::SetKeyMapping(pspKey_, kdf, false);
KeyMap::SetInputMapping(pspKey_, kdf, false);
addButton_->SetFocus();
break;
case REPLACEALL:
KeyMap::SetKeyMapping(pspKey_, kdf, true);
KeyMap::SetInputMapping(pspKey_, kdf, true);
replaceAllButton_->SetFocus();
break;
case REPLACEONE:
@ -346,7 +346,7 @@ bool KeyMappingNewKeyDialog::key(const KeyInput &key) {
return true;
mapped_ = true;
KeyDef kdf(key.deviceId, key.keyCode);
InputMapping kdf(key.deviceId, key.keyCode);
TriggerFinish(DR_YES);
if (callback_ && pspBtn_ != VIRTKEY_SPEED_ANALOG)
callback_(kdf);
@ -378,7 +378,7 @@ bool KeyMappingNewMouseKeyDialog::key(const KeyInput &key) {
}
mapped_ = true;
KeyDef kdf(key.deviceId, key.keyCode);
InputMapping kdf(key.deviceId, key.keyCode);
TriggerFinish(DR_YES);
g_Config.bMapMouse = false;
if (callback_)
@ -409,7 +409,7 @@ void KeyMappingNewKeyDialog::axis(const AxisInput &axis) {
if (axis.value > AXIS_BIND_THRESHOLD) {
mapped_ = true;
KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, 1));
InputMapping kdf(axis.deviceId, axis.axisId, 1);
TriggerFinish(DR_YES);
if (callback_)
callback_(kdf);
@ -417,7 +417,7 @@ void KeyMappingNewKeyDialog::axis(const AxisInput &axis) {
if (axis.value < -AXIS_BIND_THRESHOLD) {
mapped_ = true;
KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, -1));
InputMapping kdf(axis.deviceId, axis.axisId, -1);
TriggerFinish(DR_YES);
if (callback_)
callback_(kdf);
@ -432,7 +432,7 @@ void KeyMappingNewMouseKeyDialog::axis(const AxisInput &axis) {
if (axis.value > AXIS_BIND_THRESHOLD) {
mapped_ = true;
KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, 1));
InputMapping kdf(axis.deviceId, axis.axisId, 1);
TriggerFinish(DR_YES);
if (callback_)
callback_(kdf);
@ -440,7 +440,7 @@ void KeyMappingNewMouseKeyDialog::axis(const AxisInput &axis) {
if (axis.value < -AXIS_BIND_THRESHOLD) {
mapped_ = true;
KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, -1));
InputMapping kdf(axis.deviceId, axis.axisId, -1);
TriggerFinish(DR_YES);
if (callback_)
callback_(kdf);
@ -1088,7 +1088,7 @@ void VisualMappingScreen::CreateViews() {
bool VisualMappingScreen::key(const KeyInput &key) {
if (key.flags & KEY_DOWN) {
std::vector<int> pspKeys;
KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &pspKeys);
KeyMap::InputMappingToPspButton(InputMapping(key.deviceId, key.keyCode), &pspKeys);
for (int pspKey : pspKeys) {
switch (pspKey) {
case VIRTKEY_AXIS_X_MIN:
@ -1109,9 +1109,9 @@ bool VisualMappingScreen::key(const KeyInput &key) {
void VisualMappingScreen::axis(const AxisInput &axis) {
std::vector<int> results;
if (axis.value >= g_Config.fAnalogDeadzone * 0.7f)
KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, 1, &results);
KeyMap::InputMappingToPspButton(InputMapping(axis.deviceId, axis.axisId, 1), &results);
if (axis.value <= g_Config.fAnalogDeadzone * -0.7f)
KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, -1, &results);
KeyMap::InputMappingToPspButton(InputMapping(axis.deviceId, axis.axisId, -1), &results);
for (int result : results) {
switch (result) {
@ -1147,8 +1147,8 @@ UI::EventReturn VisualMappingScreen::OnBindAll(UI::EventParams &e) {
return UI::EVENT_DONE;
}
void VisualMappingScreen::HandleKeyMapping(KeyDef key) {
KeyMap::SetKeyMapping(nextKey_, key, replace_);
void VisualMappingScreen::HandleKeyMapping(InputMapping key) {
KeyMap::SetInputMapping(nextKey_, key, replace_);
if (bindAll_ < 0) {
// For analog, we do each direction in a row.

View file

@ -56,7 +56,7 @@ private:
class KeyMappingNewKeyDialog : public PopupScreen {
public:
explicit KeyMappingNewKeyDialog(int btn, bool replace, std::function<void(KeyDef)> callback, std::shared_ptr<I18NCategory> i18n)
explicit KeyMappingNewKeyDialog(int btn, bool replace, std::function<void(InputMapping)> callback, std::shared_ptr<I18NCategory> i18n)
: PopupScreen(i18n->T("Map Key"), "Cancel", ""), pspBtn_(btn), callback_(callback) {}
const char *tag() const override { return "KeyMappingNewKey"; }
@ -75,14 +75,14 @@ protected:
private:
int pspBtn_;
std::function<void(KeyDef)> callback_;
std::function<void(InputMapping)> callback_;
bool mapped_ = false; // Prevent double registrations
double delayUntil_ = 0.0f;
};
class KeyMappingNewMouseKeyDialog : public PopupScreen {
public:
KeyMappingNewMouseKeyDialog(int btn, bool replace, std::function<void(KeyDef)> callback, std::shared_ptr<I18NCategory> i18n)
KeyMappingNewMouseKeyDialog(int btn, bool replace, std::function<void(InputMapping)> callback, std::shared_ptr<I18NCategory> i18n)
: PopupScreen(i18n->T("Map Mouse"), "", ""), pspBtn_(btn), callback_(callback), mapped_(false) {}
const char *tag() const override { return "KeyMappingNewMouseKey"; }
@ -99,7 +99,7 @@ protected:
private:
int pspBtn_;
std::function<void(KeyDef)> callback_;
std::function<void(InputMapping)> callback_;
bool mapped_; // Prevent double registrations
};
@ -189,7 +189,7 @@ protected:
private:
UI::EventReturn OnMapButton(UI::EventParams &e);
UI::EventReturn OnBindAll(UI::EventParams &e);
void HandleKeyMapping(KeyDef key);
void HandleKeyMapping(InputMapping key);
void MapNext(bool successive);
MockPSP *psp_ = nullptr;

View file

@ -97,7 +97,7 @@ extern AndroidAudioState *g_audioState;
GameSettingsScreen::GameSettingsScreen(const Path &gamePath, std::string gameID, bool editThenRestore)
: UIDialogScreenWithGameBackground(gamePath), gameID_(gameID), editThenRestore_(editThenRestore) {
prevInflightFrames_ = g_Config.iInflightFrames;
analogSpeedMapped_ = KeyMap::AxisFromPspButton(VIRTKEY_SPEED_ANALOG, nullptr, nullptr, nullptr);
analogSpeedMapped_ = KeyMap::InputMappingsFromPspButton(VIRTKEY_SPEED_ANALOG, nullptr, true);
}
// This needs before run CheckGPUFeatures()
@ -1405,7 +1405,7 @@ void GameSettingsScreen::dialogFinished(const Screen *dialog, DialogResult resul
RecreateViews();
}
bool mapped = KeyMap::AxisFromPspButton(VIRTKEY_SPEED_ANALOG, nullptr, nullptr, nullptr);
bool mapped = KeyMap::InputMappingsFromPspButton(VIRTKEY_SPEED_ANALOG, nullptr, true);
if (mapped != analogSpeedMapped_) {
analogSpeedMapped_ = mapped;
RecreateViews();

View file

@ -158,7 +158,7 @@ public:
std::vector<int> pspKeys;
bool showInfo = false;
if (KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &pspKeys)) {
if (KeyMap::InputMappingToPspButton(InputMapping(key.deviceId, key.keyCode), &pspKeys)) {
for (auto it = pspKeys.begin(), end = pspKeys.end(); it != end; ++it) {
// If the button mapped to triangle, then show the info.
if (HasFocus() && (key.flags & KEY_UP) && *it == CTRL_TRIANGLE) {

View file

@ -1261,7 +1261,7 @@ bool NativeKey(const KeyInput &key) {
if (g_Config.bPauseExitsEmulator) {
static std::vector<int> pspKeys;
pspKeys.clear();
if (KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &pspKeys)) {
if (KeyMap::InputMappingToPspButton(InputMapping(key.deviceId, key.keyCode), &pspKeys)) {
if (std::find(pspKeys.begin(), pspKeys.end(), VIRTKEY_PAUSE) != pspKeys.end()) {
System_ExitApp();
return true;