Gainput  v1.0.0
GainputInputManager.h
1 
2 #ifndef GAINPUTINPUTMANAGER_H_
3 #define GAINPUTINPUTMANAGER_H_
4 
5 
6 namespace gainput
7 {
8 
10 
24 class GAINPUT_LIBEXPORT InputManager
25 {
26 public:
28 
36  InputManager(bool useSystemTime = true, Allocator& allocator = GetDefaultAllocator());
37 
39  ~InputManager();
40 
42 
46  void SetDisplaySize(int width, int height) { displayWidth_ = width; displayHeight_ = height; }
47 
48 #if defined(GAINPUT_PLATFORM_LINUX)
49 
54  void HandleEvent(XEvent& event);
55 #endif
56 #if defined(GAINPUT_PLATFORM_WIN)
57 
62  void HandleMessage(const MSG& msg);
63 #endif
64 #if defined(GAINPUT_PLATFORM_ANDROID)
65  int32_t HandleInput(AInputEvent* event);
67 
68  struct DeviceInput
69  {
70  InputDevice::DeviceType deviceType;
71  unsigned deviceIndex;
72  ButtonType buttonType;
73  DeviceButtonId buttonId;
74  union
75  {
76  float f;
77  bool b;
78  } value;
79  };
80  void HandleDeviceInput(DeviceInput const& input);
81 #endif
82 
84 
89  void Update();
90 
92 
100  void Update(uint64_t deltaTime);
101 
103  Allocator& GetAllocator() const { return allocator_; }
104 
106  uint64_t GetTime() const;
107 
109 
116  template<class T> DeviceId CreateDevice(unsigned index = InputDevice::AutoIndex, InputDevice::DeviceVariant variant = InputDevice::DV_STANDARD);
118 
125  template<class T> T* CreateAndGetDevice(unsigned index = InputDevice::AutoIndex, InputDevice::DeviceVariant variant = InputDevice::DV_STANDARD);
127 
130  InputDevice* GetDevice(DeviceId deviceId);
132 
135  const InputDevice* GetDevice(DeviceId deviceId) const;
137 
142  DeviceId FindDeviceId(const char* typeName, unsigned index) const;
144 
149  DeviceId FindDeviceId(InputDevice::DeviceType type, unsigned index) const;
150 
156 
158  iterator begin() { return devices_.begin(); }
160  iterator end() { return devices_.end(); }
162  const_iterator begin() const { return devices_.begin(); }
164  const_iterator end() const { return devices_.end(); }
165 
167 
170  ListenerId AddListener(InputListener* listener);
172  void RemoveListener(ListenerId listenerId);
174 
179  void ReorderListeners();
180 
182 
187  size_t GetAnyButtonDown(DeviceButtonSpec* outButtons, size_t maxButtonCount) const;
188 
190  unsigned GetDeviceCountByType(InputDevice::DeviceType type) const;
192  int GetDisplayWidth() const { return displayWidth_; }
194  int GetDisplayHeight() const { return displayHeight_; }
195 
197  ModifierId AddDeviceStateModifier(DeviceStateModifier* modifier);
199  void RemoveDeviceStateModifier(ModifierId modifierId);
200 
201  void EnqueueConcurrentChange(InputDevice& device, InputState& state, InputDeltaState* delta, DeviceButtonId buttonId, bool value);
202  void EnqueueConcurrentChange(InputDevice& device, InputState& state, InputDeltaState* delta, DeviceButtonId buttonId, float value);
203 
205  void ConnectForStateSync(const char* ip, unsigned port);
207  void StartDeviceStateSync(DeviceId deviceId);
208 
210  void SetDebugRenderingEnabled(bool enabled);
212  bool IsDebugRenderingEnabled() const { return debugRenderingEnabled_; }
214  void SetDebugRenderer(DebugRenderer* debugRenderer);
216  DebugRenderer* GetDebugRenderer() const { return debugRenderer_; }
217 
218 private:
219  Allocator& allocator_;
220 
221  DeviceMap devices_;
222  unsigned nextDeviceId_;
223 
225  unsigned nextListenerId_;
226  Array<InputListener*> sortedListeners_;
227 
229  unsigned nextModifierId_;
230 
231  InputDeltaState* deltaState_;
232 
233  uint64_t currentTime_;
234  struct Change
235  {
236  InputDevice* device;
237  InputState* state;
238  InputDeltaState* delta;
239  DeviceButtonId buttonId;
240  ButtonType type;
241  union
242  {
243  bool b;
244  float f;
245  };
246  };
247 
248  GAINPUT_CONC_QUEUE(Change) concurrentInputs_;
249 
250  int displayWidth_;
251  int displayHeight_;
252  bool useSystemTime_;
253 
254  bool debugRenderingEnabled_;
255  DebugRenderer* debugRenderer_;
256 
257  void DeviceCreated(InputDevice* device);
258 
259  // Do not copy.
260  InputManager(const InputManager &);
261  InputManager& operator=(const InputManager &);
262 
263 };
264 
265 
266 template<class T>
267 inline
268 DeviceId
270 {
271  T* device = allocator_.New<T>(*this, nextDeviceId_, index, variant);
272  devices_[nextDeviceId_] = device;
273  DeviceCreated(device);
274  return nextDeviceId_++;
275 }
276 
277 template<class T>
278 inline
279 T*
281 {
282  T* device = allocator_.New<T>(*this, nextDeviceId_, index, variant);
283  devices_[nextDeviceId_] = device;
284  ++nextDeviceId_;
285  DeviceCreated(device);
286  return device;
287 }
288 
289 inline
292 {
293  DeviceMap::iterator it = devices_.find(deviceId);
294  if (it == devices_.end())
295  {
296  return 0;
297  }
298  return it->second;
299 }
300 
301 inline
302 const InputDevice*
304 {
305  DeviceMap::const_iterator it = devices_.find(deviceId);
306  if (it == devices_.end())
307  {
308  return 0;
309  }
310  return it->second;
311 }
312 
313 
316 {
317 public:
319 
325  virtual void Update(InputDeltaState* delta) = 0;
326 };
327 
328 }
329 
330 #endif
331 
InputDevice * GetDevice(DeviceId deviceId)
Returns the input device with the given ID.
Definition: GainputInputManager.h:291
A std::vector-like data container for POD-types.
Definition: GainputContainers.h:96
Interface for debug rendering of input device states.
Definition: GainputDebugRenderer.h:15
DeviceId CreateDevice(unsigned index=InputDevice::AutoIndex, InputDevice::DeviceVariant variant=InputDevice::DV_STANDARD)
Creates an input device and registers it with the manager.
Definition: GainputInputManager.h:269
T * CreateAndGetDevice(unsigned index=InputDevice::AutoIndex, InputDevice::DeviceVariant variant=InputDevice::DV_STANDARD)
Creates an input device, registers it with the manager and returns it.
Definition: GainputInputManager.h:280
Interface for modifiers that change device input states after they have been updated.
Definition: GainputInputManager.h:315
Manages all input devices and some other helpful stuff.
Definition: GainputInputManager.h:24
iterator begin()
Returns the begin iterator over all registered devices.
Definition: GainputInputManager.h:158
int GetDisplayHeight() const
Returns the graphical display&#39;s height in pixels.
Definition: GainputInputManager.h:194
DeviceMap::const_iterator const_iterator
Const iterator over all registered devices.
Definition: GainputInputManager.h:155
GAINPUT_LIBEXPORT DefaultAllocator & GetDefaultAllocator()
Returns the default instance of the default allocator.
DeviceMap::iterator iterator
Iterator over all registered devices.
Definition: GainputInputManager.h:153
ButtonType
Type of an input device button.
Definition: GainputInputDevice.h:10
V second
The element&#39;s value.
Definition: GainputContainers.h:242
Definition: GainputInputManager.h:68
iterator end()
Returns the end iterator over all registered devices.
Definition: GainputInputManager.h:160
Listener interface that allows to receive notifications when device button states change...
Definition: GainputInputListener.h:9
Stores a list of input state changes.
Definition: GainputInputDeltaState.h:9
Describes a device button on a specific device.
Definition: gainput.h:112
Allocator & GetAllocator() const
Returns the allocator to be used for memory allocations.
Definition: GainputInputManager.h:103
DebugRenderer * GetDebugRenderer() const
Returns the previously set debug renderer.
Definition: GainputInputManager.h:216
unsigned int DeviceId
ID of an input device.
Definition: gainput.h:107
DeviceVariant
Variant of an input device type.
Definition: GainputInputDevice.h:51
unsigned int ListenerId
ID of an input listener.
Definition: gainput.h:123
unsigned int ModifierId
ID of a device state modifier.
Definition: gainput.h:125
DeviceType
Type of an input device.
Definition: GainputInputDevice.h:37
Interface for anything that provides device inputs.
Definition: GainputInputDevice.h:33
void SetDisplaySize(int width, int height)
Sets the window resolution.
Definition: GainputInputManager.h:46
const_iterator end() const
Returns the end iterator over all registered devices.
Definition: GainputInputManager.h:164
bool IsDebugRenderingEnabled() const
Returns true if debug rendering is enabled, false otherwise.
Definition: GainputInputManager.h:212
int GetDisplayWidth() const
Returns the graphical display&#39;s width in pixels.
Definition: GainputInputManager.h:192
An element of the hash table.
Definition: GainputContainers.h:239
const_iterator begin() const
Returns the begin iterator over all registered devices.
Definition: GainputInputManager.h:162
unsigned int DeviceButtonId
ID of a specific button unique to an input device.
Definition: gainput.h:109
State of an input device.
Definition: GainputInputState.h:9
Interface used to pass custom allocators to the library.
Definition: GainputAllocator.h:16
The standard implementation of the given device type.
Definition: GainputInputDevice.h:53
Contains all Gainput related classes, types, and functions.
Definition: gainput.h:103