Skip to content

Commit be3e804

Browse files
committed
Simpler control panel interface
1 parent d29c786 commit be3e804

File tree

23 files changed

+149
-804
lines changed

23 files changed

+149
-804
lines changed

contributed/bezier_playground.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,15 @@ def print_equation(id)
6565
def setup
6666
sketch_title 'Bezier playground'
6767
@curves = []
68-
@hide = false
6968
control_panel do |c|
7069
c.look_feel 'Nimbus'
7170
c.button :new_curve
7271
c.button :print_equations
73-
@panel = c
7472
end
7573
generate_curve
7674
end
7775

7876
def draw
79-
unless hide
80-
panel.set_visible true
81-
@hide = true
82-
end
8377
background 50
8478
draw_control_tangent_lines
8579
draw_curves
@@ -128,8 +122,7 @@ def mouse_pressed
128122
end
129123

130124
def mouse_released
131-
@control, @end_point = nil, nil
132-
@hide = false
125+
@control, @end_point = nil, nil
133126
end
134127

135128
def mouse_dragged

contributed/chladni.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Chladni plate interference surfaces
2+
# —
3+
# Based on :
4+
# http://paulbourke.net/geometry/chladni/
5+
#
6+
# —
7+
# Developed on:
8+
# - Processing 2.1.1 on MacOSX (10.9.2)
9+
# Julien Gachadoat aka v3ga
10+
# www.v3ga.net
11+
# www.2roqs.com
12+
#
13+
# Translated and tested on:
14+
# - JRubyArt-1.4.6
15+
# Martin Prout
16+
17+
attr_reader :m, :n, :epsilon, :recompute
18+
19+
def settings
20+
size(500, 500, P2D)
21+
end
22+
23+
def setup
24+
sketch_title 'Chladni'
25+
@epsilon = 0.05
26+
@recompute = true
27+
@m = 10
28+
@n = 2
29+
end
30+
31+
def draw
32+
return unless recompute
33+
@recompute = false
34+
background(255)
35+
load_pixels
36+
grid(width, height) do |x, y|
37+
chladni = cos(n * PI * x / width) * cos(m * PI * y / width) - cos(m * PI * x / width) * cos(n * PI * y / width)
38+
pixels[x + y * width] = 0 if chladni.abs <= epsilon
39+
end
40+
update_pixels
41+
format_string = "m= %d n = %d \nepsilon= %0.3f"
42+
infos = format(format_string, m, n, epsilon)
43+
fill(255, 0, 0)
44+
text(infos, 4, 16)
45+
end
46+
47+
def key_pressed
48+
case key
49+
when '+'
50+
@epsilon += 0.01
51+
when '-'
52+
@epsilon -= 0.01
53+
when CODED
54+
case key_code
55+
when UP
56+
@m += 1
57+
when DOWN
58+
@m -= 1
59+
when LEFT
60+
@n -= 1
61+
when RIGHT
62+
@n += 1
63+
end
64+
end
65+
@recompute = true
66+
@m = constrain(m, 1, 20)
67+
@n = constrain(n, 1, 20)
68+
end

contributed/jwishy.rb

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22
# It was originally adapted to Shoes in Ruby,
33
# from a Python example for Nodebox, and then, now
44
# to JRubyArt.
5-
65
# For fun, try running it using live mode, and
76
# playing with the attr_accessors, as
87
# well as the background.
9-
108
# This example now demonstrates the use of the control_panel.
11-
129
# -- omygawshkenas
13-
1410
load_library :control_panel
15-
16-
attr_reader :alpha, :back_color, :bluish, :hide, :magnitude, :panel
17-
attr_reader :x_wiggle, :y_wiggle
11+
attr_reader :alpha, :back_color, :bluish, :magnitude, :x_wiggle, :y_wiggle
1812

1913
def setup
2014
sketch_title 'Wishy Worm'
@@ -26,9 +20,7 @@ def setup
2620
c.checkbox :go_big
2721
c.button :reset
2822
c.menu :shape, %w(oval square triangle)
29-
@panel = c
3023
end
31-
@hide = false
3224
@shape = 'oval'
3325
@go_big = false
3426
@x_wiggle, @y_wiggle = 10.0, 0
@@ -49,26 +41,17 @@ def reset
4941
end
5042

