CUGL
Cornell University Game Library
CUAccelerometer.h
1 //
2 // CUAcclerometer.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides basic acclerometer support. It uses the joystick
6 // subsystem, which is guaranteed to work on both iOS and Android.
7 //
8 // This class is a singleton and should never be allocated directly. It
9 // should only be accessed via the Input dispatcher.
10 //
11 // CUGL zlib License:
12 // This software is provided 'as-is', without any express or implied
13 // warranty. In no event will the authors be held liable for any damages
14 // arising from the use of this software.
15 //
16 // Permission is granted to anyone to use this software for any purpose,
17 // including commercial applications, and to alter it and redistribute it
18 // freely, subject to the following restrictions:
19 //
20 // 1. The origin of this software must not be misrepresented; you must not
21 // claim that you wrote the original software. If you use this software
22 // in a product, an acknowledgment in the product documentation would be
23 // appreciated but is not required.
24 //
25 // 2. Altered source versions must be plainly marked as such, and must not
26 // be misrepresented as being the original software.
27 //
28 // 3. This notice may not be removed or altered from any source distribution.
29 //
30 // Author: Walker White
31 // Version: 7/12/16
32 //
33 #ifndef __CU_ACCELEROMETER_H__
34 #define __CU_ACCELEROMETER_H__
35 
36 #include "CUInput.h"
37 #include "../math/CUVec3.h"
38 
39 namespace cugl {
40 
45 public:
52 
57 
65  AccelerationEvent(const Vec3& roll, const Vec3& diff, const Timestamp& stamp) {
66  axis = roll; delta = diff; timestamp = stamp;
67  }
68 
69 };
70 
71 #pragma mark -
72 #pragma mark AccelerationListener
73 
102 typedef std::function<void(const AccelerationEvent& event, bool focus)> AccelerationListener;
103 
104 #pragma mark -
105 
126 class Accelerometer : public InputDevice {
127 private:
129  SDL_Joystick* _input;
131  float _threshold;
132 
134  bool _update;
136  Vec3 _current;
138  Vec3 _previous;
140  Vec3 _anchor;
141 
143  int _xaxis;
145  int _yaxis;
146 
148  std::unordered_map<Uint32, AccelerationListener> _listeners;
149 
150 #pragma mark Constructor
151 
157  Accelerometer();
158 
162  virtual ~Accelerometer() { dispose(); }
163 
169  virtual bool init() override;
170 
176  virtual void dispose() override;
177 
178 public:
179 #pragma mark Data Polling
180 
193  float getTreshold() const { return _threshold; }
194 
208  void setThreshold(float value) const;
209 
218  float getAccelerationX() const { return _current.x; }
219 
228  float getAccelerationY() const { return _current.y; }
229 
238  float getAccelerationZ() const { return _current.z; }
239 
248  const Vec3& getAcceleration() const { return _current; }
249 
259  float getDeltaX() const { return _current.x-_previous.x; }
260 
270  float getDeltaY() const { return _current.y-_previous.y; }
271 
281  float getDeltaZ() const { return _current.z-_previous.z; }
282 
292  const Vec3 getDelta() const { return _current-_previous; }
293 
294 #pragma mark Listeners
295 
305  virtual bool requestFocus(Uint32 key) override;
306 
314  bool isListener(Uint32 key) const;
315 
325  const AccelerationListener getListener(Uint32 key) const;
326 
339  bool addListener(Uint32 key, AccelerationListener listener);
340 
351  bool removeListener(Uint32 key);
352 
353 
354 #pragma mark Input Device
355 
361  virtual void clearState() override;
362 
374  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
375 
386  virtual void queryEvents(std::vector<Uint32>& eventset) override;
387 
388  // Apparently friends are not inherited
389  friend class Input;
390 
391 
392 };
393 
394 }
395 
396 #endif /* __CU_ACCELEROMETER_H__ */
Definition: CUAccelerometer.h:44
Definition: CUAccelerometer.h:126
bool addListener(Uint32 key, AccelerationListener listener)
AccelerationEvent(const Vec3 &roll, const Vec3 &diff, const Timestamp &stamp)
Definition: CUAccelerometer.h:65
float x
Definition: CUVec3.h:66
bool removeListener(Uint32 key)
Definition: CUInput.h:77
Definition: CUTimestamp.h:61
float z
Definition: CUVec3.h:70
float getDeltaX() const
Definition: CUAccelerometer.h:259
Vec3 delta
Definition: CUAccelerometer.h:51
Vec3 axis
Definition: CUAccelerometer.h:49
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
void setThreshold(float value) const
float getDeltaY() const
Definition: CUAccelerometer.h:270
float getTreshold() const
Definition: CUAccelerometer.h:193
const Vec3 & getAcceleration() const
Definition: CUAccelerometer.h:248
float getDeltaZ() const
Definition: CUAccelerometer.h:281
AccelerationEvent()
Definition: CUAccelerometer.h:56
float getAccelerationZ() const
Definition: CUAccelerometer.h:238
Timestamp timestamp
Definition: CUAccelerometer.h:47
virtual bool requestFocus(Uint32 key) override
std::function< void(const AccelerationEvent &event, bool focus)> AccelerationListener
Definition: CUAccelerometer.h:102
const Vec3 getDelta() const
Definition: CUAccelerometer.h:292
virtual void queryEvents(std::vector< Uint32 > &eventset) override
float getAccelerationX() const
Definition: CUAccelerometer.h:218
bool isListener(Uint32 key) const
float getAccelerationY() const
Definition: CUAccelerometer.h:228
Definition: CUVec3.h:61
float y
Definition: CUVec3.h:68
Definition: CUAnimationNode.h:52
const AccelerationListener getListener(Uint32 key) const
virtual void clearState() override
Definition: CUInput.h:298