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.h
Views: 1401
#pragma once12// For more detailed and configurable input, implement NativeTouch, NativeKey and NativeAxis and do your3// own mapping. Might later move the mapping system from PPSSPP to native.45#include <unordered_map>6#include <vector>7#include <string>89#include "Common/Input/KeyCodes.h"10#include "Common/Log.h"1112// Default device IDs1314enum InputDeviceID {15DEVICE_ID_ANY = -1, // Represents any device ID16DEVICE_ID_DEFAULT = 0, // Old Android17DEVICE_ID_KEYBOARD = 1, // PC keyboard, android keyboards18DEVICE_ID_MOUSE = 2, // PC mouse only (not touchscreen!)19DEVICE_ID_PAD_0 = 10, // Generic joypads20DEVICE_ID_PAD_1 = 11, // these should stay as contiguous numbers21DEVICE_ID_PAD_2 = 12,22DEVICE_ID_PAD_3 = 13,23DEVICE_ID_PAD_4 = 14,24DEVICE_ID_PAD_5 = 15,25DEVICE_ID_PAD_6 = 16,26DEVICE_ID_PAD_7 = 17,27DEVICE_ID_PAD_8 = 18,28DEVICE_ID_PAD_9 = 19,29DEVICE_ID_XINPUT_0 = 20, // XInput joypads30DEVICE_ID_XINPUT_1 = 21,31DEVICE_ID_XINPUT_2 = 22,32DEVICE_ID_XINPUT_3 = 23,33DEVICE_ID_ACCELEROMETER = 30, // no longer used34DEVICE_ID_XR_CONTROLLER_LEFT = 40,35DEVICE_ID_XR_CONTROLLER_RIGHT = 41,36DEVICE_ID_TOUCH = 42,37DEVICE_ID_COUNT,38};3940inline InputDeviceID operator +(InputDeviceID deviceID, int addend) {41return (InputDeviceID)((int)deviceID + addend);42}4344//number of contiguous generic joypad IDs45const int MAX_NUM_PADS = 10;4647const char *GetDeviceName(int deviceId);4849#ifndef MAX_KEYQUEUESIZE50#define MAX_KEYQUEUESIZE 2051#endif5253// Represents a single bindable key54static const int AXIS_BIND_NKCODE_START = 4000;5556inline int TranslateKeyCodeToAxis(int keyCode, int *direction) {57if (keyCode < AXIS_BIND_NKCODE_START)58return 0;59int k = keyCode - AXIS_BIND_NKCODE_START;60// Even/odd for direction.61if (direction)62*direction = k & 1 ? -1 : 1;63return k / 2;64}6566class InputMapping {67private:68static inline int TranslateKeyCodeFromAxis(int axisId, int direction) {69return AXIS_BIND_NKCODE_START + axisId * 2 + (direction < 0 ? 1 : 0);70}71public:72InputMapping() : deviceId(DEVICE_ID_DEFAULT), keyCode(0) {}73// From a key mapping74InputMapping(InputDeviceID _deviceId, int key) : deviceId(_deviceId), keyCode(key) {}75// From an axis76InputMapping(InputDeviceID _deviceId, int axis, int direction) : deviceId(_deviceId), keyCode(TranslateKeyCodeFromAxis(axis, direction)) {77_dbg_assert_(direction != 0);78}7980static InputMapping FromConfigString(std::string_view str);81std::string ToConfigString() const;8283InputDeviceID deviceId;84int keyCode; // Can also represent an axis with direction, if encoded properly.8586bool IsAxis() const {87return keyCode >= AXIS_BIND_NKCODE_START;88}8990int Axis(int *direction) const {91_dbg_assert_(IsAxis());92return TranslateKeyCodeToAxis(keyCode, direction);93}9495InputMapping FlipDirection() const {96_dbg_assert_(IsAxis());97InputMapping other = *this;98other.keyCode ^= 1;99return other;100}101102// If you want to use std::find and match ANY, you need to perform an explicit search for that.103bool operator < (const InputMapping &other) const {104if (deviceId < other.deviceId) return true;105if (deviceId > other.deviceId) return false;106if (keyCode < other.keyCode) return true;107return false;108}109// Needed for composition.110bool operator > (const InputMapping &other) const {111if (deviceId > other.deviceId) return true;112if (deviceId < other.deviceId) return false;113if (keyCode > other.keyCode) return true;114return false;115}116117// This one is iffy with the != ANY checks. Should probably be a named method.118bool operator == (const InputMapping &other) const {119if (deviceId != other.deviceId && deviceId != DEVICE_ID_ANY && other.deviceId != DEVICE_ID_ANY) return false;120if (keyCode != other.keyCode) return false;121return true;122}123bool operator != (const InputMapping &other) const {124return !(*this == other);125}126127void FormatDebug(char *buffer, size_t bufSize) const;128};129130enum {131TOUCH_MOVE = 1 << 0,132TOUCH_DOWN = 1 << 1,133TOUCH_UP = 1 << 2,134TOUCH_CANCEL = 1 << 3, // Sent by scrollviews to their children when they detect a scroll135TOUCH_WHEEL = 1 << 4, // Scrollwheel event. Usually only affects Y but can potentially affect X.136TOUCH_MOUSE = 1 << 5, // Identifies that this touch event came from a mouse137TOUCH_RELEASE_ALL = 1 << 6, // Useful for app focus switches when events may be lost.138139// These are the Android getToolType() codes, shifted by 10.140TOUCH_TOOL_MASK = 7 << 10,141TOUCH_TOOL_UNKNOWN = 0 << 10,142TOUCH_TOOL_FINGER = 1 << 10,143TOUCH_TOOL_STYLUS = 2 << 10,144TOUCH_TOOL_MOUSE = 3 << 10,145TOUCH_TOOL_ERASER = 4 << 10,146};147148// Used for asynchronous touch input.149// DOWN is always on its own.150// MOVE and UP can be combined.151struct TouchInput {152float x;153float y;154int id; // Needs to be <= GestureDetector::MAX_PTRS (10.)155int flags;156double timestamp;157};158159#undef KEY_DOWN160#undef KEY_UP161162enum {163KEY_DOWN = 1 << 0,164KEY_UP = 1 << 1,165KEY_HASWHEELDELTA = 1 << 2,166KEY_IS_REPEAT = 1 << 3,167KEY_CHAR = 1 << 4, // Unicode character input. Cannot detect keyups of these so KEY_DOWN and KEY_UP are zero when this is set.168};169170struct KeyInput {171KeyInput() {}172KeyInput(InputDeviceID devId, InputKeyCode code, int fl) : deviceId(devId), keyCode(code), flags(fl) {}173KeyInput(InputDeviceID devId, int unicode) : deviceId(devId), unicodeChar(unicode), flags(KEY_CHAR) {}174InputDeviceID deviceId;175union {176InputKeyCode keyCode; // Android keycodes are the canonical keycodes, everyone else map to them.177int unicodeChar; // for KEY_CHAR178};179int flags;180};181182struct AxisInput {183InputDeviceID deviceId;184InputAxis axisId;185float value;186};187188// Is there a nicer place for this stuff? It's here to avoid dozens of linking errors in UnitTest..189extern std::vector<InputMapping> dpadKeys;190extern std::vector<InputMapping> confirmKeys;191extern std::vector<InputMapping> cancelKeys;192extern std::vector<InputMapping> infoKeys;193extern std::vector<InputMapping> tabLeftKeys;194extern std::vector<InputMapping> tabRightKeys;195void SetDPadKeys(const std::vector<InputMapping> &leftKey, const std::vector<InputMapping> &rightKey,196const std::vector<InputMapping> &upKey, const std::vector<InputMapping> &downKey);197void SetConfirmCancelKeys(const std::vector<InputMapping> &confirm, const std::vector<InputMapping> &cancel);198void SetTabLeftRightKeys(const std::vector<InputMapping> &tabLeft, const std::vector<InputMapping> &tabRight);199void SetInfoKeys(const std::vector<InputMapping> &info);200201// 0 means unknown (attempt autodetect), -1 means flip, 1 means original direction.202void SetAnalogFlipY(const std::unordered_map<InputDeviceID, int> &flipYByDeviceId);203int GetAnalogYDirection(InputDeviceID deviceId);204205206