FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
joystick.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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 
39  class Joystick {
40  public:
41 
42  // Hat positions.
43  enum Hat {
46  HAT_UP = 1,
47  HAT_RIGHT = 2,
48  HAT_DOWN = 4,
49  HAT_LEFT = 8,
54  };
55 
56  // The list of axes available from a controller.
66  };
67 
68  // The list of buttons available from a controller.
87  };
88 
93  Joystick(int32_t joystickId, int32_t deviceIndex);
94 
97  ~Joystick();
98 
101  int32_t getInstanceId() const;
102 
105  int32_t getJoystickId() const;
106 
109  void setDeviceIndex(int32_t deviceIndex);
110 
113  int32_t getDeviceIndex() const;
114 
117  const std::string& getGuid();
118 
121  const std::string& getName();
122 
125  void open();
126 
129  void close();
130 
133  bool isConnected() const;
134 
137  bool isController() const;
138 
141  void openController();
142 
145  void closeController();
146 
149  uint8_t getNumberOfAxes() const;
150 
153  uint8_t getNumberOfButtons() const;
154 
157  uint8_t getNumberOfHats() const;
158 
162  float getAxisValue(int8_t axis) const;
163 
166  int8_t getHatValue(int8_t hat) const;
167 
171  bool isButtonPressed(int8_t button) const;
172 
173  private:
176  float convertRange(int16_t value) const;
177 
179  SDL_Joystick* m_joystickHandle;
181  SDL_GameController* m_controllerHandle;
183  SDL_JoystickID m_instanceId;
185  int32_t m_joystickId;
187  int32_t m_deviceIndex;
189  std::string m_guidStr;
191  std::string m_name;
192  };
193 
194 } //FIFE
195 
196 #endif
uint8_t getNumberOfAxes() const
Return the number of axes.
Definition: joystick.cpp:139
int32_t m_joystickId
Our joystick id.
Definition: joystick.h:185
void closeController()
Closes / deactivates the gamecontroller.
Definition: joystick.cpp:132
bool isController() const
Indicates if this a controller.
Definition: joystick.cpp:119
int32_t m_deviceIndex
SDLs device index.
Definition: joystick.h:187
SDL_JoystickID m_instanceId
SDLs joystick id (different from device index)
Definition: joystick.h:183
uint8_t getNumberOfButtons() const
Return the number of buttons.
Definition: joystick.cpp:147
int32_t getDeviceIndex() const
Return the device index of the joystick.
Definition: joystick.cpp:68
void close()
Closes / deactivates the joystick.
Definition: joystick.cpp:105
std::string m_name
The name of the joystick / controller.
Definition: joystick.h:191
int32_t getJoystickId() const
Sets the instance id of the joystick.
Definition: joystick.cpp:60
int32_t getInstanceId() const
Return the instance id of the joystick.
Definition: joystick.cpp:56
void setDeviceIndex(int32_t deviceIndex)
Sets the device index of the joystick.
Definition: joystick.cpp:64
Represents a Joystick and if available the Gamecontroller.
Definition: joystick.h:39
unsigned char uint8_t
Definition: core.h:38
int8_t getHatValue(int8_t hat) const
Return the hat value (see Hat positions), for given hat index.
Definition: joystick.cpp:176
const std::string & getGuid()
Return the GUID of the joystick / gamecontroller class as string.
Definition: joystick.cpp:72
Joystick(int32_t joystickId, int32_t deviceIndex)
Constructor.
Definition: joystick.cpp:41
float getAxisValue(int8_t axis) const
Return the current value for given axis.
Definition: joystick.cpp:163
bool isButtonPressed(int8_t button) const
Return the current value for given axis.
Definition: joystick.cpp:183
float convertRange(int16_t value) const
Converts the int16 in -1.0 to 1.0 range.
Definition: joystick.cpp:201
void openController()
Opens / activates the gamecontroller, only possible with valid GUID mapping.
Definition: joystick.cpp:123
uint8_t getNumberOfHats() const
Return the number of hats.
Definition: joystick.cpp:155
bool isConnected() const
Indicates if the joystick / gamecontroller is connected.
Definition: joystick.cpp:115
~Joystick()
Destructor.
Definition: joystick.cpp:52
SDL_GameController * m_controllerHandle
SDLs controller handle.
Definition: joystick.h:181
const std::string & getName()
Return the name of the joystick.
Definition: joystick.cpp:76
SDL_Joystick * m_joystickHandle
SDLs joystick handle.
Definition: joystick.h:179
void open()
Opens / activates the joystick and sets values.
Definition: joystick.cpp:80
std::string m_guidStr
The GUID as string.
Definition: joystick.h:189