|
| 1 | +/* Gamepad Input State |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H |
| 8 | +#define PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H |
| 9 | + |
| 10 | +#include <stdint.h> |
| 11 | +#include "ControllerInput/ControllerInput.h" |
| 12 | + |
| 13 | +namespace PokemonAutomation{ |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +enum GamepadInputButton : uint32_t{ |
| 18 | + GAMEPAD_BUTTON_NONE = 0, |
| 19 | + GAMEPAD_BUTTON_SOUTH = ((uint32_t)1 << 0), // A on Xbox, B on Switch. |
| 20 | + GAMEPAD_BUTTON_EAST = ((uint32_t)1 << 1), // B on Xbox, A on Switch. |
| 21 | + GAMEPAD_BUTTON_WEST = ((uint32_t)1 << 2), // X on Xbox, Y on Switch. |
| 22 | + GAMEPAD_BUTTON_NORTH = ((uint32_t)1 << 3), // Y on Xbox, X on Switch. |
| 23 | + GAMEPAD_BUTTON_DPAD_UP = ((uint32_t)1 << 4), |
| 24 | + GAMEPAD_BUTTON_DPAD_RIGHT = ((uint32_t)1 << 5), |
| 25 | + GAMEPAD_BUTTON_DPAD_DOWN = ((uint32_t)1 << 6), |
| 26 | + GAMEPAD_BUTTON_DPAD_LEFT = ((uint32_t)1 << 7), |
| 27 | + GAMEPAD_BUTTON_LEFT_SHOULDER = ((uint32_t)1 << 8), |
| 28 | + GAMEPAD_BUTTON_RIGHT_SHOULDER = ((uint32_t)1 << 9), |
| 29 | + GAMEPAD_BUTTON_LEFT_TRIGGER = ((uint32_t)1 << 10), |
| 30 | + GAMEPAD_BUTTON_RIGHT_TRIGGER = ((uint32_t)1 << 11), |
| 31 | + GAMEPAD_BUTTON_SELECT = ((uint32_t)1 << 12), // Back/View. |
| 32 | + GAMEPAD_BUTTON_START = ((uint32_t)1 << 13), // Start/Menu. |
| 33 | + GAMEPAD_BUTTON_LEFT_THUMB = ((uint32_t)1 << 14), |
| 34 | + GAMEPAD_BUTTON_RIGHT_THUMB = ((uint32_t)1 << 15), |
| 35 | + GAMEPAD_BUTTON_GUIDE = ((uint32_t)1 << 16), // Xbox logo button. |
| 36 | +}; |
| 37 | + |
| 38 | +inline constexpr GamepadInputButton empty_value(const GamepadInputButton*){ |
| 39 | + return GAMEPAD_BUTTON_NONE; |
| 40 | +} |
| 41 | +inline constexpr bool is_empty(GamepadInputButton x){ |
| 42 | + return x == GAMEPAD_BUTTON_NONE; |
| 43 | +} |
| 44 | +inline constexpr GamepadInputButton operator|(GamepadInputButton x, GamepadInputButton y){ |
| 45 | + return (GamepadInputButton)((uint32_t)x | (uint32_t)y); |
| 46 | +} |
| 47 | +inline constexpr void operator|=(GamepadInputButton& x, GamepadInputButton y){ |
| 48 | + x = (GamepadInputButton)((uint32_t)x | (uint32_t)y); |
| 49 | +} |
| 50 | +inline constexpr GamepadInputButton operator&(GamepadInputButton x, GamepadInputButton y){ |
| 51 | + return (GamepadInputButton)((uint32_t)x & (uint32_t)y); |
| 52 | +} |
| 53 | +inline constexpr void operator&=(GamepadInputButton& x, GamepadInputButton y){ |
| 54 | + x = (GamepadInputButton)((uint32_t)x & (uint32_t)y); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +class GamepadInputState final : public ControllerInputState{ |
| 61 | +public: |
| 62 | + GamepadInputState(GamepadInputState&& x) |
| 63 | + : ControllerInputState(ControllerInputType::StandardGamepad) |
| 64 | + , m_buttons(x.m_buttons) |
| 65 | + , m_left_x(x.m_left_x) |
| 66 | + , m_left_y(x.m_left_y) |
| 67 | + , m_right_x(x.m_right_x) |
| 68 | + , m_right_y(x.m_right_y) |
| 69 | + {} |
| 70 | + void operator=(GamepadInputState&& x){ |
| 71 | + if (this == &x){ |
| 72 | + return; |
| 73 | + } |
| 74 | + m_buttons = x.m_buttons; |
| 75 | + m_left_x = x.m_left_x; |
| 76 | + m_left_y = x.m_left_y; |
| 77 | + m_right_x = x.m_right_x; |
| 78 | + m_right_y = x.m_right_y; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +public: |
| 83 | + GamepadInputState() |
| 84 | + : ControllerInputState(ControllerInputType::StandardGamepad) |
| 85 | + {} |
| 86 | + |
| 87 | + |
| 88 | + virtual void clear() override{ |
| 89 | + m_buttons = GAMEPAD_BUTTON_NONE; |
| 90 | + m_left_x = 0; |
| 91 | + m_left_y = 0; |
| 92 | + m_right_x = 0; |
| 93 | + m_right_y = 0; |
| 94 | + } |
| 95 | + virtual bool is_neutral() const override{ |
| 96 | + return m_buttons == GAMEPAD_BUTTON_NONE |
| 97 | + && m_left_x == 0 |
| 98 | + && m_left_y == 0 |
| 99 | + && m_right_x == 0 |
| 100 | + && m_right_y == 0; |
| 101 | + } |
| 102 | + virtual bool operator==(const ControllerInputState& state) const override{ |
| 103 | + const GamepadInputState* x = dynamic_cast<const GamepadInputState*>(&state); |
| 104 | + if (x == nullptr){ |
| 105 | + return false; |
| 106 | + } |
| 107 | + return m_buttons == x->m_buttons |
| 108 | + && m_left_x == x->m_left_x |
| 109 | + && m_left_y == x->m_left_y |
| 110 | + && m_right_x == x->m_right_x |
| 111 | + && m_right_y == x->m_right_y; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | +public: |
| 116 | + GamepadInputButton buttons() const{ |
| 117 | + return m_buttons; |
| 118 | + } |
| 119 | + bool button(GamepadInputButton button) const{ |
| 120 | + return !is_empty(m_buttons & button); |
| 121 | + } |
| 122 | + |
| 123 | + int16_t left_x() const{ |
| 124 | + return m_left_x; |
| 125 | + } |
| 126 | + int16_t left_y() const{ |
| 127 | + return m_left_y; |
| 128 | + } |
| 129 | + int16_t right_x() const{ |
| 130 | + return m_right_x; |
| 131 | + } |
| 132 | + int16_t right_y() const{ |
| 133 | + return m_right_y; |
| 134 | + } |
| 135 | + |
| 136 | + void set_buttons(GamepadInputButton buttons){ |
| 137 | + m_buttons = buttons; |
| 138 | + } |
| 139 | + void set_sticks(int16_t left_x, int16_t left_y, int16_t right_x, int16_t right_y){ |
| 140 | + m_left_x = left_x; |
| 141 | + m_left_y = left_y; |
| 142 | + m_right_x = right_x; |
| 143 | + m_right_y = right_y; |
| 144 | + } |
| 145 | + void set( |
| 146 | + GamepadInputButton buttons, |
| 147 | + int16_t left_x, int16_t left_y, |
| 148 | + int16_t right_x, int16_t right_y |
| 149 | + ){ |
| 150 | + m_buttons = buttons; |
| 151 | + m_left_x = left_x; |
| 152 | + m_left_y = left_y; |
| 153 | + m_right_x = right_x; |
| 154 | + m_right_y = right_y; |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | +private: |
| 159 | + GamepadInputButton m_buttons = GAMEPAD_BUTTON_NONE; |
| 160 | + int16_t m_left_x = 0; |
| 161 | + int16_t m_left_y = 0; |
| 162 | + int16_t m_right_x = 0; |
| 163 | + int16_t m_right_y = 0; |
| 164 | +}; |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +} |
| 170 | +#endif |
0 commit comments