-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathEventManager.hx
More file actions
49 lines (42 loc) · 1.17 KB
/
EventManager.hx
File metadata and controls
49 lines (42 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package funkin.backend.scripting;
import flixel.FlxState;
import funkin.backend.scripting.events.*;
final class EventManager {
public static var eventCache:Map<String, CancellableEvent> = [];
#if cpp
public static var cppEventCache:haxe.ds.ObjectMap<Dynamic, CancellableEvent> = new haxe.ds.ObjectMap();
public static inline function get<T:CancellableEvent>(cl:Class<T>):T {
var event:CancellableEvent = cppEventCache.get(cl);
if (event == null) {
event = Type.createInstance(cl, []);
cppEventCache.set(cl, event);
}
return cast event;
}
#else
public static inline function get<T:CancellableEvent>(cl:Class<T>):T {
var className = Type.getClassName(cl);
var event = eventCache.get(className);
if (event == null) {
event = Type.createInstance(cl, []);
eventCache.set(className, event);
}
return cast event;
}
#end
public static function reset() {
#if cpp
cppEventCache = new haxe.ds.ObjectMap();
#else
for (event in eventCache) {
event.destroy();
}
eventCache = [];
#end
}
public static function init() {
FlxG.signals.preStateCreate.add(onStateSwitch);
}
private static inline function onStateSwitch(newState:FlxState)
reset();
}