CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUPanInput.h
1 //
2 // CUPanInput.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides basic support for multifinger pan gestures. SDL blurs
6 // pinches, rotations, and pans all into a single input event. Therefore, you
7 // need to 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_PAN_INPUT_H__
35 #define __CU_PAN_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 PanEvent
43 
47 class PanEvent {
48 public:
58  int fingers;
59 
63  PanEvent() : fingers(0) {}
64 
73  PanEvent(const Vec2& point, const Vec2& offset, int down, const Timestamp& stamp) {
74  position = point; fingers = down; pan = offset; delta = offset; timestamp = stamp;
75  }
76 };
77 
78 
79 #pragma mark -
80 #pragma mark PanListener
81 
103 typedef std::function<void(const PanEvent& event, bool focus)> PanListener;
104 
105 
106 #pragma mark -
107 #pragma mark PanInput
108 
147 class PanInput : public InputDevice {
148 protected:
150  bool _screen;
152  bool _active;
154  bool _fingery;
156  float _threshold;
158  float _spread;
161 
163  std::unordered_map<Uint32, PanListener> _beginListeners;
165  std::unordered_map<Uint32, PanListener> _finishListeners;
167  std::unordered_map<Uint32, PanListener> _motionListeners;
168 
169 
170 #pragma mark Constructor
171 
177  PanInput();
178 
182  virtual ~PanInput() {}
183 
189  virtual void dispose() override;
190 
191 
192 #pragma mark Device Attributes
193 public:
210  bool isTouchScreen() const { return _screen; }
211 
228  void setTouchScreen(bool flag);
229 
244  float getThreshold() const { return sqrtf(_threshold); }
245 
260  void setThreshold(float threshold);
261 
271  bool isFingerSensitive() const { return _fingery; }
272 
282  void setFingerSensitive(bool flag) { _fingery = flag; }
283 
284 
285 #pragma mark Data Polling
286 
294  bool isActive() const { return _active; }
295 
301  const Vec2& getDelta() const { return _active ? _event.delta : Vec2::ZERO; }
302 
308  const Vec2& getPan() const { return _active ? _event.pan : Vec2::ZERO; }
309 
315  const Vec2& getPosition() const { return _active ? _event.position : Vec2::ZERO; }
316 
325  int getFingers() const { return _active ? _event.fingers : 0; }
326 
327 
328 #pragma mark Listeners
329 
339  virtual bool requestFocus(Uint32 key) override;
340 
351  bool isListener(Uint32 key) const;
352 
364  const PanListener getBeginListener(Uint32 key) const;
365 
378  const PanListener getEndListener(Uint32 key) const;
379 
389  const PanListener getMotionListener(Uint32 key) const;
390 
405  bool addBeginListener(Uint32 key, PanListener listener);
406 
422  bool addEndListener(Uint32 key, PanListener listener);
423 
438  bool addMotionListener(Uint32 key, PanListener listener);
439 
452  bool removeBeginListener(Uint32 key);
453 
467  bool removeEndListener(Uint32 key);
468 
481  bool removeMotionListener(Uint32 key);
482 
483 
484 protected:
485 #pragma mark Input Device
486 
492  virtual void clearState() override;
493 
505  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
506 
517  virtual void queryEvents(std::vector<Uint32>& eventset) override;
518 
528  Vec2 getScaledPosition(float x, float y);
529 
530  // Apparently friends are not inherited
531  friend class Input;
532 };
533 
534 }
535 #endif /* __CU_PAN_INPUT_H__ */
bool removeBeginListener(Uint32 key)
bool isFingerSensitive() const
Definition: CUPanInput.h:271
const PanListener getEndListener(Uint32 key) const
void setTouchScreen(bool flag)
bool isListener(Uint32 key) const
Definition: CUVec2.h:61
Definition: CUInput.h:77
const PanListener getBeginListener(Uint32 key) const
Definition: CUTimestamp.h:61
float getThreshold() const
Definition: CUPanInput.h:244
Vec2 pan
Definition: CUPanInput.h:54
const Vec2 & getPan() const
Definition: CUPanInput.h:308
Definition: CUPanInput.h:147
float _spread
Definition: CUPanInput.h:158
PanEvent _event
Definition: CUPanInput.h:160
float _threshold
Definition: CUPanInput.h:156
std::unordered_map< Uint32, PanListener > _motionListeners
Definition: CUPanInput.h:167
Vec2 getScaledPosition(float x, float y)
virtual void dispose() override
bool _fingery
Definition: CUPanInput.h:154
virtual ~PanInput()
Definition: CUPanInput.h:182
Timestamp timestamp
Definition: CUPanInput.h:50
std::function< void(const PanEvent &event, bool focus)> PanListener
Definition: CUPanInput.h:103
bool isActive() const
Definition: CUPanInput.h:294
Vec2 position
Definition: CUPanInput.h:52
const Vec2 & getPosition() const
Definition: CUPanInput.h:315
int fingers
Definition: CUPanInput.h:58
bool isTouchScreen() const
Definition: CUPanInput.h:210
Vec2 delta
Definition: CUPanInput.h:56
bool addEndListener(Uint32 key, PanListener listener)
bool _screen
Definition: CUPanInput.h:150
Definition: CUPanInput.h:47
std::unordered_map< Uint32, PanListener > _beginListeners
Definition: CUPanInput.h:163
virtual void clearState() override
PanEvent(const Vec2 &point, const Vec2 &offset, int down, const Timestamp &stamp)
Definition: CUPanInput.h:73
void setThreshold(float threshold)
int getFingers() const
Definition: CUPanInput.h:325
bool addMotionListener(Uint32 key, PanListener listener)
bool _active
Definition: CUPanInput.h:152
std::unordered_map< Uint32, PanListener > _finishListeners
Definition: CUPanInput.h:165
bool addBeginListener(Uint32 key, PanListener listener)
bool removeEndListener(Uint32 key)
PanEvent()
Definition: CUPanInput.h:63
void setFingerSensitive(bool flag)
Definition: CUPanInput.h:282
Definition: CUAnimationNode.h:52
virtual void queryEvents(std::vector< Uint32 > &eventset) override
virtual bool requestFocus(Uint32 key) override
static const Vec2 ZERO
Definition: CUVec2.h:71
bool removeMotionListener(Uint32 key)
const PanListener getMotionListener(Uint32 key) const
const Vec2 & getDelta() const
Definition: CUPanInput.h:301
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
Definition: CUInput.h:298