module Processing::HelperMethods
Provides some convenience methods
Public Instance Methods
Uses PImage class method under hood
# File lib/jruby_art/helper_methods.rb, line 111 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/jruby_art/helper_methods.rb, line 14 def buffer(buf_width = width, buf_height = height, renderer = @render_mode) create_graphics(buf_width, buf_height, renderer).tap do |buffer| buffer.begin_draw yield buffer buffer.end_draw end end
# File lib/jruby_art/helper_methods.rb, line 55 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/jruby_art/helper_methods.rb, line 99 def dist(*args) case args.length when 4 dist2d(*args) when 6 dist3d(*args) else raise ArgumentError, 'takes 4 or 6 parameters' end end
There's just so many functions in Processing
, Here's a convenient way to look for them.
# File lib/jruby_art/helper_methods.rb, line 117 def find_method(method_name) reg = Regexp.new(method_name.to_s, true) methods.sort.select { |meth| reg.match?(meth) } end
frame_rate
needs to support reading and writing
# File lib/jruby_art/helper_methods.rb, line 169 def frame_rate(fps = nil) return @jfields['frameRate'].value(java_self) unless fps super(fps) end
hue, sat, brightness in range 0..1.0 returns RGB color int
# File lib/jruby_art/helper_methods.rb, line 51 def hsb_color(hue, sat, brightness) Java::Monkstone::ColorUtil.hsbToRgB(hue, sat, brightness) end
# File lib/jruby_art/helper_methods.rb, line 65 def int_to_ruby_colors(p5color) warn '[DEPRECATION] `int_to_ruby_colors` is deprecated. Please use `p52ruby` instead.' p52ruby(p5color) end
Provide a convenient handle for the Java-space version of self.
# File lib/jruby_art/helper_methods.rb, line 138 def java_self @java_self ||= to_java(Java::ProcessingCore::PApplet) end
# File lib/jruby_art/helper_methods.rb, line 22 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
Fix java conversion problems getting the last key If it's ASCII, return the character, otherwise the integer
# File lib/jruby_art/helper_methods.rb, line 132 def key int = @jfields['key'].value(java_self) int < 256 ? int.chr : int end
Is a key pressed for this frame?
# File lib/jruby_art/helper_methods.rb, line 181 def key_pressed? @jfields['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/jruby_art/helper_methods.rb, line 46 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/jruby_art/helper_methods.rb, line 158 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 see above
# File lib/jruby_art/helper_methods.rb, line 94 def max(*args) args.max end
explicitly provide 'processing.org' min instance method to return a float:- a, b and c need to be floats you might choose to use ruby method directly and then provide a block to alter comparator eg args.min(&block) # { |a, b| a.value <=> b.value }
# File lib/jruby_art/helper_methods.rb, line 87 def min(*args) args.min end
Is the mouse pressed for this frame?
# File lib/jruby_art/helper_methods.rb, line 176 def mouse_pressed? @jfields['mousePressed'].value(java_self) end
# File lib/jruby_art/helper_methods.rb, line 70 def p52ruby(p5color) Java::Monkstone::ColorUtil.rubyString(p5color) end
# File lib/jruby_art/helper_methods.rb, line 30 def perspektiv( fov: Math::PI / 3.0, aspect_ratio: width.to_f / height, near_z: (height / 20.0) / tan(fov / 2.0), far_z: (height * 5) / tan(fov / 2.0) ) perspective(fov, aspect_ratio, near_z, far_z) 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/jruby_art/helper_methods.rb, line 124 def proxy_java_fields fields = %w[key frameRate mousePressed keyPressed] methods = fields.map { |field| java_class.field(field) } @jfields = 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/jruby_art/helper_methods.rb, line 164 def save_strings(filename, strings) saveStrings(filename, [strings].flatten.to_java(:String)) end
Overrides Processing
convenience function thread, which takes a String arg (for a function) to more rubylike version, takes a blockā¦
# File lib/jruby_art/helper_methods.rb, line 76 def thread(&block) warn 'A Block is Needed' unless block_given? Java::JavaLang::Thread.new(&block).start end
# File lib/jruby_art/helper_methods.rb, line 61 def web_to_color_array(web) Java::Monkstone::ColorUtil.webArray(web.to_java(:string)) end
Private Instance Methods
# File lib/jruby_art/helper_methods.rb, line 203 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/jruby_art/helper_methods.rb, line 211 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/jruby_art/helper_methods.rb, line 188 def hex_color(arg) case arg when Integer Java::Monkstone::ColorUtil.colorLong(arg) when String raise StandardError, 'Dodgy Hexstring' unless /#\h{6}$/.match?(arg) Java::Monkstone::ColorUtil.colorString(arg) when Float Java::Monkstone::ColorUtil.colorDouble(arg) else raise StandardError, 'Dodgy Color Conversion' end end