| | 1 | | using System; |
| | 2 | | using static SDL2.SDL_events; |
| | 3 | |
|
| | 4 | | namespace Imagini |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Allows subscribing to the app events. |
| | 8 | | /// </summary> |
| | 9 | | public class Events |
| | 10 | | { |
| | 11 | | private static Events _global = new Events(); |
| | 12 | | /// <summary> |
| | 13 | | /// Returns the global event handler. |
| | 14 | | /// </summary> |
| | 15 | | public static Events Global => _global; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Provides access to window events. |
| | 19 | | /// </summary> |
| | 20 | | public readonly WindowEvents Window = new WindowEvents(); |
| | 21 | | /// <summary> |
| | 22 | | /// Provides access to IME input events. |
| | 23 | | /// </summary> |
| | 24 | | public readonly InputEvents Input = new InputEvents(); |
| | 25 | | /// <summary> |
| | 26 | | /// Provides access to keyboard events. |
| | 27 | | /// </summary> |
| | 28 | | public readonly KeyboardEvents Keyboard = new KeyboardEvents(); |
| | 29 | | /// <summary> |
| | 30 | | /// Provides access to mouse events. |
| | 31 | | /// </summary> |
| | 32 | | public readonly MouseEvents Mouse = new MouseEvents(); |
| | 33 | | /// <summary> |
| | 34 | | /// Provides access to joystick events. |
| | 35 | | /// </summary> |
| | 36 | | public readonly JoystickEvents Joystick = new JoystickEvents(); |
| | 37 | | /// <summary> |
| | 38 | | /// Provides access to controller events. |
| | 39 | | /// </summary> |
| | 40 | | public readonly ControllerEvents Controller = new ControllerEvents(); |
| | 41 | | /// <summary> |
| | 42 | | /// Provides access to touch events. |
| | 43 | | /// </summary> |
| | 44 | | public readonly TouchEvents Touch = new TouchEvents(); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Returns the owner of this event queue, or null if this queue is global. |
| | 48 | | /// </summary> |
| | 49 | | public AppBase Owner { get; private set; } |
| | 50 | |
|
| | 51 | | internal Events() { } |
| | 52 | | internal Events(AppBase owner) => Owner = owner; |
| | 53 | |
|
| | 54 | | internal unsafe void Process(SDL_Event e) |
| | 55 | | { |
| | 56 | | switch ((SDL_EventType)e.type) |
| | 57 | | { |
| | 58 | | // Window |
| | 59 | | case SDL_EventType.SDL_WINDOWEVENT: |
| | 60 | | Window.Fire(*((SDL_WindowEvent*)&e)); |
| | 61 | | break; |
| | 62 | | // IME input |
| | 63 | | case SDL_EventType.SDL_TEXTEDITING: |
| | 64 | | Input.Fire(*((SDL_TextEditingEvent*)&e)); |
| | 65 | | break; |
| | 66 | | case SDL_EventType.SDL_TEXTINPUT: |
| | 67 | | Input.Fire(*((SDL_TextInputEvent*)&e)); |
| | 68 | | break; |
| | 69 | | // Keyboard |
| | 70 | | case SDL_EventType.SDL_KEYDOWN: |
| | 71 | | case SDL_EventType.SDL_KEYUP: |
| | 72 | | Keyboard.Fire(*((SDL_KeyboardEvent*)&e)); |
| | 73 | | // the keyboard is on fire, we should probably take a break |
| | 74 | | break; |
| | 75 | | // Mouse |
| | 76 | | case SDL_EventType.SDL_MOUSEMOTION: |
| | 77 | | Mouse.Fire(*((SDL_MouseMotionEvent*)&e)); |
| | 78 | | break; |
| | 79 | | case SDL_EventType.SDL_MOUSEBUTTONDOWN: |
| | 80 | | case SDL_EventType.SDL_MOUSEBUTTONUP: |
| | 81 | | Mouse.Fire(*((SDL_MouseButtonEvent*)&e)); |
| | 82 | | break; |
| | 83 | | case SDL_EventType.SDL_MOUSEWHEEL: |
| | 84 | | Mouse.Fire(*((SDL_MouseWheelEvent*)&e)); |
| | 85 | | break; |
| | 86 | | // Joystick |
| | 87 | | case SDL_EventType.SDL_JOYAXISMOTION: |
| | 88 | | Joystick.Fire(*((SDL_JoyAxisEvent*)&e)); |
| | 89 | | break; |
| | 90 | | case SDL_EventType.SDL_JOYBALLMOTION: |
| | 91 | | Joystick.Fire(*((SDL_JoyBallEvent*)&e)); |
| | 92 | | break; |
| | 93 | | case SDL_EventType.SDL_JOYHATMOTION: |
| | 94 | | Joystick.Fire(*((SDL_JoyHatEvent*)&e)); |
| | 95 | | break; |
| | 96 | | case SDL_EventType.SDL_JOYBUTTONUP: |
| | 97 | | case SDL_EventType.SDL_JOYBUTTONDOWN: |
| | 98 | | Joystick.Fire(*((SDL_JoyButtonEvent*)&e)); |
| | 99 | | break; |
| | 100 | | case SDL_EventType.SDL_JOYDEVICEADDED: |
| | 101 | | case SDL_EventType.SDL_JOYDEVICEREMOVED: |
| | 102 | | Joystick.Fire(*((SDL_JoyDeviceEvent*)&e)); |
| | 103 | | break; |
| | 104 | | // Controller |
| | 105 | | case SDL_EventType.SDL_CONTROLLERAXISMOTION: |
| | 106 | | Controller.Fire(*((SDL_ControllerAxisEvent*)&e)); |
| | 107 | | break; |
| | 108 | | case SDL_EventType.SDL_CONTROLLERBUTTONDOWN: |
| | 109 | | case SDL_EventType.SDL_CONTROLLERBUTTONUP: |
| | 110 | | Controller.Fire(*((SDL_ControllerButtonEvent*)&e)); |
| | 111 | | break; |
| | 112 | | case SDL_EventType.SDL_CONTROLLERDEVICEADDED: |
| | 113 | | case SDL_EventType.SDL_CONTROLLERDEVICEREMOVED: |
| | 114 | | Controller.Fire(*((SDL_ControllerDeviceEvent*)&e)); |
| | 115 | | break; |
| | 116 | | // Touch |
| | 117 | | case SDL_EventType.SDL_FINGERMOTION: |
| | 118 | | case SDL_EventType.SDL_FINGERDOWN: |
| | 119 | | case SDL_EventType.SDL_FINGERUP: |
| | 120 | | // don't touch the fire |
| | 121 | | Touch.Fire(*((SDL_TouchFingerEvent*)&e)); |
| | 122 | | break; |
| | 123 | | } |
| | 124 | | } |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Contains window-related events. |
| | 128 | | /// </summary> |
| | 129 | | public class WindowEvents |
| | 130 | | { |
| | 131 | | /// <summary> |
| | 132 | | /// Fired when a window changes state. |
| | 133 | | /// </summary> |
| | 134 | | public event EventHandler<WindowStateChangeEventArgs> StateChanged; |
| | 135 | | internal WindowEvents() { } |
| | 136 | | internal void Fire(SDL_WindowEvent e) => |
| | 137 | | StateChanged?.Invoke(this, new WindowStateChangeEventArgs(e)); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Contains input-related events. |
| | 142 | | /// </summary> |
| | 143 | | public class InputEvents |
| | 144 | | { |
| | 145 | | /// <summary> |
| | 146 | | /// Fires when the editing text changes. |
| | 147 | | /// </summary> |
| | 148 | | public EventHandler<TextEditingEventArgs> OnTextEdit; |
| | 149 | | /// <summary> |
| | 150 | | /// Fires when the editing text is entered. |
| | 151 | | /// </summary> |
| | 152 | | public EventHandler<TextInputEventArgs> OnTextInput; |
| | 153 | |
|
| | 154 | | internal void Fire(SDL_TextEditingEvent e) => |
| | 155 | | OnTextEdit?.Invoke(this, new TextEditingEventArgs(e)); |
| | 156 | |
|
| | 157 | | internal void Fire(SDL_TextInputEvent e) => |
| | 158 | | OnTextInput?.Invoke(this, new TextInputEventArgs(e)); |
| | 159 | | } |
| | 160 | |
|
| | 161 | | /// <summary> |
| | 162 | | /// Contains keyboard-related events. |
| | 163 | | /// </summary> |
| | 164 | | public class KeyboardEvents |
| | 165 | | { |
| | 166 | | /// <summary> |
| | 167 | | /// Fired when a keyboard key is pressed. |
| | 168 | | /// </summary> |
| | 169 | | public event EventHandler<KeyboardEventArgs> KeyPressed; |
| | 170 | | /// <summary> |
| | 171 | | /// Fired when a keyboard key is released. |
| | 172 | | /// </summary> |
| | 173 | | public event EventHandler<KeyboardEventArgs> KeyReleased; |
| | 174 | |
|
| | 175 | | internal KeyboardEvents() { } |
| | 176 | | internal void Fire(SDL_KeyboardEvent e) |
| | 177 | | { |
| | 178 | | if (e.type == (uint)SDL_EventType.SDL_KEYDOWN) |
| | 179 | | KeyPressed?.Invoke(this, new KeyboardEventArgs(e)); |
| | 180 | | else KeyReleased?.Invoke(this, new KeyboardEventArgs(e)); |
| | 181 | | } |
| | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// Contains mouse-related events. |
| | 186 | | /// </summary> |
| | 187 | | public class MouseEvents |
| | 188 | | { |
| | 189 | | /// <summary> |
| | 190 | | /// Fires when a mouse is moved. |
| | 191 | | /// </summary> |
| | 192 | | public event EventHandler<MouseMoveEventArgs> MouseMoved; |
| | 193 | | /// <summary> |
| | 194 | | /// Fires when a mouse button is pressed. |
| | 195 | | /// </summary> |
| | 196 | | public event EventHandler<MouseButtonEventArgs> MouseButtonPressed; |
| | 197 | | /// <summary> |
| | 198 | | /// Fires when a mouse button is released. |
| | 199 | | /// </summary> |
| | 200 | | public event EventHandler<MouseButtonEventArgs> MouseButtonReleased; |
| | 201 | |
|
| | 202 | | public event EventHandler<MouseWheelEventArgs> MouseWheelScrolled; |
| | 203 | |
|
| | 204 | | internal void Fire(SDL_MouseMotionEvent e) => |
| | 205 | | MouseMoved?.Invoke(this, new MouseMoveEventArgs(e)); |
| | 206 | |
|
| | 207 | | internal void Fire(SDL_MouseButtonEvent e) |
| | 208 | | { |
| | 209 | | if (e.type == (uint)SDL_EventType.SDL_MOUSEBUTTONDOWN) |
| | 210 | | MouseButtonPressed?.Invoke(this, new MouseButtonEventArgs(e)); |
| | 211 | | else |
| | 212 | | MouseButtonReleased?.Invoke(this, new MouseButtonEventArgs(e)); |
| | 213 | | } |
| | 214 | |
|
| | 215 | | internal void Fire(SDL_MouseWheelEvent e) => |
| | 216 | | MouseWheelScrolled?.Invoke(this, new MouseWheelEventArgs(e)); |
| | 217 | | } |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Contains joystick-related events. |
| | 221 | | /// </summary> |
| | 222 | | public class JoystickEvents |
| | 223 | | { |
| | 224 | | /// <summary> |
| | 225 | | /// Fires when a joystick axis is moved. |
| | 226 | | /// </summary> |
| | 227 | | public event EventHandler<JoyAxisMotionEventArgs> AxisMoved; |
| | 228 | | /// <summary> |
| | 229 | | /// Fires when a joystick trackball is moved. |
| | 230 | | /// </summary> |
| | 231 | | public event EventHandler<JoyBallMotionEventArgs> BallMoved; |
| | 232 | | /// <summary> |
| | 233 | | /// Fires when a joystick hat is moved. |
| | 234 | | /// </summary> |
| | 235 | | public event EventHandler<JoyHatMotionEventArgs> HatMoved; |
| | 236 | | /// <summary> |
| | 237 | | /// Fires when a joystick button is pressed. |
| | 238 | | /// </summary> |
| | 239 | | public event EventHandler<JoyButtonEventArgs> ButtonPressed; |
| | 240 | | /// <summary> |
| | 241 | | /// Fires when a joystick button is released. |
| | 242 | | /// </summary> |
| | 243 | | public event EventHandler<JoyButtonEventArgs> ButtonReleased; |
| | 244 | |
|
| | 245 | | /// <summary> |
| | 246 | | /// Fires when a joystick is connected or disconnected. |
| | 247 | | /// </summary> |
| | 248 | | /// <remarks>Fires only on <see cref="Events.Global" />.</remarks> |
| | 249 | | public event EventHandler<JoyDeviceStateEventArgs> StateChanged; |
| | 250 | |
|
| | 251 | | internal void Fire(SDL_JoyAxisEvent e) => |
| | 252 | | AxisMoved?.Invoke(this, new JoyAxisMotionEventArgs(e)); |
| | 253 | |
|
| | 254 | | internal void Fire(SDL_JoyBallEvent e) => |
| | 255 | | BallMoved?.Invoke(this, new JoyBallMotionEventArgs(e)); |
| | 256 | |
|
| | 257 | | internal void Fire(SDL_JoyHatEvent e) => |
| | 258 | | HatMoved?.Invoke(this, new JoyHatMotionEventArgs(e)); |
| | 259 | |
|
| | 260 | | internal void Fire(SDL_JoyButtonEvent e) |
| | 261 | | { |
| | 262 | | if (e.type == (uint)SDL_EventType.SDL_JOYBUTTONDOWN) |
| | 263 | | ButtonPressed?.Invoke(this, new JoyButtonEventArgs(e)); |
| | 264 | | else |
| | 265 | | ButtonReleased?.Invoke(this, new JoyButtonEventArgs(e)); |
| | 266 | | } |
| | 267 | |
|
| | 268 | | internal void Fire(SDL_JoyDeviceEvent e) => |
| | 269 | | StateChanged?.Invoke(this, new JoyDeviceStateEventArgs(e)); |
| | 270 | | } |
| | 271 | | } |
| | 272 | |
|
| | 273 | | /// <summary> |
| | 274 | | /// Contains controller-related events. |
| | 275 | | /// </summary> |
| | 276 | | public class ControllerEvents |
| | 277 | | { |
| | 278 | | /// <summary> |
| | 279 | | /// Fires when a controller axis is moved. |
| | 280 | | /// </summary> |
| | 281 | | public event EventHandler<ControllerAxisEventArgs> AxisMoved; |
| | 282 | | /// <summary> |
| | 283 | | /// Fires when a controller button is pressed. |
| | 284 | | /// </summary> |
| | 285 | | public event EventHandler<ControllerButtonEventArgs> ButtonPressed; |
| | 286 | | /// <summary> |
| | 287 | | /// Fires when a controller button is released. |
| | 288 | | /// </summary> |
| | 289 | | public event EventHandler<ControllerButtonEventArgs> ButtonReleased; |
| | 290 | | /// <summary> |
| | 291 | | /// Fires when a joystick is connected or disconnected. |
| | 292 | | /// </summary> |
| | 293 | | /// <remarks>Fires only on <see cref="Events.Global" />.</remarks> |
| | 294 | | public event EventHandler<ControllerDeviceStateEventArgs> StateChanged; |
| | 295 | |
|
| | 296 | | internal void Fire(SDL_ControllerAxisEvent e) => |
| | 297 | | AxisMoved?.Invoke(this, new ControllerAxisEventArgs(e)); |
| | 298 | |
|
| | 299 | | internal void Fire(SDL_ControllerButtonEvent e) |
| | 300 | | { |
| | 301 | | if (e.type == (uint)SDL_EventType.SDL_CONTROLLERBUTTONDOWN) |
| | 302 | | ButtonPressed?.Invoke(this, new ControllerButtonEventArgs(e)); |
| | 303 | | else |
| | 304 | | ButtonReleased?.Invoke(this, new ControllerButtonEventArgs(e)); |
| | 305 | | } |
| | 306 | |
|
| | 307 | | internal void Fire(SDL_ControllerDeviceEvent e) => |
| | 308 | | StateChanged?.Invoke(this, new ControllerDeviceStateEventArgs(e)); |
| | 309 | | } |
| | 310 | |
|
| | 311 | | public class TouchEvents |
| | 312 | | { |
| | 313 | | /// <summary> |
| | 314 | | /// Fires when a finger is moved. |
| | 315 | | /// </summary> |
| | 316 | | public event EventHandler<TouchFingerEventArgs> FingerMoved; |
| | 317 | | /// <summary> |
| | 318 | | /// Fires on finger press. |
| | 319 | | /// </summary> |
| | 320 | | public event EventHandler<TouchFingerEventArgs> FingerPressed; |
| | 321 | | /// <summary> |
| | 322 | | /// Fires on finger release. |
| | 323 | | /// </summary> |
| | 324 | | public event EventHandler<TouchFingerEventArgs> FingerReleased; |
| | 325 | |
|
| | 326 | | internal void Fire(SDL_TouchFingerEvent e) |
| | 327 | | { |
| 3 | 328 | | var args = new TouchFingerEventArgs(e); |
| 3 | 329 | | switch((SDL_EventType)e.type) |
| | 330 | | { |
| | 331 | | case SDL_EventType.SDL_FINGERMOTION: |
| 2 | 332 | | FingerMoved?.Invoke(this, args); break; |
| | 333 | | case SDL_EventType.SDL_FINGERDOWN: |
| 2 | 334 | | FingerPressed?.Invoke(this, args); break; |
| | 335 | | case SDL_EventType.SDL_FINGERUP: |
| 1 | 336 | | FingerReleased?.Invoke(this, args); break; |
| | 337 | | } |
| 1 | 338 | | } |
| | 339 | | } |
| | 340 | | } |