1 module sily.tui.event;
2 
3 import io = sily.terminal.input;
4 
5 private void delegate(io.InputEvent)[] _inputCallback;
6 private void delegate()[] _updateCallback;
7 
8 /**
9 Contains event types.
10 ---
11 EventType.input - will be triggered when new input is polled
12 EventType.update - will be triggered each frame
13 ---
14 */
15 enum EventType {
16     input,
17     update,
18 }
19 /// Ditto
20 alias EventInput = EventType.input;
21 /// Ditto
22 alias EventUpdate = EventType.update;
23 
24 // TODO: rename
25 template addEventListener(EventType t) {
26     static if (t == EventType.input) {
27         void addEventListener(void delegate(io.InputEvent) f) {
28             _inputCallback ~= f;
29         }
30     } else {
31         void addEventListener(void delegate() f) {
32             if (t == EventType.update) {
33                 _updateCallback ~= f;
34             } else assert(0, "Unknown event type.");
35         }
36     }
37 }
38 
39 size_t getListenerCount(EventType t)() {
40     static if (t == EventType.input) {
41         return _inputCallback.length;
42     } else
43     static if (t == EventType.update) {
44         return _updateCallback.length;
45     }
46     else assert(0, "Unknown event type.");
47 }
48 
49 /// Triggers generic events (that do not require arguments, i.e this will not trigger input)
50 void triggerEvent(EventType t)() if (t != EventType.input) {
51     if (t == EventType.update) {
52         foreach (f; _updateCallback) f();
53     } else assert(0, "Unknown event type.");
54 }
55 
56 // FIXME: shouldnt pollEvent pool event from queue and peek just show it?
57 void pollInputEvent() {
58     io.pollEvent();
59     while (!io.queueEmpty()) {
60         io.InputEvent e = io.peekEvent();
61         foreach (f; _inputCallback) {
62             f(e);
63         }
64     }
65 }
66 
67 void addTimer(bool repeating = false)(ulong time, void delegate() f) {
68 
69     // ADD LOGIC
70 
71     static if (repeating) {
72         addTimer!true(time, f);
73     }
74 }