CUGL 1.3
Cornell University Game Library
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 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: 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 
83 
84 #pragma mark -
85 
120 class Touchscreen : public InputDevice {
121 public:
122 #pragma mark Listeners
123 
151  typedef std::function<void(const TouchEvent& event, bool focus)> ContactListener;
152 
183  typedef std::function<void(const TouchEvent& event, const Vec2& previous, bool focus)> MotionListener;
184 
185 protected:
187  std::unordered_map<TouchID,Vec2> _previous;
189  std::unordered_map<TouchID,Vec2> _current;
190 
192  std::unordered_map<Uint32, ContactListener> _beginListeners;
194  std::unordered_map<Uint32, ContactListener> _finishListeners;
196  std::unordered_map<Uint32, MotionListener> _moveListeners;
197 
198 #pragma mark Constructors
199 
206 
210  virtual ~Touchscreen() {}
211 
217  virtual void dispose() override;
218 
219 #pragma mark Data Polling
220 public:
231  bool touchDown(TouchID touch) const {
232  return _current.find(touch) != _current.end();
233  }
234 
242  bool touchPressed(TouchID touch) const {
243  return (_current.find(touch) != _current.end() &&
244  _previous.find(touch) == _previous.end());
245  }
246 
258  bool touchReleased(TouchID touch) {
259  return (_current.find(touch) == _current.end() &&
260  _previous.find(touch) != _previous.end());
261  }
262 
273  Vec2 touchPosition(TouchID touch) const;
274 
286  Vec2 touchOffset(TouchID touch) const;
287 
293  unsigned int touchCount() const { return (unsigned int)_current.size(); }
294 
300  const std::vector<TouchID> touchSet() const;
301 
302 #pragma mark Listeners
303 
313  virtual bool requestFocus(Uint32 key) override;
314 
325  bool isListener(Uint32 key) const;
326 
338  const ContactListener getBeginListener(Uint32 key) const;
339 
351  const ContactListener getEndListener(Uint32 key) const;
352 
362  const MotionListener getMotionListener(Uint32 key) const;
363 
378  bool addBeginListener(Uint32 key, ContactListener listener);
379 
394  bool addEndListener(Uint32 key, ContactListener listener);
395 
410  bool addMotionListener(Uint32 key, MotionListener listener);
411 
424  bool removeBeginListener(Uint32 key);
425 
438  bool removeEndListener(Uint32 key);
439 
452  bool removeMotionListener(Uint32 key);
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 
495 #endif /* __CU_TOUCHSCREEN_H__ */
cugl::Touchscreen::touchDown
bool touchDown(TouchID touch) const
Definition: CUTouchscreen.h:231
cugl::Touchscreen::getEndListener
const ContactListener getEndListener(Uint32 key) const
cugl::Touchscreen::touchSet
const std::vector< TouchID > touchSet() const
cugl::Touchscreen::addMotionListener
bool addMotionListener(Uint32 key, MotionListener listener)
cugl::Touchscreen::dispose
virtual void dispose() override
cugl::Touchscreen::touchPressed
bool touchPressed(TouchID touch) const
Definition: CUTouchscreen.h:242
cugl::Touchscreen::touchPosition
Vec2 touchPosition(TouchID touch) const
cugl::Timestamp
Definition: CUTimestamp.h:61
cugl::Touchscreen::removeEndListener
bool removeEndListener(Uint32 key)
cugl::Touchscreen::clearState
virtual void clearState() override
cugl::Touchscreen::isListener
bool isListener(Uint32 key) const
cugl::Touchscreen::updateState
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
cugl::Touchscreen::removeBeginListener
bool removeBeginListener(Uint32 key)
cugl::Touchscreen::touchOffset
Vec2 touchOffset(TouchID touch) const
cugl::Touchscreen::getBeginListener
const ContactListener getBeginListener(Uint32 key) const
cugl::TouchEvent::timestamp
Timestamp timestamp
Definition: CUTouchscreen.h:56
cugl::Touchscreen::MotionListener
std::function< void(const TouchEvent &event, const Vec2 &previous, bool focus)> MotionListener
Definition: CUTouchscreen.h:183
cugl::Touchscreen::_beginListeners
std::unordered_map< Uint32, ContactListener > _beginListeners
Definition: CUTouchscreen.h:192
cugl::TouchEvent::touch
TouchID touch
Definition: CUTouchscreen.h:58
cugl::TouchEvent::TouchEvent
TouchEvent(TouchID finger, const Vec2 &point, float force, const Timestamp &stamp)
Definition: CUTouchscreen.h:77
cugl::Touchscreen::_previous
std::unordered_map< TouchID, Vec2 > _previous
Definition: CUTouchscreen.h:187
cugl::Touchscreen::touchReleased
bool touchReleased(TouchID touch)
Definition: CUTouchscreen.h:258
cugl::Touchscreen::~Touchscreen
virtual ~Touchscreen()
Definition: CUTouchscreen.h:210
cugl::Touchscreen::addBeginListener
bool addBeginListener(Uint32 key, ContactListener listener)
cugl::Touchscreen::ContactListener
std::function< void(const TouchEvent &event, bool focus)> ContactListener
Definition: CUTouchscreen.h:151
cugl::Vec2
Definition: CUVec2.h:61
cugl::Touchscreen::_current
std::unordered_map< TouchID, Vec2 > _current
Definition: CUTouchscreen.h:189
cugl::Touchscreen::_finishListeners
std::unordered_map< Uint32, ContactListener > _finishListeners
Definition: CUTouchscreen.h:194
cugl::Touchscreen::Touchscreen
Touchscreen()
Definition: CUTouchscreen.h:205
cugl::Touchscreen::addEndListener
bool addEndListener(Uint32 key, ContactListener listener)
cugl::Touchscreen::queryEvents
virtual void queryEvents(std::vector< Uint32 > &eventset) override
cugl::Input
Definition: CUInput.h:77
cugl::Touchscreen::getMotionListener
const MotionListener getMotionListener(Uint32 key) const
cugl::Touchscreen::_moveListeners
std::unordered_map< Uint32, MotionListener > _moveListeners
Definition: CUTouchscreen.h:196
cugl::TouchEvent
Definition: CUTouchscreen.h:53
cugl::TouchEvent::position
Vec2 position
Definition: CUTouchscreen.h:60
cugl::InputDevice
Definition: CUInput.h:298
cugl::TouchEvent::TouchEvent
TouchEvent()
Definition: CUTouchscreen.h:67
cugl::Touchscreen::touchCount
unsigned int touchCount() const
Definition: CUTouchscreen.h:293
cugl::TouchEvent::pressure
float pressure
Definition: CUTouchscreen.h:62
cugl::Touchscreen::removeMotionListener
bool removeMotionListener(Uint32 key)
cugl::Touchscreen::requestFocus
virtual bool requestFocus(Uint32 key) override
cugl::Touchscreen
Definition: CUTouchscreen.h:120