This is also a follow up to the Button Picture State tutorial and shows various types of buttons like toggles and disabled buttons and some common use cases. Below are the six possible button’s picture states again, listed in the right order. However via KSP (script) we can only address the on and off state. So unfortunately, the hover & pressed state can neither be read nor set. Well fortunately in most cases at least the pressed state equals the on state in terms of the functionality.
- off
- on
- pressed off
- pressed on
- mouse hover off
- mouse hover on
CONTENT
-
How to SET a button state
-
How to read a button state
-
How to SET a button state via set_control_par
-
How to read a button state via set_control_par
-
simple use cases
-
precursor: customizing buttons (animations & picture states)
How to SET a button state
on init declare ui_button $myButton $myButton := 1 {sets the button into the on state} end on
This is a simple as it can be.
declare ui_button $myButton
first we declare a button of any name. In this case $myButton.
And then we change the button’s state by simply setting the $myButton variable to 0 or 1.
But keep in mind again that there are only 2 options: 0 (off state) & 1 (on state), no hover or pressed state.
$myButton1 := 1
sets the button to the on state.
This automatically affects the picture state if you have set any. You really don’t have to take care about anything other than setting a picture with the 6 button states.
Learn more about creating & setting a picture with all 6 button states
$myButton1 := 0
sets the button to the off state.
This automatically affects the picture state if you have set any. You really don’t have to take care about anything other than setting a picture with the 6 button states.
Learn more about creating & setting a picture with all 6 button states
How to read a button state
on init declare ui_button $myButton end on on ui_control($myButton) message($myButton) end on
This is as simple as setting a button state.
$myButton
always holds the actual Button state (0 = off, 1 = on), and gets automatically updated whenever the button is used
The state can then can be read throughout the whole code, by simply using $myButton as it is a global variable.
on init declare ui_button $myButton declare $buttonState end on on ui_control($myButton) $buttonState := $myButton end on on note message($myButton & $buttonState) {in this case both $buttonState & $myButton hold the same values, but are still independent variables} end on
Of course, we can also use the $myButton variable like any other variable and pass its value on to another variable.
The example above doesn’t seem to make much sense since both variables always hold the same value but these variables are independent from each other.
So we can work with the $buttonState variable without ever affecting the actual button state which may be a big advantage in some cases.
How to SET a button state via set_control_par
on init declare ui_button $myButton set_control_par(get_ui_id($myButton), $CONTROL_PAR_VALUE, 1) {sets the button into the on state, including the picture state} end on
Well, this method requires some more code, but has the advantage that we can now toggle several buttons at once like shown in the Switching Multiple Buttons example, below
How to read a button state via set_control_par
on init declare ui_button $myButton declare $buttonState end on on ui_control($myButton) $buttonState := get_control_par(get_ui_id($myButton), $CONTROL_PAR_VALUE) message($buttonState) end on
get_control_par(get_ui_id($myButton),...
simply gets the button state. Again its more code like simply reading the $myButton variable, but this way we can read several button states at once in a while loop.
examples & use cases
free premium content
unlock the full article
including all downloads!
unlock premium content
Leave a Reply