Greetings All...

I have been trying to get a SpinButton to adjust another value, but I can't seem to make it send the Callback.  Is there anything I missing?

Here is some code I made from the "Full example" with most things removed.

Code: [Select]

//#include <TGUI/TGUI.hpp>
#include <tgui.h>
#include <string>
#include <sstream>

#define THEME_CONFIG_FILE "../../widgets/Black.conf"

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "TGUI window");
    tgui::Gui gui(window);
tgui::setResourcePath("../../../TGUI-0.6/examples/FullExample");

    if (gui.setGlobalFont("../../fonts/DejaVuSans.ttf") == false)
        return 1;

unsigned int callback_count = 0;

//control
int current_mouse_x = 0;
int current_mouse_y = 0;


//fonts
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Font fixedfont;
fixedfont.loadFromFile("courbd.ttf");

    tgui::Picture::Ptr picture(gui);
    picture->load("../Linux.jpg");


    tgui::MenuBar::Ptr menu(gui);
    menu->load(THEME_CONFIG_FILE);
    menu->setSize(window.getSize().x, 20);
    menu->addMenu("File");
    menu->addMenuItem("File", "Load");
    menu->addMenuItem("File", "Save");
    menu->addMenuItem("File", "Exit");
    menu->bindCallback(tgui::MenuBar::MenuItemClicked);
    menu->setCallbackId(2);


    tgui::SpinButton::Ptr spinButton(gui);
    spinButton->load(THEME_CONFIG_FILE);
    spinButton->setCallbackId(1);
    spinButton->setPosition(100, 80);
    spinButton->setVerticalScroll(false);
    spinButton->setSize(40, 20);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
//exits
if ((event.type == sf::Event::Closed) ||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
            || (event.type == sf::Event::Closed))
                window.close();
if(event.type == sf::Event::MouseMoved)
{
current_mouse_x = event.mouseMove.x;//keep track of mouse current position
current_mouse_y = event.mouseMove.y;
}

            gui.handleEvent(event);
        }

        tgui::Callback callback;
        while (gui.pollCallback(callback))
        {
            if (callback.id == 1)sf::sleep(sf::seconds(0.016f));//spinbutton, does nothing important

            if (callback.id == 2)
            {
                if (callback.text == "Exit")
                    window.close();
            }
callback_count++;
        }

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

//test text
std::stringstream info_conv;
info_conv << "CallBack Count: " << callback_count <<
//"\nchecked " << (int)checkbox->Checked <<
//"\nunchecked " << (int)checkbox->Unchecked <<
"\nX: " << current_mouse_x << "\nY: " << current_mouse_y;
sf::Text info_text;
info_text.setFont(font);
info_text.setCharacterSize(18);
info_text.setPosition(240.0f, 70.0f);
info_text.setString(info_conv.str());
window.draw(info_text);


        window.display();
sf::sleep(sf::seconds(0.016f));
    }

    return 0;
}

There are a few modifications...
-I used a <tgui.h> file to select the libs required for debug/release.  It may changed back on another computer.
-Added "spinButton->setCallbackId(1);" so it should function.
-Added some text for displaying "CallBack Count" and X and Y just for testing.
-I kept the menu so if the "Save" or "Load" were selected, it will increase the "CallBack Count".  "Quit" it too quick to see.
-I changed "if(callback.id == 1)" to do nothing important so a click from the SpinButton doesn't Quit the program, it should only increment the "CallBack Count", however it doesn't.

That's where I ran into the issue and haven't figured out how to solve yet.

I hope that provides enough information.

Ezk.

*

texus

  • *****
  • 1161
    • View Profile
    • Texus's Blog
Re: I haven't been able to get the SpinButton to send a Callback
« Reply #1 on: 11 June 2015, 03:01:58 »
You didn't bind any callback for the spin button. You are basically missing the following line:
spinButton->bindCallback(tgui::SpinButton::ValueChanged);

Calling just setCallbackId won't do anything, the function just changes an integer so that you could later identify callbacks for that widget. But without a call to bindCallback or bindCallbackEx the widget won't send callbacks.

Re: I haven't been able to get the SpinButton to send a Callback
« Reply #2 on: 11 June 2015, 04:10:10 »
Greetings All...

My apologies, I forgot to add something to the demo code I posted, here's what I should have included, so the correct answer should have been...

"Change..."
Code: [Select]
spinButton->bindCallback(tgui::SpinButton::LeftMouseClicked);

"...to..."
Code: [Select]
spinButton->bindCallback(tgui::SpinButton::ValueChanged);

Making the change made it work properly.  Thank you for the prompt response.

Is there any documentation I should read over before miss some differences between the widgets, I followed information on the "Full example" and it shows many widgets but few bindCallback's covering the different types of the callbacks used by different widgets.

This answer should give me enough information to complete my project, a little more work of course.  This is way better than when I tried to make my own menu and buttons when I couldn't use this since XP limited Visual Studio 2008, which didn't allow C++11.

Ezk.

*

texus

  • *****
  • 1161
    • View Profile
    • Texus's Blog
Re: I haven't been able to get the SpinButton to send a Callback
« Reply #3 on: 11 June 2015, 12:46:13 »
I actually noticed that LeftMouseClicked didn't seem to generate a callback yesterday when I wrote my answer. But it was 3am and I was about to go to sleep when you posted your question, so I didn't look into it. But that bug will be fixed later today. But LeftMouseClicked is a bit useless since it doesn't tell on which arrow you clicked.

Quote
but few bindCallback's covering the different types of the callbacks used by different widgets
Although not very easy to find (it is spread over multiple places due to the widgets inheriting from each other), the information can be found in the documentation. You would have to look for the <WidgetName>Callbacks enum inside the class. For SpinButton that would be here (hmm, small typo in that part, I'll fix that in the next version). In the diagram on top of the page you see that SpinButton inherits from ClickableWidget, so if you click on that ClickableWidget node then you will end on a page containing this. And if you do that again then you end up in the Widget class which gives you the remaining callback triggers here.

Quote
I couldn't use this since XP limited Visual Studio 2008, which didn't allow C++11.
Actually Windows XP supports up to VS2010 which is the minimum supported version of tgui 0.6. It is only from tgui 0.7 onwards that I drop support for VS on XP (but MinGW can still be used) as the minimum compiler becomes VS2013.