ianharrigan / haxeui

IMPORTANT NOTE! This repository is no longer maintained. Please consider the newer version: https://github.com/haxeui/haxeui-core
http://haxeui.org/
392 stars 46 forks source link

How to add additional parameter aside from UIEvent in buttonObject.onClick? #338

Closed notalentgeek closed 8 years ago

notalentgeek commented 8 years ago

buttonObject.onClick = function(_e:UIEvent){ trace("LALALA!"); } The code above is the default way to call a function when you pressed a button. I want to add additional arguments, perhaps something like this. buttonObject.onClick = function(_e:UIEvent, ?_object:ObjectMuseum){ trace(_object.GetName()); } But the code above returns an error like this. e : haxe.ui.toolkit.events.UIEvent -> ?_selectedMuseumObject : Null<ObjectMuseum> -> Void should be haxe.ui.toolkit.events.UIEvent -> Void

How to solve this problem? I want to add parameter on my button's onClick().

ianharrigan commented 8 years ago

The best advice i can give is to use the userData property on component. Its not an ideal solution though. Im sure it wouldnt be hugely complicated to get the macro that builds the event functions to add an additional Dynamic param, but honestly i dont have time at the moment. :)

notalentgeek commented 8 years ago

It is okay, your response is very fast tho :)). Honestly I have no clue on userData, neither on how it works.

In which file does field of onClick exist? I did search on Button.hx but I could not find anything named onClick. Perhaps I can hack a bit your code.

ianharrigan commented 8 years ago

Its build by a macro at compile time, which basically creates a function onClick which internally calls addEventListener. The macro is being called here: https://github.com/ianharrigan/haxeui/blob/master/haxe/ui/toolkit/core/DisplayObject.hx#L17 and that macro itself is: https://github.com/ianharrigan/haxeui/blob/master/haxe/ui/toolkit/core/Macros.hx#L174.

For the userData its simply a Dynamic you can set to anything you want, but its related to a component (which is why it might not be ideal). Heres an example:

var button:Button = new Button();
button.userData = "this is user defined";
button.onClick = function(e:UIEvent) {
    trace(e.component.userData);
}
notalentgeek commented 8 years ago

Thanks Ian, I got my problem fixed by now!

ianharrigan commented 8 years ago

How did you do it in the end? Just in case anyone else happens across this issue? Did you use .userData?

notalentgeek commented 8 years ago

Like you said in your example. What I have is a default button and 100 other buttons that have unique id. What I want to have is for the 100 other buttons to pass the id String to the button when one of them is clicked. Hence I passed the id of the button pressed into each of the 100 buttons. Here is the example code.

collectionGlobalObject.GetExhibitionObjectArray([i].GetMuseumUIObject().GetButtonObject().userData = collectionGlobalObject.GetExhibitionObjectArray()[i].GetMuseumUIObject().GetButtonObject().id;

ianharrigan commented 8 years ago

Great! :)