diff --git a/src/lib/ui/reactive/IElement.hx b/src/lib/ui/reactive/IElement.hx deleted file mode 100644 index 7289ab9..0000000 --- a/src/lib/ui/reactive/IElement.hx +++ /dev/null @@ -1,10 +0,0 @@ -package lib.ui.reactive; - -using tink.CoreApi; - -import util.Vec.Vec2; - -interface IElement { - public function render(bounds:Vec2):Canvas; - public var changed(default, null):Signal; -} diff --git a/src/lib/ui/reactive/ReactiveUI.hx b/src/lib/ui/reactive/ReactiveUI.hx index 7b1a307..b3ed562 100644 --- a/src/lib/ui/reactive/ReactiveUI.hx +++ b/src/lib/ui/reactive/ReactiveUI.hx @@ -1,31 +1,48 @@ package lib.ui.reactive; +import util.Rect; import kernel.Log; import util.Color; import util.Vec.Vec2; import kernel.ui.WindowContext; +using tink.CoreApi; + class ReactiveUI { private final context:WindowContext; - private final children:Array; + private final children:Array; + private var elementMap: Map = new Map(); - public function new(context:WindowContext, children:Array) { + public function new(context:WindowContext, children:Array) { this.context = context; this.children = children; for (child in children) { - child.changed.handle(v -> { + child.changed.handle(_ -> { + context.reset(); render(); }); } + + setupEvents(); + } + + private function setupEvents() { + context.clickSignal.handle(params ->{ + for (k => v in this.elementMap){ + if (k.isInside(params.pos)){ + v.handleClickEvent(params.pos,params.button); + } + } + }); } public function render() { var size = context.getSize(); + var result = renderChildren(children, size); + this.elementMap = result.map; - var screen = renderChildren(children, size); - - writeToContext(screen); + writeToContext(result.canvas); } private function writeToContext(screen:Canvas) { @@ -62,8 +79,9 @@ class ReactiveUI { } } - public static function renderChildren(children:Array, bounds:Vec2):Canvas { - var rtn:Canvas = new Canvas(); + public static function renderChildren(children:Array, bounds:Vec2):{canvas: Canvas, map: Map} { + var canvas:Canvas = new Canvas(); + var elementMap: Map = new Map(); var writePoint:Vec2 = {x: 0, y: 0}; @@ -79,11 +97,12 @@ class ReactiveUI { y: bounds.y - writePoint.y }); - rtn.combine(childRender, writePoint); + canvas.combine(childRender, writePoint); + elementMap.set(new Rect(writePoint,{x: childRender.maxWidth(), y: writePoint.y + childRender.hight()}),child); writePoint = {x: 0, y: writePoint.y + childRender.hight()}; } - return rtn; + return {canvas: canvas,map: elementMap}; } } diff --git a/src/lib/ui/reactive/TextElement.hx b/src/lib/ui/reactive/TextElement.hx index 9f2eef2..f7fd7a5 100644 --- a/src/lib/ui/reactive/TextElement.hx +++ b/src/lib/ui/reactive/TextElement.hx @@ -1,5 +1,7 @@ package lib.ui.reactive; +import kernel.Log; +import kernel.ButtonType; import util.Observable; using tink.CoreApi; @@ -7,17 +9,13 @@ import util.Color; import util.Vec.Vec2; import util.MathI; -class TextElement implements IElement { - public var changed(default, null):Signal; - - private var changedTrigger:SignalTrigger; - +class TextElement extends UIElement { private final text:Observable; private final bg:Color; private final fg:Color; public function new(text:Observable, ?background:Color = Black, ?textColor:Color = White) { - setupTrigger(); + super(); this.text = text; this.bg = background; @@ -28,11 +26,6 @@ class TextElement implements IElement { }); } - private function setupTrigger() { - changedTrigger = Signal.trigger(); - changed = changedTrigger.asSignal(); - } - public function render(bounds:Vec2):Canvas { var rtn = new Canvas(); @@ -45,4 +38,8 @@ class TextElement implements IElement { return rtn; } + + public override function handleClickEvent(pos: Vec2,button: ButtonType):Void { + Log.debug('Click: ${this.text.get()}'); + } } diff --git a/src/lib/ui/reactive/UIElement.hx b/src/lib/ui/reactive/UIElement.hx new file mode 100644 index 0000000..6509ffa --- /dev/null +++ b/src/lib/ui/reactive/UIElement.hx @@ -0,0 +1,20 @@ +package lib.ui.reactive; + +import kernel.Log; +import kernel.ButtonType; +import util.Vec.Vec2; + +using tink.CoreApi; + +abstract class UIElement { + abstract public function render(bounds:Vec2):Canvas; + public var changed(default, null):Signal; + private final changedTrigger:SignalTrigger = Signal.trigger(); + + public function new() { + changed = changedTrigger.asSignal(); + } + + public function handleClickEvent(pos: Vec2,button: ButtonType):Void { + } +}