Jump to content

How detect click event dispatcher?


photo

Recommended Posts

Actually don't know, how it's doing on C-oriented languages, so I describe pattern at first.

I have variable set of icons - from 0 to 8. This icons added/removed dynamically (player's inventory) and one click callback for they all. So, when I add object to inventory I do next:

Gui gui = engine.getGui();

WidgetIcon icon = new WidgetIcon(gui, "source/textures/game/gui/item_" + id + ".png");

icon.setCallback(GUI_CLICKED, "Source::Inventory::eventCallbackRedirector", this, "eventItemClick");

itemListHBox.addChild(icon, GUI_ALIGN_LEFT);

icons.append(icon);

 

I add all icons to array icons.

 

Now, when player clicks on icon I wont get pointer to this icon.

 

In another languages I could do next:

function eventItemClick(event:Event):void
{
 for (var i:int = 0; i < icons.length; i++)
   if (event.target === icons[i])
   {
     // I was found my dispatcher and can get some extra data, for example, I can define some parallel array with objects, and I can bind icon with object

     break;
   }
}

 

What analog I can apply in unigine? Thanks.

Link to comment

It can be done, using next code:

icon.setCallback(GUI_CLICKED, "Source::Inventory::eventItemClick", icons.size());

. . .

void eventItemClick(int index)
{
log.message("eventItemClick: " + index + "\n");
}

 

Now in callback passes index of the icon from icons array.

But, it's not possible to use classes for UI in this case :)

Link to comment
Use callback redirectors. There are a lot of samples in the data/samples/widgets directory.

 

Yes, I see callback redirectors - it's not very flexible to use classes for gui instead namespaces now. It could be convenient if callbacks described in classes will have class's namespace.

 

For update icons indexes I use next functionality:

void updateCallbacks()
{
for (int i = 0; i < icons.size(); i++)
{
icons[i].setCallback(GUI_CLICKED, "Source::Inventory::eventItemClick", i);
}
}

That I call after add/remove icon of objects from inventory.

Link to comment
×
×
  • Create New...