-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rebuild / Bind #20
Comments
What's the status of this? I see there is an implementation for EDIT: I'm currently working on an implementation for @Bind, which supports both component-centric "global" binding as well as binding to single members of data-models within assignments, which expose themselves as "bind-functions" in that they accept a callback of signature |
Sorry for the answer delay ;) The code I'm using @:uiInitFunction(rebuild) @:uiNoComponent
class BaseComponent extends h2d.Object {
var bindList : Array<Void->Void> = [];
function registerCheckRebuild<T>( f : Void -> T ) : T {
var value = f();
registerBind(() -> {
var value2 = f();
var arr2 = Std.downcast((value2:Dynamic),Array);
if( arr2 != null ) {
var arr1 = Std.downcast((value:Dynamic),Array);
if( arr1 == null || arr1.length != arr2.length ) {
rebuild();
return;
}
for( i in 0...arr1.length )
if( arr1[i] != arr2[i] ) {
rebuild();
return;
}
} else {
if( value != value2 ) {
rebuild();
return;
}
}
});
return value;
}
function registerBind( f : Void -> Void ) {
bindList.push(f);
f();
}
override function sync(ctx) {
var cur = bindList;
for( f in cur ) {
f();
if( bindList != cur ) {
dom.applyStyle(ui.style, true);
break; // was rebuilt
}
}
super.sync(ctx);
}
public function rebuild() {
bindList = [];
removeChildren();
}
} The idea is that each app/framework can decide itself what it means by "data has been modified and needs rebuild", thus not requiring domkit to make strong assumptions on specific things. |
Thanks, finally I get a clearer picture of how this should work. I see a few issues with this for my use-case though:
Please have a look at my PR for Maybe something similar could be added for binding if-conditions & for-loops; along-side the current approach that is. |
|
Could be done like my
Yes, I understand that only
I have some lists where the dom.active property (and styling) gets lost if an entire list gets re-rendered if any element changes I saw that the UI in Northgard and Darksburg are looking fine ;-) But UI in my game is currently a flickering nightmare due to the lack of data-binding and minimalistic updates, because I have a lot going on in terms of realtime data |
I added @Bind support |
Do you have any basic examples for both bind and rebuild? Or is it working like this? #20 (comment) A simple count++ (for bind) or a for loop (for rebuild) would be helpful. Edit: After 4 hours of tinkering I found out how to update text variables, but I still don't know how to update a loop. class HUD extends h2d.Flow implements h2d.domkit.Object {
var update:Array<() -> Void> = [];
static var SRC = <hud class="box" layout="vertical">
<text text={@bind Main.hp}/>
<text text={@bind Main.mp}/>
for (item in Main.items)
<text text={item}/>
</hud>
override function sync(ctx) {
for (f in update) f();
super.sync(ctx);
}
public function registerBind(f:() -> Void) {
update.push(f);
f();
}
}
class Main extends hxd.App {
// UI
var hudsystem:HUD;
// State
public static var items:Array<String> = ['Item'];
public static var hp:String = '100';
public static var mp:String = '50';
override function init() {
hudsystem = new HUD(s2d);
}
override function update(dt:Float) {
// Collect Item
if (hxd.Key.isPressed(hxd.Key.SPACE)) {
Main.items.concat(["Item"]);
// inventorySystem = new Inventory(s2d);
// styles.addObject(inventorySystem);
}
// Change HP
if (hxd.Key.isPressed(hxd.Key.H)) {
// Increase by 1
Main.hp = Std.string(Std.parseInt(Main.hp) + 1);
}
// Change MP
if (hxd.Key.isPressed(hxd.Key.M)) {
// Decrease by 1
Main.mp = Std.string(Std.parseInt(Main.mp) - 1);
}
}
static function main() {
#if hl
hxd.Res.initLocal();
#else
hxd.Res.initEmbed();
#end
new Main();
}
} |
In order to allow for UI self-rebuild and automatic update within DomkitML declaration, we will introduce new metadata:
Gets translated to:
registerCheckRebuild can then be implemented on the framework base class to perform regular checks and rebuild the UI if data has changed, and registerBind for updating values regularly.
We also need something like:
That translates to:
So some vars can be updated before following rebuilds take place.
The text was updated successfully, but these errors were encountered: