TGUI Forum

General Category => Help requests => Topic started by: Tyler on 20 May 2013, 14:15:08

Title: Binding ReturnKeyPressed callback to EditBox
Post by: Tyler on 20 May 2013, 14:15:08
I'm trying to bind a return key callback to an EditBox but have a bit of a problem: the callback is activated when the EditBox is clicked on. Whenever the return key is pressed when the EditBox is focused, the callback works as intended. Basically, two events (clicking on the EditBox and pressing Return in the EditBox) trigger the same callback. I stripped down my code to the absolute basic and the problem still occurs, leading me to believe either I am binding the callback incorrectly or there is a bug in TGUI.

I am using the TGUI code posted in the May 10 blog post.

Here's my basic, reproducible code.

Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>
#include <TGUI/TGUI.hpp>


// Declare global variables
sf::RenderWindow appWindow;
tgui::Window guiWindow(appWindow);

int main()
{
    appWindow.create( sf::VideoMode( 1024, 768, 32 ), "Test" );
    guiWindow.setGlobalFont( "resources/DejaVuSans.ttf" );

    tgui::EditBox::Ptr editBox(guiWindow);
    editBox->load("resources/objects/Black.conf");
    editBox->setPosition(40, 200);
    editBox->setSize(300, 30);
    editBox->setCallbackId(1);
    editBox->bindCallback( tgui::EditBox::EditBoxCallbacks::ReturnKeyPressed );

    while (appWindow.isOpen())
    {
        sf::Event event;
        while (appWindow.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                appWindow.close();

            guiWindow.handleEvent(event);
        }

        tgui::Callback callback;
        while (guiWindow.pollCallback(callback))
        {
            if (callback.id == 1)
                std::cout << "Return pressed\n";
        }

        appWindow.clear();
        guiWindow.drawGUI();
        appWindow.display();
    }

    return 0;
}
Title: Re: Binding ReturnKeyPressed callback to EditBox
Post by: texus on 20 May 2013, 14:31:23
This is indeed a bug in tgui, thanks for reporting.

The EditBoxCallbacks enum in EditBox.hpp uses ObjectCallbacksCount. This should be changed to ClickableObjectCallbacksCount.
Either change this manually or just download tgui again, and then recompile.

PS: You can just write tgui::EditBox::ReturnKeyPressed instead of tgui::EditBox::EditBoxCallbacks::ReturnKeyPressed.

EDIT: I have a small problem with my dropbox, so I can't upload the new version immediately.
Title: Re: Binding ReturnKeyPressed callback to EditBox
Post by: Tyler on 20 May 2013, 15:08:33
Thanks for the quick response! I recompiled and everything worked as it should.