Debian Codegraph @

engine/core/eventchannel/joystick/joystick.h

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #ifndef FIFE_EVENTCHANNEL_JOYSTICK_H
23 #define FIFE_EVENTCHANNEL_JOYSTICK_H
24
25 // Standard C++ library includes
26
27 // 3rd party library includes
28 #include <SDL.h>
29
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34
35 namespace FIFE {
36
37 /** Represents a Joystick and if available the Gamecontroller.
38 */
39 class Joystick {
40 public:
41
42 // Hat positions.
43 enum Hat {
44 HAT_INVALID = -1,
45 HAT_CENTERED = 0,
46 HAT_UP = 1,
47 HAT_RIGHT = 2,
48 HAT_DOWN = 4,
49 HAT_LEFT = 8,
50 HAT_RIGHTUP = 3,
51 HAT_RIGHTDOWN = 6,
52 HAT_LEFTUP = 9,
53 HAT_LEFTDOWN = 12
54 };
55
56 // The list of axes available from a controller.
57 enum ContollerAxis {
58 CONTOLLER_AXIS_INVALID = -1,
59 CONTOLLER_AXIS_LEFTX,
60 CONTOLLER_AXIS_LEFTY,
61 CONTOLLER_AXIS_RIGHTX,
62 CONTOLLER_AXIS_RIGHTY,
63 CONTOLLER_AXIS_TRIGGERLEFT,
64 CONTOLLER_AXIS_TRIGGERRIGHT,
65 CONTOLLER_AXIS_MAX
66 };
67
68 // The list of buttons available from a controller.
69 enum ControllerButton {
70 CONTOLLER_BUTTON_INVALID = -1,
71 CONTOLLER_BUTTON_A,
72 CONTOLLER_BUTTON_B,
73 CONTOLLER_BUTTON_X,
74 CONTOLLER_BUTTON_Y,
75 CONTOLLER_BUTTON_BACK,
76 CONTOLLER_BUTTON_GUIDE,
77 CONTOLLER_BUTTON_START,
78 CONTOLLER_BUTTON_LEFTSTICK,
79 CONTOLLER_BUTTON_RIGHTSTICK,
80 CONTOLLER_BUTTON_LEFTSHOULDER,
81 CONTOLLER_BUTTON_RIGHTSHOULDER,
82 CONTOLLER_BUTTON_DPAD_UP,
83 CONTOLLER_BUTTON_DPAD_DOWN,
84 CONTOLLER_BUTTON_DPAD_LEFT,
85 CONTOLLER_BUTTON_DPAD_RIGHT,
86 CONTOLLER_BUTTON_MAX
87 };
88
89 /** Constructor
90 * @param joystickId The identifier for the Joystick.
91 * @param deviceIndex The index number from SDL2.
92 */
93 Joystick(int32_t joystickId, int32_t deviceIndex);
94
95 /** Destructor
96 */
97 ~Joystick();
98
99 /** Return the instance id of the joystick.
100 */
101 int32_t getInstanceId() const;
102
103 /** Sets the instance id of the joystick.
104 */
105 int32_t getJoystickId() const;
106
107 /** Sets the device index of the joystick.
108 */
109 void setDeviceIndex(int32_t deviceIndex);
110
111 /** Return the device index of the joystick.
112 */
113 int32_t getDeviceIndex() const;
114
115 /** Return the GUID of the joystick / gamecontroller class as string.
116 */
117 const std::string& getGuid();
118
119 /** Return the name of the joystick.
120 */
121 const std::string& getName();
122
123 /** Opens / activates the joystick and sets values.
124 */
125 void open();
126
127 /** Closes / deactivates the joystick.
128 */
129 void close();
130
131 /** Indicates if the joystick / gamecontroller is connected.
132 */
133 bool isConnected() const;
134
135 /** Indicates if this a controller.
136 */
137 bool isController() const;
138
139 /** Opens / activates the gamecontroller, only possible with valid GUID mapping.
140 */
141 void openController();
142
143 /** Closes / deactivates the gamecontroller.
144 */
145 void closeController();
146
147 /** Return the number of axes.
148 */
149 uint8_t getNumberOfAxes() const;
150
151 /** Return the number of buttons.
152 */
153 uint8_t getNumberOfButtons() const;
154
155 /** Return the number of hats.
156 */
157 uint8_t getNumberOfHats() const;
158
159 /** Return the current value for given axis.
160 * @param axis For joysticks the index number, for controller see ContollerAxis.
161 */
162 float getAxisValue(int8_t axis) const;
163
164 /** Return the hat value (see Hat positions), for given hat index.
165 */
166 int8_t getHatValue(int8_t hat) const;
167
168 /** Return the current value for given axis.
169 * @param button For joysticks the index number, for controller see ControllerButton.
170 */
171 bool isButtonPressed(int8_t button) const;
172
173 private:
174 /** Converts the int16 in -1.0 to 1.0 range.
175 */
176 float convertRange(int16_t value) const;
177
178 //! SDLs joystick handle
179 SDL_Joystick* m_joystickHandle;
180 //! SDLs controller handle
181 SDL_GameController* m_controllerHandle;
182 //! SDLs joystick id (different from device index)
183 SDL_JoystickID m_instanceId;
184 //! Our joystick id
185 int32_t m_joystickId;
186 //! SDLs device index
187 int32_t m_deviceIndex;
188 //! The GUID as string
189 std::string m_guidStr;
190 //! The name of the joystick / controller
191 std::string m_name;
192 };
193
194 } //FIFE
195
196 #endif