Module: Processing::HelperMethods
Overview
Provides some convenience methods
Instance Method Summary collapse
-
#blend_color(c1, c2, mode) ⇒ Object
Uses PImage class method under hood.
-
#buffer(buf_width = width, buf_height = height, renderer = @render_mode) {|buf| ... } ⇒ Object
Nice block method to draw to a buffer.
- #color(*args) ⇒ Object
-
#dist(*args) ⇒ Object
explicitly provide ‘processing.org’ dist instance method.
-
#find_method(method_name) ⇒ Object
There’s just so many functions in Processing, Here’s a convenient way to look for them.
-
#frame_rate(fps = nil) ⇒ Object
frame_rate needs to support reading and writing.
-
#grid(cols, rows, col_size = 1, row_size = 1) ⇒ Object
A nice method to run a given block for a grid.
-
#java_self ⇒ Object
Provide a convenient handle for the Java-space version of self.
- #kamera(eye: Vec3D.new(width / 2.0, height / 2.0, (height / 2.0) / tan(PI * 30.0 / 180.0)), center: Vec3D.new(width / 2.0, height / 2.0, 0), up: Vec3D.new(0, 1.0, 0)) ⇒ Object
-
#key ⇒ Object
Fix java conversion problems getting the last key If it’s ASCII, return the character, otherwise the integer.
-
#key_pressed? ⇒ Boolean
Is a key pressed for this frame?.
-
#lerp_color(*args) ⇒ Object
lerp_color takes three or four arguments, in Java that’s two different methods, one regular and one static, so:.
-
#load_strings(file_or_url) ⇒ Object
Ensure that load_strings returns a real Ruby array.
-
#max(*args) ⇒ Object
explicitly provide ‘processing.org’ max instance method to return a float:- a, b and c need to be floats.
-
#min(*args) ⇒ Object
explicitly provide ‘processing.org’ min instance method to return a float:- a, b and c need to be floats.
-
#mouse_pressed? ⇒ Boolean
Is the mouse pressed for this frame?.
-
#proxy_java_fields ⇒ Object
Proxy over a list of Java declared fields that have the same name as some methods.
-
#save_strings(filename, strings) ⇒ Object
Writes an array of strings to a file, one line per string.
-
#sketch_path ⇒ Object
Get the sketch path.
-
#thread(&block) ⇒ Object
Overrides Processing convenience function thread, which takes a String arg (for a function) to more rubylike version, takes a block…
Instance Method Details
#blend_color(c1, c2, mode) ⇒ Object
Uses PImage class method under hood
84 85 86 |
# File 'lib/jruby_art/helper_methods.rb', line 84 def blend_color(c1, c2, mode) Java::ProcessingCore::PImage.blendColor(c1, c2, mode) end |
#buffer(buf_width = width, buf_height = height, renderer = @render_mode) {|buf| ... } ⇒ Object
Nice block method to draw to a buffer. You can optionally pass it a width, a height, and a renderer. Takes care of starting and ending the draw for you.
12 13 14 15 16 17 18 |
# File 'lib/jruby_art/helper_methods.rb', line 12 def buffer(buf_width = width, buf_height = height, renderer = @render_mode) buf = create_graphics(buf_width, buf_height, renderer) buf.begin_draw yield buf buf.end_draw buf end |
#color(*args) ⇒ Object
43 44 45 46 |
# File 'lib/jruby_art/helper_methods.rb', line 43 def color(*args) return super(*args) unless args.length == 1 super(hex_color(args[0])) end |
#dist(*args) ⇒ Object
explicitly provide ‘processing.org’ dist instance method
73 74 75 76 77 78 79 80 81 |
# File 'lib/jruby_art/helper_methods.rb', line 73 def dist(*args) len = args.length if len == 4 return dist2d(*args) elsif len == 6 return dist3d(*args) end fail ArgumentError, 'takes 4 or 6 parameters' end |
#find_method(method_name) ⇒ Object
There’s just so many functions in Processing, Here’s a convenient way to look for them.
90 91 92 93 |
# File 'lib/jruby_art/helper_methods.rb', line 90 def find_method(method_name) reg = Regexp.new("#{method_name}", true) methods.sort.select { |meth| reg.match(meth) } end |
#frame_rate(fps = nil) ⇒ Object
frame_rate needs to support reading and writing
147 148 149 150 |
# File 'lib/jruby_art/helper_methods.rb', line 147 def frame_rate(fps = nil) return @declared_fields['frameRate'].value(java_self) unless fps super(fps) end |
#grid(cols, rows, col_size = 1, row_size = 1) ⇒ Object
A nice method to run a given block for a grid. Lifted from action_coding/Nodebox.
29 30 31 32 33 34 35 |
# File 'lib/jruby_art/helper_methods.rb', line 29 def grid(cols, rows, col_size = 1, row_size = 1) (0...cols * rows).map do |i| x = col_size * (i % cols) y = row_size * i.div(cols) yield x, y end end |
#java_self ⇒ Object
Provide a convenient handle for the Java-space version of self.
111 112 113 |
# File 'lib/jruby_art/helper_methods.rb', line 111 def java_self @java_self ||= to_java(Java::ProcessingCore::PApplet) end |
#kamera(eye: Vec3D.new(width / 2.0, height / 2.0, (height / 2.0) / tan(PI * 30.0 / 180.0)), center: Vec3D.new(width / 2.0, height / 2.0, 0), up: Vec3D.new(0, 1.0, 0)) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/jruby_art/helper_methods.rb', line 20 def kamera( eye: Vec3D.new(width / 2.0, height / 2.0, (height / 2.0) / tan(PI * 30.0 / 180.0)), center: Vec3D.new(width / 2.0, height / 2.0, 0), up: Vec3D.new(0, 1.0, 0)) camera(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z) end |
#key ⇒ Object
Fix java conversion problems getting the last key If it’s ASCII, return the character, otherwise the integer
105 106 107 108 |
# File 'lib/jruby_art/helper_methods.rb', line 105 def key int = @declared_fields['key'].value(java_self) int < 256 ? int.chr : int end |
#key_pressed? ⇒ Boolean
Is a key pressed for this frame?
158 159 160 |
# File 'lib/jruby_art/helper_methods.rb', line 158 def key_pressed? @declared_fields['keyPressed'].value(java_self) end |
#lerp_color(*args) ⇒ Object
lerp_color takes three or four arguments, in Java that’s two different methods, one regular and one static, so:
39 40 41 |
# File 'lib/jruby_art/helper_methods.rb', line 39 def lerp_color(*args) args.length > 3 ? self.class.lerp_color(*args) : super(*args) end |
#load_strings(file_or_url) ⇒ Object
Ensure that load_strings returns a real Ruby array
136 137 138 |
# File 'lib/jruby_art/helper_methods.rb', line 136 def load_strings(file_or_url) loadStrings(file_or_url).to_a end |
#max(*args) ⇒ Object
explicitly provide ‘processing.org’ max instance method to return a float:- a, b and c need to be floats
68 69 70 |
# File 'lib/jruby_art/helper_methods.rb', line 68 def max(*args) args.max # { |a, b| a <=> b } optional block not reqd end |
#min(*args) ⇒ Object
explicitly provide ‘processing.org’ min instance method to return a float:- a, b and c need to be floats
61 62 63 |
# File 'lib/jruby_art/helper_methods.rb', line 61 def min(*args) args.min # { |a,b| a <=> b } optional block not reqd end |
#mouse_pressed? ⇒ Boolean
Is the mouse pressed for this frame?
153 154 155 |
# File 'lib/jruby_art/helper_methods.rb', line 153 def mouse_pressed? @declared_fields['mousePressed'].value(java_self) end |
#proxy_java_fields ⇒ Object
Proxy over a list of Java declared fields that have the same name as some methods. Add to this list as needed.
97 98 99 100 101 |
# File 'lib/jruby_art/helper_methods.rb', line 97 def proxy_java_fields fields = %w(sketchPath key frameRate mousePressed keyPressed) methods = fields.map { |field| java_class.declared_field(field) } @declared_fields = Hash[fields.zip(methods)] end |
#save_strings(filename, strings) ⇒ Object
Writes an array of strings to a file, one line per string. This file is saved to the sketch’s data folder
142 143 144 |
# File 'lib/jruby_art/helper_methods.rb', line 142 def save_strings(filename, strings) saveStrings(filename, [strings].flatten.to_java(:String)) end |
#sketch_path ⇒ Object
Get the sketch path
116 117 118 |
# File 'lib/jruby_art/helper_methods.rb', line 116 def sketch_path @declared_fields['sketchPath'].value(java_self) end |
#thread(&block) ⇒ Object
Overrides Processing convenience function thread, which takes a String arg (for a function) to more rubylike version, takes a block…
50 51 52 53 54 55 56 |
# File 'lib/jruby_art/helper_methods.rb', line 50 def thread(&block) if block_given? Thread.new(&block) else fail ArgumentError, 'thread must be called with a block', caller end end |