{
   "is_private" : null,
   "id" : "1-5ff",
   "code" : "(\r\n\r\nvar win = Window( \"Switch\", Rect( 0, 300, 200, 300 ) );\r\nvar switch;\r\nvar demo_view = UserView();\r\nvar demo_string = \"Hello World\";\r\n\r\nvar demo_states = \t[\r\n\t[\"Never\", Color.black, Color.white, Color.red ],\r\n\t[\"Gonna\", Color.black, Color.white, Color.green ]\r\n].asList;\r\n\r\n~get_switch = { | input_states, default_state, font, has_inside_margin |\r\n\tvar switch = UserView();\r\n\tvar states = input_states;\r\n\tvar current_state = default_state;\r\n\tvar border_color2_ratio = 0.5;\r\n\tvar binded_function = nil;\r\n\r\n\tswitch.drawFunc = { | view |\r\n\t\tvar margin = view.bounds.height * 0.01;\r\n\r\n\t\tif( margin < 4, { margin = 4 } );\r\n\t\tPen.fillColor_( states[current_state][2] );\r\n\t\tPen.fillRect(\r\n\t\t\tRect(\r\n\t\t\t\t0,\r\n\t\t\t\t0,\r\n\t\t\t\tview.bounds.width,\r\n\t\t\t\tview.bounds.height\r\n\t\t\t)\r\n\t\t);\r\n\t\tPen.fillColor_(\r\n\t\t\tColor(\r\n\t\t\t\tstates[current_state][2].red * border_color2_ratio,\r\n\t\t\t\tstates[current_state][2].green * border_color2_ratio,\r\n\t\t\t\tstates[current_state][2].blue * border_color2_ratio\r\n\t\t\t)\r\n\t\t);\r\n\t\tPen.fillRect(\r\n\t\t\tRect(\r\n\t\t\t\tmargin / 2,\r\n\t\t\t\tmargin / 2,\r\n\t\t\t\tview.bounds.width - margin,\r\n\t\t\t\tview.bounds.height - margin\r\n\t\t\t)\r\n\t\t);\r\n\t\tif( has_inside_margin, {\r\n\t\t\tPen.fillColor_( Color.black );\r\n\t\t\tPen.fillRect(\r\n\t\t\t\tRect(\r\n\t\t\t\t\tmargin,\r\n\t\t\t\t\tmargin,\r\n\t\t\t\t\tview.bounds.width - ( margin * 2 ),\r\n\t\t\t\t\tview.bounds.height - ( margin * 2 )\r\n\t\t\t\t)\r\n\t\t\t);\r\n\t\t\tmargin = margin * 2;\r\n\t\t} );\r\n\t\tPen.fillColor_( states[current_state][3] );\r\n\t\tPen.fillRect(\r\n\t\t\tRect(\r\n\t\t\t\tmargin,\r\n\t\t\t\tmargin,\r\n\t\t\t\tview.bounds.width - ( margin * 2 ),\r\n\t\t\t\tview.bounds.height - ( margin * 2 )\r\n\t\t\t)\r\n\t\t);\r\n\t\tPen.stringCenteredIn(\r\n\t\t\tstates[current_state][0],\r\n\t\t\tRect(\r\n\t\t\t\t0,\r\n\t\t\t\t0,\r\n\t\t\t\tview.bounds.width,\r\n\t\t\t\tview.bounds.height,\r\n\t\t\t),\r\n\t\t\tfont,\r\n\t\t\tstates[current_state][1]\r\n\t\t);\r\n\t};\r\n\tswitch.mouseDownAction = {\r\n\t\tcurrent_state = current_state + 1;\r\n\t\tif( current_state == states.size, { current_state = 0 } );\r\n\t\tif( binded_function != nil, { binded_function.value( current_state ) } );\r\n\t\tswitch.refresh\r\n\t};\r\n\r\n\tswitch.addUniqueMethod( \\bindFunction, { | object, function | binded_function = function } );\r\n\r\n\tswitch.addUniqueMethod( \\updateStates, { | object, new_states |\r\n\t\tstates = new_states;\r\n\t\tif( current_state >= states.size, { current_state = 0 } );\r\n\t\tswitch.refresh;\r\n\t} );\r\n\r\n\tswitch.addUniqueMethod( \\setState, { | object, new_state |\r\n\t\tif( ( ( new_state >= 0 ) && ( new_state <= states.size ) ), {\r\n\t\t\tcurrent_state = new_state;\r\n\t\t\tswitch.refresh;\r\n\t\t} );\r\n\t} );\r\n\r\n\tswitch.addUniqueMethod( \\valueState, { | object, new_state |\r\n\t\tif( ( ( new_state >= 0 ) && ( new_state <= states.size ) ), {\r\n\t\t\tcurrent_state = new_state;\r\n\t\t\tif( binded_function != nil, { binded_function.value( current_state ) } );\r\n\t\t\tswitch.refresh;\r\n\t\t} );\r\n\t} );\r\n\tswitch\r\n};\r\n\r\ndemo_states.add( [\"Give\", Color.black, Color.white, Color.blue ] );\r\n\r\ndemo_view.drawFunc = { | view |\r\n\tPen.stringCenteredIn(\r\n\t\tdemo_string,\r\n\t\tRect(\r\n\t\t\t0,\r\n\t\t\t0,\r\n\t\t\tview.bounds.width,\r\n\t\t\tview.bounds.height\r\n\t\t)\r\n\t)\r\n};\r\n\r\ndemo_states.add( [\"You\", Color.black, Color.white, Color.cyan ] );\r\n\r\nswitch = ~get_switch.value(\r\n\tdemo_states,\r\n\t0,\r\n\tFont.default,\r\n\ttrue\r\n);\r\n\r\ndemo_states.add( [\"Up\", Color.black, Color.white, Color.magenta ] );\r\n\r\nswitch.updateStates( demo_states );\r\nswitch.bindFunction( { | index |\r\n\tdemo_string = demo_states[ index ][0];\r\n\tdemo_view.refresh;\r\n} );\r\nswitch.valueState( 0 );\r\n\r\nwin.layout_( VLayout( demo_view, switch ) );\r\nwin.front\r\n\r\n)",
   "labels" : [
      "gui",
      "tool",
      "interface",
      "menu"
   ],
   "description" : "Demo of a custom switch button, allowing you to cycle between several states. Connect to a function using view.bindFunction( function ). Supports layout integration, colors customisation, states and current state reassignment.",
   "ancestor_list" : [],
   "author" : "Dindoléon",
   "name" : "Switch"
}
