Multiple callbacks
« on: 21 June 2015, 19:02:38 »
How can I play a sound whenever the button is hovered, but at the same time executing an action when pressed?
Is there a way that I can use 2 callbacks?

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Multiple callbacks
« Reply #1 on: 21 June 2015, 19:09:32 »
Just call bindCallback twice, once for the hover and once for the pressed trigger. You can even call it multiple times with the same trigger, all the functions you have bound will be called.

Re: Multiple callbacks
« Reply #2 on: 21 June 2015, 19:27:26 »
playButton->bindCallback(tgui::Button::LeftMouseClicked);
playButton->setCallbackId(1);
playButton->bindCallback(tgui::Button::MouseEntered);
playButton->setCallbackId(2);

Uhm sorry I'm new to TGUI, maybe I'm missing something but this won't work.
Also, this doesn't work:
playButton->bindCallback(tgui::Button::LeftMouseClicked | tgui::Button::MouseEntered);

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Multiple callbacks
« Reply #3 on: 21 June 2015, 19:34:32 »
Every widget can have exactly one id so that you can see from which widget the callback came. Calling setCallbackId will just change the current id so in your code example the widget will use id 2 for both callbacks.

In the callback loop you should just check the trigger like this:
while (gui.pollCallback(callback))
{
    if (callback.id == /* id of the widget */) {
        if (callback.trigger == tgui::Button::LeftMouseClicked) {
            // Widget was clicked
        }
        else if (callback.trigger == tgui::Button::MouseEntered) {
            // Mouse entered widget
        }
    }
}

See the callback tutorial for more info.

Re: Multiple callbacks
« Reply #4 on: 21 June 2015, 19:37:21 »
Thank you!