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 enum EventType : string
16 {
17   onSceneChange = "onSceneChange",
18   onTempScreenClear = "onTempScreenClear",
19   onTempScreenShow = "onTempScreenShow",
20   onResolutionChange = "onResolutionChange",
21   onLoadingCreditsVideo = "onLoadingCreditsVideo",
22   onClearingAllLayersButBackground = "onClearingAllLayersButBackground",
23   onScreenChange = "onScreenChange",
24   onRender = "onRender"
25 }
26 
27 private
28 {
29   alias _EVENTDELEGATE = void delegate();
30 
31   _EVENTDELEGATE[] onSceneChange;
32   _EVENTDELEGATE[] onTempScreenClear;
33   _EVENTDELEGATE[] onTempScreenShow;
34   _EVENTDELEGATE[] onResolutionChange;
35   _EVENTDELEGATE[] onLoadingCreditsVideo;
36   _EVENTDELEGATE[] onClearingAllLayersButBackground;
37   _EVENTDELEGATE[] onScreenChange;
38   _EVENTDELEGATE[] onRender;
39 }
40 
41 void addEventHandler(EventType eventType)(_EVENTDELEGATE handler)
42 {
43   mixin((cast(string)eventType) ~ " ~= handler;");
44 }
45 
46 void fireEvent(EventType eventType)()
47 {
48   mixin("auto handlers = " ~ (cast(string)eventType) ~ ";");
49 
50   if (!handlers || !handlers.length)
51   {
52     return;
53   }
54 
55   foreach (handler; handlers)
56   {
57     handler();
58   }
59 }