CUGL
Cornell University Game Library
CURotationInput.h
1 //
2 // CURotationInput.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides basic support for rotation gestures. SDL blurs pinches,
6 // rotations, and pans all into a single input event. Therefore, you need to
7 // set the sensitivity threshold to distinguish them.
8 //
9 // This class is a singleton and should never be allocated directly. It
10 // should only be accessed via the Input dispatcher.
11 //
12 // CUGL zlib License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 12/20/16
33 //
34 #ifndef __CU_ROTATION_INPUT_H__
35 #define __CU_ROTATION_INPUT_H__
36 #include <cugl/input/CUInput.h>
37 #include <cugl/math/CUVec2.h>
38 
39 namespace cugl {
40 
41 #pragma mark -
42 #pragma mark RotationEvent
43 
48 public:
54  int fingers;
56  float rotation;
58  float delta;
59 
63  RotationEvent() : fingers(0), rotation(0.0f), delta(0.0f) {}
64 
73  RotationEvent(const Vec2& point, int down, float angle, const Timestamp& stamp) {
74  position = point; fingers = down; rotation = angle; delta = angle; timestamp = stamp;
75  }
76 };
77 
78 
79 #pragma mark -
80 #pragma mark RotationListener
81 
103 typedef std::function<void(const RotationEvent& event, bool focus)> RotationListener;
104 
105 #pragma mark -
106 #pragma mark RotationInput
107 
145 class RotationInput : public InputDevice {
146 protected:
148  bool _screen;
150  bool _active;
152  float _threshold;
155 
157  std::unordered_map<Uint32, RotationListener> _beginListeners;
159  std::unordered_map<Uint32, RotationListener> _finishListeners;
161  std::unordered_map<Uint32, RotationListener> _changeListeners;
162 
163 
164 #pragma mark Constructor
165 
171  RotationInput();
172 
176  virtual ~RotationInput() {}
177 
183  virtual void dispose() override;
184 
185 #pragma mark Device Attributes
186 public:
203  bool isTouchScreen() const { return _screen; }
204 
221  void setTouchScreen(bool flag);
222 
233  float getThreshold() const { return _threshold; }
234 
245  void setThreshold(float threshold);
246 
247 
248 #pragma mark Data Polling
249 
257  bool isActive() const { return _active; }
258 
267  float getDelta() const { return _active ? _event.delta : 0.0f; }
268 
277  float getRotation() const { return _active ? _event.rotation : 0.0f; }
278 
287  int getFingers() const { return _active ? _event.fingers : 0; }
288 
296  const Vec2& getPosition() const { return _active ? _event.position : Vec2::ZERO; }
297 
298 
299 #pragma mark Listeners
300 
310  virtual bool requestFocus(Uint32 key) override;
311 
322  bool isListener(Uint32 key) const;
323 
335  const RotationListener getBeginListener(Uint32 key) const;
336 
349  const RotationListener getEndListener(Uint32 key) const;
350 
360  const RotationListener getChangeListener(Uint32 key) const;
361 
376  bool addBeginListener(Uint32 key, RotationListener listener);
377 
393  bool addEndListener(Uint32 key, RotationListener listener);
394 
409  bool addChangeListener(Uint32 key, RotationListener listener);
410 
423  bool removeBeginListener(Uint32 key);
424 
438  bool removeEndListener(Uint32 key);
439 
452  bool removeChangeListener(Uint32 key);
453 
454 
455 protected:
456 #pragma mark Input Device
457 
463  virtual void clearState() override;
464 
476  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
477 
488  virtual void queryEvents(std::vector<Uint32>& eventset) override;
489 
490  // Apparently friends are not inherited
491  friend class Input;
492 };
493 
494 }
495 #endif /* __CU_ROTATION_INPUT_H__ */
bool removeEndListener(Uint32 key)
virtual void dispose() override
bool _active
Definition: CURotationInput.h:150
Definition: CUVec2.h:61
bool addChangeListener(Uint32 key, RotationListener listener)
Definition: CUInput.h:77
Definition: CUTimestamp.h:61
std::unordered_map< Uint32, RotationListener > _changeListeners
Definition: CURotationInput.h:161
float delta
Definition: CURotationInput.h:58
float getThreshold() const
Definition: CURotationInput.h:233
Vec2 position
Definition: CURotationInput.h:52
Timestamp timestamp
Definition: CURotationInput.h:50
const RotationListener getEndListener(Uint32 key) const
virtual ~RotationInput()
Definition: CURotationInput.h:176
bool removeChangeListener(Uint32 key)
float rotation
Definition: CURotationInput.h:56
float getRotation() const
Definition: CURotationInput.h:277
virtual void clearState() override
bool removeBeginListener(Uint32 key)
float getDelta() const
Definition: CURotationInput.h:267
float _threshold
Definition: CURotationInput.h:152
std::unordered_map< Uint32, RotationListener > _beginListeners
Definition: CURotationInput.h:157
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
virtual bool requestFocus(Uint32 key) override
RotationEvent _event
Definition: CURotationInput.h:154
Definition: CURotationInput.h:145
virtual void queryEvents(std::vector< Uint32 > &eventset) override
void setThreshold(float threshold)
bool addEndListener(Uint32 key, RotationListener listener)
RotationEvent()
Definition: CURotationInput.h:63
std::unordered_map< Uint32, RotationListener > _finishListeners
Definition: CURotationInput.h:159
bool isActive() const
Definition: CURotationInput.h:257
int getFingers() const
Definition: CURotationInput.h:287
const Vec2 & getPosition() const
Definition: CURotationInput.h:296
std::function< void(const RotationEvent &event, bool focus)> RotationListener
Definition: CURotationInput.h:103
bool isListener(Uint32 key) const
int fingers
Definition: CURotationInput.h:54
Definition: CURotationInput.h:47
bool _screen
Definition: CURotationInput.h:148
RotationEvent(const Vec2 &point, int down, float angle, const Timestamp &stamp)
Definition: CURotationInput.h:73
const RotationListener getBeginListener(Uint32 key) const
const RotationListener getChangeListener(Uint32 key) const
Definition: CUAnimationNode.h:52
static const Vec2 ZERO
Definition: CUVec2.h:71
void setTouchScreen(bool flag)
bool addBeginListener(Uint32 key, RotationListener listener)
Definition: CUInput.h:298
bool isTouchScreen() const
Definition: CURotationInput.h:203