Skip to content

Commit 911b0ce

Browse files
committed
keyEvent example
1 parent 37a28e9 commit 911b0ce

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env jruby
2+
# A simple demonstration of vanilla processing 'reflection' methods using
3+
# propane :library_proxy.
4+
# See library/my_library/my_library.rb code for the guts.
5+
require 'propane'
6+
7+
class KeyEventDemo < Propane::App
8+
load_libraries :library_proxy, :my_library
9+
10+
attr_reader :visible
11+
12+
def settings
13+
size 300, 200
14+
@visible = false
15+
end
16+
17+
def setup
18+
sketch_title 'KeyEvent Demo'
19+
MyLibrary.new self
20+
end
21+
22+
def hide(val) # we call this method from my_library
23+
@visible = !val
24+
end
25+
26+
def draw
27+
if visible
28+
fill(0, 0, 200)
29+
ellipse(170, 115, 70, 100)
30+
else
31+
background 0
32+
end
33+
end
34+
end
35+
36+
KeyEventDemo .new
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
6+
attr_reader :app
7+
8+
def initialize(parent)
9+
@app = parent
10+
end
11+
12+
def draw # optional
13+
fill app.color(200, 0, 0, 100) # partially transparent
14+
app.rect 100, 100, 60, 90
15+
end
16+
17+
# favor guard clause no_op unless key pressed
18+
# and no_op unless ascii key else we can't use :chr
19+
def keyEvent(e) # NB: need camel case for reflection to work
20+
return unless e.get_action == KeyEvent::PRESS
21+
return if e.get_key >= 'z'.ord
22+
case e.get_key.chr.upcase
23+
when 'S'
24+
app.send :hide, false
25+
when 'H'
26+
app.send :hide, true
27+
else
28+
puts e.get_key.chr
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)