Skip to content

Commit bb871c6

Browse files
committed
add ruby hook examples
1 parent 0a205de commit bb871c6

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env jruby
2+
# Hooky class demonstrates how to use the post_initialize hook, creating three
3+
# sketches on a single thread in propane
4+
require 'propane'
5+
6+
class Hooky < Propane::App
7+
attr_reader :back, :title
8+
9+
def setup
10+
sketch_title title
11+
end
12+
13+
def post_initialize(args)
14+
@back = args.fetch(:back_color)
15+
@title = args.fetch(:title)
16+
end
17+
18+
def settings
19+
size 200, 200
20+
end
21+
22+
def draw
23+
background(color(back))
24+
end
25+
end
26+
27+
colors = %w[#ff0000 #00ff00 #0000ff].freeze
28+
title = %w[one two three].freeze
29+
opt_values = colors.zip(title)
30+
31+
options = opt_values.flat_map { |values| [back_color: values[0], title: values[1]] }
32+
options.each do |opts|
33+
Hooky.new(opts)
34+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env jruby
2+
# Creating threaded instance of Hooky maty not be mush use because we create a
3+
# global $app
4+
require_relative 'lib/hooky_instance'
5+
6+
colors = %w[#ff0000 #00ff00 #0000ff].freeze
7+
title = %w[one two three].freeze
8+
opt_values = colors.zip(title)
9+
10+
options = opt_values.flat_map { |values| [back_color: values[0], title: values[1]] }
11+
options.each do |opts|
12+
java.lang.Thread.new(Hooky.new(opts)).start
13+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env jruby
2+
# Hooky class demonstrates how to use the post_initialize hook with java
3+
# thread
4+
require 'propane'
5+
6+
class Hooky < Propane::App
7+
include java.lang.Runnable
8+
attr_reader :back, :title
9+
10+
def setup
11+
sketch_title title
12+
end
13+
14+
def post_initialize(args)
15+
@back = args.fetch(:back_color)
16+
@title = args.fetch(:title)
17+
end
18+
19+
def settings
20+
size 200, 200
21+
end
22+
23+
def draw
24+
background(color(back))
25+
end
26+
27+
def run
28+
puts "starting #{title}"
29+
end
30+
end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
4+
class SubClassSketch < Propane::App
5+
6+
def setup
7+
sketch_title 'Subclass with a Hook'
8+
@arm = SpinArm.new(x: width / 2, y: height / 2, s: 0.01)
9+
@spots = SpinSpots.new(x: width / 2, y: height / 2, s: -0.02, d: 90.0)
10+
end
11+
12+
def draw
13+
background 204
14+
@arm.display
15+
@spots.display
16+
end
17+
18+
def settings
19+
size 640, 360
20+
end
21+
end
22+
23+
class Spin
24+
include Propane::Proxy
25+
attr_accessor :x, :y, :speed
26+
attr_accessor :angle
27+
28+
def initialize(args = {})
29+
@x, @y = args.fetch(:x, 0), args.fetch(:y, 0)
30+
@speed = args.fetch(:s, 0.1)
31+
@angle = args.fetch(:angle, 0.0)
32+
post_initialize(args) # this is the hook
33+
end
34+
35+
def update
36+
@angle += speed
37+
end
38+
39+
def post_initialize(_args)
40+
nil
41+
end
42+
43+
end
44+
45+
class SpinArm < Spin # inherit from (or "extend") class Spin
46+
# NB: initialize inherited from Spin class
47+
48+
def display
49+
stroke_weight 1
50+
stroke 0
51+
push_matrix
52+
translate x, y
53+
update
54+
rotate angle
55+
line 0, 0, 165, 0
56+
pop_matrix
57+
end
58+
end
59+
60+
class SpinSpots < Spin
61+
attr_accessor :dim
62+
63+
def post_initialize(args)
64+
@dim = args.fetch(:d, 10)
65+
end
66+
67+
def display
68+
no_stroke
69+
push_matrix
70+
translate x, y
71+
update
72+
rotate angle
73+
ellipse(-dim / 2, 0, dim, dim)
74+
ellipse(dim / 2, 0, dim, dim)
75+
pop_matrix
76+
end
77+
end
78+
79+
80+
SubClassSketch.new

0 commit comments

Comments
 (0)