5143
def draw
52-
# only make control_panel visible once, or again when hide is false
53-
unless hide
54-
@hide = true
55-
panel.set_visible(hide)
56-
end
5744
draw_background
58-
5945
# Seed the random numbers for consistent placement from frame to frame
6046
srand(0)
6147
horiz, vert, mag = x_wiggle, y_wiggle, magnitude
62-
6348
if @go_big
6449
mag *= 2
6550
vert /= 2
6651
end
67-
6852
blu = bluish
6953
x, y = (width / 2), -27
7054
c = 0.0
71-
7255
64.times do
7356
x += cos(horiz) * mag
7457
y += log10(vert) * mag + sin(vert) * 2
@@ -80,16 +63,10 @@ def draw
8063
horiz += rand * 0.25
8164
c += 0.1
8265
end
83-
8466
@x_wiggle += 0.05
8567
@y_wiggle += 0.1
8668
end
8769

88-
def mouse_pressed
89-
return unless hide
90-
@hide = false
91-
end
92-
9370
def draw_shape(args)
9471
case args[0]
9572
when 'triangle'

contributed/quadraticvertex.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,22 @@
66
# translated for JRubyArt by Martin Prout
77
# Note how to specify fill and background using hexadecimal string for color,
88
# this is different from vanilla processing
9-
#
10-
119
load_library :control_panel
1210

13-
attr_reader :debug, :save_one, :step_angle, :cr, :detail, :panel, :hide
11+
attr_reader :debug, :save_one, :step_angle, :cr, :detail
1412

1513
def setup
1614
sketch_title 'Quadratic Vertex'
17-
@hide = false
1815
control_panel do |c|
1916
c.title 'Controller'
2017
c.menu(:detail, %w(4 5 6 7 8 9 10), '7')
2118
c.checkbox :debug
2219
c.button :save_image
23-
@panel = c
2420
end
2521
@save_one = false
2622
end
2723

2824
def draw
29-
unless hide
30-
panel.set_visible true
31-
@hide = true
32-
end
3325
background color('#BDF018')
3426
translate width / 2, height / 2
3527
@step_angle = TAU / (detail.to_i - 1)
@@ -70,10 +62,6 @@ def draw
7062
@save_one = false
7163
end
7264

73-
def mouse_pressed
74-
@hide = false
75-
end
76-
7765
def cos_x(n)
7866
cos(step_angle * n) * 100
7967
end

contributed/tree.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# http://processing.org/learning/topics/tree.html
22
# by Joe Holt
3-
3+
TIME_FORMAT = "Time after this iteration: %.2f"
4+
RATE_FORMAT = 'fps = %0.1f'
45
def setup
56
sketch_title 'Tree'
67
color_mode RGB, 1
78
frame_rate 30
89
@x = 0.0
910
@dx = width / 100
10-
@start_time = Time.now
11-
@frame_time = nil
11+
@start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
12+
@frame_time = 0
1213
end
1314

1415
def draw
15-
t = Time.now
16-
if @frame_time
16+
t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
17+
if @frame_time % 10 > 8
1718
fps = 1.0 / (t - @frame_time)
18-
# printf "%0.1f fps\n", fps
19+
puts format(RATE_FORMAT, fps)
1920
end
2021
@frame_time = t
2122
background 0
@@ -35,7 +36,9 @@ def draw
3536
branch(h)
3637
@x += @dx
3738
if @x < 0
38-
puts "Time after this iteration: " + (Time.now - @start_time).to_s
39+
puts format(
40+
TIME_FORMAT, Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start_time
41+
)
3942
end
4043
if @x > width || @x < 0
4144
@dx = - @dx

examples/grid_method/luciernagas.rb

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# features use of Vec2D class and grid method
66
load_library :control_panel
77

8-
attr_reader :panel, :hide, :image_mask, :spots
8+
attr_reader :image_mask, :positions
99
ACCURACY = 4
1010

1111
# Firefly holder
12-
Flyer = Struct.new(:pos, :to_pos, :rotation, :positions)
12+
Flyer = Struct.new(:pos, :to_pos, :rotation, :tail)
1313

