Simulant  21.12-1303
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 #include "input/input_state.h"
8 
9 namespace smlt {
10 
11 /* EventListeners can directly handle events received by the window
12  * (e.g. input)
13  *
14  * An EventListener must be properly registered/unregistered with the window
15  * with window->register_event_listener() and window->unregister_event_listener()
16  */
17 
18 typedef uint32_t TouchPointID;
19 
20 enum TouchEventType {
21  TOUCH_EVENT_TYPE_FINGER_DOWN,
22  TOUCH_EVENT_TYPE_FINGER_UP,
23  TOUCH_EVENT_TYPE_FINGER_MOVE
24 };
25 
26 struct TouchEvent {
27  TouchEventType type;
28  TouchPointID touch_id;
29  Vec2 coord;
30  Vec2 normalized_coord;
31  Vec2 movement;
32  float pressure;
33 };
34 
35 enum KeyEventType {
36  KEY_EVENT_TYPE_KEY_DOWN,
37  KEY_EVENT_TYPE_KEY_UP
38 };
39 
41  bool lshift = false;
42  bool rshift = false;
43  bool lctrl = false;
44  bool rctrl = false;
45  bool lalt = false;
46  bool ralt = false;
47  bool lsuper = false;
48  bool rsuper = false;
49  bool num_lock = false;
50  bool caps_lock = false;
51  bool mode = false; // AltGr
52 
53  bool ctrl() const { return lctrl || rctrl; }
54  bool shift() const { return lshift || rshift; }
55  bool alt() const { return lalt || ralt; }
56  bool super() const { return lsuper || rsuper; }
57 };
58 
59 struct KeyEvent {
60  KeyEventType type;
61  KeyboardCode keyboard_code;
62  ModifierKeyState modifiers;
63 };
64 
65 enum MouseEventType : uint8_t {
66  MOUSE_EVENT_TYPE_BUTTON_DOWN,
67  MOUSE_EVENT_TYPE_BUTTON_UP,
68  MOUSE_EVENT_TYPE_MOTION
69 };
70 
71 struct MouseEvent {
72  MouseID id;
73  MouseEventType type;
74  uint8_t button;
75  int32_t x;
76  int32_t y;
77  bool is_touch_device;
78 };
79 
80 enum GameControllerEventType {
81  GAME_CONTROLLER_EVENT_TYPE_BUTTON_DOWN,
82  GAME_CONTROLLER_EVENT_TYPE_BUTTON_UP
83 };
84 
86  GameControllerIndex index;
87  GameControllerEventType type;
88  JoystickButton button;
89 };
90 
92 public:
93  EventListener() {}
94  virtual ~EventListener() {}
95 
96  void handle_touch_begin(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y, float pressure);
97  void handle_touch_end(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y);
98  void handle_touch_move(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y, float dx, float dy);
99 
100  void handle_key_down(Window* window, KeyboardCode code, ModifierKeyState modifiers);
101  void handle_key_up(Window* window, KeyboardCode code, ModifierKeyState modifiers);
102 
103  void handle_mouse_down(Window* window, MouseID id, uint8_t mouse_button, int32_t x, int32_t y, bool touch_device);
104  void handle_mouse_up(Window* window, MouseID id, uint8_t mouse_button, int32_t x, int32_t y, bool touch_device);
105  void handle_mouse_move(Window* window, MouseID id, int32_t x, int32_t y, bool touch_device);
106 
107  void handle_controller_button_down(GameControllerIndex controller, JoystickButton button);
108  void handle_controller_button_up(GameControllerIndex controller, JoystickButton button);
109 
110 private:
111  virtual void on_key_down(const KeyEvent& evt) {
112  _S_UNUSED(evt);
113  }
114 
115  virtual void on_key_up(const KeyEvent& evt) {
116  _S_UNUSED(evt);
117  }
118 
119  virtual void on_mouse_down(const MouseEvent& evt) {
120  _S_UNUSED(evt);
121  }
122 
123  virtual void on_mouse_up(const MouseEvent& evt) {
124  _S_UNUSED(evt);
125  }
126 
127  virtual void on_mouse_move(const MouseEvent& evt) {
128  _S_UNUSED(evt);
129  }
130 
131  virtual void on_touch_begin(const TouchEvent& evt) {
132  _S_UNUSED(evt);
133  }
134 
135  virtual void on_touch_end(const TouchEvent& evt) {
136  _S_UNUSED(evt);
137  }
138 
139  virtual void on_touch_move(const TouchEvent& evt) {
140  _S_UNUSED(evt);
141  }
142 
143  virtual void on_game_controller_button_down(const GameControllerEvent& evt) {
144  _S_UNUSED(evt);
145  }
146 
147  virtual void on_game_controller_button_up(const GameControllerEvent& evt) {
148  _S_UNUSED(evt);
149  }
150 
151  virtual void on_window_focus() {}
152  virtual void on_window_blur() {}
153  virtual void on_window_minimize() {}
154  virtual void on_window_restore() {}
155 };
156 
158 public:
160  virtual ~EventListenerManager() {}
161 
162  void register_event_listener(EventListener* listener);
163  void unregister_event_listener(EventListener* listener);
164 
165  void each_event_listener(std::function<void (EventListener*)> callback);
166 
167 private:
168  std::list<EventListener*> listeners_;
169 };
170 
171 }
smlt::EventListener
Definition: event_listener.h:91
smlt::GameControllerEvent
Definition: event_listener.h:85
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:68
smlt::KeyEvent
Definition: event_listener.h:59
smlt::ModifierKeyState
Definition: event_listener.h:40
smlt::EventListenerManager
Definition: event_listener.h:157
smlt::TouchEvent
Definition: event_listener.h:26
smlt::MouseEvent
Definition: event_listener.h:71
smlt::Vec2
Definition: vec2.h:16