48 lines
1.1 KiB
Haxe
48 lines
1.1 KiB
Haxe
|
package kernel;
|
||
|
|
||
|
import cc.OS;
|
||
|
import lua.Coroutine;
|
||
|
import util.EventBus;
|
||
|
|
||
|
using lua.Table;
|
||
|
|
||
|
/**
|
||
|
Class for interacting with the native pullEvent system.
|
||
|
**/
|
||
|
class KernelEvents{
|
||
|
public static final instance:KernelEvents = new KernelEvents();
|
||
|
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());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function on(eventName:String, callback:Array<Dynamic> -> Void):EventBusListner<Array<Dynamic>> {
|
||
|
return eventBus.on(eventName,callback);
|
||
|
}
|
||
|
|
||
|
public function once(eventName:String, callback:Array<Dynamic> -> Void):EventBusListner<Array<Dynamic>> {
|
||
|
return eventBus.once(eventName,callback);
|
||
|
}
|
||
|
|
||
|
public function removeListner(id:EventBusListner<Array<Dynamic>>) {
|
||
|
return eventBus.removeListner(id);
|
||
|
}
|
||
|
}
|