cc-haxe/src/kernel/KernelEvents.hx

229 lines
10 KiB
Haxe
Raw Normal View History

2021-12-20 00:55:30 +00:00
package kernel;
2022-02-21 14:17:38 +00:00
import util.Vec.Vec2;
import haxe.Exception;
2022-02-21 14:35:37 +00:00
2022-02-21 14:17:38 +00:00
using tink.CoreApi;
2021-12-20 00:55:30 +00:00
import cc.OS;
using lua.Table;
/**
Class for interacting with the native pullEvent system.
**/
2022-02-21 14:35:37 +00:00
class KernelEvents {
2022-02-24 18:47:27 +00:00
/**
Depends on: (Nothing)
**/
2022-02-21 00:50:19 +00:00
public static var instance:KernelEvents;
2022-02-21 14:17:38 +00:00
2022-02-21 14:35:37 +00:00
public final onAlarm:Signal<Int>;
public final onChar:Signal<String>;
public final onDisk:Signal<String>;
public final onDiskEject:Signal<String>;
public final onHttpCheck:Signal<{url:String, success:Bool, failReason:Any}>;
public final onHttpFailure:Signal<{url:String, failReason:String, handle:Any}>;
public final onHttpSuccess:Signal<{url:String, handle:Any}>;
public final onKey:Signal<{keyCode:Int, isHeld:Bool}>;
public final onKeyUp:Signal<Int>;
public final onModemMessage:Signal<{
addr:String,
channel:Int,
replyChannel:Int,
message:Dynamic,
distance:Int
}>;
public final onMonitorResize:Signal<String>;
public final onMonitorTouch:Signal<{addr:String, pos:Vec2<Int>}>;
public final onMouseClick:Signal<{button:ButtonType, pos:Vec2<Int>}>;
public final onMouseDrag:Signal<{button:ButtonType, pos:Vec2<Int>}>;
public final onMouseScroll:Signal<{dir:Int, pos:Vec2<Int>}>;
public final onMouseUp:Signal<{button:ButtonType, pos:Vec2<Int>}>;
public final onPaste:Signal<String>;
public final onPeripheral:Signal<String>;
public final onPeripheralDetach:Signal<String>;
public final onRednetMessage:Signal<{sender:Int, message:Any, protocol:Any}>;
public final onRedstone:Signal<Noise>;
public final onSpeakerAudioEmpty:Signal<String>;
public final onTaskComplete:Signal<{id:Int, success:Bool, failedReason:String}>;
public final onTermResize:Signal<Noise>;
public final onTerminate:Signal<Noise>;
public final onTimer:Signal<Int>;
public final onTurtleInventory:Signal<Noise>;
public final onWebsocketClose:Signal<String>;
public final onWebsocketFailure:Signal<{url:String, failReason:String}>;
public final onWebsocketMessage:Signal<{url:String, message:String, isBinary:Bool}>;
public final onWebsocketSuccess:Signal<{url:String, handle:Any}>;
private final onAlarmTrigger:SignalTrigger<Int> = Signal.trigger();
private final onCharTrigger:SignalTrigger<String> = Signal.trigger();
private final onDiskTrigger:SignalTrigger<String> = Signal.trigger();
private final onDiskEjectTrigger:SignalTrigger<String> = Signal.trigger();
private final onHttpCheckTrigger:SignalTrigger<{url:String, success:Bool, failReason:Any}> = Signal.trigger();
private final onHttpFailureTrigger:SignalTrigger<{url:String, failReason:String, handle:Any}> = Signal.trigger();
private final onHttpSuccessTrigger:SignalTrigger<{url:String, handle:Any}> = Signal.trigger();
private final onKeyTrigger:SignalTrigger<{keyCode:Int, isHeld:Bool}> = Signal.trigger();
private final onKeyUpTrigger:SignalTrigger<Int> = Signal.trigger();
private final onModemMessageTrigger:SignalTrigger<{
addr:String,
channel:Int,
replyChannel:Int,
message:Dynamic,
distance:Int
}> = Signal.trigger();
private final onMonitorResizeTrigger:SignalTrigger<String> = Signal.trigger();
private final onMonitorTouchTrigger:SignalTrigger<{addr:String, pos:Vec2<Int>}> = Signal.trigger();
private final onMouseClickTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}> = Signal.trigger();
private final onMouseDragTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}> = Signal.trigger();
private final onMouseScrollTrigger:SignalTrigger<{dir:Int, pos:Vec2<Int>}> = Signal.trigger();
private final onMouseUpTrigger:SignalTrigger<{button:ButtonType, pos:Vec2<Int>}> = Signal.trigger();
private final onPasteTrigger:SignalTrigger<String> = Signal.trigger();
private final onPeripheralTrigger:SignalTrigger<String> = Signal.trigger();
private final onPeripheralDetachTrigger:SignalTrigger<String> = Signal.trigger();
private final onRednetMessageTrigger:SignalTrigger<{sender:Int, message:Any, protocol:Any}> = Signal.trigger();
private final onRedstoneTrigger:SignalTrigger<Noise> = Signal.trigger();
private final onSpeakerAudioEmptyTrigger:SignalTrigger<String> = Signal.trigger();
private final onTaskCompleteTrigger:SignalTrigger<{id:Int, success:Bool, failedReason:String}> = Signal.trigger();
private final onTermResizeTrigger:SignalTrigger<Noise> = Signal.trigger();
private final onTerminateTrigger:SignalTrigger<Noise> = Signal.trigger();
private final onTimerTrigger:SignalTrigger<Int> = Signal.trigger();
private final onTurtleInventoryTrigger:SignalTrigger<Noise> = Signal.trigger();
private final onWebsocketCloseTrigger:SignalTrigger<String> = Signal.trigger();
private final onWebsocketFailureTrigger:SignalTrigger<{url:String, failReason:String}> = Signal.trigger();
private final onWebsocketMessageTrigger:SignalTrigger<{url:String, message:String, isBinary:Bool}> = Signal.trigger();
private final onWebsocketSuccessTrigger:SignalTrigger<{url:String, handle:Any}> = Signal.trigger();
2021-12-20 00:55:30 +00:00
2022-02-21 14:17:38 +00:00
@:allow(kernel.Init)
2022-02-21 14:35:37 +00:00
private function new() {
2022-02-21 14:17:38 +00:00
this.onAlarm = onAlarmTrigger.asSignal();
this.onChar = onCharTrigger.asSignal();
this.onDisk = onDiskTrigger.asSignal();
this.onDiskEject = onDiskEjectTrigger.asSignal();
this.onHttpCheck = onHttpCheckTrigger.asSignal();
this.onHttpFailure = onHttpFailureTrigger.asSignal();
this.onHttpSuccess = onHttpSuccessTrigger.asSignal();
this.onKey = onKeyTrigger.asSignal();
this.onKeyUp = onKeyUpTrigger.asSignal();
this.onModemMessage = onModemMessageTrigger.asSignal();
this.onMonitorResize = onMonitorResizeTrigger.asSignal();
this.onMonitorTouch = onMonitorTouchTrigger.asSignal();
this.onMouseClick = onMouseClickTrigger.asSignal();
this.onMouseDrag = onMouseDragTrigger.asSignal();
this.onMouseScroll = onMouseScrollTrigger.asSignal();
this.onMouseUp = onMouseUpTrigger.asSignal();
this.onPaste = onPasteTrigger.asSignal();
this.onPeripheral = onPeripheralTrigger.asSignal();
this.onPeripheralDetach = onPeripheralDetachTrigger.asSignal();
this.onRednetMessage = onRednetMessageTrigger.asSignal();
this.onRedstone = onRedstoneTrigger.asSignal();
this.onSpeakerAudioEmpty = onSpeakerAudioEmptyTrigger.asSignal();
this.onTaskComplete = onTaskCompleteTrigger.asSignal();
this.onTermResize = onTermResizeTrigger.asSignal();
this.onTerminate = onTerminateTrigger.asSignal();
this.onTimer = onTimerTrigger.asSignal();
this.onTurtleInventory = onTurtleInventoryTrigger.asSignal();
this.onWebsocketClose = onWebsocketCloseTrigger.asSignal();
this.onWebsocketFailure = onWebsocketFailureTrigger.asSignal();
this.onWebsocketMessage = onWebsocketMessageTrigger.asSignal();
this.onWebsocketSuccess = onWebsocketSuccessTrigger.asSignal();
}
2021-12-20 00:55:30 +00:00
/**
Start pulling events. Blocking.
**/
public function startEventLoop() {
2022-02-21 14:35:37 +00:00
while (true) {
2021-12-20 00:55:30 +00:00
var event:Table<Int, Dynamic> = OS.pullEventRaw();
2022-02-21 14:35:37 +00:00
2021-12-20 00:55:30 +00:00
var eventName:String = event[1];
2022-02-21 14:35:37 +00:00
2022-02-21 14:17:38 +00:00
switch eventName {
case "alarm":
this.onAlarmTrigger.trigger(event[2]);
case "char":
this.onCharTrigger.trigger(event[2]);
case "disk":
this.onDiskTrigger.trigger(event[2]);
case "disk_eject":
this.onDiskEjectTrigger.trigger(event[2]);
case "http_check":
2022-02-21 14:35:37 +00:00
this.onHttpCheckTrigger.trigger({url: event[2], success: event[3], failReason: event[4]});
2022-02-21 14:17:38 +00:00
case "http_failure":
2022-02-21 14:35:37 +00:00
this.onHttpFailureTrigger.trigger({url: event[2], failReason: event[3], handle: event[4]});
2022-02-21 14:17:38 +00:00
case "http_success":
2022-02-21 14:35:37 +00:00
this.onHttpSuccessTrigger.trigger({url: event[2], handle: event[3]});
2022-02-21 14:17:38 +00:00
case "key":
2022-02-21 14:35:37 +00:00
this.onKeyTrigger.trigger({keyCode: event[2], isHeld: event[3]});
2022-02-21 14:17:38 +00:00
case "key_up":
this.onKeyUpTrigger.trigger(event[2]);
case "modem_message":
2022-02-21 14:35:37 +00:00
this.onModemMessageTrigger.trigger({
addr: event[2],
channel: event[3],
replyChannel: event[4],
message: event[5],
distance: event[6]
});
2022-02-21 14:17:38 +00:00
case "monitor_resize":
this.onMonitorResizeTrigger.trigger(event[2]);
case "monitor_touch":
2022-02-21 14:35:37 +00:00
this.onMonitorTouchTrigger.trigger({addr: event[2], pos: {x: (event[3] : Int) - 1, y: (event[4] : Int) - 1}});
2022-02-21 14:17:38 +00:00
case "mouse_click":
2022-02-21 14:35:37 +00:00
this.onMouseClickTrigger.trigger({button: ccButtonToEnum(event[2]), pos: {x: (event[3] : Int) - 1, y: (event[4] : Int) - 1}});
2022-02-21 14:17:38 +00:00
case "mouse_drag":
2022-02-21 14:35:37 +00:00
this.onMouseDragTrigger.trigger({button: ccButtonToEnum(event[2]), pos: {x: (event[3] : Int) - 1, y: (event[4] : Int) - 1}});
2022-02-21 14:17:38 +00:00
case "mouse_scroll":
2022-02-21 14:35:37 +00:00
this.onMouseScrollTrigger.trigger({dir: event[2], pos: {x: (event[3] : Int) - 1, y: (event[4] : Int) - 1}});
2022-02-21 14:17:38 +00:00
case "mouse_up":
2022-02-21 14:35:37 +00:00
this.onMouseUpTrigger.trigger({button: ccButtonToEnum(event[2]), pos: {x: (event[3] : Int) - 1, y: (event[4] : Int) - 1}});
2022-02-21 14:17:38 +00:00
case "paste":
this.onPasteTrigger.trigger(event[2]);
case "peripheral":
this.onPeripheralTrigger.trigger(event[2]);
case "peripheral_detach":
this.onPeripheralDetachTrigger.trigger(event[2]);
case "rednet_message":
2022-02-21 14:35:37 +00:00
this.onRednetMessageTrigger.trigger({sender: event[2], message: event[3], protocol: event[4]});
2022-02-21 14:17:38 +00:00
case "redstone":
this.onRedstoneTrigger.trigger(null);
case "speaker_audio_empty":
this.onSpeakerAudioEmptyTrigger.trigger(event[2]);
case "task_complete":
2022-02-21 14:35:37 +00:00
this.onTaskCompleteTrigger.trigger({id: event[2], success: event[3], failedReason: event[4]});
2022-02-21 14:17:38 +00:00
case "term_resize":
this.onTermResizeTrigger.trigger(null);
case "terminate":
this.onTerminateTrigger.trigger(null);
case "timer":
this.onTimerTrigger.trigger(event[2]);
case "turtle_inventory":
this.onTurtleInventoryTrigger.trigger(null);
case "websocket_closed":
this.onWebsocketCloseTrigger.trigger(event[2]);
case "websocket_failure":
2022-02-21 14:35:37 +00:00
this.onWebsocketFailureTrigger.trigger({url: event[2], failReason: event[3]});
2022-02-21 14:17:38 +00:00
case "websocket_message":
2022-02-21 14:35:37 +00:00
this.onWebsocketMessageTrigger.trigger({url: event[2], message: event[3], isBinary: event[4]});
2022-02-21 14:17:38 +00:00
case "websocket_success":
2022-02-21 14:35:37 +00:00
this.onWebsocketSuccessTrigger.trigger({url: event[2], handle: event[3]});
2022-02-21 14:17:38 +00:00
default:
Log.error("Unknown cc event: " + eventName);
2021-12-20 00:55:30 +00:00
}
}
}
2022-02-21 14:35:37 +00:00
private static function ccButtonToEnum(button:Dynamic):ButtonType {
2022-02-21 14:17:38 +00:00
switch button {
case 1:
return Left;
case 2:
return Middle;
case 3:
return Right;
default:
throw new Exception("Invalid input");
}
2021-12-20 00:55:30 +00:00
}
}