Module: Propane::HelperMethods

Included in:
App
Defined in:
lib/propane/helper_methods.rb

Defined Under Namespace

Classes: VersionError

Instance Method Summary collapse

Instance Method Details

#blend_color(c1, c2, mode) ⇒ Object

Uses PImage class method under hood



96
97
98
# File 'lib/propane/helper_methods.rb', line 96

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


10
11
12
13
14
15
16
# File 'lib/propane/helper_methods.rb', line 10

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



49
50
51
52
# File 'lib/propane/helper_methods.rb', line 49

def color(*args)
  return super(*args) unless args.length == 1
  super(hex_color(args[0]))
end

#dist(*args) ⇒ Object

explicitly provide ‘processing.org’ dist instance method

Raises:

  • (ArgumentError)


88
89
90
91
92
93
# File 'lib/propane/helper_methods.rb', line 88

def dist(*args)
  len = args.length
  return dist2d(*args) if len == 4
  return dist3d(*args) if len == 6
  raise 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.



102
103
104
105
# File 'lib/propane/helper_methods.rb', line 102

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

#frame_rate(fps = nil) ⇒ Object

frame_rate needs to support reading and writing



166
167
168
169
# File 'lib/propane/helper_methods.rb', line 166

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.



35
36
37
38
39
40
41
# File 'lib/propane/helper_methods.rb', line 35

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

#int_to_ruby_colors(hex) ⇒ Object



58
59
60
# File 'lib/propane/helper_methods.rb', line 58

def int_to_ruby_colors(hex)
  Java::Monkstone::ColorUtil.rubyString(hex)
end

#java_selfObject

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



130
131
132
# File 'lib/propane/helper_methods.rb', line 130

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

#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)) ⇒ Object



18
19
20
21
22
23
# File 'lib/propane/helper_methods.rb', line 18

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

#lerp_color(*args) ⇒ Object

lerp_color takes three or four arguments, in Java that’s two different methods, one regular and one static, so:



45
46
47
# File 'lib/propane/helper_methods.rb', line 45

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



155
156
157
# File 'lib/propane/helper_methods.rb', line 155

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

#max(*args) ⇒ Object

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



83
84
85
# File 'lib/propane/helper_methods.rb', line 83

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



76
77
78
# File 'lib/propane/helper_methods.rb', line 76

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

#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)) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/propane/helper_methods.rb', line 25

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_java_fieldsObject

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



109
110
111
112
113
# File 'lib/propane/helper_methods.rb', line 109

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

#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



161
162
163
# File 'lib/propane/helper_methods.rb', line 161

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.



120
121
122
123
124
125
126
127
# File 'lib/propane/helper_methods.rb', line 120

def set_sketch_path(spath = nil)
  field = @declared_fields['sketchPath']
  begin
    field.set_value(java_self, spath || SKETCH_ROOT)
  rescue TypeError
    raise VersionError, 'Use JRubyArt for processing-3.0'
  end
end

#sketch_pathObject

Get the sketch path



135
136
137
# File 'lib/propane/helper_methods.rb', line 135

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…



65
66
67
68
69
70
71
# File 'lib/propane/helper_methods.rb', line 65

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

#web_to_color_array(web) ⇒ Object



54
55
56
# File 'lib/propane/helper_methods.rb', line 54

def web_to_color_array(web)
  Java::Monkstone::ColorUtil.webArray(web)
end