CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUTouchscreen.h
1 //
2 // CUTouchscreen.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides basic touch support. While it supports multitouch, it
6 // only receives one touch per event. For complex multitouch events (such as
7 // gestures) you should use GestureInput instead.
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: 7/12/16
33 
34 #ifndef __CU_TOUCHSCREEN_H__
35 #define __CU_TOUCHSCREEN_H__
36 
37 #include "CUInput.h"
38 #include <cugl/math/CURect.h>
39 
40 namespace cugl {
41 
43 #define CU_INVALID_TOUCH -1
44 
45 #pragma mark -
46 
48 typedef Sint64 TouchID;
49 
53 class TouchEvent {
54 public:
58  TouchID touch;
62  float pressure;
63 
67  TouchEvent() : touch(-1), pressure(0) {}
68 
77  TouchEvent(TouchID finger, const Vec2& point, float force, const Timestamp& stamp) {
78  touch = finger; position = point; pressure = force; timestamp = stamp;
79  }
80 };
81 
82 #pragma mark -
83 #pragma mark TouchListener
84 
113 typedef std::function<void(const TouchEvent& event, bool focus)> TouchListener;
114 
115 #pragma mark -
116 #pragma mark TouchMotionListener
117 
148 typedef std::function<void(const TouchEvent& event, const Vec2& previous, bool focus)> TouchMotionListener;
149 
150 #pragma mark -
151 
187 class Touchscreen : public InputDevice {
188 protected:
190  std::unordered_map<TouchID,Vec2> _previous;
192  std::unordered_map<TouchID,Vec2> _current;
193 
195  std::unordered_map<Uint32, TouchListener> _beginListeners;
197  std::unordered_map<Uint32, TouchListener> _finishListeners;
199  std::unordered_map<Uint32, TouchMotionListener> _moveListeners;
200 
208 
212  virtual ~Touchscreen() {}
213 
219  virtual void dispose() override;
220 
221 #pragma mark Data Polling
222 public:
233  bool touchDown(TouchID touch) const {
234  return _current.find(touch) != _current.end();
235  }
236 
244  bool touchPressed(TouchID touch) const {
245  return (_current.find(touch) != _current.end() &&
246  _previous.find(touch) == _previous.end());
247  }
248 
260  bool touchReleased(TouchID touch) {
261  return (_current.find(touch) == _current.end() &&
262  _previous.find(touch) != _previous.end());
263  }
264 
275  Vec2 touchPosition(TouchID touch) const;
276 
288  Vec2 touchOffset(TouchID touch) const;
289 
295  unsigned int touchCount() const { return (unsigned int)_current.size(); }
296 
302  const std::vector<TouchID> touchSet() const;
303 
304 #pragma mark Listeners
305 
315  virtual bool requestFocus(Uint32 key) override;
316 
327  bool isListener(Uint32 key) const;
328 
340  const TouchListener getBeginListener(Uint32 key) const;
341 
353  const TouchListener getEndListener(Uint32 key) const;
354 
364  const TouchMotionListener getMotionListener(Uint32 key) const;
365 
380  bool addBeginListener(Uint32 key, TouchListener listener);
381 
396  bool addEndListener(Uint32 key, TouchListener listener);
397 
412  bool addMotionListener(Uint32 key, TouchMotionListener listener);
413 
426  bool removeBeginListener(Uint32 key);
427 
440  bool removeEndListener(Uint32 key);
441 
454  bool removeMotionListener(Uint32 key);
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 
497 #endif /* __CU_TOUCHSCREEN_H__ */
std::function< void(const TouchEvent &event, bool focus)> TouchListener
Definition: CUTouchscreen.h:113
Touchscreen()
Definition: CUTouchscreen.h:207
virtual void clearState() override
std::unordered_map< Uint32, TouchListener > _beginListeners
Definition: CUTouchscreen.h:195
virtual ~Touchscreen()
Definition: CUTouchscreen.h:212
Definition: CUVec2.h:61
Vec2 touchOffset(TouchID touch) const
bool isListener(Uint32 key) const
Definition: CUInput.h:77
const TouchMotionListener getMotionListener(Uint32 key) const
Definition: CUTimestamp.h:61
bool removeBeginListener(Uint32 key)
std::unordered_map< TouchID, Vec2 > _previous
Definition: CUTouchscreen.h:190
const TouchListener getBeginListener(Uint32 key) const
Definition: CUTouchscreen.h:53
TouchEvent()
Definition: CUTouchscreen.h:67
std::function< void(const TouchEvent &event, const Vec2 &previous, bool focus)> TouchMotionListener
Definition: CUTouchscreen.h:148
float pressure
Definition: CUTouchscreen.h:62
const TouchListener getEndListener(Uint32 key) const
TouchID touch
Definition: CUTouchscreen.h:58
const std::vector< TouchID > touchSet() const
virtual void queryEvents(std::vector< Uint32 > &eventset) override
virtual bool requestFocus(Uint32 key) override
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
Definition: CUTouchscreen.h:187
Sint64 TouchID
Definition: CUTouchscreen.h:48
bool addEndListener(Uint32 key, TouchListener listener)
virtual void dispose() override
bool touchReleased(TouchID touch)
Definition: CUTouchscreen.h:260
Timestamp timestamp
Definition: CUTouchscreen.h:56
bool removeEndListener(Uint32 key)
bool addBeginListener(Uint32 key, TouchListener listener)
TouchEvent(TouchID finger, const Vec2 &point, float force, const Timestamp &stamp)
Definition: CUTouchscreen.h:77
std::unordered_map< TouchID, Vec2 > _current
Definition: CUTouchscreen.h:192
Definition: CUAnimationNode.h:52
Vec2 position
Definition: CUTouchscreen.h:60
bool removeMotionListener(Uint32 key)
bool touchPressed(TouchID touch) const
Definition: CUTouchscreen.h:244
Vec2 touchPosition(TouchID touch) const
unsigned int touchCount() const
Definition: CUTouchscreen.h:295
std::unordered_map< Uint32, TouchMotionListener > _moveListeners
Definition: CUTouchscreen.h:199
bool addMotionListener(Uint32 key, TouchMotionListener listener)
std::unordered_map< Uint32, TouchListener > _finishListeners
Definition: CUTouchscreen.h:197
Definition: CUInput.h:298
bool touchDown(TouchID touch) const
Definition: CUTouchscreen.h:233