Simulant  21.09-46
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
event_listener.h
1 #pragma once
2 
3 #include <list>
4 #include "types.h"
5 #include "keycodes.h"
6 #include "macros.h"
7 
8 namespace smlt {
9 
10 /* EventListeners can directly handle events received by the window
11  * (e.g. input)
12  *
13  * An EventListener must be properly registered/unregistered with the window
14  * with window->register_event_listener() and window->unregister_event_listener()
15  */
16 
17 typedef uint32_t TouchPointID;
18 
19 enum TouchEventType {
20  TOUCH_EVENT_TYPE_FINGER_DOWN,
21  TOUCH_EVENT_TYPE_FINGER_UP,
22  TOUCH_EVENT_TYPE_FINGER_MOVE
23 };
24 
25 struct TouchEvent {
26  TouchEventType type;
27  TouchPointID touch_id;
28  Vec2 coord;
29  Vec2 normalized_coord;
30  Vec2 movement;
31  float pressure;
32 };
33 
34 enum KeyEventType {
35  KEY_EVENT_TYPE_KEY_DOWN,
36  KEY_EVENT_TYPE_KEY_UP
37 };
38 
40  bool lshift = false;
41  bool rshift = false;
42  bool lctrl = false;
43  bool rctrl = false;
44  bool lalt = false;
45  bool ralt = false;
46  bool lsuper = false;
47  bool rsuper = false;
48  bool num_lock = false;
49  bool caps_lock = false;
50  bool mode = false; // AltGr
51 
52  bool ctrl() const { return lctrl || rctrl; }
53  bool shift() const { return lshift || rshift; }
54  bool alt() const { return lalt || ralt; }
55  bool super() const { return lsuper || rsuper; }
56 };
57 
58 struct KeyEvent {
59  KeyEventType type;
60  KeyboardCode keyboard_code;
61  ModifierKeyState modifiers;
62 };
63 
65 public:
66  EventListener() {}
67  virtual ~EventListener() {}
68 
69  void handle_touch_begin(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y, float pressure);
70  void handle_touch_end(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y);
71  void handle_touch_move(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y, float dx, float dy);
72 
73  void handle_key_down(Window* window, KeyboardCode code, ModifierKeyState modifiers);
74  void handle_key_up(Window* window, KeyboardCode code, ModifierKeyState modifiers);
75 
76 private:
77  virtual void on_key_down(const KeyEvent& evt) {
78  _S_UNUSED(evt);
79  }
80 
81  virtual void on_key_up(const KeyEvent& evt) {
82  _S_UNUSED(evt);
83  }
84 
85  virtual void on_touch_begin(const TouchEvent& evt) {
86  _S_UNUSED(evt);
87  }
88 
89  virtual void on_touch_end(const TouchEvent& evt) {
90  _S_UNUSED(evt);
91  }
92 
93  virtual void on_touch_move(const TouchEvent& evt) {
94  _S_UNUSED(evt);
95  }
96 
97  virtual void on_window_focus() {}
98  virtual void on_window_blur() {}
99  virtual void on_window_minimize() {}
100  virtual void on_window_restore() {}
101 };
102 
104 public:
106  virtual ~EventListenerManager() {}
107 
108  void register_event_listener(EventListener* listener);
109  void unregister_event_listener(EventListener* listener);
110 
111  void each_event_listener(std::function<void (EventListener*)> callback);
112 
113 private:
114  std::list<EventListener*> listeners_;
115 };
116 
117 }
smlt::EventListener
Definition: event_listener.h:64
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:90
smlt::KeyEvent
Definition: event_listener.h:58
smlt::ModifierKeyState
Definition: event_listener.h:39
smlt::EventListenerManager
Definition: event_listener.h:103
smlt::TouchEvent
Definition: event_listener.h:25
smlt::Vec2
Definition: vec2.h:13