Menu hot-keys

Started by starkhorn, 18 January 2015, 04:05:36


So is there a way to have multiple call-backs be defined to the same call-backID?

i.e. so I define an ENUM like below for each call-back event

Code (cpp) Select

enum GUICallBackID {

Now I define radio buttons for each of these menu options, with the mouse clicked event bound to each id. for example, this is the quit menu callback ID.
(RadioButtonList is vector containing each radio button.)

Code (cpp) Select


However what I also want to do is for the QUIT_GAME callbackID to be trigger if the user presses a hot-key, like say Q or 0 for menu option 0. Is there a way to do this? I tried adding a second bindCallback function but that obviously simply overwrote the left mouse clicked event.

Many thanks in advance.


The id itself has only a single purpose: to identify where the callback came from. It doesn't have any special meaning inside tgui. So there is absolutely no problem with reusing the same id in multiple places.

That being said, you can only bind callbacks from tgui widgets. I do not control hot-keys. If you want your code to be executed when you press a keyboard key then you must call the code yourself. There are two ways to handle this.

The first way requires no change to your current code, but I would advice the second method.
When the key is pressed, you will create the callback and add it in the gui (using an undocumented function :) )
Code (cpp) Select
Callback callback; = QUIT_GAME;

Of course where you handle this callback, you can only check because the other properties of the callback are not set.

The second method is is by binding callback functions instead of using ids.
Code (cpp) Select
void quit() {
  std::cout << "Quit" << std::endl;
button->bindCallback(quit, tgui::Button::LeftMouseClicked);

// When you press the Q button

You would have to take a look at the callback tutorial for more info on how to use the bindCallback function. In this example the quit function has no access to the sf::RenderWindow so it can't close it. This could be solved if it is in a class, but then you would have to bind differently (because of the 'this' pointer in class functions), or by using std::bind like the tutorial shows on a tgui::Gui object.