Module: Propane::HelperMethods

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

Overview

Provides some convenience methods

Instance Method Summary collapse

Instance Method Details

#blend_color(c1, c2, mode) ⇒ Object

Uses PImage class method under hood



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

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)


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

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



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

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



92
93
94
95
96
97
98
99
100
101
# File 'lib/propane/helper_methods.rb', line 92

def dist(*args)
  case args.length
  when 4
    dist2d(*args)
  when 6
    dist3d(*args)
  else
    raise ArgumentError, 'takes 4 or 6 parameters'
  end
end

#find_method(method_name) ⇒ Object

There's just so many functions in Processing, Here's a convenient way to look for them.



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

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



162
163
164
165
166
# File 'lib/propane/helper_methods.rb', line 162

def frame_rate(fps = nil)
  return @declared_fields['frameRate'].value(java_self) unless fps

  super(fps)
end

#hsb_color(hue, sat, brightness) ⇒ Object

hue, sat, brightness in range 0..1.0 returns RGB color int



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

def hsb_color(hue, sat, brightness)
  Java::Monkstone::ColorUtil.hsbToRgB(hue, sat, brightness)
end

#int_to_ruby_colors(hex) ⇒ Object



63
64
65
# File 'lib/propane/helper_methods.rb', line 63

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.



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

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



20
21
22
23
24
25
26
# File 'lib/propane/helper_methods.rb', line 20

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

#keyObject

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



125
126
127
128
# File 'lib/propane/helper_methods.rb', line 125

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

#key_pressed?Boolean

Is a key pressed for this frame?

Returns:

  • (Boolean)


174
175
176
# File 'lib/propane/helper_methods.rb', line 174

def key_pressed?
  @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:



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

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



151
152
153
# File 'lib/propane/helper_methods.rb', line 151

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 see above



87
88
89
# File 'lib/propane/helper_methods.rb', line 87

def max(*args)
  args.max
end

#min(*args) ⇒ Object

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 }



80
81
82
# File 'lib/propane/helper_methods.rb', line 80

def min(*args)
  args.min
end

#mouse_pressed?Boolean

Is the mouse pressed for this frame?

Returns:

  • (Boolean)


169
170
171
# File 'lib/propane/helper_methods.rb', line 169

def mouse_pressed?
  @declared_fields['mousePressed'].value(java_self)
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



28
29
30
31
32
33
34
35
# File 'lib/propane/helper_methods.rb', line 28

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.



117
118
119
120
121
# File 'lib/propane/helper_methods.rb', line 117

def proxy_java_fields
  fields = %w[key frameRate 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



157
158
159
# File 'lib/propane/helper_methods.rb', line 157

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

#thread(&block) ⇒ Object

Overrides Processing convenience function thread, which takes a String arg (for a function) to more rubylike version, takes a block...



69
70
71
72
# File 'lib/propane/helper_methods.rb', line 69

def thread(&block)
  warn 'A Block is Needed' unless block_given?
  Java::JavaLang::Thread.new(&block).start
end

#web_to_color_array(web) ⇒ Object



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

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