Multiple callbacks

Started by ScArL3T, 21 June 2015, 19:02:38

ScArL3T

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

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.

ScArL3T

Code (cpp) Select

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:
Code (cpp) Select
playButton->bindCallback(tgui::Button::LeftMouseClicked | tgui::Button::MouseEntered);

texus

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:
Code (cpp) Select
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.

ScArL3T