CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUPinchInput.h
1 //
2 // CUPinchInput.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides basic support for pinch 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_PINCH_INPUT_H__
35 #define __CU_PINCH_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 PinchEvent
43 
47 class PinchEvent {
48 public:
54  int fingers;
56  float pinch;
58  float delta;
59 
63  PinchEvent() : fingers(0), pinch(0.0f), delta(0.0f) {}
64 
73  PinchEvent(const Vec2& point, int down, float distance, const Timestamp& stamp) {
74  position = point; fingers = down; pinch = distance; delta = pinch; timestamp = stamp;
75  }
76 };
77 
78 
79 #pragma mark -
80 #pragma mark PinchInput
81 
120 class PinchInput : public InputDevice {
121 public:
122 #pragma mark Listener
123 
145  typedef std::function<void(const PinchEvent& event, bool focus)> Listener;
146 
147 protected:
149  bool _screen;
151  bool _active;
153  float _threshold;
156 
158  std::unordered_map<Uint32, Listener> _beginListeners;
160  std::unordered_map<Uint32, Listener> _finishListeners;
162  std::unordered_map<Uint32, Listener> _changeListeners;
163 
164 
165 #pragma mark Constructor
166 
172  PinchInput();
173 
177  virtual ~PinchInput() {}
178 
184  virtual void dispose() override;
185 
186 #pragma mark Device Attributes
187 public:
204  bool isTouchScreen() const { return _screen; }
205 
222  void setTouchScreen(bool flag);
223 
234  float getThreshold() const { return _threshold; }
235 
246  void setThreshold(float threshold);
247 
248 
249 #pragma mark Data Polling
250 
258  bool isActive() const { return _active; }
259 
268  float getDelta() const { return _active ? _event.delta : 0.0f; }
269 
278  float getPinch() const { return _active ? _event.pinch : 0.0f; }
279 
288  int getFingers() const { return _active ? _event.fingers : 0; }
289 
297  const Vec2& getPosition() const { return _active ? _event.position : Vec2::ZERO; }
298 
299 
300 #pragma mark Listeners
301 
311  virtual bool requestFocus(Uint32 key) override;
312 
323  bool isListener(Uint32 key) const;
324 
336  const Listener getBeginListener(Uint32 key) const;
337 
350  const Listener getEndListener(Uint32 key) const;
351 
361  const Listener getChangeListener(Uint32 key) const;
362 
377  bool addBeginListener(Uint32 key, Listener listener);
378 
394  bool addEndListener(Uint32 key, Listener listener);
395 
410  bool addChangeListener(Uint32 key, Listener listener);
411 
424  bool removeBeginListener(Uint32 key);
425 
439  bool removeEndListener(Uint32 key);
440 
453  bool removeChangeListener(Uint32 key);
454 
455 
456 protected:
457 #pragma mark Input Device
458 
464  virtual void clearState() override;
465 
477  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
478 
489  virtual void queryEvents(std::vector<Uint32>& eventset) override;
490 
491  // Apparently friends are not inherited
492  friend class Input;
493 };
494 
495 }
496 #endif /* __CU_PINCH_INPUT_H__ */
bool removeEndListener(Uint32 key)
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
void setTouchScreen(bool flag)
Definition: CUVec2.h:61
Definition: CUInput.h:77
Definition: CUTimestamp.h:61
Vec2 position
Definition: CUPinchInput.h:52
const Listener getBeginListener(Uint32 key) const
bool removeBeginListener(Uint32 key)
bool addEndListener(Uint32 key, Listener listener)
float delta
Definition: CUPinchInput.h:58
virtual void queryEvents(std::vector< Uint32 > &eventset) override
PinchEvent _event
Definition: CUPinchInput.h:155
Timestamp timestamp
Definition: CUPinchInput.h:50
float pinch
Definition: CUPinchInput.h:56
bool addChangeListener(Uint32 key, Listener listener)
std::unordered_map< Uint32, Listener > _changeListeners
Definition: CUPinchInput.h:162
bool isListener(Uint32 key) const
bool removeChangeListener(Uint32 key)
virtual bool requestFocus(Uint32 key) override
bool isTouchScreen() const
Definition: CUPinchInput.h:204
float getPinch() const
Definition: CUPinchInput.h:278
std::function< void(const PinchEvent &event, bool focus)> Listener
Definition: CUPinchInput.h:145
float getDelta() const
Definition: CUPinchInput.h:268
Definition: CUPinchInput.h:47
bool _active
Definition: CUPinchInput.h:151
std::unordered_map< Uint32, Listener > _beginListeners
Definition: CUPinchInput.h:158
virtual void dispose() override
virtual ~PinchInput()
Definition: CUPinchInput.h:177
int getFingers() const
Definition: CUPinchInput.h:288
bool isActive() const
Definition: CUPinchInput.h:258
bool addBeginListener(Uint32 key, Listener listener)
virtual void clearState() override
PinchEvent()
Definition: CUPinchInput.h:63
std::unordered_map< Uint32, Listener > _finishListeners
Definition: CUPinchInput.h:160
const Listener getChangeListener(Uint32 key) const
float getThreshold() const
Definition: CUPinchInput.h:234
float _threshold
Definition: CUPinchInput.h:153
const Vec2 & getPosition() const
Definition: CUPinchInput.h:297
Definition: CUPinchInput.h:120
bool _screen
Definition: CUPinchInput.h:149
Definition: CUAction.h:51
const Listener getEndListener(Uint32 key) const
static const Vec2 ZERO
Definition: CUVec2.h:71
PinchEvent(const Vec2 &point, int down, float distance, const Timestamp &stamp)
Definition: CUPinchInput.h:73
int fingers
Definition: CUPinchInput.h:54
void setThreshold(float threshold)
Definition: CUInput.h:298