CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Input/InputState.cpp
Views: 1401
#include <vector>1#include <cstdio>23#include "Common/Input/InputState.h"4#include "Common/Input/KeyCodes.h"5#include "Common/StringUtils.h"67const char *GetDeviceName(int deviceId) {8switch (deviceId) {9case DEVICE_ID_ANY: return "any";10case DEVICE_ID_DEFAULT: return "built-in";11case DEVICE_ID_KEYBOARD: return "kbd";12case DEVICE_ID_PAD_0: return "pad1";13case DEVICE_ID_PAD_1: return "pad2";14case DEVICE_ID_PAD_2: return "pad3";15case DEVICE_ID_PAD_3: return "pad4";16case DEVICE_ID_PAD_4: return "pad5";17case DEVICE_ID_PAD_5: return "pad6";18case DEVICE_ID_PAD_6: return "pad7";19case DEVICE_ID_PAD_7: return "pad8";20case DEVICE_ID_PAD_8: return "pad9";21case DEVICE_ID_PAD_9: return "pad10";22case DEVICE_ID_XINPUT_0: return "x360"; // keeping these strings for backward compat. Hm, what would break if we changed them to xbox?23case DEVICE_ID_XINPUT_1: return "x360_2";24case DEVICE_ID_XINPUT_2: return "x360_3";25case DEVICE_ID_XINPUT_3: return "x360_4";26case DEVICE_ID_ACCELEROMETER: return "accelerometer";27case DEVICE_ID_MOUSE: return "mouse";28case DEVICE_ID_XR_CONTROLLER_LEFT: return "xr_l";29case DEVICE_ID_XR_CONTROLLER_RIGHT: return "xr_r";30default:31return "unknown";32}33}3435std::vector<InputMapping> dpadKeys;36std::vector<InputMapping> confirmKeys;37std::vector<InputMapping> cancelKeys;38std::vector<InputMapping> infoKeys;39std::vector<InputMapping> tabLeftKeys;40std::vector<InputMapping> tabRightKeys;41static std::unordered_map<InputDeviceID, int> uiFlipAnalogY;4243static void AppendKeys(std::vector<InputMapping> &keys, const std::vector<InputMapping> &newKeys) {44for (const auto &key : newKeys) {45keys.push_back(key);46}47}4849void SetDPadKeys(const std::vector<InputMapping> &leftKey, const std::vector<InputMapping> &rightKey,50const std::vector<InputMapping> &upKey, const std::vector<InputMapping> &downKey) {51dpadKeys.clear();5253// Store all directions into one vector for now. In the future it might be54// useful to keep track of the different directions separately.55AppendKeys(dpadKeys, leftKey);56AppendKeys(dpadKeys, rightKey);57AppendKeys(dpadKeys, upKey);58AppendKeys(dpadKeys, downKey);59}6061void SetConfirmCancelKeys(const std::vector<InputMapping> &confirm, const std::vector<InputMapping> &cancel) {62confirmKeys = confirm;63cancelKeys = cancel;64}6566void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::vector<InputMapping> &tabRight) {67tabLeftKeys = tabLeft;68tabRightKeys = tabRight;69}7071void SetInfoKeys(const std::vector<InputMapping> &info) {72infoKeys = info;73}7475void SetAnalogFlipY(const std::unordered_map<InputDeviceID, int> &flipYByDeviceId) {76uiFlipAnalogY = flipYByDeviceId;77}7879int GetAnalogYDirection(InputDeviceID deviceId) {80auto configured = uiFlipAnalogY.find(deviceId);81if (configured != uiFlipAnalogY.end())82return configured->second;83return 0;84}8586// NOTE: Changing the format of FromConfigString/ToConfigString breaks controls.ini backwards compatibility.87InputMapping InputMapping::FromConfigString(const std::string_view str) {88std::vector<std::string_view> parts;89SplitString(str, '-', parts);90// We only convert to std::string here to add null terminators for atoi.91InputDeviceID deviceId = (InputDeviceID)(atoi(std::string(parts[0]).c_str()));92InputKeyCode keyCode = (InputKeyCode)atoi(std::string(parts[1]).c_str());9394InputMapping mapping;95mapping.deviceId = deviceId;96mapping.keyCode = keyCode;97return mapping;98}99100std::string InputMapping::ToConfigString() const {101return StringFromFormat("%d-%d", (int)deviceId, keyCode);102}103104void InputMapping::FormatDebug(char *buffer, size_t bufSize) const {105if (IsAxis()) {106int direction;107int axis = Axis(&direction);108snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", (int)deviceId, axis, direction);109} else {110snprintf(buffer, bufSize, "Device: %d Key: %d", (int)deviceId, keyCode);111}112}113114115