module Processing::HelperMethods
Public Instance Methods
Uses PImage class method under hood
# File lib/ruby-processing/helper_methods.rb, line 79 def blend_color(c1, c2, mode) Java::ProcessingCore::PImage.blendColor(c1, c2, mode) end
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.
# File lib/ruby-processing/helper_methods.rb, line 14 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
# File lib/ruby-processing/helper_methods.rb, line 38 def color(*args) return super(*args) unless args.length == 1 super(hex_color(args[0])) end
explicitly provide 'processing.org' dist instance method
# File lib/ruby-processing/helper_methods.rb, line 68 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
There's just so many functions in Processing
, Here's a convenient way to look for them.
# File lib/ruby-processing/helper_methods.rb, line 85 def find_method(method_name) reg = Regexp.new("#{method_name}", true) methods.sort.select { |meth| reg.match(meth) } end
frame_rate
needs to support reading and writing
# File lib/ruby-processing/helper_methods.rb, line 156 def frame_rate(fps = nil) return @declared_fields['frameRate'].value(java_self) unless fps super(fps) end
A nice method to run a given block for a grid. Lifted from action_coding/Nodebox.
# File lib/ruby-processing/helper_methods.rb, line 24 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
Provide a convenient handle for the Java-space version of self.
# File lib/ruby-processing/helper_methods.rb, line 120 def java_self @java_self ||= to_java(Java::ProcessingCore::PApplet) end
Fix java conversion problems getting the last key If it's ASCII, return the character, otherwise the integer
# File lib/ruby-processing/helper_methods.rb, line 114 def key int = @declared_fields['key'].value(java_self) int < 256 ? int.chr : int end
Is a key pressed for this frame?
# File lib/ruby-processing/helper_methods.rb, line 167 def key_pressed? @declared_fields['keyPressed'].value(java_self) end
lerp_color
takes three or four arguments, in Java that's two different methods, one regular and one static, so:
# File lib/ruby-processing/helper_methods.rb, line 34 def lerp_color(*args) args.length > 3 ? self.class.lerp_color(*args) : super(*args) end
Ensure that load_strings
returns a real Ruby array
# File lib/ruby-processing/helper_methods.rb, line 145 def load_strings(file_or_url) loadStrings(file_or_url).to_a end
explicitly provide 'processing.org' max instance method to return a float:- a, b and c need to be floats
# File lib/ruby-processing/helper_methods.rb, line 63 def max(*args) args.max # { |a, b| a <=> b } optional block not reqd end
explicitly provide 'processing.org' min instance method to return a float:- a, b and c need to be floats
# File lib/ruby-processing/helper_methods.rb, line 56 def min(*args) args.min # { |a,b| a <=> b } optional block not reqd end
Is the mouse pressed for this frame?
# File lib/ruby-processing/helper_methods.rb, line 162 def mouse_pressed? @declared_fields['mousePressed'].value(java_self) end
Proxy
over a list of Java declared fields that have the same name as some methods. Add to this list as needed.
# File lib/ruby-processing/helper_methods.rb, line 92 def proxy_java_fields fields = %w(sketchPath key frameRate frame mousePressed keyPressed) methods = fields.map { |field| java_class.declared_field(field) } @declared_fields = Hash[fields.zip(methods)] end
Writes an array of strings to a file, one line per string. This file is saved to the sketch's data folder
# File lib/ruby-processing/helper_methods.rb, line 151 def save_strings(filename, strings) saveStrings(filename, [strings].flatten.to_java(:String)) end
By default, your sketch path is the folder that your sketch is in. If you'd like to do something fancy, feel free.
# File lib/ruby-processing/helper_methods.rb, line 103 def set_sketch_path(spath = nil) field = @declared_fields['sketchPath'] begin field.set_value(java_self, spath || SKETCH_ROOT) rescue TypeError fail VersionError, 'Use JRubyArt for processing-3.0' end end
Get the sketch path
# File lib/ruby-processing/helper_methods.rb, line 125 def sketch_path @declared_fields['sketchPath'].value(java_self) end
Overrides Processing
convenience function thread, which takes a String arg (for a function) to more rubylike version, takes a blockā¦
# File lib/ruby-processing/helper_methods.rb, line 45 def thread(&block) if block_given? Thread.new(&block) else fail ArgumentError, 'thread must be called with a block', caller end end
Private Instance Methods
# File lib/ruby-processing/helper_methods.rb, line 184 def dist2d(*args) dx = args[0] - args[2] dy = args[1] - args[3] return 0 if dx.abs < EPSILON && dy.abs < EPSILON Math.hypot(dx, dy) end
# File lib/ruby-processing/helper_methods.rb, line 191 def dist3d(*args) dx = args[0] - args[3] dy = args[1] - args[4] dz = args[2] - args[5] return 0 if dx.abs < EPSILON && dy.abs < EPSILON && dz.abs < EPSILON Math.sqrt(dx * dx + dy * dy + dz * dz) end
parse single argument color int/double/String
# File lib/ruby-processing/helper_methods.rb, line 174 def hex_color(a) if a.is_a?(Fixnum) return Java::Monkstone::ColorUtil.colorLong(a) elsif a.is_a?(String) return Java::Monkstone::ColorUtil.colorString(a) if a =~ /#\h+/ fail StandardError, 'Dodgy Hexstring' end Java::Monkstone::ColorUtil.colorDouble(a) end