Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Gamepad.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_GAMEPAD_H
22 #define GF_GAMEPAD_H
23 
24 #include <cstdint>
25 
26 #include "Portability.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
33  /**
34  * @ingroup window
35  * @brief The gamepad buttons
36  *
37  * The buttons are named after the
38  * [Xbox 360 controller](https://en.wikipedia.org/wiki/Xbox_360_controller).
39  *
40  * @sa gf::Gamepad::getButtonName()
41  */
42  enum class GamepadButton {
43  Invalid, ///< Invalid button
44  A, ///< The A button
45  B, ///< The B button
46  X, ///< The X button
47  Y, ///< The Y button
48  Back, ///< The Back button
49  Guide, ///< The Guide button
50  Start, ///< The Start button
51  LeftStick, ///< The left stick button
52  RightStick, ///< The right stick button
53  LeftBumper, ///< The left bumper button
54  RightBumper, ///< The right bumper button
55  DPadUp, ///< The directional pad up button
56  DPadDown, ///< The directional pad down button
57  DPadLeft, ///< The directional pad left button
58  DPadRight, ///< The directional pad right button
59  };
60 
61  /**
62  * @ingroup window
63  * @brief The gamepad axis
64  *
65  * The axis are named after the
66  * [Xbox 360 controller](https://en.wikipedia.org/wiki/Xbox_360_controller).
67  *
68  * @sa gf::Gamepad::getAxisName()
69  */
70  enum class GamepadAxis {
71  Invalid, ///< Invalid axis
72  LeftX, ///< The left stick X axis
73  LeftY, ///< The left stick Y axis
74  RightX, ///< The right stick X axis
75  RightY, ///< The right stick Y axis
76  TriggerLeft, ///< The left trigger axis
77  TriggerRight, ///< The right trigger axis
78  };
79 
80  /**
81  * @ingroup window
82  * @brief A gamepad axis direction
83  *
84  * The positive direction is right with a X axis and down with a Y axis.
85  *
86  * For triggers, there is only a positive direction.
87  */
88  enum class GamepadAxisDirection {
89  Positive, ///< Positive direction of the axis.
90  Negative, ///< Negative direction of the axis.
91  };
92 
93 
94  /**
95  * @ingroup window
96  * @brief A gamepad hardware identifier
97  *
98  * This identifier is given when a gamepad has just been connected. It must
99  * be transformed in a gf::GamepadId with gf::Gamepad::open().
100  *
101  * @sa GamepadId
102  */
103  enum class GamepadHwId : int
104  #ifdef DOXYGEN_SHOULD_SKIP_THIS
105  {
106  }
107  #endif
108  ;
109 
110  /**
111  * @ingroup window
112  * @brief A gamepad identifier
113  *
114  * This identifier is the representation of a connected gamepad that has
115  * been opened with gf::Gamepad::open(). It is used in the subsequent events
116  * related to this gamepad.
117  *
118  * @sa GamepadHwId
119  */
120  enum class GamepadId : int32_t
121  #ifdef DOXYGEN_SHOULD_SKIP_THIS
122  {
123  }
124  #endif
125  ;
126 
127  /**
128  * @ingroup window
129  * @brief Some gamepad related functions
130  */
131  class GF_API Gamepad {
132  public:
133  /**
134  * @brief Get the axis name
135  *
136  * @param axis A gamepad axis
137  * @return A string representation of the axis
138  */
139  static const char *getAxisName(GamepadAxis axis);
140 
141  /**
142  * @brief Get the button name
143  *
144  * @param button A gamepad button
145  * @return A string representation of the button
146  */
147  static const char *getButtonName(GamepadButton button);
148 
149  /**
150  * @name Gamepad management
151  * @{
152  */
153 
154  /**
155  * @brief Open a gamepad
156  *
157  * Generally, this function is called after a GamepadConnected event.
158  *
159  * Example:
160  *
161  * ~~~{.cc}
162  * if (event.type == gf::EventType::GamepadConnected) {
163  * gf::GamepadId id = gf::Gamepad::open(event.gamepadConnection.id);
164  * // do something with id
165  * }
166  * ~~~
167  *
168  * @param hwid The hardware identifier
169  * @return The identifier of the gamepad
170  * @sa close()
171  */
172  static GamepadId open(GamepadHwId hwid);
173 
174  /**
175  * @brief Check if a gamepad is attached
176  *
177  * @param id The gamepad id
178  * @return True if the gamepad is attached
179  */
180  static bool isAttached(GamepadId id);
181 
182  /**
183  * @brief Close a gamepad
184  *
185  * Generally, this function is called after a GamepadDisconnected event.
186  *
187  * Example:
188  *
189  * ~~~{.cc}
190  * if (event.type == gf::EventType::GamepadDisconnected) {
191  * gf::Gamepad::close(event.gamepadDisconnection.id);
192  * }
193  * ~~~
194  *
195  * @param id The gamepad id
196  * @sa open()
197  */
198  static void close(GamepadId id);
199 
200  /**
201  * @brief Get the name of the gamepad
202  *
203  * @param id The gamepad id
204  * @return A string representation of the gamepad
205  */
206  static const char *getName(GamepadId id);
207 
208  /** @} */
209 
210  /**
211  * @brief Initialize the already connected gamepads
212  */
213  static void initialize();
214 
215  /**
216  * @brief Deleted constructor
217  */
218  Gamepad() = delete;
219  };
220 
221 #ifndef DOXYGEN_SHOULD_SKIP_THIS
222 }
223 #endif
224 }
225 
226 #endif // GF_GAMEPAD_H
static bool isAttached(GamepadId id)
Check if a gamepad is attached.
GamepadAxisDirection
A gamepad axis direction.
Definition: Gamepad.h:88
static const char * getButtonName(GamepadButton button)
Get the button name.
GamepadButton
The gamepad buttons.
Definition: Gamepad.h:42
The Guide button.
The right bumper button.
static void initialize()
Initialize the already connected gamepads.
The left stick Y axis.
Some gamepad related functions.
Definition: Gamepad.h:131
The left trigger axis.
static const char * getName(GamepadId id)
Get the name of the gamepad.
The right trigger axis.
The B button.
The left stick button.
The left bumper button.
static void close(GamepadId id)
Close a gamepad.
GamepadHwId
A gamepad hardware identifier.
Definition: Gamepad.h:103
The Start button.
The right stick button.
GamepadAxis
The gamepad axis.
Definition: Gamepad.h:70
The Y button.
Negative direction of the axis.
Definition: Action.h:34
The left stick X axis.
GamepadId
A gamepad identifier.
Definition: Gamepad.h:120
static const char * getAxisName(GamepadAxis axis)
Get the axis name.
The directional pad right button.
The X button.
The directional pad up button.
The right stick X axis.
Positive direction of the axis.
The directional pad left button.
The Back button.
#define GF_API
Definition: Portability.h:35
The A button.
The directional pad down button.
Gamepad()=delete
Deleted constructor.
The right stick Y axis.
static GamepadId open(GamepadHwId hwid)
Open a gamepad.