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 { public static var instance:WindowManager; @:allow(kernel.Init) private function new() { KernelEvents.instance.on("key",params -> { var keyCode: Int = params[1]; var isHeld: Bool = params[2]; if (currentMainContext != null){ currentMainContext.keyTrigger.trigger({keyCode: keyCode,isHeld: isHeld}); } }); KernelEvents.instance.on("key_up",params -> { var keyCode: Int = params[1]; if (currentMainContext != null){ currentMainContext.keyUpTrigger.trigger(keyCode); } }); KernelEvents.instance.on("mouse_click",params -> { var button: ButtonType = ccButtonToEnum(params[1]); var clickPos: Vec2 = { x: (params[2]:Int) - 1, y: (params[3]:Int) - 1 }; if (currentMainContext != null){ currentMainContext.clickTrigger.trigger({button: button,pos: clickPos}); } }); KernelEvents.instance.on("mouse_drag",params -> { var button: ButtonType = ccButtonToEnum(params[1]); var pos: Vec2 = { x: (params[2]:Int) - 1, y: (params[3]:Int) - 1, } if (currentMainContext != null){ currentMainContext.mouseDragTrigger.trigger({button: button,pos: pos}); } }); KernelEvents.instance.on("mouse_scroll",params -> { var dir: Int = params[1]; var pos: Vec2 = { x: (params[2]:Int) - 1, y: (params[3]:Int) - 1, } if (currentMainContext != null){ currentMainContext.mouseScrollTrigger.trigger({dir: dir,pos: pos}); } }); KernelEvents.instance.on("mouse_up",params -> { var button: ButtonType = ccButtonToEnum(params[1]); var pos: Vec2 = { x: (params[2]:Int) - 1, y: (params[2]:Int) - 1, } if (currentMainContext != null){ currentMainContext.mouseUpTrigger.trigger({button: button,pos: pos}); } }); KernelEvents.instance.on("paste",params -> { var text: String = params[1]; if (currentMainContext != null){ currentMainContext.pasteTrigger.trigger(text); } }); KernelEvents.instance.on("monitor_touch",array -> { // TODO }); } private var currentMainContext:WindowContext; private final allContexts:Array = new Array(); private final outputMap:Map = new Map(); public function createNewContext(): WindowContext { var newContext = new WindowContext(new VirtualTermWriter()); allContexts.push(newContext); // newContext.setTarget(MainTerm.instance); newContext.enable(); currentMainContext = newContext; return newContext; } public function getOutputs(): Array { 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"); } } }