Skip to content

Commit 4baf7b3

Browse files
committed
new functionality for LibraryProxy
1 parent 86c1e62 commit 4baf7b3

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# A simple demonstration of vanilla processing 'reflection' methods using
2+
# JRubyArt :library_proxy.
3+
# See library/my_library/my_library.rb code for the guts.
4+
load_libraries :library_proxy, :my_library
5+
6+
attr_reader :visible
7+
8+
def settings
9+
size 300, 200
10+
@visible = false
11+
end
12+
13+
def setup
14+
sketch_title 'KeyEvent Demo'
15+
MyLibrary.new self
16+
end
17+
18+
def hide(val)
19+
@visible = !val
20+
end
21+
22+
def draw
23+
if visible
24+
fill(0, 0, 200)
25+
ellipse(170, 115, 70, 100)
26+
else
27+
background 0
28+
end
29+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This class demonstrates how by inheriting from the abstract class CustomProxy
2+
# we can access 'keyEvent' and 'draw' (Note we need a draw method even
3+
# though can be empty)
4+
class MyLibrary < LibraryProxy
5+
java_import 'processing.event.KeyEvent'
6+
7+
attr_reader :app
8+
9+
def initialize(parent)
10+
@app = parent
11+
end
12+
13+
def draw # optional
14+
fill app.color(200, 0, 0, 100)
15+
app.rect 100, 100, 60, 90
16+
end
17+
18+
# favor guard clause no_op unless key pressed
19+
# and no_op unless ascii key
20+
def keyEvent(e) # NB: need camel case for reflection to work
21+
return unless e.get_action == KeyEvent::PRESS
22+
return if e.get_key > 122 # else we can't use :chr
23+
case e.get_key.chr.upcase
24+
when 'S'
25+
app.send :hide, false
26+
when 'H'
27+
app.send :hide, true
28+
else
29+
puts e.get_key.chr
30+
end
31+
end
32+
end
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# proxy_library.rb
22
# LibraryProxy is an AbstractJava class to give us access to the java
3-
# reflection methods 'pre', 'post' and 'draw' (which we should implement)
3+
# reflection methods 'pre', 'post' and 'draw' (latter we should implement)
4+
# also mouseEvent(e) and keyEvent(e) NB: need camel-case for reflection
45
# width, height, fill, stroke, background are available, otherwise use app.
56
class TestProxy < LibraryProxy # subclass LibraryProxy
6-
def mousePressed(event); end # empty method is OK
7-
def keyPressed(event); end # empty method is OK
8-
def pre; end # empty method is OK
9-
7+
# draw is abstract, empty method would be OK
108
def draw
119
fill 200
1210
app.ellipse(width / 2, height / 2, 20, 20)
1311
end
1412

15-
def post; end # empty method is OK
13+
# def mouseEvent(e)
14+
# use e in block to access MouseEvent
15+
# end
1616
end

0 commit comments

Comments
 (0)