Module: Processing::HelperMethods

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

Instance Method Summary collapse

Instance Method Details

#blend_color(c1, c2, mode) ⇒ Object

Uses PImage class method under hood



124
125
126
# File 'lib/ruby-processing/helper_methods.rb', line 124

def blend_color(c1, c2, mode)
  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.

Yields:

  • (buf)


8
9
10
11
12
13
14
# File 'lib/ruby-processing/helper_methods.rb', line 8

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-processing/helper_methods.rb', line 32

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].eql?('#')
      h = a[1..-1].rjust(6, '0').prepend('ff')
      return color(h.hex)
    end
  end
  super(*args)
end

#constrain(amt, low, high) ⇒ Object

explicitly provide ‘processing.org’ constrain instance method to return a float:- amt, low and high need to be floats



119
120
121
# File 'lib/ruby-processing/helper_methods.rb', line 119

def constrain(amt, low, high)
  (low..high).clip(amt)
end

#dist(*args) ⇒ Object

explicitly provide ‘processing.org’ dist instance method



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ruby-processing/helper_methods.rb', line 100

def dist(*args)
  len = args.length
  if len == 4
    dx = args[0] - args[2]
    dy = args[1] - args[3]
    return 0 if dx.abs < EPSILON && dy.abs < EPSILON
    return Math.hypot(dx, dy)
  elsif len == 6
    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
    return Math.sqrt(dx * dx + dy * dy + dz * dz)
  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.



130
131
132
133
# File 'lib/ruby-processing/helper_methods.rb', line 130

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

#frame_countObject



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

def frame_count;  frameCount;   end

#frame_rate(fps = nil) ⇒ Object

frame_rate needs to support reading and writing



195
196
197
198
# File 'lib/ruby-processing/helper_methods.rb', line 195

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.



18
19
20
21
22
23
24
# File 'lib/ruby-processing/helper_methods.rb', line 18

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.



158
159
160
# File 'lib/ruby-processing/helper_methods.rb', line 158

def java_self
  @java_self ||= to_java(Java::ProcessingCore::PApplet)
end

#keyObject

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



152
153
154
155
# File 'lib/ruby-processing/helper_methods.rb', line 152

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

#key_codeObject



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

def key_code;     keyCode;      end

#key_pressed?Boolean

Is a key pressed for this frame?

Returns:

  • (Boolean)


206
207
208
# File 'lib/ruby-processing/helper_methods.rb', line 206

def key_pressed?
  @declared_fields['keyPressed'].value(java_self)
end

#lerp(start, stop, amt) ⇒ Object

explicitly provide ‘processing.org’ lerp instance method



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

def lerp(start, stop, amt)
  start + (stop - start) * amt
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:



28
29
30
# File 'lib/ruby-processing/helper_methods.rb', line 28

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



184
185
186
# File 'lib/ruby-processing/helper_methods.rb', line 184

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

#map(value, start1, stop1, start2, stop2) ⇒ float

Explicitly provides ‘processing.org’ map instance method, in which value is mapped from range 1, to range 2 (NB: values are not clamped to range 1). It may be better to explicitly write your own interpolate function

Parameters:

  • value (float)

    input

  • start1, (range)

    stop1

  • start1, (range)

    stop2

Returns:

  • (float)

    mapped value



64
65
66
# File 'lib/ruby-processing/helper_methods.rb', line 64

def map(value, start1, stop1, start2, stop2)
  start2 + (stop2 - start2) * ((value - start1).to_f / (stop1 - start1))
end

#map1d(val, r_in, r_out) ⇒ Object

ruby alternative implementation of map using range parameters (begin..end) and excluded end (begin…end) produce the same result



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

def map1d(val, r_in, r_out)
  r_out.begin + (r_out.end - r_out.begin) *
    ((val - r_in.begin).to_f / (r_in.end - r_in.begin))
end

#max(*args) ⇒ Object

explicitly provide ‘processing.org’ max instance method to return a float:- a, b and c need to be floats



95
96
97
# File 'lib/ruby-processing/helper_methods.rb', line 95

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



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

def min(*args)
  args.min  #  { |a,b| a <=> b } optional block not reqd
end

#mouse_buttonObject



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

def mouse_button; mouseButton;  end

#mouse_pressed?Boolean

Is the mouse pressed for this frame?

Returns:

  • (Boolean)


201
202
203
# File 'lib/ruby-processing/helper_methods.rb', line 201

def mouse_pressed?
  @declared_fields['mousePressed'].value(java_self)
end

#mouse_xObject

Fields that should be made accessible as under_scored.



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

def mouse_x;      mouseX;       end

#mouse_yObject



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

def mouse_y;      mouseY;       end

#norm(value, start, stop) ⇒ Object

explicitly provide ‘processing.org’ norm instance method



76
77
78
# File 'lib/ruby-processing/helper_methods.rb', line 76

def norm(value, start, stop)
  (value - start).to_f / (stop - start)
end

#pmouse_xObject



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

def pmouse_x;     pmouseX;      end

#pmouse_yObject



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

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.



137
138
139
140
141
# File 'lib/ruby-processing/helper_methods.rb', line 137

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



190
191
192
# File 'lib/ruby-processing/helper_methods.rb', line 190

def save_strings(filename, strings)
  saveStrings(filename, [strings].flatten.to_java(:String))
end

#set_sketch_path(spath = 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.



145
146
147
148
# File 'lib/ruby-processing/helper_methods.rb', line 145

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

#sketch_pathObject

Get the sketch path



164
165
166
# File 'lib/ruby-processing/helper_methods.rb', line 164

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…



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

def thread(&block)
  if block_given?
    Thread.new(&block)
  else
    fail ArgumentError, 'thread must be called with a block', caller
  end
end