Last active
December 21, 2015 10:58
-
-
Save monkstone/6295147 to your computer and use it in GitHub Desktop.
control_panel.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Here's a little library for quickly hooking up controls to sketches. | |
# For messing with the parameters and such. | |
# These controls will set instance variables on the sketches. | |
# You can make sliders, checkboxes, buttons, and drop-down menus. | |
# (optionally) pass the range and default value. | |
module ControlPanel | |
class Slider < javax.swing.JSlider | |
def initialize(control_panel, name, range, initial_value, proc=nil) | |
min = range.begin * 100 | |
max = ((range.exclude_end? && range.begin.respond_to?(:succ)) ? range.max : range.end) * 100 | |
super(min, max) | |
set_minor_tick_spacing((max - min).abs / 10) | |
set_paint_ticks true | |
paint_labels = true | |
set_preferred_size(java.awt.Dimension.new(190, 30)) | |
label = control_panel.add_element(self, name) | |
add_change_listener do | |
update_label(label, name, value) | |
$app.instance_variable_set("@#{name}", value) unless value.nil? | |
proc.call(value) if proc | |
end | |
set_value(initial_value ? initial_value*100 : min) | |
fire_state_changed | |
end | |
def value | |
get_value / 100.0 | |
end | |
def update_label(label, name, value) | |
value = value.to_s | |
value << "0" if value.length < 4 | |
label.set_text "<html><br><b>#{name.to_s}: #{value}</b></html>" | |
end | |
end | |
class Menu < javax.swing.JComboBox | |
def initialize(control_panel, name, elements, initial_value, proc=nil) | |
super(elements.to_java(:String)) | |
set_preferred_size(java.awt.Dimension.new(190, 30)) | |
control_panel.add_element(self, name) | |
add_action_listener do | |
$app.instance_variable_set("@#{name}", value) unless value.nil? | |
proc.call(value) if proc | |
end | |
set_selected_index(initial_value ? elements.index(initial_value) : 0) | |
end | |
def value | |
get_selected_item | |
end | |
end | |
class Checkbox < javax.swing.JCheckBox | |
def initialize(control_panel, name, proc=nil) | |
@control_panel = control_panel | |
super(name.to_s) | |
set_preferred_size(java.awt.Dimension.new(190, 64)) | |
set_horizontal_alignment javax.swing.SwingConstants::CENTER | |
control_panel.add_element(self, name, false) | |
add_action_listener do | |
$app.instance_variable_set("@#{name}", value) unless value.nil? | |
proc.call(value) if proc | |
end | |
end | |
def value | |
is_selected | |
end | |
end | |
class Button < javax.swing.JButton | |
def initialize(control_panel, name, proc=nil) | |
super(name.to_s) | |
set_preferred_size(java.awt.Dimension.new(170, 64)) | |
control_panel.add_element(self, name, false, true) | |
add_action_listener do | |
$app.send(name.to_s) | |
proc.call(value) if proc | |
end | |
end | |
end | |
class Panel < javax.swing.JFrame | |
java_import javax.swing.UIManager | |
attr_accessor :elements | |
def initialize | |
super() | |
@elements = [] | |
@panel = javax.swing.JPanel.new(java.awt.FlowLayout.new(1, 0, 0)) | |
end | |
def look_feel lf = "Metal" | |
lafs = javax.swing.UIManager::getInstalledLookAndFeels.select{|info| info.getName.eql? lf} | |
javax.swing.UIManager::setLookAndFeel(lafs[0].getClassName) if lafs.size > 0 | |
end | |
def display | |
add @panel | |
set_size 200, 30 + (64 * @elements.size) | |
set_default_close_operation javax.swing.JFrame::HIDE_ON_CLOSE | |
set_resizable false | |
end | |
def add_element(element, name, has_label=true, button=false) | |
if has_label | |
label = javax.swing.JLabel.new("<html><br><b>#{name}</b></html>") | |
@panel.add label | |
end | |
@elements << element | |
@panel.add element | |
return has_label ? label : nil | |
end | |
def remove | |
remove_all | |
dispose | |
end | |
def slider(name, range=0..100, initial_value = nil, &block) | |
slider = Slider.new(self, name, range, initial_value, block || nil) | |
end | |
def menu(name, elements, initial_value = nil, &block) | |
menu = Menu.new(self, name, elements, initial_value, block || nil) | |
end | |
def checkbox(name, initial_value = nil, &block) | |
checkbox = Checkbox.new(self, name, block || nil) | |
checkbox.do_click if initial_value == true | |
end | |
def button(name, &block) | |
button = Button.new(self, name, block || nil) | |
end | |
end | |
module InstanceMethods | |
def control_panel | |
@control_panel = ControlPanel::Panel.new unless @control_panel | |
return @control_panel unless block_given? | |
yield(@control_panel) | |
@control_panel.display | |
end | |
end | |
end | |
Processing::App.send :include, ControlPanel::InstanceMethods |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This one has a long lineage: | |
# It was originally adapted to Shoes in Ruby, | |
# from a Python example for Nodebox, and then, now | |
# to Ruby-Processing. | |
# For fun, try running it via jirb, and | |
# playing with the attr_accessors, as | |
# well as the background. | |
# This example now demonstrates the use of the control_panel. | |
# -- omygawshkenas | |
load_library :control_panel | |
attr_accessor :x_wiggle, :y_wiggle, :magnitude, :bluish, :panel, :laf, :hide | |
def setup | |
size 600, 600 | |
@show = false | |
control_panel do |c| | |
c.look_feel "Nimbus" | |
c.slider :bluish, 0.0..1.0, 0.5 | |
c.slider :alpha, 0.0..1.0, 0.5 | |
c.checkbox :go_big | |
c.button :reset | |
c.menu :shape, ['oval', 'square'] | |
@panel = c | |
end | |
@hide = false | |
@shape = 'oval' | |
@alpha, @bluish = 0.5, 0.5 | |
@x_wiggle, @y_wiggle = 10.0, 0 | |
@magnitude = 8.15 | |
@background = [0.06, 0.03, 0.18] | |
color_mode RGB, 1 | |
ellipse_mode CORNER | |
smooth | |
end | |
def background=(*args) | |
@background = args.flatten | |
end | |
def draw_background | |
@background[3] = @alpha | |
fill *@background if @background[0] | |
rect 0, 0, width, height | |
end | |
def reset | |
@y_wiggle = 0 | |
end | |
def draw | |
unless hide | |
@hide = true | |
panel.setVisible(hide) | |
end | |
# panel is visible if sketch shown | |
draw_background | |
# Seed the random numbers for consistent placement from frame to frame | |
srand(0) | |
horiz, vert, mag = @x_wiggle, @y_wiggle, @magnitude | |
if @go_big | |
mag *= 2 | |
vert /= 2 | |
end | |
blu = bluish | |
x, y = (self.width / 2), -27 | |
c = 0.0 | |
64.times do | |
x += cos(horiz)*mag | |
y += log10(vert)*mag + sin(vert) * 2 | |
fill(sin(@y_wiggle + c), rand * 0.2, rand * blu, 0.5) | |
s = 42 + cos(vert) * 17 | |
args = [x-s/2, y-s/2, s, s] | |
@shape == 'oval' ? oval(*args) : rect(*args) | |
vert += rand * 0.25 | |
horiz += rand * 0.25 | |
c += 0.1 | |
end | |
@x_wiggle += 0.05 | |
@y_wiggle += 0.1 | |
end | |
def mouse_pressed | |
@hide = false if hide | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alternative way of controlling control_panel visibility, much more flexible.