Module: Processing::HelperMethods

Included in:
App
Defined in:
lib/ruby-processing/helper_methods.rb

Instance Method Summary collapse

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.

Yields:

  • (buf)


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
# File 'lib/ruby-processing/helper_methods.rb', line 31

def color(*args)
  a = args[0]
  # convert to signed int
  if args.length == 1 && a.is_a?(Fixnum) && a >= 2**31
    args = [ a - 2**32 ]
  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.



42
43
44
45
# File 'lib/ruby-processing/helper_methods.rb', line 42

def find_method(method_name)
  reg = Regexp.new("#{method_name}", true)
  self.methods.sort.select {|meth| reg.match(meth)}
end

#frame_countObject



87
# File 'lib/ruby-processing/helper_methods.rb', line 87

def frame_count;  frameCount;   end

#frame_rate(fps = nil) ⇒ Object

frame_rate needs to support reading and writing



104
105
106
107
# File 'lib/ruby-processing/helper_methods.rb', line 104

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_selfObject

Provide a convenient handle for the Java-space version of self.



72
73
74
# File 'lib/ruby-processing/helper_methods.rb', line 72

def java_self
  @java_self ||= Java.ruby_to_java self
end

#keyObject

Fix java conversion problems getting the last key If it’s ASCII, return the character, otherwise the integer



66
67
68
69
# File 'lib/ruby-processing/helper_methods.rb', line 66

def key
  int = @declared_fields['key'].value(java_self)
  int < 256 ? int.chr : int
end

#key_codeObject



89
# File 'lib/ruby-processing/helper_methods.rb', line 89

def key_code;     keyCode;      end

#key_pressed?Boolean

Is a key pressed for this frame?

Returns:

  • (Boolean)


115
116
117
# File 'lib/ruby-processing/helper_methods.rb', line 115

def key_pressed?
  Java.java_to_primitive(java_class.field("keyPressed").value(java_object))
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



93
94
95
# File 'lib/ruby-processing/helper_methods.rb', line 93

def load_strings(file_or_url)
  loadStrings(file_or_url).to_a
end

#mouse_buttonObject



88
# File 'lib/ruby-processing/helper_methods.rb', line 88

def mouse_button; mouseButton;  end

#mouse_pressed?Boolean

Is the mouse pressed for this frame?

Returns:

  • (Boolean)


110
111
112
# File 'lib/ruby-processing/helper_methods.rb', line 110

def mouse_pressed?
  Java.java_to_primitive(java_class.field("mousePressed").value(java_object))
end

#mouse_xObject

Fields that should be made accessible as under_scored.



83
# File 'lib/ruby-processing/helper_methods.rb', line 83

def mouse_x;      mouseX;       end

#mouse_yObject



84
# File 'lib/ruby-processing/helper_methods.rb', line 84

def mouse_y;      mouseY;       end

#pmouse_xObject



85
# File 'lib/ruby-processing/helper_methods.rb', line 85

def pmouse_x;     pmouseX;      end

#pmouse_yObject



86
# File 'lib/ruby-processing/helper_methods.rb', line 86

def pmouse_y;     pmouseY;      end

#proxy_java_fieldsObject

Proxy over a list of Java declared fields that have the same name as some methods. Add to this list as needed.



49
50
51
52
53
# File 'lib/ruby-processing/helper_methods.rb', line 49

def proxy_java_fields
  @declared_fields = {}
  fields = %w(sketchPath key frameRate frame)
  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



99
100
101
# File 'lib/ruby-processing/helper_methods.rb', line 99

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.



57
58
59
60
# File 'lib/ruby-processing/helper_methods.rb', line 57

def set_sketch_path(path=nil)
  field = @declared_fields['sketchPath']
  field.set_value(java_self, path || SKETCH_ROOT)
end

#sketch_pathObject

Get the sketch path



78
79
80
# File 'lib/ruby-processing/helper_methods.rb', line 78

def sketch_path
  @declared_fields['sketchPath'].value(java_self)
end