improved reactive ui and events
This commit is contained in:
15
src/lib/ui/UIEvents.hx
Normal file
15
src/lib/ui/UIEvents.hx
Normal file
@@ -0,0 +1,15 @@
|
||||
package lib.ui;
|
||||
|
||||
import util.Vec.Vec2;
|
||||
import kernel.ButtonType;
|
||||
using tink.CoreApi;
|
||||
|
||||
typedef UIEvents = {
|
||||
public var ?onClick:Callback<{button:ButtonType, pos:Vec2<Int>}>;
|
||||
public var ?onKey:Callback<{keyCode:Int, isHeld:Bool}>;
|
||||
public var ?onKeyUp:Callback<Int>;
|
||||
public var ?onMouseDrag:Callback<{button:ButtonType, pos:Vec2<Int>}>;
|
||||
public var ?onMouseScroll:Callback<{dir:Int, pos:Vec2<Int>}>;
|
||||
public var ?onMouseUp:Callback<{button:ButtonType, pos:Vec2<Int>}>;
|
||||
public var ?onPaste:Callback<String>;
|
||||
}
|
||||
@@ -28,13 +28,76 @@ class ReactiveUI {
|
||||
}
|
||||
|
||||
private function setupEvents() {
|
||||
context.clickSignal.handle(params ->{
|
||||
for (k => v in this.elementMap){
|
||||
context.onClick.handle(params ->{
|
||||
for (k => v in elementMap){
|
||||
if (k.isInside(params.pos)){
|
||||
v.handleClickEvent(params.pos,params.button);
|
||||
if (v.eventListner.onClick != null){
|
||||
v.eventListner.onClick.invoke(params);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// context.onKey.handle(params ->{
|
||||
// for (k => v in elementMap){
|
||||
// if (k.isInside(params.pos)){
|
||||
// if (v.eventListner.onKey != null){
|
||||
// v.eventListner.onKey.invoke(params);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
// context.onKeyUp.handle(params ->{
|
||||
// for (k => v in elementMap){
|
||||
// if (k.isInside(params.pos)){
|
||||
// if (v.eventListner.onKeyUp != null){
|
||||
// v.eventListner.onKeyUp.invoke(params);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
// context.onMouseDrag.handle(params ->{
|
||||
// for (k => v in elementMap){
|
||||
// if (k.isInside(params.pos)){
|
||||
// if (v.eventListner.onMouseDrag != null){
|
||||
// v.eventListner.onMouseDrag.invoke(params);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
context.onMouseScroll.handle(params ->{
|
||||
for (k => v in elementMap){
|
||||
if (k.isInside(params.pos)){
|
||||
if (v.eventListner.onMouseScroll != null){
|
||||
v.eventListner.onMouseScroll.invoke(params);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
context.onMouseUp.handle(params ->{
|
||||
for (k => v in elementMap){
|
||||
if (k.isInside(params.pos)){
|
||||
if (v.eventListner.onMouseUp != null){
|
||||
v.eventListner.onMouseUp.invoke(params);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// context.onPaste.handle(params ->{
|
||||
// for (k => v in elementMap){
|
||||
// if (k.isInside(params.pos)){
|
||||
// if (v.eventListner.onPaste != null){
|
||||
// v.eventListner.onPaste.invoke(params);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
}
|
||||
|
||||
public function render() {
|
||||
@@ -88,7 +151,6 @@ class ReactiveUI {
|
||||
for (child in children) {
|
||||
if (bounds.y - writePoint.y <= 0) {
|
||||
// No more space to render children
|
||||
Log.debug("No more space");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ class TextElement extends UIElement {
|
||||
private final bg:Color;
|
||||
private final fg:Color;
|
||||
|
||||
public function new(text:Observable<String>, ?background:Color = Black, ?textColor:Color = White) {
|
||||
super();
|
||||
public function new(text:Observable<String>, ?background:Color = Black, ?textColor:Color = White, events: UIEvents = null) {
|
||||
super(events);
|
||||
|
||||
this.text = text;
|
||||
this.bg = background;
|
||||
@@ -38,8 +38,4 @@ class TextElement extends UIElement {
|
||||
|
||||
return rtn;
|
||||
}
|
||||
|
||||
public override function handleClickEvent(pos: Vec2<Int>,button: ButtonType):Void {
|
||||
Log.debug('Click: ${this.text.get()}');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package lib.ui.reactive;
|
||||
|
||||
import kernel.Log;
|
||||
import kernel.ButtonType;
|
||||
import util.Vec.Vec2;
|
||||
|
||||
using tink.CoreApi;
|
||||
@@ -10,11 +8,12 @@ abstract class UIElement {
|
||||
abstract public function render(bounds:Vec2<Int>):Canvas;
|
||||
public var changed(default, null):Signal<Noise>;
|
||||
private final changedTrigger:SignalTrigger<Noise> = Signal.trigger();
|
||||
public final eventListner:UIEvents = {};
|
||||
|
||||
public function new() {
|
||||
public function new(events: UIEvents = null) {
|
||||
changed = changedTrigger.asSignal();
|
||||
}
|
||||
|
||||
public function handleClickEvent(pos: Vec2<Int>,button: ButtonType):Void {
|
||||
if (events != null){
|
||||
this.eventListner = events;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user