2021-12-20 00:55:30 +00:00
|
|
|
package kernel;
|
|
|
|
|
|
|
|
import cc.OS;
|
|
|
|
import util.EventBus;
|
|
|
|
|
|
|
|
using lua.Table;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Class for interacting with the native pullEvent system.
|
|
|
|
**/
|
|
|
|
class KernelEvents{
|
2022-02-21 00:50:19 +00:00
|
|
|
public static var instance:KernelEvents;
|
|
|
|
|
|
|
|
@:allow(kernel.Init)
|
2021-12-20 00:55:30 +00:00
|
|
|
private function new () {}
|
|
|
|
|
|
|
|
private var eventBus: util.EventBus<Array<Dynamic>> = new EventBus();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Start pulling events. Blocking.
|
|
|
|
**/
|
|
|
|
public function startEventLoop() {
|
|
|
|
// Log.info("Starting event loop");
|
|
|
|
while (true){
|
|
|
|
var event:Table<Int, Dynamic> = OS.pullEventRaw();
|
|
|
|
|
|
|
|
var eventName:String = event[1];
|
|
|
|
|
|
|
|
if (eventName == "terminate"){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventBus.emit(eventName,event.toArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 00:50:19 +00:00
|
|
|
public function on(eventName:String, callback:Array<Dynamic> -> Void):EventBusListner {
|
2021-12-20 00:55:30 +00:00
|
|
|
return eventBus.on(eventName,callback);
|
|
|
|
}
|
|
|
|
|
2022-02-21 00:50:19 +00:00
|
|
|
public function once(eventName:String, callback:Array<Dynamic> -> Void):EventBusListner {
|
2021-12-20 00:55:30 +00:00
|
|
|
return eventBus.once(eventName,callback);
|
|
|
|
}
|
|
|
|
|
2022-02-21 00:50:19 +00:00
|
|
|
public function removeListner(id:EventBusListner) {
|
2021-12-20 00:55:30 +00:00
|
|
|
return eventBus.removeListner(id);
|
|
|
|
}
|
|
|
|
}
|