CUGL 1.2
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 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_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 PanInput
81 
120 class PanInput : public InputDevice {
121 public:
122 #pragma mark Listener
123 
144  typedef std::function<void(const PanEvent& event, bool focus)> Listener;
145 
146 protected:
148  bool _screen;
150  bool _active;
152  bool _fingery;
154  float _threshold;
156  float _spread;
159 
161  std::unordered_map<Uint32, Listener> _beginListeners;
163  std::unordered_map<Uint32, Listener> _finishListeners;
165  std::unordered_map<Uint32, Listener> _motionListeners;
166 
167 
168 #pragma mark Constructor
169 
175  PanInput();
176 
180  virtual ~PanInput() {}
181 
187  virtual void dispose() override;
188 
189 
190 #pragma mark Device Attributes
191 public:
208  bool isTouchScreen() const { return _screen; }
209 
226  void setTouchScreen(bool flag);
227 
242  float getThreshold() const { return sqrtf(_threshold); }
243 
258  void setThreshold(float threshold);
259 
269  bool isFingerSensitive() const { return _fingery; }
270 
280  void setFingerSensitive(bool flag) { _fingery = flag; }
281 
282 
283 #pragma mark Data Polling
284 
292  bool isActive() const { return _active; }
293 
299  const Vec2& getDelta() const { return _active ? _event.delta : Vec2::ZERO; }
300 
306  const Vec2& getPan() const { return _active ? _event.pan : Vec2::ZERO; }
307 
313  const Vec2& getPosition() const { return _active ? _event.position : Vec2::ZERO; }
314 
323  int getFingers() const { return _active ? _event.fingers : 0; }
324 
325 
326 #pragma mark Listeners
327 
337  virtual bool requestFocus(Uint32 key) override;
338 
349  bool isListener(Uint32 key) const;
350 
362  const Listener getBeginListener(Uint32 key) const;
363 
376  const Listener getEndListener(Uint32 key) const;
377 
387  const Listener getMotionListener(Uint32 key) const;
388 
403  bool addBeginListener(Uint32 key, Listener listener);
404 
420  bool addEndListener(Uint32 key, Listener listener);
421 
436  bool addMotionListener(Uint32 key, Listener listener);
437 
450  bool removeBeginListener(Uint32 key);
451 
465  bool removeEndListener(Uint32 key);
466 
479  bool removeMotionListener(Uint32 key);
480 
481 
482 protected:
483 #pragma mark Input Device
484 
490  virtual void clearState() override;
491 
503  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
504 
515  virtual void queryEvents(std::vector<Uint32>& eventset) override;
516 
526  Vec2 getScaledPosition(float x, float y);
527 
528  // Apparently friends are not inherited
529  friend class Input;
530 };
531 
532 }
533 #endif /* __CU_PAN_INPUT_H__ */
bool addBeginListener(Uint32 key, Listener listener)
bool removeBeginListener(Uint32 key)
bool isFingerSensitive() const
Definition: CUPanInput.h:269
std::unordered_map< Uint32, Listener > _motionListeners
Definition: CUPanInput.h:165
void setTouchScreen(bool flag)
bool isListener(Uint32 key) const
Definition: CUVec2.h:61
Definition: CUInput.h:77
Definition: CUTimestamp.h:61
float getThreshold() const
Definition: CUPanInput.h:242
Vec2 pan
Definition: CUPanInput.h:54
const Vec2 & getPan() const
Definition: CUPanInput.h:306
std::unordered_map< Uint32, Listener > _finishListeners
Definition: CUPanInput.h:163
Definition: CUPanInput.h:120
float _spread
Definition: CUPanInput.h:156
PanEvent _event
Definition: CUPanInput.h:158
float _threshold
Definition: CUPanInput.h:154
Vec2 getScaledPosition(float x, float y)
virtual void dispose() override
bool _fingery
Definition: CUPanInput.h:152
virtual ~PanInput()
Definition: CUPanInput.h:180
Timestamp timestamp
Definition: CUPanInput.h:50
bool isActive() const
Definition: CUPanInput.h:292
Vec2 position
Definition: CUPanInput.h:52
const Vec2 & getPosition() const
Definition: CUPanInput.h:313
std::unordered_map< Uint32, Listener > _beginListeners
Definition: CUPanInput.h:161
const Listener getEndListener(Uint32 key) const
int fingers
Definition: CUPanInput.h:58
bool isTouchScreen() const
Definition: CUPanInput.h:208
Vec2 delta
Definition: CUPanInput.h:56
bool _screen
Definition: CUPanInput.h:148
const Listener getMotionListener(Uint32 key) const
bool addEndListener(Uint32 key, Listener listener)
Definition: CUPanInput.h:47
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:323
bool _active
Definition: CUPanInput.h:150
const Listener getBeginListener(Uint32 key) const
std::function< void(const PanEvent &event, bool focus)> Listener
Definition: CUPanInput.h:144
bool removeEndListener(Uint32 key)
PanEvent()
Definition: CUPanInput.h:63
void setFingerSensitive(bool flag)
Definition: CUPanInput.h:280
Definition: CUAction.h:51
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 Vec2 & getDelta() const
Definition: CUPanInput.h:299
bool addMotionListener(Uint32 key, Listener listener)
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
Definition: CUInput.h:298