Making a XML-built component evoke a non-interface code

169 Views Asked by At

I'm making a game using HaxeUI. I've designed a simple UI using XML definition. I need the button to execute a code that is unrelated to the UI elements, however, the parsed code from onClick property does not see any local identifiers defined in the area, in which the interface is being built.

How to work around this without the necessity to refrain from using XML definition?

1

There are 1 best solutions below

0
On BEST ANSWER

Currently there isnt a method exactly as you describe, in the sense that you define your click handler in XML and that links to haxe code - there is this open issue which is essentially as you describe: https://github.com/haxeui/haxeui-core/issues/196 - and I think it would a useful addition.

There is however this method (using build macros to build a custom component):

@:build(haxe.ui.macros.ComponentMacros.build("myxml.xml"))
class MySomething extends Box {
    public function new() {
        super();
        myButton.onClick = function(e) {
            myLabel.text = "Clicked";
        }
    }
}

This macro takes your xml (see below) and creates correctly typed member variables of anything that has an id.

<vbox>
    <button id="myButton" />
    <label id="myLabel" />
</vbox>

This can then be used in code or xml freely:

Screen.instance.addComponent(new MySomething());
Screen.instance.addComponent(new MySomething());
Screen.instance.addComponent(new MySomething());

or

<vbox>
    <mysomething />
    <mysomething />
    <mysomething />
</vbox>