Cabbage Logo
Back to Cabbage Site

Button with more than 2 states?

Hi, a bit of an odd request maybe, but I’m looking for a way to make a button with three states (0, 1, 2). Is this possible with the button widget or do I need to make my own to achieve this?

Edit: Actually I think I’m onto something now, will post when it’s done

gkButton_Test chnget "TestButton"
gkShapeArray[] fillarray giShapeHanning, giShapeBartlett, giShapeRectangle
gkArrayNum init 0
gkShape = gkShapeArray[gkArrayNum]

Stest_1 = "Test_1"
Stest_2 = "Test_2"
Stest_3 = "Test_3"

if (changed2(gkButton_Test) == 1) then    
    gkArrayNum += 1
        if gkArrayNum == 3 then
        gkArrayNum = 0
    endif
endif   

if gkShape == gkShapeArray[0] then
    SIdent sprintfk "text(%s)", Stest_1
    chnset SIdent, "testIdent"
    
elseif gkShape == gkShapeArray[1] then
    SIdent sprintfk "text(%s)", Stest_2
    chnset SIdent, "testIdent" 
    
elseif gkShape == gkShapeArray[2] then
    SIdent sprintfk "text(%s)", Stest_3
    chnset SIdent, "testIdent" 
    
endif

Here’s a way to do it :+1: :grin:

That’s it. And it’s definitely not the oddest thing to have been asked on this list :rofl:

2 Likes

Okey, so I have a bit of a problem with it when combined with the preset system. Notice both of the buttons have 3 states. Is there any way to set the maximum states on the button? Or is a custom button necessary?

CabbageButtonWeird

I’m not sure I get the problem? Do you not set the max when you do this:

if gkArrayNum == 3 then
        gkArrayNum = 0

An image can be made to behave like a button too, but I’m not sure you need to create your own here?

Look at how it swaps automatically to another state when switching preset. I think it has something to do with it switching between value 0 and 1 in the DAW, but not sure.

Right. So it doesn’t really matter what state the button is at. As soon as you change it, it is going to increment by 1, and the presets only save the current state of the binary button, rather than the state of your ternary button! You’ve created a nice little mess for yourself here :rofl:

Hmm, I’m just thinking about how best to address it. Let me mock up something. Hang on…

I think it’s because of the “if (changed2(gkButton_Test) == 1)”, because when I switch the preset and they don’t have the same value it will trigger this if-statement right?

Yes, I think so. The preset only saves the value of the button, but doesn’t know where it is in the cycle of 3. I’ve one possible solution here. I use a label to be the button, and I place an invisible combobox behind it. When users click on the label, they increment the combobox underneath.

3StateButtonSolution.csd (1.1 KB)

The preset system will save the combobox state, and whenever it changes, it should update the text of the label. I used a label because its channel won’t appear in the DAW. But the combobox channel will, so it will be automatable. I had to jump through some i-time/k-time hoops to get this to work. That’s the reason I use the event opcode to trigger updates.

Nice, I will try to implement this. Also, I noticed that in the docs it doesn’t mention that you can change fontsize, thought it would be nice for others to know about it.

Did you by any chance take a look at the preset problem with presets deleting? Doing a beta soon, so I will send it to you when the beta is ready :wink:

Going to take a look now. I haven’t updated the docs in a while. I will do for the next public release. I have a few things, which I’ve discussed on the forum, that I want to get out there for testing first.

1 Like

Hmm, so when I integrate it in my code, this happens. ThreeStateButton

It’s because of the kdur of the event-opcode, but how did you manage to run yours forever?

Edit: Oh nevermind, it’s because I ran instrument 21 also in the score section.

1 Like

Works quite nicely Rory, great job! :metal:

Edit: Or actually, now that I tried saving presets with it, it’s stuck on the same state in all presets :sob:

Actually, It might be from the old presets in the .snaps file, will test some more…

Still something weird about it, hmm…

So, the presets are not updating at all now?

Nono, only the three state buttons are adding 1 extra number when I switch preset. Other parameters are working fine.

I’ll try the example I created in a DAW. Hang on…

Okey nice, notice the print from p4 in the console window of the gif. It keeps adding +1

I think I may have painted myself into a corner with this approach! I think we just got lucky it work at all. The problem is that we are depending on the label channel value to trigger the update:

if changed2(kLabel) == 1 then        
        event "i", 2, 0, 1, kIndex
        kIndex = kIndex < 3 ? kIndex+1 : 1
endif

but of course this only works if the label value has changes, and in some case it won’t have changed. Hmm, back to square one! The system has be agnostic of the label channel value. I’ll keep thinking. Worst case scenario, I can add a custom button for this, but I would like to work out a native solution if I can.

Okey, so I should probably switch over to comboboxes instead for now?

That might be the best solution for now. If it works with comboboxes, I will add a new custom switchable state button. I can use the combobox as my base class. We just need a name for this new widget? What about optionbutton?