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

112 lines
2.7 KiB
Haxe

package kernel.ui;
import haxe.ds.ReadOnlyArray;
import kernel.ui.TermWriteable;
import kernel.peripherals.Peripherals.Peripheral;
/**
Responsable for creating window context, forwarding UI events to the context
and switching context to real screens.
**/
class WindowManager {
/**
Depends on: KernelEvents, Peripheral
**/
public static var instance:WindowManager;
private var currentMainContext:WindowContext;
private final allContexts:Array<WindowContext> = new Array();
private final outputMap:Map<String, WindowContext> = new Map();
@:allow(kernel.Init)
private function new() {
KernelEvents.instance.onKey.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onKeyTrigger.trigger(params);
}
});
KernelEvents.instance.onKeyUp.handle(keyCode -> {
if (currentMainContext != null) {
currentMainContext.onKeyUpTrigger.trigger(keyCode);
}
});
KernelEvents.instance.onMouseClick.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onClickTrigger.trigger(params);
}
});
KernelEvents.instance.onMouseDrag.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onMouseDragTrigger.trigger(params);
}
});
KernelEvents.instance.onMouseScroll.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onMouseScrollTrigger.trigger(params);
}
});
KernelEvents.instance.onMouseUp.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onMouseUpTrigger.trigger(params);
}
});
KernelEvents.instance.onPaste.handle(text -> {
if (currentMainContext != null) {
currentMainContext.onPasteTrigger.trigger(text);
}
});
KernelEvents.instance.onMonitorTouch.handle(params -> {
// TODO
});
KernelEvents.instance.onChar.handle(char -> {
if (currentMainContext != null) {
currentMainContext.onCharTrigger.trigger(char);
}
});
}
public function createNewContext():WindowContext {
var newContext = new WindowContext(new VirtualTermWriter());
allContexts.push(newContext);
return newContext;
}
public function getOutputs():ReadOnlyArray<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;
currentMainContext = context;
} 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();
}
}