Because this is a topic that pops up regularly, i decided to upload an example for anyone who is in need of such a useful thing. I made two rows with a diffrent number of buttons, where each row acts “radio-like”.
radiobuttons_example.gig (142.5 KB)
This is the code that is contained in the rackspace:
var
btn_u1, btn_u2, btn_u3, btn_l1, btn_l2, btn_l3, btn_l4, btn_l5 : Widget //used button's names
u_last, l_last : Integer // buffer variables to store the last pressed button
//Declaration of widget arrays for upper and lower preset buttons
u_buttons : Widget Array = [btn_u1, btn_u2, btn_u3]
l_buttons : Widget Array = [btn_l1, btn_l2, btn_l3, btn_l4, btn_l5]
Initialization //Called on first run
var i : Integer
//Check & store state of buttons - if all off, assume the first as "ON"
For i = 0; i < Size(u_buttons); i = i + 1 Do
If GetWidgetValue(u_buttons[i]) == 1.0 Then
u_last = i
Else
u_last = 0
End
End
For i = 0; i < Size(l_buttons); i = i + 1 Do
If GetWidgetValue(l_buttons[i]) == 1.0 Then
l_last = i
Else
l_last = 0
End
End
End
// Called when any of several widgets changed -> This is the block for UPPER buttons
On WidgetValueChanged(w : Widget, index: integer, newValue : double) from btn_u1, btn_u2, btn_u3
var i : integer
if newValue == 1.0 and index <> u_last then
// Deselect other buttons
for i = 0; i < Size(u_buttons); i = i + 1 do
if i <> index then SetWidgetValue(u_buttons[i],0.0) end
end
u_last = index
end
//Prevent a button to be switched off manually
If newValue == 0.0 and index == u_last Then
SetWidgetValue(u_buttons[index],1.0)
End
End
// Called when any of several widgets changed -> This is the block for LOWER buttons
On WidgetValueChanged(w : Widget, index: integer, newValue : double) from btn_l1, btn_l2, btn_l3, btn_l4, btn_l5
var i : integer
if newValue == 1.0 and index <> l_last then
// Deselect other buttons
for i = 0; i < Size(l_buttons); i = i + 1 do
if i <> index then SetWidgetValue(l_buttons[i],0.0) end
end
l_last = index
end
//Prevent a button to be switched off manually
If newValue == 0.0 and index == l_last Then
SetWidgetValue(l_buttons[index],1.0)
End
End
If you should have any questions, don’t hesitate to ask.
Cheers
Erik