Skip to content

Commit f8ef949

Browse files
committed
wordcram examples
1 parent 4d1ddf1 commit f8ef949

25 files changed

+6794
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
require 'ruby_wordcram'
4+
5+
class Callbacks < Propane::App
6+
include Observer # include java interface as a module to enable callbacks
7+
attr_reader :words_drawn, :words_skipped
8+
9+
def settings
10+
size 1000, 500
11+
end
12+
13+
def setup
14+
sketch_title 'Call-Backs'
15+
background 255
16+
@words_drawn = 0
17+
@words_skipped = 0
18+
WordCram.new(self)
19+
.from_text_file(data_path('kari-the-elephant.txt'))
20+
.draw_all
21+
end
22+
23+
# keep snake case for java reflection
24+
def wordsCounted(words)
25+
puts(format('counted %d words!', words.length))
26+
end
27+
28+
# keep snake case for java reflection
29+
def beginDraw
30+
puts 'drawing the sketch...'
31+
end
32+
33+
# keep snake case for java reflection
34+
def wordDrawn(word)
35+
@words_drawn += 1
36+
end
37+
38+
# keep snake case for java reflection
39+
def wordSkipped(word)
40+
@words_skipped += 1
41+
end
42+
43+
# keep snake case for java reflection
44+
def endDraw
45+
puts 'end draw!'
46+
puts(format('- skipped: %d', words_skipped))
47+
puts(format('- drawn: %d', words_drawn))
48+
end
49+
end
50+
51+
Callbacks.new
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
require 'ruby_wordcram'
4+
# How to make a wordcram that pops up extra information
5+
# when you click on a word.
6+
#
7+
# Each frame, we want to draw the wordcram, and some info
8+
# about the word that's been clicked on (or hovered over).
9+
# Each frame, you need to clean up what the last frame drew.
10+
#
11+
# Since laying out a wordcram is expensive, and there's
12+
# (currently) no way to quickly re-render a wordcram,
13+
# we don't want to make the wordcram in draw().
14+
# (Besides, each frame would probably come out different.)
15+
# Instead, render the wordcram in the setup() method, and
16+
# cache it as a PImage. Then, in draw(), render that image
17+
# first, which will overlay the last frame's image.
18+
class ClickableWords < Propane::App
19+
attr_reader :wc, :cached_image, :last_clicked_word
20+
21+
def setup
22+
sketch_title 'Clickable Words'
23+
background(255)
24+
# Make the wordcram
25+
@wc = WordCram.new(self).from_web_page('http://wikipedia.org')
26+
wc.draw_all
27+
# Save the image of the wordcram
28+
@cached_image = get
29+
# Set up styles for when we draw stuff to the screen (later)
30+
text_font(create_font('sans', 150))
31+
text_align(CENTER, CENTER)
32+
@last_clicked_word = nil
33+
end
34+
35+
def draw
36+
# First, wipe out the last frame: re-draw the cached image
37+
image(cached_image, 0, 0)
38+
# If the user's last click was on a word, render it big and blue:
39+
unless last_clicked_word.nil?
40+
no_stroke
41+
fill 255, 190
42+
rect 0, height / 2 - text_ascent / 2, width, text_ascent + text_descent
43+
fill(30, 144, 13, 150)
44+
text(last_clicked_word.word, width / 2, height / 2)
45+
end
46+
end
47+
48+
def mouse_clicked
49+
@last_clicked_word = wc.get_word_at(mouse_x, mouse_y)
50+
end
51+
52+
def settings
53+
size(700, 400)
54+
end
55+
end
56+
57+
ClickableWords.new
Binary file not shown.
82.3 KB
Binary file not shown.
43.1 KB
Loading

0 commit comments

Comments
 (0)