Empirical
KeypressManager.h
Go to the documentation of this file.
1 
43 #ifndef EMP_WEB_KEYPRESS_MANAGER_H
44 #define EMP_WEB_KEYPRESS_MANAGER_H
45 
46 #include <functional>
47 #include <map>
48 
49 #include "events.h"
50 #include "JSWrap.h"
51 
52 namespace emp {
53 namespace web {
54 
55  using namespace std::placeholders;
56 
58  private:
59  std::map<int, std::function<bool(const KeyboardEvent &)> > fun_map;
60  int next_order; // Ordering to use if not specified (always last)
61  uint32_t callback_id;
62 
63  bool DoCallback(const KeyboardEvent & evt_info) {
64  bool handled = false;
65  for (auto fun_entry : fun_map) {
66  if (fun_entry.second(evt_info) == true) {
67  handled = true;
68  break;
69  }
70  }
71 
72  return handled;
73  };
74 
75 
76  public:
77  KeypressManager() : next_order(0) {
78  std::function<bool(const KeyboardEvent &)> callback_fun =
79  std::bind( &KeypressManager::DoCallback, this, _1 );
80  callback_id = JSWrap( callback_fun );
81 
82  EM_ASM_ARGS({
83  document.addEventListener('keydown', function(evt) {
84  var is_used = emp.Callback($0, evt);
85  if (is_used == 1) evt.preventDefault();
86  }, false);
87 
88  }, callback_id);
89  }
91  }
92 
93  int GetFunCount() const { return (int) fun_map.size(); }
94  int GetNextOrder() const { return next_order; }
95 
100  void AddKeydownCallback(std::function<bool(const KeyboardEvent &)> cb_fun, int order=-1)
101  {
102  if (order == -1) order = next_order;
103  if (order >= next_order) next_order = order+1;
104 
105  fun_map[order] = cb_fun;
106  }
107 
110  void AddKeydownCallback(char key, std::function<void()> cb_fun, int order=-1)
111  {
112  if (order == -1) order = next_order;
113  if (order >= next_order) next_order = order+1;
114 
115  fun_map[order] =
116  [key, cb_fun](const KeyboardEvent & evt)
117  { if (evt.keyCode == key) { cb_fun(); return true; } return false; };
118  }
119 
122  void AddKeydownCallback(const std::string & key_set, const std::function<void()> & cb_fun,
123  int order)
124  {
125  if (order >= next_order) next_order = order+1;
126 
127  fun_map[order] =
128  [key_set, cb_fun](const KeyboardEvent & evt)
129  { if (key_set.find((char)evt.keyCode) == std::string::npos) return false; cb_fun(); return true;};
130  }
131 
133  void AddKeydownCallback(const std::string & key_set, const std::function<void()> & cb_fun)
134  {
135  AddKeydownCallback(key_set, cb_fun, next_order);
136  }
137  };
138 
139 };
140 };
141 
142 #endif
~KeypressManager()
Definition: KeypressManager.h:90
void AddKeydownCallback(char key, std::function< void()> cb_fun, int order=-1)
Definition: KeypressManager.h:110
int GetNextOrder() const
Definition: KeypressManager.h:94
Keyboard-specific information about web events.
Definition: events.h:99
KeypressManager()
Definition: KeypressManager.h:77
void AddKeydownCallback(const std::string &key_set, const std::function< void()> &cb_fun)
Provide a whole set of keys that should all trigger the same function; use default ordering...
Definition: KeypressManager.h:133
int GetFunCount() const
Definition: KeypressManager.h:93
Event handlers that use JQuery.
Wrap a C++ function and convert it to an integer that can be called from Javascript.
If we are in emscripten, make sure to include the header.
Definition: array.h:37
void AddKeydownCallback(const std::string &key_set, const std::function< void()> &cb_fun, int order)
Definition: KeypressManager.h:122
Definition: KeypressManager.h:57
void AddKeydownCallback(std::function< bool(const KeyboardEvent &)> cb_fun, int order=-1)
Definition: KeypressManager.h:100