implemented UIEvents delegate in WindowContext

This commit is contained in:
Djeeberjr 2023-01-30 16:44:17 +01:00
parent a1ce5957d1
commit 8f7739c26a
4 changed files with 65 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package kernel.ui;
import lib.ui.rendere.UIEventDelegate;
import lib.Pos;
import lib.Color;
import kernel.ButtonType;
@ -14,6 +15,8 @@ using tink.CoreApi;
class WindowContext implements TermWriteable {
private final writer:VirtualTermWriter;
@:allow(kernel.ui.WindowManager) private var eventDelegate: Null<UIEventDelegate>;
public var onClick(default, null):Signal<{button:ButtonType, pos:Pos}>;
public var onKey(default, null):Signal<{keyCode:Int, isHeld:Bool}>;
public var onKeyUp(default, null):Signal<Int>;
@ -137,4 +140,12 @@ class WindowContext implements TermWriteable {
public function reset() {
writer.reset();
}
/**
Delegate events to an UIEventDelegate.
Set to null to stop delegating events.
**/
public function delegateEvents(delegate: Null<UIEventDelegate>){
this.eventDelegate = delegate;
}
}

View File

@ -20,43 +20,78 @@ class WindowManager {
private function new() {
KernelEvents.instance.onKey.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onKeyTrigger.trigger(params);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onKey != null ? foo.onKey.invoke(params) : null;
}else{
currentMainContext.onKeyTrigger.trigger(params);
}
}
});
KernelEvents.instance.onKeyUp.handle(keyCode -> {
if (currentMainContext != null) {
currentMainContext.onKeyUpTrigger.trigger(keyCode);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onKeyUp != null ? foo.onKeyUp.invoke(keyCode) : null;
}else{
currentMainContext.onKeyUpTrigger.trigger(keyCode);
}
}
});
KernelEvents.instance.onMouseClick.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onClickTrigger.trigger(params);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onClick != null ? foo.onClick.invoke(params) : null;
}else{
currentMainContext.onClickTrigger.trigger(params);
}
}
});
KernelEvents.instance.onMouseDrag.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onMouseDragTrigger.trigger(params);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onMouseDrag != null ? foo.onMouseDrag.invoke(params) : null;
}else{
currentMainContext.onMouseDragTrigger.trigger(params);
}
}
});
KernelEvents.instance.onMouseScroll.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onMouseScrollTrigger.trigger(params);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onMouseScroll != null ? foo.onMouseScroll.invoke(params) : null;
}else{
currentMainContext.onMouseScrollTrigger.trigger(params);
}
}
});
KernelEvents.instance.onMouseUp.handle(params -> {
if (currentMainContext != null) {
currentMainContext.onMouseUpTrigger.trigger(params);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onMouseUp != null ? foo.onMouseUp.invoke(params) : null;
}else{
currentMainContext.onMouseUpTrigger.trigger(params);
}
}
});
KernelEvents.instance.onPaste.handle(text -> {
if (currentMainContext != null) {
currentMainContext.onPasteTrigger.trigger(text);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onPaste != null ? foo.onPaste.invoke(text) : null;
}else{
currentMainContext.onPasteTrigger.trigger(text);
}
}
});
@ -66,7 +101,12 @@ class WindowManager {
KernelEvents.instance.onChar.handle(char -> {
if (currentMainContext != null) {
currentMainContext.onCharTrigger.trigger(char);
if (currentMainContext.eventDelegate != null){
var foo = currentMainContext.eventDelegate.getEventHandlers();
foo.onChar != null ? foo.onChar.invoke(char) : null;
}else{
currentMainContext.onCharTrigger.trigger(char);
}
}
});
}

View File

@ -13,4 +13,5 @@ typedef UIEvents = {
public var ?onMouseScroll:Callback<{dir:Int, pos:Pos}>;
public var ?onMouseUp:Callback<{button:ButtonType, pos:Pos}>;
public var ?onPaste:Callback<String>;
public var ?onChar:Callback<String>;
}

View File

@ -0,0 +1,5 @@
package lib.ui.rendere;
interface UIEventDelegate {
public function getEventHandlers(): UIEvents;
}