When you download Ruby-Processing, you get a folder of scripts for generating blank sketches,
applets, and applications, a handful of libraries, and this: the entire Ruby-Processing source.
1
2
3
4
5
6
7
8
9
10
11
12 require 'java'
13
14 module Processing
15
16
17 require File.expand_path(File.dirname(__FILE__)) + "/core.jar" unless Object.const_defined?(:JRUBY_APPLET)
18 include_package "processing.core"
19
20 class App < PApplet
21
22 include_class "javax.swing.JFrame"
23 include_class "javax.swing.JPanel"
24 include_class "javax.swing.JLabel"
25
26
27 attr_accessor :frame, :title
28 alias_method :oval, :ellipse
29 alias_method :stroke_width, :stroke_weight
30 alias_method :rgb, :color
31 alias_method :gray, :color
32
33
34
35 METHODS_TO_WATCH_FOR = { :mouse_pressed => :mousePressed,
36 :mouse_dragged => :mouseDragged,
37 :mouse_clicked => :mouseClicked,
38 :mouse_moved => :mouseMoved,
39 :mouse_released => :mouseReleased,
40 :key_pressed => :keyPressed,
41 :key_released => :keyReleased }
42
43 def self.method_added(method_name)
44 if METHODS_TO_WATCH_FOR.keys.include?(method_name)
45 alias_method METHODS_TO_WATCH_FOR[method_name], method_name
46 end
47 end
48
49 def self.current=(app); @current_app = app; end
50 def self.current; @current_app; end
51 def self.slider_frame; @slider_frame; end
52
53
54 @@loaded_libraries = Hash.new(false)
55 def self.library_loaded?(folder)
56 @@loaded_libraries[folder.to_sym]
57 end
58 def library_loaded?(folder); self.class.library_loaded?(folder); end
59
60
61
62
63 def self.load_ruby_library(folder)
64 unless @@loaded_libraries[folder.to_sym]
65 Object.const_defined?(:JRUBY_APPLET) ? prefix = "" : prefix = "library/"
66 @@loaded_libraries[folder.to_sym] = require "#{prefix}#{folder}/#{folder}"
67 end
68 return @@loaded_libraries[folder.to_sym]
69 end
70
71
72
73
74 def self.load_java_library(folder)
75 unless @@loaded_libraries[folder.to_sym]
76 if Object.const_defined?(:JRUBY_APPLET)
77 @@loaded_libraries[folder.to_sym] = true if JRUBY_APPLET.get_parameter("archive").match(%r(#{folder}))
78 else
79 base = "library#{File::SEPARATOR}#{folder}#{File::SEPARATOR}"
80 jars = Dir.glob("#{base}*.jar")
81 base2 = "#{base}library#{File::SEPARATOR}"
82 jars = jars + Dir.glob("#{base2}*.jar")
83 jars.each {|jar| require jar }
84 return false if jars.length == 0
85
86 sep = java.io.File.pathSeparator
87 path = java.lang.System.getProperty("java.library.path")
88 new_path = base + sep + base + "library" + sep + path
89 java.lang.System.setProperty("java.library.path", new_path)
90 field = java.lang.Class.for_name("java.lang.ClassLoader").get_declared_field("sys_paths")
91 if field
92 field.accessible = true
93 field.set(java.lang.Class.for_name("java.lang.System").get_class_loader, nil)
94 end
95 @@loaded_libraries[folder.to_sym] = true
96 end
97 end
98 return @@loaded_libraries[folder.to_sym]
99 end
100
101
102
103 def self.has_slider(name, range=0..100)
104 return if Object.const_defined?(:JRUBY_APPLET)
105 min, max = range.begin * 100, range.end * 100
106 attr_accessor name
107 initialize_slider_frame unless @slider_frame
108 slider = Slider.new(min, max)
109 slider.add_change_listener do
110 val = slider.get_value / 100.0
111 slider.update_label(val)
112 Processing::App.current.send(name.to_s + "=", val)
113 end
114 slider.set_minor_tick_spacing((max - min).abs / 10)
115 slider.set_paint_ticks true
116 slider.paint_labels = true
117 label = JLabel.new("<html><br>" + name.to_s + "</html>")
118 slider.label = label
119 slider.name = name
120 @slider_frame.sliders << slider
121 @slider_frame.panel.add label
122 @slider_frame.panel.add slider
123 end
124
125 def initialize(options = {})
126 super()
127 App.current = self
128 options = {:width => 400,
129 :height => 400,
130 :title => "",
131 :full_screen => false}.merge(options)
132 @width, @height, @title = options[:width], options[:height], options[:title]
133 display options
134 display_slider_frame if self.class.slider_frame
135 end
136
137 def setup() end
138 def draw() end
139
140 def display_slider_frame
141 f = self.class.slider_frame
142 f.add f.panel
143 f.set_size 200, 32 + (71 * f.sliders.size)
144 f.setDefaultCloseOperation(JFrame::DISPOSE_ON_CLOSE)
145 f.set_resizable false
146 f.set_location(@width + 10, 0)
147 f.show
148 end
149
150 def display_full_screen(graphics_env)
151 @frame = java.awt.Frame.new
152 mode = graphics_env.display_mode
153 @width, @height = mode.get_width, mode.get_height
154 gc = graphics_env.get_default_configuration
155 @frame.set_undecorated true
156 @frame.set_background java.awt.Color.black
157 @frame.set_layout java.awt.BorderLayout.new
158 @frame.add(self, java.awt.BorderLayout::CENTER)
159 @frame.pack
160 graphics_env.set_full_screen_window @frame
161 @frame.set_location(0, 0)
162 @frame.show
163 self.init
164 self.request_focus
165 end
166
167 def display_in_a_window
168 @frame = JFrame.new(@title)
169 @frame.add(self)
170 @frame.setSize(@width, @height + 22)
171 @frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
172 @frame.setResizable(false)
173 @frame.show
174 self.init
175 end
176
177 def display_in_an_applet
178 JRUBY_APPLET.setSize(@width, @height)
179 JRUBY_APPLET.background_color = nil
180 JRUBY_APPLET.double_buffered = false
181 JRUBY_APPLET.add self
182 JRUBY_APPLET.validate
183
184 JRUBY_APPLET.on_stop { self.stop }
185 JRUBY_APPLET.on_destroy { self.destroy }
186 self.init
187 end
188
189
190 def display(options)
191 if Object.const_defined?(:JRUBY_APPLET)
192 display_in_an_applet
193 elsif options[:full_screen]
194 graphics_env = java.awt.GraphicsEnvironment.get_local_graphics_environment.get_default_screen_device
195 graphics_env.is_full_screen_supported ? display_full_screen(graphics_env) : display_in_a_window
196 else
197 display_in_a_window
198 end
199 end
200
201
202
203 def find_method(method_name)
204 reg = Regexp.new("#{method_name}", true)
205 self.methods.sort.select {|meth| reg.match(meth)}
206 end
207
208
209 def render_mode(mode_const)
210 size(@width, @height, mode_const)
211 end
212
213 def mouse_x; mouseX; end
214 def mouse_y; mouseY; end
215 def pmouse_x; pmouseX; end
216 def pmouse_y; pmouseY; end
217
218 def close
219 @frame.dispose
220 end
221
222 def quit
223 java.lang.System.exit(0)
224 end
225
226
227 private
228
229 def self.initialize_slider_frame
230 @slider_frame ||= JFrame.new
231 class << @slider_frame
232 attr_accessor :sliders, :panel
233 end
234 @slider_frame.sliders ||= []
235 slider_panel ||= JPanel.new(java.awt.FlowLayout.new(1, 0, 0))
236 @slider_frame.panel = slider_panel
237 end
238
239 end
240
241 class Slider < javax.swing.JSlider
242 attr_accessor :name, :label
243
244 def initialize(*args)
245 super(*args)
246 end
247
248 def update_label(value)
249 value = value.to_s
250 value << "0" if value.length < 4
251 label.set_text "<html><br>#{@name.to_s}: #{value}</html>"
252 end
253 end
254
255 end
256