1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21// LOVE
22#include "Joystick.h"
23
24// STL
25#include <cmath>
26
27namespace love
28{
29namespace joystick
30{
31
32love::Type Joystick::type("Joystick", &Object::type);
33
34float Joystick::clampval(float x)
35{
36 if (fabsf(x) < 0.01)
37 return 0.0f;
38
39 if (x < -0.99f) return -1.0f;
40 if (x > 0.99f) return 1.0f;
41
42 return x;
43}
44
45bool Joystick::getConstant(const char *in, Joystick::Hat &out)
46{
47 return hats.find(in, out);
48}
49
50bool Joystick::getConstant(Joystick::Hat in, const char *&out)
51{
52 return hats.find(in, out);
53}
54
55bool Joystick::getConstant(const char *in, Joystick::GamepadAxis &out)
56{
57 return gpAxes.find(in, out);
58}
59
60bool Joystick::getConstant(Joystick::GamepadAxis in, const char *&out)
61{
62 return gpAxes.find(in, out);
63}
64
65bool Joystick::getConstant(const char *in, Joystick::GamepadButton &out)
66{
67 return gpButtons.find(in, out);
68}
69
70bool Joystick::getConstant(Joystick::GamepadButton in, const char *&out)
71{
72 return gpButtons.find(in, out);
73}
74
75bool Joystick::getConstant(const char *in, Joystick::InputType &out)
76{
77 return inputTypes.find(in, out);
78}
79
80bool Joystick::getConstant(Joystick::InputType in, const char *&out)
81{
82 return inputTypes.find(in, out);
83}
84
85StringMap<Joystick::Hat, Joystick::HAT_MAX_ENUM>::Entry Joystick::hatEntries[] =
86{
87 {"c", Joystick::HAT_CENTERED},
88 {"u", Joystick::HAT_UP},
89 {"r", Joystick::HAT_RIGHT},
90 {"d", Joystick::HAT_DOWN},
91 {"l", Joystick::HAT_LEFT},
92 {"ru", Joystick::HAT_RIGHTUP},
93 {"rd", Joystick::HAT_RIGHTDOWN},
94 {"lu", Joystick::HAT_LEFTUP},
95 {"ld", Joystick::HAT_LEFTDOWN},
96};
97
98StringMap<Joystick::Hat, Joystick::HAT_MAX_ENUM> Joystick::hats(Joystick::hatEntries, sizeof(Joystick::hatEntries));
99
100StringMap<Joystick::GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM>::Entry Joystick::gpAxisEntries[] =
101{
102 {"leftx", GAMEPAD_AXIS_LEFTX},
103 {"lefty", GAMEPAD_AXIS_LEFTY},
104 {"rightx", GAMEPAD_AXIS_RIGHTX},
105 {"righty", GAMEPAD_AXIS_RIGHTY},
106 {"triggerleft", GAMEPAD_AXIS_TRIGGERLEFT},
107 {"triggerright", GAMEPAD_AXIS_TRIGGERRIGHT},
108};
109
110StringMap<Joystick::GamepadAxis, Joystick::GAMEPAD_AXIS_MAX_ENUM> Joystick::gpAxes(Joystick::gpAxisEntries, sizeof(Joystick::gpAxisEntries));
111
112StringMap<Joystick::GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM>::Entry Joystick::gpButtonEntries[] =
113{
114 {"a", GAMEPAD_BUTTON_A},
115 {"b", GAMEPAD_BUTTON_B},
116 {"x", GAMEPAD_BUTTON_X},
117 {"y", GAMEPAD_BUTTON_Y},
118 {"back", GAMEPAD_BUTTON_BACK},
119 {"guide", GAMEPAD_BUTTON_GUIDE},
120 {"start", GAMEPAD_BUTTON_START},
121 {"leftstick", GAMEPAD_BUTTON_LEFTSTICK},
122 {"rightstick", GAMEPAD_BUTTON_RIGHTSTICK},
123 {"leftshoulder", GAMEPAD_BUTTON_LEFTSHOULDER},
124 {"rightshoulder", GAMEPAD_BUTTON_RIGHTSHOULDER},
125 {"dpup", GAMEPAD_BUTTON_DPAD_UP},
126 {"dpdown", GAMEPAD_BUTTON_DPAD_DOWN},
127 {"dpleft", GAMEPAD_BUTTON_DPAD_LEFT},
128 {"dpright", GAMEPAD_BUTTON_DPAD_RIGHT},
129};
130
131StringMap<Joystick::GamepadButton, Joystick::GAMEPAD_BUTTON_MAX_ENUM> Joystick::gpButtons(Joystick::gpButtonEntries, sizeof(Joystick::gpButtonEntries));
132
133StringMap<Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM>::Entry Joystick::inputTypeEntries[] =
134{
135 {"axis", Joystick::INPUT_TYPE_AXIS},
136 {"button", Joystick::INPUT_TYPE_BUTTON},
137 {"hat", Joystick::INPUT_TYPE_HAT},
138};
139
140StringMap<Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM> Joystick::inputTypes(Joystick::inputTypeEntries, sizeof(Joystick::inputTypeEntries));
141
142} // joystick
143} // love
144