Skip to content

Instantly share code, notes, and snippets.

@monkstone
Last active May 6, 2017 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/91f5f84622c47b836da04d09c57d77d3 to your computer and use it in GitHub Desktop.
Save monkstone/91f5f84622c47b836da04d09c57d77d3 to your computer and use it in GitHub Desktop.

Inspired by Nodebox, JRubyArt (copied from ruby-processing) provides a way to control the instance variables of your sketch with a control panel. You can create sliders, buttons, menus and checkboxes that set instance variables on your sketch. Since ruby-processing-2.0 you need to explicitly set the panel visible from the processing sketch (see included examples). Start by loading in the control_panel library, and then define your panel like so:

  load_library :control_panel
  attr_reader :panel, :hide

  def setup
    size(200, 200)
    @hide = false
    control_panel do |c|
      c.look_feel "Nimbus"  # since processing-2.1.4 set look and feel (optional, metal by default)
      c.slider :opacity
      c.slider(:app_width, 5..60, 20) { reset! }
      c.menu(:options, ['one', 'two', 'three'], 'two') {|m| load_menu_item(m) }
      c.checkbox :paused
      c.button :reset!
      @panel = true
    end
  end
  
  def draw
    unless hide
      panel.set_visible true
      @hide = true
    end
  # Rest of the code follows

This code will create a sketch with a control panel for adjusting the value of the @opacity, @app_width, @options, and @paused instance variables. The button will call the reset! method when clicked (a method defined by you in your sketch). The app_width slider will range from 5 to 60 instead of (the default) 0 to 100. The instance variable will be initialized at 20 when the sketch is loaded. The app_width and options controls have had callbacks attached to them. The callbacks will run, passing in the value of the control, any time the control changes. It all looks like this:

control panel

(control_panel replaces the previous has_slider functionality)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment