get updated value as slider changes

Started by wmbuRn, 08 March 2014, 13:12:42

wmbuRn

I can always get slider value by using code
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

texus

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?

Quoteside 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.

wmbuRn

#2
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:

#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;
}

texus

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.
#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;
}

wmbuRn

It works. I will check the docs. Thank you very much