Callback bound with trigger tgui::Button::ReturnKeyPressed don't work

Started by knarf0, 08 September 2014, 00:32:00

knarf0

Hi,

I call bindCallback from a tgui::Button::Ptr with the trigger set to tgui::Button::ReturnKeyPressed, to close the window when the return key is pressed. It does not work and nothing is happening when I press the return key.

Here is the smallest code that show my problem :

#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <TGUI/TGUI.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Title");
tgui::Gui gui(window);

tgui::Button::Ptr button(gui);
button->load("widgets/Black.conf");
button->setSize(260, 60);
button->setPosition(270, 240);
button->bindCallback(&sf::Window::close, (sf::Window *)&window , tgui::Button::ReturnKeyPressed);

while (window.isOpen())
{
sf::Event event;

while (window.pollEvent(event))
{
gui.handleEvent(event);

if (event.type == sf::Event::Closed)
window.close();
}

window.clear();
gui.draw();
window.display();
}

return 0;
}



If I change tgui::Button::ReturnKeyPressed to tgui::Button::LeftMouseClicked the window close when I click the button.

Can someone please help me  :)

texus

The ReturnKeyPressed callback is only triggered when the button is focused while you press the return key. This was done so that buttons can be "pressed" by selecting them and using space or return key instead of only when clicking on them with the mouse.

But the Black theme provides no focus state for the button (the image contains it, but it is not loaded by Black.conf) so this part does not work with the default Black theme. The Black theme is meant to only be used with a mouse (same for White and BabyBlue themes).

The focusing part isn't really advanced enough to be decently used, thats why it is disabled by default.

knarf0

Thanks for the quick answer  ;)

Is there a way to modify the .conf so that the method tgui::Button::focus will work ?

texus

You can add the following to the Button section in Black.conf
FocusedImage_L    = "Black.png" (  0, 175,  50, 50)
FocusedImage_M    = "Black.png" ( 50, 175, 100, 50)
FocusedImage_R    = "Black.png" (150, 175,  50, 50)


You can use the tab key in the program to change the focused widget (in case you have more buttons) and you may want to call button->focus() to have it focused when the program starts.
Just know that changing the focus with the tab key doesn't tend to work very well when stuff like child windows or panels are involved.