Newbie: setState()


#1

I have an imageButton and I want it to stay in a down state after being pressed and return to the normal state when pressed again. I tried using setState() but that returns things to normal once the mouse leaves the button area. Is there a way to do this? I also tried setting setToggleState to true but I don’t know if that is making any difference. Any ideas?


#2

You have to call setClickingTogglesState(true) on the button to tell it to act as a toggle. It should then toggle on each click without you having to do anything else. You can then use getToggleState() to see if the button is ‘on’ or not.


#3

Thanks Dave, I tried that but still no joy. Here’s my code:

button = new ImageButton(T("test"));
Image* toolbarImage = ImageFileFormat::loadFrom(File::File("test.png"));
button->setImages(true,true,true,
                        toolbarImage, 
		1.0f,Colours::transparentWhite,
                        0, 0.5f, Colours::transparentWhite,
                        0, 0.0f, Colours::transparentWhite,
                        0.0f); 

addAndMakeVisible(button);
button->addButtonListener(this);
button->setClickingTogglesState(true);
button->setBounds(5,15,50,90);
button->setVisible(true);

Any other ideas? I don’t have a down or over image but I’m assuming that the opaqueness of the button would change once it’s hit? The same thing still happens as before, i.e., it works while the mouse is over the button but then returns to normal once the mouse leaves the button area.


#4

I see what you mean. Anyone feel free to correct me as I’m fairly new to c++ and Juce but there doesn’t seem to be a reference to getToggleState() anywhere in the paint methods of ImageButton in the same way as in TextButton eg. findColour (getToggleState() ? buttonOnColourId : buttonColourId).

I guess they are just not designed have their look stay with the state the same way as TextButtons are. It is the buttonColourId that changes with the state, not the image, overlay or opacity.

I supose you could modify a LookAndFeel to use this (I haven’t tested this yet though):

getLookAndFeel().drawImageButton (g, im, imageX, imageY, imageW, imageH,
                                  getToggleState() ? downOverlay : (isButtonDown ? downOverlay
                                                       				 : (isMouseOverButton ? overOverlay
                                                                            			      : normalOverlay)),
                                  getToggleState() ? downOverlay : (isButtonDown ? downOpacity
                                                       				 : (isMouseOverButton ? overOpacity
                                                                            			      : normalOpacity)),
                                  *this);

or something similar. Sound right?

You could also modify the ImageButton::getCurrentImage() to return the down image if the toggle state is ‘on’ if you really need to.


#5

Thanks again Dave. I’ll look at some other ways of doing this although I’m hesitant to go near the look and feel class, I’m trying to keep things simple. I’ll see if it’s possible with a drawable button.


#6

I actually just checked in some changes last week that allow exactly this!


#7

Thanks Jules. I’ll try out the latest source now.