TGUI Forum

General Category => Help requests => Topic started by: Hexile on 14 December 2015, 02:31:30

Title: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 14 December 2015, 02:31:30
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:

Code: [Select]
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);

Code: [Select]
/* 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);

Code: [Select]
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;
}
Code: [Select]
case Knob_CALLBACK_ID + 9:
{
invert_Knob_SYNC = !invert_Knob_SYNC;
break;
}
Code: [Select]
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;
}
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 14 December 2015, 10:09:25
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:
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
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.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 14 December 2015, 11:09:03
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
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 14 December 2015, 13:29:06
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.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 19 December 2015, 04:24:09
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.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 19 December 2015, 12:45:53
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.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 19 December 2015, 16:10:27
Code: [Select]
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);
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 19 December 2015, 16:14:28
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?
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 19 December 2015, 16:48:13
Ah, found a reliable way to trigger it; It happens when clicking on the area not specified, i.e:

(https://puu.sh/m1lcG/7135907109.png)

Most of the time it sets the knob to near/at maximum level
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 19 December 2015, 16:53:06
That's actually intended behaviour, it sets the value to the closest possible location.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 19 December 2015, 17:26:43
I see. But it only works towards maximum, i would have expected it to work both ways
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 19 December 2015, 17:32:21
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).
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 19 December 2015, 21:41:03
it's pretty long, added it as attachment.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 19 December 2015, 21:49:45
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?
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 19 December 2015, 22:41:03
\/
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 19 December 2015, 23:02:14
The config file seems to be wrong. Try replacing "ImageRotation = 0" with "ImageRotation = 90", does it work fine then? The rotation is closely related with mathematics, so 0° would mean that the line in the front image is facing to the right while 90° is upward like in your image.

Edit: You would also have to add 90 to the start and end rotations in your code to get the same knob as you have now.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 19 December 2015, 23:15:58
Yea, that works. All is good now. :)
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 20 August 2016, 18:18:40
I left this project for a while but decided to do some cleaning to get around some oddities and visual bugs, there is one thing that i can't seem to get working now: flipping the visual range of scroll bars.
Theoretically 255-value should do the trick but the application gets stuck in a loop. "value" can only range from 0 to 255 anyway.

Initialization:
Code: [Select]
#define SCROLLBAR_BTN_SIZE 12 // makes the scrollbar visible

tgui::Scrollbar::Ptr scrollbar1(gui);
scrollbar1->load(THEME_CONFIG_FILE);
scrollbar1->setVerticalScroll(true);
scrollbar1->setPosition(20, 20);
scrollbar1->setSize(18, 256);
scrollbar1->setMaximum(255 + SCROLLBAR_BTN_SIZE);
scrollbar1->setLowValue(SCROLLBAR_BTN_SIZE);

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

//...  ... knob/slider 8

Callback handling:
Code: [Select]
// Knobs
case Knob_CALLBACK_ID + 1:
{
value = knob1->getValue();

if (invert_Knob_SYNC == true) scrollbar1->setValue(value); // Do something here ??
else scrollbar1->setValue(value); // default
_itoa_s(value, valText, 10);
reverse(valText);
char lvalue[7];
lvalue[0] = valText[0];
lvalue[1] = '\n';
lvalue[2] = valText[1];
lvalue[3] = '\n';
lvalue[4] = valText[2];
lvalue[5] = '\0';
val1->setText(lvalue);

std::cout << value << '\t' << "knob1" << '\t';

break;
}
//...  ... case 8
case Knob_CALLBACK_ID + 9:
{
invert_Knob_SYNC = !invert_Knob_SYNC;
break;
}
case KnobScroll_CALLBACK_ID + 1:
{
value = scrollbar1->getValue();

if (invert_Knob_SYNC == true) knob1->setValue(value); // Do something here as well ???
else knob1->setValue(value); // default
break;
}
//...  ... case 8

Any suggestions on how to accomplish this?
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: texus on 20 August 2016, 18:31:07
Only one of the 2 should do "255-value" while the other should still set "value". Otherwise you are endlessly inverting the values.
Title: Re: Trouble syncing scrollbar and knob widgets
Post by: Hexile on 20 August 2016, 18:47:49
Fixed. I overlooked the callback's lower half when testing before