get updated value as slider changes
« on: 08 March 2014, 13:12:42 »
I can always get slider value by using code
Code: [Select]
slider->getValue()
But i dont get new values when i move slider left or right. How to constantly get new values when i move slider left or right ?

side question: which one should i use as slider for music volume? slider2d or slider?

Thanks in advance
« Last Edit: 08 March 2014, 13:14:16 by wmbuRn »

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: get updated value as slider changes
« Reply #1 on: 08 March 2014, 13:16:38 »
I didn't entirely understood the question.
Are you asking for a way to get notified when the value changed, or are you saying that getValue always returns the same value even when moving the thumb of the slider?

Quote
side question: which one should i use as slider for music volume? slider2d or slider?
The normal slider. The 2d version is more for something like a color palette.

Re: get updated value as slider changes
« Reply #2 on: 08 March 2014, 13:19:41 »
getValue() returns current value when program loads. When i move the thumb of the slider i dont get new values. I will code only for slider in 1-2 mins

Here is minimal code:
Code: [Select]
#include <TGUI/TGUI.hpp>

int main()
{
    // Create the main window
    sf::RenderWindow app(sf::VideoMode(400, 400), "slider test");
    tgui::Gui gui(app);

    tgui::Slider::Ptr slider(gui);
    slider->load("Black.conf");
    slider->setVerticalScroll(false);
    slider->setPosition(10, 10);
    slider->setSize(300, 25);
    slider->setMinimum(0);
    slider->setMaximum(100);

    std::cout << "Value is: " << slider->getValue() << std::endl;



// Start the game loop
    while (app.isOpen())
    {
        // Process events
        sf::Event event;
        while (app.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                app.close();

            gui.handleEvent(event);
        }

        tgui::Callback callback;
        while (gui.pollCallback(callback))
        {
        }

        // Clear screen
        app.clear();

        // Draw the sprite
        gui.draw();

        // Update the window
        app.display();
    }

    return EXIT_SUCCESS;
}
« Last Edit: 08 March 2014, 13:27:37 by wmbuRn »

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: get updated value as slider changes
« Reply #3 on: 08 March 2014, 13:34:31 »
You should bind a callback to the slider to get notified about changed.
This can be done in different ways so make sure to read the callback tutorial.

Here is an (untested) example.
Code: [Select]
#include <TGUI/TGUI.hpp>

void sliderValueChanged(const tgui::Callback& callback)
{
    std::cout << "Value is: " << callback.value << std::endl;
}

int main()
{
    sf::RenderWindow window{sf::VideoMode{800, 600}, "TGUI window"};
    tgui::Gui gui(window);
    gui.setGlobalFont("TGUI/fonts/DejaVuSans.ttf");

    tgui::Slider::Ptr slider(gui);
    slider->load("TGUI/widgets/Black.conf");
    slider->setVerticalScroll(false);
    slider->setPosition(50, 50);
    slider->setSize(250, 25);

    std::cout << "Value is: " << slider->getValue() << std::endl;

    // Call the sliderValueChanged function when the value of the slider is changed
    slider->bindCallbackEx(sliderValueChanged, tgui::Slider::ValueChanged);

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

            gui.handleEvent(event);
        }

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

    return 0;
}

Re: get updated value as slider changes
« Reply #4 on: 08 March 2014, 13:41:17 »
It works. I will check the docs. Thank you very much