Module: Processing::HelperMethods
- Included in:
- App
- Defined in:
- lib/ruby-processing/helper_methods.rb
Instance Method Summary collapse
-
#buffer(buf_width = width, buf_height = height, renderer = @render_mode) {|buf| ... } ⇒ Object
Nice block method to draw to a buffer.
- #color(*args) ⇒ Object
-
#find_method(method_name) ⇒ Object
There's just so many functions in Processing, Here's a convenient way to look for them.
- #frame_count ⇒ Object
-
#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.
-
#key ⇒ Object
Fix java conversion problems getting the last key If it's ASCII, return the character, otherwise the integer.
- #key_code ⇒ Object
-
#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.
-
#loop(&block) ⇒ Object
Overrides convenience function loop, to add ability to loop over a block if supplied, otherwise perform as the PApplet class would.
- #mouse_button ⇒ Object
-
#mouse_pressed? ⇒ Boolean
Is the mouse pressed for this frame?.
-
#mouse_x ⇒ Object
Fields that should be made accessible as under_scored.
- #mouse_y ⇒ Object
- #pmouse_x ⇒ Object
- #pmouse_y ⇒ Object
-
#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.
-
#set_sketch_path(path = nil) ⇒ Object
By default, your sketch path is the folder that your sketch is in.
-
#sketch_path ⇒ Object
Get the sketch path.
-
#thread(*args, &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
#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.
7 8 9 10 11 12 13 |
# File 'lib/ruby-processing/helper_methods.rb', line 7 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ruby-processing/helper_methods.rb', line 31 def color(*args) a = args[0] # convert to signed int if args.length == 1 if a.is_a?(Fixnum) && a >= 2**31 args = [ a - 2**32 ] elsif a.is_a?(String) && a[0] == ?# h = a[1..-1] # add opaque alpha channel if h.size <= 6 h = "ff" + "0"*(6-h.size) + h end return color(h.hex) end end super(*args) end |
#find_method(method_name) ⇒ Object
There's just so many functions in Processing, Here's a convenient way to look for them.
73 74 75 76 |
# File 'lib/ruby-processing/helper_methods.rb', line 73 def find_method(method_name) reg = Regexp.new("#{method_name}", true) self.methods.sort.select {|meth| reg.match(meth)} end |
#frame_count ⇒ Object
118 |
# File 'lib/ruby-processing/helper_methods.rb', line 118 def frame_count; frameCount; end |
#frame_rate(fps = nil) ⇒ Object
frame_rate needs to support reading and writing
135 136 137 138 |
# File 'lib/ruby-processing/helper_methods.rb', line 135 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.
17 18 19 20 21 22 23 |
# File 'lib/ruby-processing/helper_methods.rb', line 17 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.
103 104 105 |
# File 'lib/ruby-processing/helper_methods.rb', line 103 def java_self @java_self ||= self.to_java(Java::ProcessingCore::PApplet) end |
#key ⇒ Object
Fix java conversion problems getting the last key If it's ASCII, return the character, otherwise the integer
97 98 99 100 |
# File 'lib/ruby-processing/helper_methods.rb', line 97 def key int = @declared_fields['key'].value(java_self) int < 256 ? int.chr : int end |
#key_code ⇒ Object
120 |
# File 'lib/ruby-processing/helper_methods.rb', line 120 def key_code; keyCode; end |
#key_pressed? ⇒ Boolean
Is a key pressed for this frame?
146 147 148 |
# File 'lib/ruby-processing/helper_methods.rb', line 146 def key_pressed? field = @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:
27 28 29 |
# File 'lib/ruby-processing/helper_methods.rb', line 27 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
124 125 126 |
# File 'lib/ruby-processing/helper_methods.rb', line 124 def load_strings(file_or_url) loadStrings(file_or_url).to_a end |
#loop(&block) ⇒ Object
Overrides convenience function loop, to add ability to loop over a block if supplied, otherwise perform as the PApplet class would
51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby-processing/helper_methods.rb', line 51 def loop(&block) if block_given? while true do yield end else super end end |
#mouse_button ⇒ Object
119 |
# File 'lib/ruby-processing/helper_methods.rb', line 119 def ; mouseButton; end |
#mouse_pressed? ⇒ Boolean
Is the mouse pressed for this frame?
141 142 143 |
# File 'lib/ruby-processing/helper_methods.rb', line 141 def mouse_pressed? field = @declared_fields['mousePressed'].value(java_self) end |
#mouse_x ⇒ Object
Fields that should be made accessible as under_scored.
114 |
# File 'lib/ruby-processing/helper_methods.rb', line 114 def mouse_x; mouseX; end |
#mouse_y ⇒ Object
115 |
# File 'lib/ruby-processing/helper_methods.rb', line 115 def mouse_y; mouseY; end |
#pmouse_x ⇒ Object
116 |
# File 'lib/ruby-processing/helper_methods.rb', line 116 def pmouse_x; pmouseX; end |
#pmouse_y ⇒ Object
117 |
# File 'lib/ruby-processing/helper_methods.rb', line 117 def pmouse_y; pmouseY; 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.
80 81 82 83 84 |
# File 'lib/ruby-processing/helper_methods.rb', line 80 def proxy_java_fields @declared_fields = {} fields = %w(sketchPath key frameRate frame mousePressed keyPressed) fields.each {|f| @declared_fields[f] = java_class.declared_field(f) } 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
130 131 132 |
# File 'lib/ruby-processing/helper_methods.rb', line 130 def save_strings(filename, strings) saveStrings(filename, [strings].flatten.to_java(:String)) end |
#set_sketch_path(path = nil) ⇒ Object
By default, your sketch path is the folder that your sketch is in. If you'd like to do something fancy, feel free.
88 89 90 91 |
# File 'lib/ruby-processing/helper_methods.rb', line 88 def set_sketch_path(path=nil) field = @declared_fields['sketchPath'] field.set_value(java_self, path || SKETCH_ROOT) end |
#sketch_path ⇒ Object
Get the sketch path
109 110 111 |
# File 'lib/ruby-processing/helper_methods.rb', line 109 def sketch_path @declared_fields['sketchPath'].value(java_self) end |
#thread(*args, &block) ⇒ Object
Overrides Processing convenience function thread, which takes a String arg (for a function) to more rubylike version, takes a block...
63 64 65 66 67 68 69 |
# File 'lib/ruby-processing/helper_methods.rb', line 63 def thread(*args, &block) if block_given? Thread.new *args, &block else raise ArgumentError, "thread must be called with a block" , caller end end |