Switch

This complex component allows to define a set of branches. Only one of it can be active at a time.

Constructor


Switch <id> (string initial_branch) {
  Component <id> {...}
  ...
}
        

The branches of a Switch are classic components, that is containers to which any kind of processes can be added. The Switch has a specific text property named state, each time this property is updated the Switch looks for a branch with the same name. If it is found, the current branch is deactivated and the new one is activated.

Warning

A switch branch can't have state as a name

Action

Activates the branch with the name given as the parameter of the Switch.

Predefined children

  • TextProperty: state whose value is the name of the current active branch if it exists.

Example


use core
use base
use gui

_main_
Component root {
  Frame f ("myButton", 0, 0, 500, 500)
  Switch button (idle) {
    Component idle {
      FillColor red (255, 0, 0)
      Rectangle r (0, 0, 100, 70, 0, 0)
    }
    Component pressed {
      FillColor green (0, 255, 0)
      Rectangle r (0, 0, 100, 70, 0, 0)
    }
  }

  FSM fsm {
    State idle
    State pressed
    idle->pressed (button.idle.r.press)
    pressed->idle (button.pressed.r.release)
    pressed->idle (f.release)
  }
  fsm.state => button.state
}

run root
run syshook