summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/sdl/sdl.cpp')
-rw-r--r--src/input_common/sdl/sdl.cpp57
1 files changed, 53 insertions, 4 deletions
diff --git a/src/input_common/sdl/sdl.cpp b/src/input_common/sdl/sdl.cpp
index ae0206909..756ee58b7 100644
--- a/src/input_common/sdl/sdl.cpp
+++ b/src/input_common/sdl/sdl.cpp
@@ -8,6 +8,7 @@
8#include <tuple> 8#include <tuple>
9#include <unordered_map> 9#include <unordered_map>
10#include <SDL.h> 10#include <SDL.h>
11#include "common/logging/log.h"
11#include "common/math_util.h" 12#include "common/math_util.h"
12#include "input_common/sdl/sdl.h" 13#include "input_common/sdl/sdl.h"
13 14
@@ -40,12 +41,16 @@ public:
40 return SDL_JoystickGetButton(joystick.get(), button) == 1; 41 return SDL_JoystickGetButton(joystick.get(), button) == 1;
41 } 42 }
42 43
43 std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const { 44 float GetAxis(int axis) const {
44 if (!joystick) 45 if (!joystick)
45 return {}; 46 return {};
46 SDL_JoystickUpdate(); 47 SDL_JoystickUpdate();
47 float x = SDL_JoystickGetAxis(joystick.get(), axis_x) / 32767.0f; 48 return SDL_JoystickGetAxis(joystick.get(), axis) / 32767.0f;
48 float y = SDL_JoystickGetAxis(joystick.get(), axis_y) / 32767.0f; 49 }
50
51 std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const {
52 float x = GetAxis(axis_x);
53 float y = GetAxis(axis_y);
49 y = -y; // 3DS uses an y-axis inverse from SDL 54 y = -y; // 3DS uses an y-axis inverse from SDL
50 55
51 // Make sure the coordinates are in the unit circle, 56 // Make sure the coordinates are in the unit circle,
@@ -97,6 +102,27 @@ private:
97 Uint8 direction; 102 Uint8 direction;
98}; 103};
99 104
105class SDLAxisButton final : public Input::ButtonDevice {
106public:
107 explicit SDLAxisButton(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_,
108 bool trigger_if_greater_)
109 : joystick(joystick_), axis(axis_), threshold(threshold_),
110 trigger_if_greater(trigger_if_greater_) {}
111
112 bool GetStatus() const override {
113 float axis_value = joystick->GetAxis(axis);
114 if (trigger_if_greater)
115 return axis_value > threshold;
116 return axis_value < threshold;
117 }
118
119private:
120 std::shared_ptr<SDLJoystick> joystick;
121 int axis;
122 float threshold;
123 bool trigger_if_greater;
124};
125
100class SDLAnalog final : public Input::AnalogDevice { 126class SDLAnalog final : public Input::AnalogDevice {
101public: 127public:
102 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_) 128 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_)
@@ -130,8 +156,14 @@ public:
130 * - "joystick": the index of the joystick to bind 156 * - "joystick": the index of the joystick to bind
131 * - "button"(optional): the index of the button to bind 157 * - "button"(optional): the index of the button to bind
132 * - "hat"(optional): the index of the hat to bind as direction buttons 158 * - "hat"(optional): the index of the hat to bind as direction buttons
159 * - "axis"(optional): the index of the axis to bind
133 * - "direction"(only used for hat): the direction name of the hat to bind. Can be "up", 160 * - "direction"(only used for hat): the direction name of the hat to bind. Can be "up",
134 * "down", "left" or "right" 161 * "down", "left" or "right"
162 * - "threshould"(only used for axis): a float value in (-1.0, 1.0) which the button is
163 * triggered if the axis value crosses
164 * - "direction"(only used for axis): "+" means the button is triggered when the axis value
165 * is greater than the threshold; "-" means the button is triggered when the axis value
166 * is smaller than the threshold
135 */ 167 */
136 std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override { 168 std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override {
137 const int joystick_index = params.Get("joystick", 0); 169 const int joystick_index = params.Get("joystick", 0);
@@ -155,6 +187,23 @@ public:
155 direction); 187 direction);
156 } 188 }
157 189
190 if (params.Has("axis")) {
191 const int axis = params.Get("axis", 0);
192 const float threshold = params.Get("threshold", 0.5f);
193 const std::string direction_name = params.Get("direction", "");
194 bool trigger_if_greater;
195 if (direction_name == "+") {
196 trigger_if_greater = true;
197 } else if (direction_name == "-") {
198 trigger_if_greater = false;
199 } else {
200 trigger_if_greater = true;
201 LOG_ERROR(Input, "Unknown direction %s", direction_name.c_str());
202 }
203 return std::make_unique<SDLAxisButton>(GetJoystick(joystick_index), axis, threshold,
204 trigger_if_greater);
205 }
206
158 const int button = params.Get("button", 0); 207 const int button = params.Get("button", 0);
159 return std::make_unique<SDLButton>(GetJoystick(joystick_index), button); 208 return std::make_unique<SDLButton>(GetJoystick(joystick_index), button);
160 } 209 }