CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MIT 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 RotationInput
81 
119 class RotationInput : public InputDevice {
120 public:
121 #pragma mark Listener
122 
143  typedef std::function<void(const RotationEvent& event, bool focus)> Listener;
144 
145 protected:
147  bool _screen;
149  bool _active;
151  float _threshold;
154 
156  std::unordered_map<Uint32, Listener> _beginListeners;
158  std::unordered_map<Uint32, Listener> _finishListeners;
160  std::unordered_map<Uint32, Listener> _changeListeners;
161 
162 
163 #pragma mark Constructor
164 
170  RotationInput();
171 
175  virtual ~RotationInput() {}
176 
182  virtual void dispose() override;
183 
184 #pragma mark Device Attributes
185 public:
202  bool isTouchScreen() const { return _screen; }
203 
220  void setTouchScreen(bool flag);
221 
232  float getThreshold() const { return _threshold; }
233 
244  void setThreshold(float threshold);
245 
246 
247 #pragma mark Data Polling
248 
256  bool isActive() const { return _active; }
257 
266  float getDelta() const { return _active ? _event.delta : 0.0f; }
267 
276  float getRotation() const { return _active ? _event.rotation : 0.0f; }
277 
286  int getFingers() const { return _active ? _event.fingers : 0; }
287 
295  const Vec2& getPosition() const { return _active ? _event.position : Vec2::ZERO; }
296 
297 
298 #pragma mark Listeners
299 
309  virtual bool requestFocus(Uint32 key) override;
310 
321  bool isListener(Uint32 key) const;
322 
334  const Listener getBeginListener(Uint32 key) const;
335 
348  const Listener getEndListener(Uint32 key) const;
349 
359  const Listener getChangeListener(Uint32 key) const;
360 
375  bool addBeginListener(Uint32 key, Listener listener);
376 
392  bool addEndListener(Uint32 key, Listener listener);
393 
408  bool addChangeListener(Uint32 key, Listener listener);
409 
422  bool removeBeginListener(Uint32 key);
423 
437  bool removeEndListener(Uint32 key);
438 
451  bool removeChangeListener(Uint32 key);
452 
453 
454 protected:
455 #pragma mark Input Device
456 
462  virtual void clearState() override;
463 
475  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
476 
487  virtual void queryEvents(std::vector<Uint32>& eventset) override;
488 
489  // Apparently friends are not inherited
490  friend class Input;
491 };
492 
493 }
494 #endif /* __CU_ROTATION_INPUT_H__ */
cugl::RotationInput::getChangeListener
const Listener getChangeListener(Uint32 key) const
cugl::RotationInput::clearState
virtual void clearState() override
cugl::RotationInput::setThreshold
void setThreshold(float threshold)
cugl::RotationInput::_event
RotationEvent _event
Definition: CURotationInput.h:153
cugl::RotationInput::requestFocus
virtual bool requestFocus(Uint32 key) override
cugl::RotationEvent::timestamp
Timestamp timestamp
Definition: CURotationInput.h:50
cugl::RotationEvent::fingers
int fingers
Definition: CURotationInput.h:54
cugl::RotationEvent::position
Vec2 position
Definition: CURotationInput.h:52
cugl::RotationInput::_active
bool _active
Definition: CURotationInput.h:149
cugl::Vec2::ZERO
static const Vec2 ZERO
Definition: CUVec2.h:71
cugl::RotationInput::dispose
virtual void dispose() override
cugl::RotationInput::RotationInput
RotationInput()
cugl::RotationInput::_changeListeners
std::unordered_map< Uint32, Listener > _changeListeners
Definition: CURotationInput.h:160
cugl::RotationInput::_beginListeners
std::unordered_map< Uint32, Listener > _beginListeners
Definition: CURotationInput.h:156
cugl::RotationInput::getBeginListener
const Listener getBeginListener(Uint32 key) const
cugl::RotationInput::addBeginListener
bool addBeginListener(Uint32 key, Listener listener)
cugl::Timestamp
Definition: CUTimestamp.h:61
cugl::RotationEvent
Definition: CURotationInput.h:47
cugl::RotationInput::getFingers
int getFingers() const
Definition: CURotationInput.h:286
cugl::RotationInput::~RotationInput
virtual ~RotationInput()
Definition: CURotationInput.h:175
cugl::RotationInput::isActive
bool isActive() const
Definition: CURotationInput.h:256
cugl::RotationInput::updateState
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
cugl::RotationInput::getRotation
float getRotation() const
Definition: CURotationInput.h:276
cugl::RotationInput::setTouchScreen
void setTouchScreen(bool flag)
cugl::RotationInput::addChangeListener
bool addChangeListener(Uint32 key, Listener listener)
cugl::RotationEvent::delta
float delta
Definition: CURotationInput.h:58
cugl::RotationInput::getPosition
const Vec2 & getPosition() const
Definition: CURotationInput.h:295
cugl::RotationInput::isTouchScreen
bool isTouchScreen() const
Definition: CURotationInput.h:202
cugl::RotationInput::isListener
bool isListener(Uint32 key) const
cugl::RotationInput::Listener
std::function< void(const RotationEvent &event, bool focus)> Listener
Definition: CURotationInput.h:143
cugl::RotationInput::removeBeginListener
bool removeBeginListener(Uint32 key)
cugl::RotationInput::_finishListeners
std::unordered_map< Uint32, Listener > _finishListeners
Definition: CURotationInput.h:158
cugl::RotationEvent::RotationEvent
RotationEvent()
Definition: CURotationInput.h:63
cugl::RotationInput::getThreshold
float getThreshold() const
Definition: CURotationInput.h:232
cugl::RotationInput::queryEvents
virtual void queryEvents(std::vector< Uint32 > &eventset) override
cugl::Vec2
Definition: CUVec2.h:61
cugl::RotationInput::addEndListener
bool addEndListener(Uint32 key, Listener listener)
cugl::RotationEvent::rotation
float rotation
Definition: CURotationInput.h:56
cugl::Input
Definition: CUInput.h:77
cugl::RotationInput::removeChangeListener
bool removeChangeListener(Uint32 key)
cugl::RotationInput::_screen
bool _screen
Definition: CURotationInput.h:147
cugl::RotationInput::getDelta
float getDelta() const
Definition: CURotationInput.h:266
cugl::RotationInput
Definition: CURotationInput.h:119
cugl::InputDevice
Definition: CUInput.h:298
cugl::RotationInput::removeEndListener
bool removeEndListener(Uint32 key)
cugl::RotationInput::getEndListener
const Listener getEndListener(Uint32 key) const
cugl::RotationInput::_threshold
float _threshold
Definition: CURotationInput.h:151
cugl::RotationEvent::RotationEvent
RotationEvent(const Vec2 &point, int down, float angle, const Timestamp &stamp)
Definition: CURotationInput.h:73