cc-haxe/src/kernel/ui/WindowManager.hx

154 lines
4.3 KiB
Haxe
Raw Normal View History

2021-12-20 00:55:30 +00:00
package kernel.ui;
import lib.TermWriteable;
import kernel.peripherals.Peripherals.Peripheral;
import haxe.Exception;
import util.Vec.Vec2;
enum ButtonType {
Left;
Middle;
Right;
}
class WindowManager {
2022-02-21 00:50:19 +00:00
public static var instance:WindowManager;
2021-12-20 00:55:30 +00:00
2022-02-21 00:50:19 +00:00
@:allow(kernel.Init)
private function new() {
2021-12-20 00:55:30 +00:00
KernelEvents.instance.on("key",params -> {
var keyCode: Int = params[1];
var isHeld: Bool = params[2];
if (currentMainContext != null){
2022-02-21 00:50:19 +00:00
currentMainContext.keyTrigger.trigger({keyCode: keyCode,isHeld: isHeld});
2021-12-20 00:55:30 +00:00
}
});
KernelEvents.instance.on("key_up",params -> {
var keyCode: Int = params[1];
if (currentMainContext != null){
2022-02-21 00:50:19 +00:00
currentMainContext.keyUpTrigger.trigger(keyCode);
2021-12-20 00:55:30 +00:00
}
});
KernelEvents.instance.on("mouse_click",params -> {
var button: ButtonType = ccButtonToEnum(params[1]);
var clickPos: Vec2<Int> = {
x: (params[2]:Int) - 1,
y: (params[3]:Int) - 1
};
if (currentMainContext != null){
2022-02-21 00:50:19 +00:00
currentMainContext.clickTrigger.trigger({button: button,pos: clickPos});
2021-12-20 00:55:30 +00:00
}
});
KernelEvents.instance.on("mouse_drag",params -> {
var button: ButtonType = ccButtonToEnum(params[1]);
var pos: Vec2<Int> = {
x: (params[2]:Int) - 1,
y: (params[3]:Int) - 1,
}
if (currentMainContext != null){
2022-02-21 00:50:19 +00:00
currentMainContext.mouseDragTrigger.trigger({button: button,pos: pos});
2021-12-20 00:55:30 +00:00
}
});
KernelEvents.instance.on("mouse_scroll",params -> {
var dir: Int = params[1];
var pos: Vec2<Int> = {
x: (params[2]:Int) - 1,
y: (params[3]:Int) - 1,
}
if (currentMainContext != null){
2022-02-21 00:50:19 +00:00
currentMainContext.mouseScrollTrigger.trigger({dir: dir,pos: pos});
2021-12-20 00:55:30 +00:00
}
});
KernelEvents.instance.on("mouse_up",params -> {
var button: ButtonType = ccButtonToEnum(params[1]);
var pos: Vec2<Int> = {
x: (params[2]:Int) - 1,
y: (params[2]:Int) - 1,
}
if (currentMainContext != null){
2022-02-21 00:50:19 +00:00
currentMainContext.mouseUpTrigger.trigger({button: button,pos: pos});
2021-12-20 00:55:30 +00:00
}
});
KernelEvents.instance.on("paste",params -> {
var text: String = params[1];
if (currentMainContext != null){
2022-02-21 00:50:19 +00:00
currentMainContext.pasteTrigger.trigger(text);
2021-12-20 00:55:30 +00:00
}
});
KernelEvents.instance.on("monitor_touch",array -> {
// TODO
});
}
2022-02-21 00:50:19 +00:00
private var currentMainContext:WindowContext;
private final allContexts:Array<WindowContext> = new Array();
private final outputMap:Map<String,WindowContext> = new Map();
2021-12-20 00:55:30 +00:00
public function createNewContext(): WindowContext {
var newContext = new WindowContext(new VirtualTermWriter());
allContexts.push(newContext);
2022-02-21 00:50:19 +00:00
// newContext.setTarget(MainTerm.instance);
2021-12-20 00:55:30 +00:00
newContext.enable();
currentMainContext = newContext;
return newContext;
}
public function getOutputs(): Array<String> {
var arr = Peripheral.instance.getScreens().map(screen -> return screen.getAddr());
arr.push("main");
return arr;
}
public function focusContextToOutput(context: WindowContext,output: String) {
var target: TermWriteable;
if (output == "main"){
target = MainTerm.instance;
}else{
target = Peripheral.instance.getScreen(output);
if (target == null){
// output target not found
return;
}
}
if (outputMap.exists(output)){
outputMap[output].disable();
}
outputMap[output] = context;
context.setTarget(target);
context.enable();
}
private static function ccButtonToEnum(button: Int): ButtonType {
switch button {
case 1:
return Left;
case 2:
return Middle;
case 3:
return Right;
case _:
throw new Exception("Invalid input");
}
}
}