Get ListBox's scrollbar [0.9]

Started by TheMole, 27 January 2022, 20:39:14

TheMole

Is it possible to get the scrollbar of a ListBox in 0.9?

I want to use the onValueChange function of the scrollbar, but I can't seem to find a function that will get me the scrollbar.

Heres how I imagined it would work, but it doesn't

auto listBox = tgui::ListBox::create();
listBox->getScrollbar()->onValueChange([&] {
        std::cout << "Scroll";
        });


I have also attempted to get the scrollbar as a child of the ListBox, but have come nowhere.

Thanks in advance

texus

It's currently not possible.

I would rather not provide direct access to the scrollbar, but maybe I could add something like listBox->onScroll(...).
In what scenario do you need to know whether the value of the scrollbar changed? I prefer to understand the use case before I add code for it.

Until then, there is already a hack to figure out if the listbox was scrolled: check every frame whether the value of listBox->getScrollbarValue() has changed compared to the last frame.

TheMole

I would like to sync several ListBoxes with eachother. So that when you scroll in one, the rest will follow.

They have the same amount of elements but different contents. I can't use a ListView, because they have to be separated on different parts of the screen.

texus

I'll see if I can add a callback for the scrolling this weekend.
Changing the scrollbar value of the second list with setScrollbarValue will cause another callback, but I guess that won't do much harm because it will call setScrollbarValue on the first list again with the value that it already had so it won't generate another callback.

TheMole

Couldn't you use .setEnabled to temporarily disable the signal for onScroll()?
If you want to avoid the unnecessary callback.

texus

Yes, that would work. I was originally going to mention that you had to do something like that to prevent an endless loop, but while writing the reply I realized that it wouldn't result in an endless loop.

The onScroll signal has been added to both TGUI 0.10 and 0.9 in the versions on github (https://github.com/texus/TGUI)