SuperTuxKart
device_config.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2010-2015 SuperTuxKart-Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_DEVICE_CONFIG_HPP
20 #define HEADER_DEVICE_CONFIG_HPP
21 
22 #include "input/binding.hpp"
23 #include "input/input.hpp"
24 #include "utils/no_copy.hpp"
25 
26 #include <assert.h>
27 #include <iosfwd>
28 #include <irrString.h>
29 #include <string>
30 
35 //==== D E V I C E C O N F I G =================================================
36 
41 class DeviceConfig : public NoCopy
42 {
43 
44 private:
47  bool m_enabled;
48 
50  int m_plugged;
51 
53  std::string m_name;
54 
56  irr::core::stringw m_config_name;
57 
58 protected:
59 
60  Binding m_bindings[PA_COUNT];
61 
62  DeviceConfig();
63 
64  bool doGetAction(Input::InputType type,
65  const int id,
66  int* value, /* inout */
67  const PlayerAction firstActionToCheck,
68  const PlayerAction lastActionToCheck,
69  PlayerAction* action /* out */ );
70 protected:
72  friend class GamePadDevice;
73  friend class KeyboardDevice;
74  bool getGameAction(Input::InputType type,
75  const int id,
76  int* value, /* inout */
77  PlayerAction* action /* out */);
78 
79 public:
80 
81  virtual ~DeviceConfig() {}
82 
83  static DeviceConfig* create(const XMLNode *config);
84  irr::core::stringw toString();
85  bool hasBindingFor(const int buttonID) const;
86  bool hasBindingFor(const int buttonID, PlayerAction from,
87  PlayerAction to) const;
88  void setBinding(const PlayerAction action,
89  const Input::InputType type,
90  const int id,
91  Input::AxisDirection direction = Input::AD_NEUTRAL,
92  Input::AxisRange range = Input::AR_HALF,
93  wchar_t character=0);
94  bool getMenuAction(Input::InputType type,
95  const int id,
96  int* value,
97  PlayerAction* action /* out */);
98  irr::core::stringw getMappingIdString (const PlayerAction action) const;
99  virtual irr::core::stringw getBindingAsString(const PlayerAction action) const;
100  virtual bool isGamePad() const = 0;
101  virtual bool isKeyboard() const = 0;
102 
103  virtual void save(std::ofstream& stream);
104  virtual bool load(const XMLNode *config);
105 
106  // ------------------------------------------------------------------------
109  virtual bool isAnalog(Input::InputType type, int id) const { return false;}
110  // ------------------------------------------------------------------------
113  virtual bool desensitize() const { return false;}
114  // ------------------------------------------------------------------------
117  virtual int getNumberOfButtons() const
118  {
119  assert(false); return 0;
120  } // getNumberOfButtons
121 
122  // ------------------------------------------------------------------------
125  virtual int getNumberOfAxes() const
126  {
127  assert(false); return 0;
128  } // getNumberOfAxes
129 
130  // ------------------------------------------------------------------------
132  void setName(const std::string &name) { m_name = name; }
133 
134  // ------------------------------------------------------------------------
136  const std::string& getName() const { return m_name; };
137 
138  // ------------------------------------------------------------------------
140  void setPlugged() { m_plugged++; }
141 
142  // ------------------------------------------------------------------------
144  bool isPlugged() const { return m_plugged > 0; }
145 
146  // ------------------------------------------------------------------------
148  void unPlugged()
149  {
150  m_plugged--;
151  assert(m_plugged >= 0);
152  }
153 
154  // ------------------------------------------------------------------------
156  int getNumberOfDevices() const { return m_plugged; }
157 
158  // ------------------------------------------------------------------------
160  const Binding& getBinding(int i) const {return m_bindings[i];}
161 
162  // ------------------------------------------------------------------------
164  bool isEnabled() const { return m_enabled; }
165 
166  // ------------------------------------------------------------------------
168  void setEnabled(bool new_value) { m_enabled = new_value; }
169 
170  // ------------------------------------------------------------------------
172  irr::core::stringw getConfigName() const { return m_config_name; }
173 
174  // ------------------------------------------------------------------------
176  void setConfigName( irr::core::stringw config_name ) { m_config_name = config_name; }
177 }; // class DeviceConfig
178 
179 #endif
virtual irr::core::stringw getBindingAsString(const PlayerAction action) const
Get a user-readable string describing the bound action.
Definition: device_config.cpp:78
const std::string & getName() const
Returns the internal name for this device configuration.
Definition: device_config.hpp:136
bool m_enabled
If set to false, this device will be ignored.
Definition: device_config.hpp:47
bool doGetAction(Input::InputType type, const int id, int *value, const PlayerAction firstActionToCheck, const PlayerAction lastActionToCheck, PlayerAction *action)
internal helper method for DeviceConfig::getGameAction and DeviceConfig::getMenuAction ...
Definition: device_config.cpp:226
contains the key bindings information related to one input device
Definition: device_config.hpp:41
PlayerAction
types of input events / what actions the players can do
Definition: input.hpp:116
void unPlugged()
Decrease ref counter.
Definition: device_config.hpp:148
void setPlugged()
Increase ref counter.
Definition: device_config.hpp:140
std::string m_name
Internal name of this configuration.
Definition: device_config.hpp:53
specialisation of Inputdevice for gamepad type devices
Definition: gamepad_device.hpp:32
bool getGameAction(Input::InputType type, const int id, int *value, PlayerAction *action)
Searches for a game actions associated with the given input event.
Definition: device_config.cpp:196
int m_plugged
How many devices connected to the system which uses this config?
Definition: device_config.hpp:50
specialisation of InputDevice for keyboard type devices
Definition: keyboard_device.hpp:33
virtual int getNumberOfButtons() const
Should only be called for gamepads, which has its own implementation.
Definition: device_config.hpp:117
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
Definition: binding.hpp:36
const Binding & getBinding(int i) const
Returns the binding of a given index.
Definition: device_config.hpp:160
int getNumberOfDevices() const
Returns the number of devices using this configuration.
Definition: device_config.hpp:156
void setBinding(const PlayerAction action, const Input::InputType type, const int id, Input::AxisDirection direction=Input::AD_NEUTRAL, Input::AxisRange range=Input::AR_HALF, wchar_t character=0)
Sets the bindings for an action.
Definition: device_config.cpp:178
void setName(const std::string &name)
Sets the internal name of this device.
Definition: device_config.hpp:132
void setConfigName(irr::core::stringw config_name)
Returns the name of this device configuration.
Definition: device_config.hpp:176
bool isEnabled() const
At this time only relevant for gamepads, keyboards are always enabled.
Definition: device_config.hpp:164
static DeviceConfig * create(const XMLNode *config)
A simple factory that creates either a gamepad or a keyboard configuration.
Definition: device_config.cpp:40
bool getMenuAction(Input::InputType type, const int id, int *value, PlayerAction *action)
Searches for a game actions associated with the given input event.
Definition: device_config.cpp:213
utility class used to parse XML files
Definition: xml_node.hpp:47
virtual int getNumberOfAxes() const
Should only be called for gamepads, which has its own implementation.
Definition: device_config.hpp:125
virtual void save(std::ofstream &stream)
Saves the configuration to a file.
Definition: device_config.cpp:288
irr::core::stringw getConfigName() const
Sets the name of this device configuration.
Definition: device_config.hpp:172
irr::core::stringw m_config_name
Name of this configuration (given by the user).
Definition: device_config.hpp:56
bool isPlugged() const
Returns if this config is sed by any devices.
Definition: device_config.hpp:144
irr::core::stringw getMappingIdString(const PlayerAction action) const
Get an internal unique string describing the bound action.
Definition: device_config.cpp:94
virtual bool desensitize() const
Returns true if this device should desensitize its input at values close to 0 (to avoid &#39;oversteering...
Definition: device_config.hpp:113
void setEnabled(bool new_value)
Sets this config to be enabled or disabled.
Definition: device_config.hpp:168
virtual bool load(const XMLNode *config)
Reads a device configuration from input.xml.
Definition: device_config.cpp:311
virtual bool isAnalog(Input::InputType type, int id) const
Returns true if this device has analog axis, so that steering values will not be affected by time-ful...
Definition: device_config.hpp:109