Cabbage Logo
Back to Cabbage Site

Toggle image changes when button is pressed

Hello,

What is the best way to switch between 2 images when a(ny) button is pressed. Assuming the original png image “wifi3.png” is already on the widget, I want to cabbageSet “wifi”, “file(“wifi1.png”)” when kToggle == 1 or whatever button is pressed

aOutL = aOutL * (kToggle == 1 ? kGating : 1)
aOutR = aOutR * (kToggle == 1 ? kGating : 1)

Thank you
Wen

You can try toggling the visibility between two images when you meet a certain condition.

kUpdateOnMetronome metro 10 ; or however many Hz you want
if kToggle == 1 && kUpdateOnMetronome == 1 then
    cabbageSet 1, "image_of_wifi3", "visible", 1
    cabbageSet 1, "image_of_wifi1", "visible", 0
elseif kUpdateOnMetronome == 1 then
    cabbageSet 1, "image_of_wifi3", "visible", 0
    cabbageSet 1, "image_of_wifi1", "visible", 1
endif

Try to keep cabbageSet behind an if-statement or something that is not triggered at every step of the instrument, because the opcode will lower performance and eat resources. That is why I added kUpdateOnMetronome, so that you can define how fast you want the images to update.

EDIT: my bad, I thought you wanted a constant change, like a light indicator. Go ahead and check out the cabbageSet opcode provided in Cabbage.

I ought to leave a better answer. You can handle functionality on a button press if the value of its channel changes. Like so:

; get button channel
kWifi cabbageGetValue "btnWifi"

; update image on button press - check if value of channel changes
if changed(kWifi) == 1 then
    ; check if button is on
    if kWifi == 1 then
        cabbageSet 1, "image_of_wifi3", "visible", 1
        cabbageSet 1, "image_of_wifi1", "visible", 0
    else ; button is off
        cabbageSet 1, "image_of_wifi3", "visible", 0
        cabbageSet 1, "image_of_wifi1", "visible", 1
    endif
endif

This way cabbageSet won’t murder your runtime. You can further improve this by multiplying the value of 1 or 0 you pass for “visible” by kWifi so you don’t need another if-else check.

if changed(kWifi) == 1 then
    cabbageSet 1, "image_of_wifi3", "visible", 1 * kWifi
    cabbageSet 1, "image_of_wifi1", "visible", 1 - 1 * kWifi
endif

In hindsight, this can also work if you make kWifi a sort of “light indicator” for if your effect is functioning or not (bad Wi-Fi signal-gate triggered).

1 Like