package kernel.ui; import lib.TermWriteable; import kernel.peripherals.Screen; import kernel.peripherals.Peripherals.Peripheral; import haxe.Exception; import util.Vec.Vec2; enum ButtonType { Left; Middle; Right; } class WindowManager { public static final instance:WindowManager = new WindowManager(); private function new() {} private var currentMainContext:WindowContext; private final allContexts:Array = new Array(); private final outputMap:Map = new Map(); public function init() { KernelEvents.instance.on("key",params -> { var keyCode: Int = params[1]; var isHeld: Bool = params[2]; if (currentMainContext != null){ currentMainContext.key(keyCode,isHeld); } }); KernelEvents.instance.on("key_up",params -> { var keyCode: Int = params[1]; if (currentMainContext != null){ currentMainContext.keyUp(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.click(button,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.mouseDrag(button,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.mouseScroll(dir,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.mouseUp(button,pos); } }); KernelEvents.instance.on("paste",params -> { var text: String = params[1]; if (currentMainContext != null){ currentMainContext.paste(text); } }); KernelEvents.instance.on("monitor_touch",array -> { // TODO }); } 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"); } } }