From 8f7739c26a6a811df825a8a539c6a80b8afc83cf Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 30 Jan 2023 16:44:17 +0100 Subject: [PATCH] implemented UIEvents delegate in WindowContext --- src/kernel/ui/WindowContext.hx | 11 ++++++ src/kernel/ui/WindowManager.hx | 56 +++++++++++++++++++++++---- src/lib/ui/UIEvents.hx | 1 + src/lib/ui/rendere/UIEventDelegate.hx | 5 +++ 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/lib/ui/rendere/UIEventDelegate.hx diff --git a/src/kernel/ui/WindowContext.hx b/src/kernel/ui/WindowContext.hx index 6f420e1..4895597 100644 --- a/src/kernel/ui/WindowContext.hx +++ b/src/kernel/ui/WindowContext.hx @@ -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; + 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; @@ -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){ + this.eventDelegate = delegate; + } } diff --git a/src/kernel/ui/WindowManager.hx b/src/kernel/ui/WindowManager.hx index 6e29275..0db57f8 100644 --- a/src/kernel/ui/WindowManager.hx +++ b/src/kernel/ui/WindowManager.hx @@ -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); + } } }); } diff --git a/src/lib/ui/UIEvents.hx b/src/lib/ui/UIEvents.hx index 3fe371d..f603749 100644 --- a/src/lib/ui/UIEvents.hx +++ b/src/lib/ui/UIEvents.hx @@ -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; + public var ?onChar:Callback; } diff --git a/src/lib/ui/rendere/UIEventDelegate.hx b/src/lib/ui/rendere/UIEventDelegate.hx new file mode 100644 index 0000000..339b140 --- /dev/null +++ b/src/lib/ui/rendere/UIEventDelegate.hx @@ -0,0 +1,5 @@ +package lib.ui.rendere; + +interface UIEventDelegate { + public function getEventHandlers(): UIEvents; +}