rl ·
The statemachine is an often overlooked module in the toolbox, but sometimes complicated logic can be expressed much simpler using a state machine.
This example shows a nice usage for it: a button that shows it's "pressed" state. It is part of a scrollbar implementation that is intended to work like a standard windows scrollbar, painted using SM drawing primitives.
Button in normal state:
Button (arrow down) in pressed state:
How the button works:
When the button is pressed, it shows it's pressed state. - if the mouse is released, the button fires and shows it's normal state again, ready. - if the mouse is moved outside, the button shows it's normal state, but still recognizes that it is pressed. - - if the mouse is released outside nothing happens, the button returns into normal state, ready. - - if the mouse is moved inside again (still pressed) the button shows it's pressed state again.
Schematic explanation:
In the center of the schematic LDown, DragXY and MOver create triggers, these switch the numbers 0, 1, 2, 3 to the input of the state machine. The string input defines the state machine rules, giving 4 rules for 4 states: For example the second rule reads:
in state 1: if input = 0 -> remain in state 1 if input = 1 -> switch to state 3 if input = 2 -> switch to state 2 if input = 3 -> switch to state 1
Right of the state-machine is a little logic to fire a trigger (mouse released) when state 3 is reached. Additionally the state-machine is initialized (instant transition back to state 0)
The pressed state is displayed as a transparent grey button overlay, either beeing fully (normal, state != 1) or partially (pressed, state=1) transparent (upper right part of schematic). Thus looking pretty close to the scrollbar widget of the mozilla "modern"-skin.