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 event handling within the engine.
12 */
13 module novelate.events;
14 
15 /// Enumeration of event types.
16 enum EventType : string
17 {
18   /// The onSceneChange event.
19   onSceneChange = "onSceneChange",
20   /// The onResolutionChange event.
21   onResolutionChange = "onResolutionChange",
22   /// The onLoadingCreditsVideo event.
23   onLoadingCreditsVideo = "onLoadingCreditsVideo",
24   /// The onClearingAllLayersButBackground event.
25   onClearingAllLayersButBackground = "onClearingAllLayersButBackground",
26   /// The onScreenChange event.
27   onScreenChange = "onScreenChange",
28   /// The onRender event.
29   onRender = "onRender"
30 }
31 
32 private
33 {
34   /// The event delegate.
35   alias _EVENTDELEGATE = void delegate();
36 
37   /// The onSceneChange event handlers.
38   _EVENTDELEGATE[] onSceneChange;
39   /// The onResolutionChange event handelers.
40   _EVENTDELEGATE[] onResolutionChange;
41   /// The onLoadingCreditsVideo event handlers.
42   _EVENTDELEGATE[] onLoadingCreditsVideo;
43   /// The onClearingAllLayersButBackground event handlers.
44   _EVENTDELEGATE[] onClearingAllLayersButBackground;
45   /// The onScreenChange event handlers.
46   _EVENTDELEGATE[] onScreenChange;
47   /// The onRender event handlers.
48   _EVENTDELEGATE[] onRender;
49 }
50 
51 /**
52 * Adds an event handler.
53 * Params:
54 *   handler = The handler for the event.
55 */
56 void addEventHandler(EventType eventType)(_EVENTDELEGATE handler)
57 {
58   mixin((cast(string)eventType) ~ " ~= handler;");
59 }
60 
61 /**
62 * Fires an event.
63 * Params:
64 *   eventType = The type of the event to fire.
65 */
66 void fireEvent(EventType eventType)()
67 {
68   mixin("auto handlers = " ~ (cast(string)eventType) ~ ";");
69 
70   if (!handlers || !handlers.length)
71   {
72     return;
73   }
74 
75   foreach (handler; handlers)
76   {
77     handler();
78   }
79 }