1414
def settings
1515
size 1024, 480, P2D
@@ -22,18 +22,16 @@ def setup
2222
control.title 'Firefly Controller'
2323
control.look_feel 'Nimbus'
2424
control.slider(:speed, 0..20, 5)
25-
control.slider(:tail_length, 0..400, 30)
25+
control.slider(:tail_length, 0..70, 30)
2626
control.slider(:rotation_max, 0..30, 7)
2727
control.slider(:target_radius, 5...100, 20)
28-
control.slider(:spot_distance, 5..200, 80)
28+
control.slider(:spot_distance, 10..100, 80)
2929
control.button :reset
30-
@panel = control
3130
end
32-
@hide = false
3331
@spotlight = create_spotlight
3432
@background = load_image(data_path('background.png'))
3533
@image_mask = load_image(data_path('mask.png'))
36-
load_spots(image_mask, ACCURACY)
34+
load_positions(image_mask, ACCURACY)
3735
reset
3836
end
3937

@@ -42,10 +40,6 @@ def reset
4240
end
4341

4442
def draw
45-
unless hide
46-
@hide = true
47-
panel.set_visible(hide)
48-
end
4943
image @background, 0, 0
5044
draw_lights
5145
draw_flyers
@@ -85,8 +79,8 @@ def draw_flyers
8579
flyer.rotation = to_rotation if flyer.rotation < to_rotation
8680
end
8781
# add tail position
88-
flyer.positions << flyer.pos.dup
89-
flyer.positions.shift while flyer.positions.size > @tail_length
82+
flyer.tail << flyer.pos.copy
83+
flyer.tail.shift while flyer.tail.length > @tail_length
9084
# set flyer position
9185
flyer.pos.x = flyer.pos.x + @speed * cos(flyer.rotation)
9286
flyer.pos.y = flyer.pos.y + @speed * sin(flyer.rotation)
@@ -103,42 +97,38 @@ def draw_flyers
10397
end
10498

10599
def create_flyer
106-
spot = rand_spot
100+
spot = positions.sample
107101
to_spot = find_spot_near spot, @spot_distance
108102
rotation = rand * TWO_PI
109103
Flyer.new(spot, to_spot, rotation, [])
110104
end
111105

112106
def draw_tail(flyer)
113-
positions = flyer.positions
114-
return unless positions && !positions.empty?
115-
alpha_add = (255 / positions.size).to_i
116-
positions.each_index do |i|
117-
stroke(255, i * alpha_add)
118-
if i < positions.size - 2
119-
line(positions[i].x, positions[i].y, positions[i + 1].x, positions[i + 1].y)
120-
end
107+
tail = flyer.tail
108+
return unless tail && !tail.empty?
109+
alpha_add = 100.0 / tail.length
110+
begin_shape(LINES)
111+
tail.each_with_index do |vec, i|
112+
stroke(color(255, i * alpha_add))
113+
vertex(vec.x, vec.y)
121114
end
115+
end_shape
122116
end
123117

124-
def load_spots(spot_image, accuracy = ACCURACY)
125-
@spots = []
118+
def load_positions(spot_image, accuracy = ACCURACY)
119+
@positions = []
126120
spot_image.load_pixels
127-
corner_color = spot_image.get 0, 0
128-
grid(spot_image.width, spot_image.height, accuracy, accuracy) do |x, y|
129-
color = spot_image.get(x, y)
130-
spots << Vec2D.new(x, y) if color != corner_color
121+
grid(width, height, accuracy, accuracy) do |x, y|
122+
positions << Vec2D.new(x, y) if color(0) == spot_image.pixels[y * width + x]
131123
end
132124
end
133125

134-
def rand_spot
135-
spots.sample
136-
end
137-
138126
def find_spot_near(pos, distance)
139-
spot = Vec2D.new(Float::INFINITY, Float::INFINITY)
140-
spot = rand_spot until spot.dist(pos) < distance
141-
spot
127+
Vec2D.new.tap do |spot|
128+
spot.x = rand(0..width) # target width
129+
spot.y = rand(140.0..310) # target text area
130+
spot = positions.sample until spot.dist(pos) < distance
131+
end
142132
end
143133

144134
def find_nearest_rotation(from, to)
@@ -155,7 +145,7 @@ def create_spotlight
155145
spotlight = buffer(size, size, P2D) do |buffer|
156146
buffer.no_stroke
157147
buffer.fill 255, 60
158-
# spotlight.fill 255, 40
148+
# spotlight.fill 255, 40
159149
buffer.ellipse half_size, half_size, half_size, half_size
160150
buffer.filter BLUR, 4
161151
end

0 commit comments

Comments
 (0)