CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUGestureInput.h
1 //
2 // CUGestureInput.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides basic support for custom defined gestures. It uses the
6 // SDL implementation of the $1 (dollar) gesture system. For more information,
7 // see the research page
8 //
9 // http://depts.washington.edu/madlab/proj/dollar/index.html
10 //
11 // This class is a singleton and should never be allocated directly. It
12 // should only be accessed via the Input dispatcher.
13 //
14 // CUGL zlib License:
15 // This software is provided 'as-is', without any express or implied
16 // warranty. In no event will the authors be held liable for any damages
17 // arising from the use of this software.
18 //
19 // Permission is granted to anyone to use this software for any purpose,
20 // including commercial applications, and to alter it and redistribute it
21 // freely, subject to the following restrictions:
22 //
23 // 1. The origin of this software must not be misrepresented; you must not
24 // claim that you wrote the original software. If you use this software
25 // in a product, an acknowledgment in the product documentation would be
26 // appreciated but is not required.
27 //
28 // 2. Altered source versions must be plainly marked as such, and must not
29 // be misrepresented as being the original software.
30 //
31 // 3. This notice may not be removed or altered from any source distribution.
32 //
33 // Author: Walker White
34 // Version: 12/20/16
35 //
36 #ifndef __CU_GESTURE_INPUT_H__
37 #define __CU_GESTURE_INPUT_H__
38 #include <cugl/input/CUInput.h>
39 #include <cugl/math/CUVec2.h>
40 #include <cugl/io/CUPathname.h>
41 #include <cugl/util/CUThreadPool.h>
42 #include <condition_variable>
43 #include <mutex>
44 
45 namespace cugl {
46 
47 #pragma mark -
48 #pragma mark GestureEvent
49 
52 class GestureEvent {
53 public:
57  std::string key;
65  float error;
67  int fingers;
68 
72  GestureEvent() : fingers(0), error(0) {}
73 
82  GestureEvent(const std::string& name, float delta, int down, const Timestamp& stamp) {
83  key = name; fingers = down; error = delta; timestamp = stamp;
84  }
85 };
86 
87 
88 #pragma mark -
89 #pragma mark GestureListener
90 
113 typedef std::function<void(const GestureEvent& event, bool focus)> GestureListener;
114 
115 
116 #pragma mark -
117 #pragma mark GestureStateListener
118 
126 enum class GestureState : int {
136  UNDEFINED = 0,
143  MATCHING = 1,
151  PAUSED = 2,
160  RECORDING = 3,
168  ABORTING = 4,
174  LOADING = 5,
180  STORING = 6
181 };
182 
210 typedef std::function<void(GestureState ostate, GestureState nstate)> GestureStateListener;
211 
212 
213 #pragma mark -
214 #pragma mark GestureInput
215 
269 class GestureInput : public InputDevice {
270 protected:
276  float _tolerance;
277 
279  std::unordered_map<SDL_GestureID, std::string> _gestures;
281  std::unordered_map<std::string, SDL_GestureID> _inverses;
282 
284  std::string _recrd;
286  bool _match;
289 
291  std::shared_ptr<ThreadPool> _loader;
293  std::mutex _lock;
295  std::condition_variable _monitor;
296 
298  std::unordered_map<Uint32, GestureListener> _matchListeners;
300  std::unordered_map<Uint32, GestureStateListener> _stateListeners;
301 
311  void changeState(GestureState state, bool thread=true);
312 
338  bool read(const std::string& file, std::function<void(bool success)> callback=nullptr);
339 
360  bool store(Pathname& file, std::function<void(bool success)> callback=nullptr);
361 
362 
363 #pragma mark Constructor
364 
370  GestureInput();
371 
375  virtual ~GestureInput() { dispose(); }
376 
382  virtual void dispose() override;
383 
384 
385 #pragma mark Device State
386 public:
396  bool ready() const { return _state == GestureState::MATCHING; }
397 
403  GestureState getState() const { return _state; }
404 
418  float getTolerance() const { return _tolerance; }
419 
433  void setTolerance(float tolerance) {
434  tolerance = _tolerance;
435  }
436 
442  std::vector<std::string> getGestures() const;
443 
463  bool pause();
464 
480  bool resume();
481 
498  bool record(const std::string& key);
499 
516  bool record(const char* key) {
517  return record(std::string(key));
518  }
519 
528  bool abort();
529 
541  bool remove(const std::string& key);
542 
554  bool remove(const char* key) {
555  return remove(std::string(key));
556  }
557 
570  bool rename(const std::string& key, const std::string& name);
571 
584  bool rename(const char* key, const std::string& name) {
585  return rename(std::string(key),name);
586  }
587 
600  bool rename(const std::string& key, const char* name) {
601  return rename(key,std::string(name));
602  }
603 
616  bool rename(const char* key, const char* name) {
617  return rename(std::string(key),std::string(name));
618  }
619 
620 #pragma mark Serialization
621 
645  bool load(const std::string& file) {
646  return load(Pathname(file));
647  }
648 
673  bool load(const char* file) {
674  return load(Pathname(file));
675  }
676 
701  bool load(const Pathname& file);
702 
728  void loadAsync(const std::string& file, std::function<void(bool success)> callback) {
729  loadAsync(Pathname(file),callback);
730  }
731 
757  void loadAsync(const char* file, std::function<void(bool success)> callback) {
758  loadAsync(Pathname(file),callback);
759  }
760 
786  void loadAsync(const Pathname& file, std::function<void(bool success)> callback);
787 
812  bool loadAsset(const std::string& file);
813 
838  bool loadAsset(const char* file) {
839  return loadAsset(std::string(file));
840  }
841 
867  void loadAssetAsync(const std::string& file, std::function<void(bool success)> callback);
868 
894  void loadAssetAsync(const char* file, std::function<void(bool success)> callback) {
895  loadAssetAsync(std::string(file),callback);
896  }
897 
917  bool save(const std::string& file) {
918  Pathname path(file);
919  return store(path);
920  }
921 
941  bool save(const char* file) {
942  Pathname path(file);
943  return store(path);
944  }
945 
965  bool save(Pathname& file);
966 
987  void saveAsync(const std::string& file, std::function<void(bool success)> callback) {
988  Pathname path(file);
989  saveAsync(path, callback);
990  }
991 
1012  void saveAsync(const char* file, std::function<void(bool success)> callback) {
1013  Pathname path(file);
1014  saveAsync(path, callback);
1015  }
1016 
1037  void saveAsync(Pathname& file, std::function<void(bool success)> callback);
1038 
1039 
1040 #pragma mark Data Polling
1041 
1049  bool didMatch() const { return _match; }
1050 
1058  const std::string getKey() const { return _match ? _event.key : ""; }
1059 
1071  const float getError() const { return _match ? _event.error : -1.0f; }
1072 
1080  int getFingers() const { return _match ? _event.fingers : 0; }
1081 
1082 
1083 #pragma mark Listeners
1084 
1094  virtual bool requestFocus(Uint32 key) override;
1095 
1106  bool isListener(Uint32 key) const;
1107 
1122  const GestureListener getMatchListener(Uint32 key) const;
1123 
1137  const GestureStateListener getStateListener(Uint32 key) const;
1138 
1156  bool addMatchListener(Uint32 key, GestureListener listener);
1157 
1174  bool addStateListener(Uint32 key, GestureStateListener listener);
1175 
1191  bool removeMatchListener(Uint32 key);
1192 
1207  bool removeStateListener(Uint32 key);
1208 
1209 
1210 protected:
1211 #pragma mark Input Device
1212 
1218  virtual void clearState() override;
1219 
1231  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
1232 
1243  virtual void queryEvents(std::vector<Uint32>& eventset) override;
1244 
1245  // Apparently friends are not inherited
1246  friend class Input;
1247 };
1248 
1249 
1250 }
1251 #endif /* __CU_GESTURE_INPUT_H__ */
int getFingers() const
Definition: CUGestureInput.h:1080
std::string _recrd
Definition: CUGestureInput.h:284
GestureEvent()
Definition: CUGestureInput.h:72
std::unordered_map< std::string, SDL_GestureID > _inverses
Definition: CUGestureInput.h:281
bool addMatchListener(Uint32 key, GestureListener listener)
const GestureStateListener getStateListener(Uint32 key) const
GestureState getState() const
Definition: CUGestureInput.h:403
GestureState
Definition: CUGestureInput.h:126
virtual ~GestureInput()
Definition: CUGestureInput.h:375
void loadAssetAsync(const std::string &file, std::function< void(bool success)> callback)
int fingers
Definition: CUGestureInput.h:67
Definition: CUInput.h:77
bool loadAsset(const std::string &file)
Definition: CUTimestamp.h:61
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
bool loadAsset(const char *file)
Definition: CUGestureInput.h:838
Definition: CUGestureInput.h:52
bool load(const std::string &file)
Definition: CUGestureInput.h:645
void loadAssetAsync(const char *file, std::function< void(bool success)> callback)
Definition: CUGestureInput.h:894
std::vector< std::string > getGestures() const
void loadAsync(const std::string &file, std::function< void(bool success)> callback)
Definition: CUGestureInput.h:728
bool rename(const std::string &key, const std::string &name)
bool _match
Definition: CUGestureInput.h:286
virtual void queryEvents(std::vector< Uint32 > &eventset) override
GestureState _state
Definition: CUGestureInput.h:272
virtual bool requestFocus(Uint32 key) override
virtual void dispose() override
bool removeMatchListener(Uint32 key)
std::unordered_map< SDL_GestureID, std::string > _gestures
Definition: CUGestureInput.h:279
bool rename(const std::string &key, const char *name)
Definition: CUGestureInput.h:600
std::shared_ptr< ThreadPool > _loader
Definition: CUGestureInput.h:291
bool save(const char *file)
Definition: CUGestureInput.h:941
GestureEvent(const std::string &name, float delta, int down, const Timestamp &stamp)
Definition: CUGestureInput.h:82
bool rename(const char *key, const char *name)
Definition: CUGestureInput.h:616
bool rename(const char *key, const std::string &name)
Definition: CUGestureInput.h:584
void setTolerance(float tolerance)
Definition: CUGestureInput.h:433
std::string key
Definition: CUGestureInput.h:57
Definition: CUGestureInput.h:269
bool save(const std::string &file)
Definition: CUGestureInput.h:917
float error
Definition: CUGestureInput.h:65
virtual void clearState() override
GestureState _waits
Definition: CUGestureInput.h:274
bool record(const char *key)
Definition: CUGestureInput.h:516
void changeState(GestureState state, bool thread=true)
bool ready() const
Definition: CUGestureInput.h:396
bool store(Pathname &file, std::function< void(bool success)> callback=nullptr)
void loadAsync(const char *file, std::function< void(bool success)> callback)
Definition: CUGestureInput.h:757
std::unordered_map< Uint32, GestureListener > _matchListeners
Definition: CUGestureInput.h:298
bool load(const char *file)
Definition: CUGestureInput.h:673
std::condition_variable _monitor
Definition: CUGestureInput.h:295
std::unordered_map< Uint32, GestureStateListener > _stateListeners
Definition: CUGestureInput.h:300
void saveAsync(const char *file, std::function< void(bool success)> callback)
Definition: CUGestureInput.h:1012
bool removeStateListener(Uint32 key)
bool isListener(Uint32 key) const
std::function< void(GestureState ostate, GestureState nstate)> GestureStateListener
Definition: CUGestureInput.h:210
bool record(const std::string &key)
const std::string getKey() const
Definition: CUGestureInput.h:1058
const GestureListener getMatchListener(Uint32 key) const
GestureEvent _event
Definition: CUGestureInput.h:288
Definition: CUAnimationNode.h:52
bool didMatch() const
Definition: CUGestureInput.h:1049
float getTolerance() const
Definition: CUGestureInput.h:418
void saveAsync(const std::string &file, std::function< void(bool success)> callback)
Definition: CUGestureInput.h:987
float _tolerance
Definition: CUGestureInput.h:276
std::mutex _lock
Definition: CUGestureInput.h:293
Definition: CUPathname.h:85
bool addStateListener(Uint32 key, GestureStateListener listener)
const float getError() const
Definition: CUGestureInput.h:1071
bool read(const std::string &file, std::function< void(bool success)> callback=nullptr)
Timestamp timestamp
Definition: CUGestureInput.h:55
Definition: CUInput.h:298
std::function< void(const GestureEvent &event, bool focus)> GestureListener
Definition: CUGestureInput.h:113