1 /** 2 * Copyright © Novelate 2020 3 * License: MIT (https://github.com/Novelate/NovelateEngine/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 * Website: https://novelate.com/ 6 * ------ 7 * Novelate is a free and open-source visual novel engine and framework written in the D programming language. 8 * It can be used freely for both personal and commercial projects. 9 * ------ 10 * Module Description: 11 * This module is used for internal definitions that external definitions don't need implementation for. 12 */ 13 module novelate.external.core; 14 15 /// A 2d float vector. 16 struct FloatVector 17 { 18 /// The x value. 19 float x; 20 /// The y value. 21 float y; 22 } 23 24 /// A 2d 32 bit uint vector. 25 struct UintVector 26 { 27 /// The x value. 28 uint x; 29 /// The y value. 30 uint y; 31 } 32 33 /// A 2d 32 bit int vector. 34 struct IntVector 35 { 36 /// The x value. 37 int x; 38 /// The y value. 39 int y; 40 } 41 42 /// A paint struct 43 struct Paint 44 { 45 /// The red channel. 46 ubyte r; 47 /// The green channel. 48 ubyte g; 49 /// The blue channel. 50 ubyte b; 51 /// The alpha channel. 52 ubyte a; 53 } 54 55 /// Enumeration of keyboard keys. 56 enum KeyboardKey 57 { 58 /// Unhandled key 59 unknown = -1, 60 /// The A key 61 a = 0, 62 /// The B key 63 b, 64 /// The C key 65 c, 66 /// The D key 67 d, 68 /// The E key 69 e, 70 /// The F key 71 f, 72 /// The G key 73 g, 74 /// The H key 75 h, 76 /// The I key 77 i, 78 /// The J key 79 j, 80 /// The K key 81 k, 82 /// The L key 83 l, 84 /// The M key 85 m, 86 /// The N key 87 n, 88 /// The O key 89 o, 90 /// The P key 91 p, 92 /// The Q key 93 q, 94 /// The R key 95 r, 96 /// The S key 97 s, 98 /// The T key 99 t, 100 /// The U key 101 u, 102 /// The V key 103 v, 104 /// The W key 105 w, 106 /// The X key 107 x, 108 /// The Y key 109 y, 110 /// The Z key 111 z, 112 /// The 0 key 113 num0, 114 /// The 1 key 115 num1, 116 /// The 2 key 117 num2, 118 /// The 3 key 119 num3, 120 /// The 4 key 121 num4, 122 /// The 5 key 123 num5, 124 /// The 6 key 125 num6, 126 /// The 7 key 127 num7, 128 /// The 8 key 129 num8, 130 /// The 9 key 131 num9, 132 /// The Escape key 133 escape, 134 /// The left Control key 135 LControl, 136 /// The left Shift key 137 LShift, 138 /// The left Alt key 139 LAlt, 140 /// The left OS specific key: window (Windows and Linux), apple (MacOS X), ... 141 LSystem, 142 /// The right Control key 143 RControl, 144 /// The right Shift key 145 RShift, 146 /// The right Alt key 147 RAlt, 148 /// The right OS specific key: window (Windows and Linux), apple (MacOS X), ... 149 RSystem, 150 /// The Menu key 151 menu, 152 /// The [ key 153 LBracket, 154 /// The ] key 155 RBracket, 156 /// The ; key 157 semiColon, 158 /// The , key 159 comma, 160 /// The . key 161 period, 162 /// The ' key 163 quote, 164 /// The / key 165 slash, 166 /// The \ key 167 backSlash, 168 /// The ~ key 169 tilde, 170 /// The = key 171 equal, 172 /// The - key 173 dash, 174 /// The Space key 175 space, 176 /// The Return key 177 returnKey, 178 /// The Backspace key 179 backSpace, 180 /// The Tabulation key 181 tab, 182 /// The Page up key 183 pageUp, 184 /// The Page down key 185 pageDown, 186 /// The End key 187 end, 188 /// The Home key 189 home, 190 /// The Insert key 191 insert, 192 /// The Delete key 193 deleteKey, 194 /// The + key 195 add, 196 /// The - key 197 subtract, 198 /// The * key 199 multiply, 200 /// The / key 201 divide, 202 /// Left arrow 203 left, 204 /// Right arrow 205 right, 206 /// Up arrow 207 up, 208 /// Down arrow 209 down, 210 /// The numpad 0 key 211 numpad0, 212 /// The numpad 1 key 213 numpad1, 214 /// The numpad 2 key 215 numpad2, 216 /// The numpad 3 key 217 numpad3, 218 /// The numpad 4 key 219 numpad4, 220 /// The numpad 5 key 221 numpad5, 222 /// The numpad 6 key 223 numpad6, 224 /// The numpad 7 key 225 numpad7, 226 /// The numpad 8 key 227 numpad8, 228 /// The numpad 9 key 229 numpad9, 230 /// The F1 key 231 f1, 232 /// The F2 key 233 f2, 234 /// The F3 key 235 f3, 236 /// The F4 key 237 f4, 238 /// The F5 key 239 f5, 240 /// The F6 key 241 f6, 242 /// The F7 key 243 f7, 244 /// The F8 key 245 f8, 246 /// The F9 key 247 f9, 248 /// The F10 key 249 f10, 250 /// The F11 key 251 f11, 252 /// The F12 key 253 f12, 254 /// The F13 key 255 f13, 256 /// The F14 key 257 f14, 258 /// The F15 key 259 f15, 260 /// The Pause key 261 pause 262 } 263 264 /// Enumeration of mouse buttons. 265 enum MouseButton 266 { 267 /// The left mouse button 268 left, 269 /// The right mouse button 270 right, 271 /// The middle (wheel) mouse button 272 middle, 273 /// The first extra mouse button 274 extraButton1, 275 /// The second extra mouse button 276 extraButton2, 277 } 278 279 /// Enumeration of event types. 280 enum ExternalEventType 281 { 282 /// The window requested to be closed (no data) 283 closed, 284 /// The window was resized (data in event.size) 285 resized, 286 /// The window lost the focus (no data) 287 lostFocus, 288 /// The window gained the focus (no data) 289 gainedFocus, 290 /// A character was entered (data in event.text) 291 textEntered, 292 /// A key was pressed (data in event.key) 293 keyPressed, 294 /// A key was released (data in event.key) 295 keyReleased, 296 /// The mouse wheel was scrolled (data in event.mouseWheel) 297 mouseWheelMoved, 298 /// A mouse button was pressed (data in event.mouseButton) 299 mouseButtonPressed, 300 /// A mouse button was released (data in event.mouseButton) 301 mouseButtonReleased, 302 /// The mouse cursor moved (data in event.mouseMove) 303 mouseMoved, 304 /// The mouse cursor entered the area of the window (no data) 305 mouseEntered, 306 /// The mouse cursor left the area of the window (no data) 307 mouseLeft, 308 /// A joystick button was pressed (data in event.joystickButton) 309 joystickButtonPressed, 310 /// A joystick button was released (data in event.joystickButton) 311 joystickButtonReleased, 312 /// The joystick moved along an axis (data in event.joystickMove) 313 joystickMoved, 314 /// A joystick was connected (data in event.joystickConnect) 315 joystickConnected, 316 /// A joystick was disconnected (data in event.joystickConnect) 317 joystickDisconnected 318 } 319 320 /// Joystick buttons events parameters (JoystickButtonPressed, JoystickButtonReleased) 321 struct ExternalJoystickButtonEvent 322 { 323 ///Index of the joystick (in range [0 .. Joystick::Count - 1]) 324 uint joystickId; 325 ///Index of the button that has been pressed (in range [0 .. Joystick::ButtonCount - 1]) 326 uint button; 327 } 328 329 /// Joystick connection events parameters (JoystickConnected, JoystickDisconnected 330 struct ExternalJoystickConnectEvent 331 { 332 ///Index of the joystick (in range [0 .. Joystick::Count - 1]) 333 uint joystickId; 334 } 335 336 /// Joystick connection events parameters (JoystickConnected, JoystickDisconnected) 337 struct ExternalJoystickMoveEvent 338 { 339 ///Index of the joystick (in range [0 .. Joystick::Count - 1]) 340 uint joystickId; 341 ///Axis on which the joystick moved 342 int axis; 343 ///New position on the axis (in range [-100 .. 100]) 344 float position; 345 } 346 347 /// Keyboard event parameters (KeyPressed, KeyReleased) 348 struct ExternalKeyEvent 349 { 350 ///Code of the key that has been pressed. 351 KeyboardKey code; 352 ///Is the Alt key pressed? 353 bool alt; 354 ///Is the Control key pressed? 355 bool control; 356 ///Is the Shift key pressed? 357 bool shift; 358 ///Is the System key pressed? 359 bool system; 360 } 361 362 /// Mouse buttons events parameters (MouseButtonPressed, MouseButtonReleased) 363 struct ExternalMouseButtonEvent 364 { 365 ///Code of the button that has been pressed 366 MouseButton button; 367 ///X position of the mouse pointer, relative to the left of the owner window 368 int x; 369 ///Y position of the mouse pointer, relative to the top of the owner window 370 int y; 371 } 372 373 /// Mouse move event parameters (MouseMoved) 374 struct ExternalMouseMoveEvent 375 { 376 ///X position of the mouse pointer, relative to the left of the owner window 377 int x; 378 ///Y position of the mouse pointer, relative to the top of the owner window 379 int y; 380 } 381 382 /// Mouse wheel events parameters (MouseWheelMoved) 383 struct ExternalMouseWheelEvent 384 { 385 ///Number of ticks the wheel has moved (positive is up, negative is down) 386 int delta; 387 ///X position of the mouse pointer, relative to the left of the owner window 388 int x; 389 ///Y position of the mouse pointer, relative to the top of the owner window 390 int y; 391 } 392 393 /// Size events parameters (Resized) 394 struct ExternalSizeEvent 395 { 396 ///New width, in pixels 397 uint width; 398 ///New height, in pixels 399 uint height; 400 } 401 402 /// Text event parameters (TextEntered) 403 struct ExternalTextEvent 404 { 405 /// UTF-32 unicode value of the character 406 dchar unicode; 407 } 408 409 /// The external event state. 410 final class ExternalEventState 411 { 412 final: 413 static: 414 415 package(novelate): 416 /// _joystickButtonEvent. 417 ExternalJoystickButtonEvent _joystickButtonEvent; 418 /// _joystickConnectEvent. 419 ExternalJoystickConnectEvent _joystickConnectEvent; 420 /// _joystickMoveEvent. 421 ExternalJoystickMoveEvent _joystickMoveEvent; 422 /// _keyEvent. 423 ExternalKeyEvent _keyEvent; 424 /// _mouseButtonEvent. 425 ExternalMouseButtonEvent _mouseButtonEvent; 426 /// _mouseMoveEvent. 427 ExternalMouseMoveEvent _mouseMoveEvent; 428 /// _mouseWheelEvent. 429 ExternalMouseWheelEvent _mouseWheelEvent; 430 /// _sizeEvent. 431 ExternalSizeEvent _sizeEvent; 432 /// _textEvent. 433 ExternalTextEvent _textEvent; 434 435 public: 436 @property 437 { 438 /// Gets joystickButtonEvent. 439 ExternalJoystickButtonEvent joystickButtonEvent() { return _joystickButtonEvent; } 440 /// Gets joystickConnectEvent. 441 ExternalJoystickConnectEvent joystickConnectEvent() { return _joystickConnectEvent; } 442 /// Gets joystickMoveEvent. 443 ExternalJoystickMoveEvent joystickMoveEvent() { return _joystickMoveEvent; } 444 /// Gets keyEvent. 445 ExternalKeyEvent keyEvent() { return _keyEvent; } 446 /// Gets mouseButtonEvent. 447 ExternalMouseButtonEvent mouseButtonEvent() { return _mouseButtonEvent; } 448 /// Gets mouseMoveEvent. 449 ExternalMouseMoveEvent mouseMoveEvent() { return _mouseMoveEvent; } 450 /// Gets mouseWheelEvent. 451 ExternalMouseWheelEvent mouseWheelEvent() { return _mouseWheelEvent; } 452 /// Gets sizeEvent. 453 ExternalSizeEvent sizeEvent() { return _sizeEvent; } 454 /// Gets textEvent. 455 ExternalTextEvent textEvent() { return _textEvent; } 456 } 457 } 458 459 /// The external event manager. 460 final class ExternalEventManager 461 { 462 private: 463 /// Delegate for event handlers. 464 alias _DELEGATE = void delegate(); 465 466 /// Collection of handlers. 467 _DELEGATE[ExternalEventType] _handlers; 468 469 public: 470 /** 471 * Adds an event handler. 472 * Params: 473 * eventType = The event type. 474 * handler = The handler. 475 */ 476 void addHandler(ExternalEventType eventType, _DELEGATE handler) 477 { 478 _handlers[eventType] = handler; 479 } 480 481 /** 482 * Removes an event handler. 483 * Params: 484 * eventType = The event type. 485 */ 486 void removeHandler(ExternalEventType eventType) 487 { 488 if (!_handlers) 489 { 490 return; 491 } 492 493 _handlers.remove(eventType); 494 } 495 496 /** 497 * Fires an event. 498 * Params: 499 * eventType = The event type. 500 */ 501 void fireEvent(ExternalEventType eventType) 502 { 503 if (!_handlers) 504 { 505 return; 506 } 507 508 auto handler = _handlers.get(eventType, null); 509 510 if (!handler) 511 { 512 return; 513 } 514 515 handler(); 516 } 517 }