Trouble syncing scrollbar and knob widgets

Started by Hexile, 14 December 2015, 02:31:30

Hexile

I have 8 knobs and 8 scrollbars in my project but i keep running into issues when i try to sync them.

Working:
Default/Inverted Mode: Knob operates Scrollbar
Inverted Mode: Knob operates Scrollbar
Not working:
Default Mode: Scrollbar operates Knob


How can i correctly sync changes between them?


Interesting bits of code:

const int vertical_control_separator = 100;

tgui::Scrollbar::Ptr scrollbar(gui);
scrollbar->load(THEME_CONFIG_FILE);
scrollbar->setVerticalScroll(true);
scrollbar->setPosition(20, 20);
scrollbar->setSize(18, 256);
scrollbar->setMaximum(127);
scrollbar->setLowValue(8);

tgui::Knob::Ptr knob(gui);
knob->load("data/widgets/Knob/Knob.conf");
knob->setPosition(55, 235);
knob->setSize(40, 40);
knob->setStartRotation(127);
knob->setValue(0);
knob->setEndRotation(-128);


/* Callback handling*/
#define Knob_CALLBACK_ID 10
#define KnobScroll_CALLBACK_ID 20

bool invert_Knob_SYNC=false;

knob->bindCallback(tgui::Knob::ValueChanged);
knob->setCallbackId(Knob_CALLBACK_ID +1);

syncbox->bindCallback(tgui::Checkbox::Checked);
syncbox->bindCallback(tgui::Checkbox::Unchecked);
syncbox->setCallbackId(Knob_CALLBACK_ID + 9);

scrollbar->bindCallback(tgui::Scrollbar::ValueChanged);
scrollbar->setCallbackId(KnobScroll_CALLBACK_ID + 1);


int value;

switch(callback.id)
{
// Knobs
case Knob_CALLBACK_ID + 1:
{
value=knob->getValue();

if (invert_Knob_SYNC == true) scrollbar->setValue(value / 3); // works but sync is flipped
else scrollbar->setValue((128 - 8) - (value / 3)); // default ( offset fixed)
break;
}

case Knob_CALLBACK_ID + 9:
{
invert_Knob_SYNC = !invert_Knob_SYNC;
break;
}

case KnobScroll_CALLBACK_ID + 1:
{
value = scrollbar->getValue();

if (invert_Knob_SYNC == true) knob8->setValue(value * 3); // works but sync is flipped
//else knob->setValue(? ); // broken
break;
}

texus

Knob also has a minimum and maximum, the start and end rotation only tell something about where the min and max are graphically, the value goes in the range of min to max which is 0 to 360 by default.

If you add this:
Code (cpp) Select
knob->setMaximum(128-8);
then your scrollbar and knob will have the exact same value range.

The "scrollbar->setValue(( 128 - 8 ) - (value / 3));" code that you had would e.g. become
Code (cpp) Select
scrollbar->setValue((128 - 8) - value);
after this change.

I don't have the time now to give the correct formula, but like this it shouldn't be too hard to find it yourself.

Hexile

Fixed. a quick question though: is it possible to change the color of a widget like SFML can do on sprites?

I.e: sprite.setColor(sf::Color(0, 255, 0)); // green

texus

No you can only set the opacity. If I were to add a setColor function to widgets I would also have to define what should happen with colored widgets as opposed to textured widgets. Before there was a setOpacity function there was a setTransparency function which had several issues, when I once tried adding a setColor function back then it made those issues even bigger making it practically impossible. I guess most of these problems will have been solved if I implemented it similar to setOpacity though, so maybe I could give it another try. But it won't happen anywhere soon.

Hexile

I see.

There is a minor glitch with the knobs; the first time they are clicked, they jump to what seems to be a random point above half of the maximum value set.

texus

I can't reproduce it here. Could you show the lines where you create the knob? Maybe the problem is specific to the minimum, maximum, start rotation and end rotation that you are using.

Hexile

tgui::Knob::Ptr knob1(gui);
knob1->load("data/widgets/Knob/Knob.conf");
knob1->setPosition(55, 235);
knob1->setSize(40, 40);
knob1->setStartRotation(127);
knob1->setValue(0);
knob1->setEndRotation(-128);
knob1->setMinimum(0);
knob1->setMaximum(127);

texus

I still can't reproduce it with that code. Can you put your code in a single file and send it to me so that I can run the identical code?
Does it matter where you click on the knob?

Hexile

Ah, found a reliable way to trigger it; It happens when clicking on the area not specified, i.e:



Most of the time it sets the knob to near/at maximum level

texus

That's actually intended behaviour, it sets the value to the closest possible location.

Hexile

I see. But it only works towards maximum, i would have expected it to work both ways

texus

It should work both ways. The code that you last showed had the invalid area on the left side. When I click just above the middle it goes up, if you click right below the middle it goes down. It still works as intended when I set the rotations to 0 and 180.

So if it is only going to the maximum then you could still show your code and then I can check why it goes wrong in that situation (if I can reproduce it here).

Hexile

it's pretty long, added it as attachment.

texus

It is still working here, but I noticed that my invalid area is on the left side, while in your screenshots it seems to be at the bottom. So the only thing that I can still think of that can be the issue is a difference in the config file. Could you also send the Knob.conf and the images so that I can test with them?

Hexile