· 8 years ago · May 26, 2018, 02:36 AM
1# Ruby-Processing is for Code Art.
2# Send suggestions, ideas, and hate-mail to jashkenas [at] gmail.com
3# Also, send samples and libraries.
4#
5# This class is a thin wrapper around Processing's PApplet.
6# Most of the code here is for interfacing with Swing,
7# web applets, going fullscreen, and drawing sliders.
8#
9# Revision 0.8
10# - omygawshkenas
11
12require 'java'
13
14module Processing
15
16 # Conditionally load core.jar
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 # Alias some methods for familiarity for Shoes coders.
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 # Watch the definition of these methods, to make sure
34 # that Processing is able to call them during events.
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 # Detect if a library has been loaded (for conditional loading)
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 # For pure ruby libs.
61 # The library should have an initialization ruby file
62 # of the same name as the library folder.
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 # Loading libraries which include native code needs to
72 # hack the Java ClassLoader, so that you don't have to
73 # futz with your PATH. But its probably bad juju.
74 def self.load_java_library(folder)
75 unless @@loaded_libraries[folder.to_sym]
76 if Object.const_defined?(:JRUBY_APPLET) # Applets preload all the java libraries.
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 # Here goes...
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 # Creates a slider, in a new window, to control an instance variable.
102 # Sliders take a name and a range (optionally), returning an integer.
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 inspect
138 "#<Processing::App:#{self.class}:#{@title}>"
139 end
140
141 def setup() end
142 def draw() end
143
144 def display_slider_frame
145 f = self.class.slider_frame
146 f.add f.panel
147 f.set_size 200, 32 + (71 * f.sliders.size)
148 f.setDefaultCloseOperation(JFrame::DISPOSE_ON_CLOSE)
149 f.set_resizable false
150 f.set_location(@width + 10, 0)
151 f.show
152 end
153
154 def display_full_screen(graphics_env)
155 @frame = java.awt.Frame.new
156 mode = graphics_env.display_mode
157 @width, @height = mode.get_width, mode.get_height
158 gc = graphics_env.get_default_configuration
159 @frame.set_undecorated true
160 @frame.set_background java.awt.Color.black
161 @frame.set_layout java.awt.BorderLayout.new
162 @frame.add(self, java.awt.BorderLayout::CENTER)
163 @frame.pack
164 graphics_env.set_full_screen_window @frame
165 @frame.set_location(0, 0)
166 @frame.show
167 self.init
168 self.request_focus
169 end
170
171 def display_in_a_window
172 @frame = JFrame.new(@title)
173 @frame.add(self)
174 @frame.setSize(@width, @height + 22)
175 @frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
176 @frame.setResizable(false)
177 @frame.show
178 self.init
179 end
180
181 def display_in_an_applet
182 JRUBY_APPLET.setSize(@width, @height)
183 JRUBY_APPLET.background_color = nil
184 JRUBY_APPLET.double_buffered = false
185 JRUBY_APPLET.add self
186 JRUBY_APPLET.validate
187 # Add the callbacks to peacefully expire.
188 JRUBY_APPLET.on_stop { self.stop }
189 JRUBY_APPLET.on_destroy { self.destroy }
190 self.init
191 end
192
193 # Tests to see which display method should run.
194 def display(options)
195 if Object.const_defined?(:JRUBY_APPLET) # Then display it in an applet.
196 display_in_an_applet
197 elsif options[:full_screen] # Then display it fullscreen.
198 graphics_env = java.awt.GraphicsEnvironment.get_local_graphics_environment.get_default_screen_device
199 graphics_env.is_full_screen_supported ? display_full_screen(graphics_env) : display_in_a_window
200 else # Then display it in a window.
201 display_in_a_window
202 end
203 end
204
205 # There's just so many functions in Processing,
206 # Here's a convenient way to look for them.
207 def find_method(method_name)
208 reg = Regexp.new("#{method_name}", true)
209 self.methods.sort.select {|meth| reg.match(meth)}
210 end
211
212 # Specify what rendering Processing should use.
213 def render_mode(mode_const)
214 size(@width, @height, mode_const)
215 end
216
217 def mouse_x; mouseX; end
218 def mouse_y; mouseY; end
219 def pmouse_x; pmouseX; end
220 def pmouse_y; pmouseY; end
221
222 def close
223 @frame.dispose
224 end
225
226 def quit
227 java.lang.System.exit(0)
228 end
229
230
231 private
232
233 def self.initialize_slider_frame
234 @slider_frame ||= JFrame.new
235 class << @slider_frame
236 attr_accessor :sliders, :panel
237 end
238 @slider_frame.sliders ||= []
239 slider_panel ||= JPanel.new(java.awt.FlowLayout.new(1, 0, 0))
240 @slider_frame.panel = slider_panel
241 end
242
243 end
244
245 class Slider < javax.swing.JSlider
246 attr_accessor :name, :label
247
248 def initialize(*args)
249 super(*args)
250 end
251
252 def update_label(value)
253 value = value.to_s
254 value << "0" if value.length < 4
255 label.set_text "<html><br>#{@name.to_s}: #{value}</html>"
256 end
257 end
258
259end