CUGL 1.3
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 MIT 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 <cugl/input/CUInput.h>
37 #include <cugl/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 
93 class Accelerometer : public InputDevice {
94 public:
95 #pragma mark Listener
96 
125  typedef std::function<void(const AccelerationEvent& event, bool focus)> Listener;
126 
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, Listener> _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 getThreshold() 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 Listener getListener(Uint32 key) const;
326 
339  bool addListener(Uint32 key, Listener 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__ */
cugl::Accelerometer::getAccelerationZ
float getAccelerationZ() const
Definition: CUAccelerometer.h:238
cugl::Accelerometer::Listener
std::function< void(const AccelerationEvent &event, bool focus)> Listener
Definition: CUAccelerometer.h:125
cugl::Vec3::x
float x
Definition: CUVec3.h:66
cugl::Vec3::z
float z
Definition: CUVec3.h:70
cugl::Accelerometer::getDelta
const Vec3 getDelta() const
Definition: CUAccelerometer.h:292
cugl::Accelerometer::addListener
bool addListener(Uint32 key, Listener listener)
cugl::Accelerometer::getDeltaY
float getDeltaY() const
Definition: CUAccelerometer.h:270
cugl::Accelerometer::clearState
virtual void clearState() override
cugl::Timestamp
Definition: CUTimestamp.h:61
cugl::Accelerometer::requestFocus
virtual bool requestFocus(Uint32 key) override
cugl::AccelerationEvent::AccelerationEvent
AccelerationEvent()
Definition: CUAccelerometer.h:56
cugl::Accelerometer::queryEvents
virtual void queryEvents(std::vector< Uint32 > &eventset) override
cugl::Accelerometer::getThreshold
float getThreshold() const
Definition: CUAccelerometer.h:193
cugl::Accelerometer::getAccelerationX
float getAccelerationX() const
Definition: CUAccelerometer.h:218
cugl::Accelerometer::getDeltaZ
float getDeltaZ() const
Definition: CUAccelerometer.h:281
cugl::Accelerometer::removeListener
bool removeListener(Uint32 key)
cugl::Accelerometer::isListener
bool isListener(Uint32 key) const
cugl::Accelerometer::setThreshold
void setThreshold(float value) const
cugl::Accelerometer
Definition: CUAccelerometer.h:93
cugl::Vec3::y
float y
Definition: CUVec3.h:68
cugl::Accelerometer::getDeltaX
float getDeltaX() const
Definition: CUAccelerometer.h:259
cugl::Accelerometer::getAccelerationY
float getAccelerationY() const
Definition: CUAccelerometer.h:228
cugl::Input
Definition: CUInput.h:77
cugl::AccelerationEvent::timestamp
Timestamp timestamp
Definition: CUAccelerometer.h:47
cugl::AccelerationEvent::AccelerationEvent
AccelerationEvent(const Vec3 &roll, const Vec3 &diff, const Timestamp &stamp)
Definition: CUAccelerometer.h:65
cugl::AccelerationEvent::delta
Vec3 delta
Definition: CUAccelerometer.h:51
cugl::Accelerometer::getAcceleration
const Vec3 & getAcceleration() const
Definition: CUAccelerometer.h:248
cugl::InputDevice
Definition: CUInput.h:298
cugl::Accelerometer::getListener
const Listener getListener(Uint32 key) const
cugl::Vec3
Definition: CUVec3.h:61
cugl::Accelerometer::updateState
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
cugl::AccelerationEvent
Definition: CUAccelerometer.h:44
cugl::AccelerationEvent::axis
Vec3 axis
Definition: CUAccelerometer.h:49