Created
June 4, 2015 10:31
-
-
Save monkstone/d4e5deead56144cafc70 to your computer and use it in GitHub Desktop.
Sketch using new ProxyLibrary library for JRubyArt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'forwardable' | |
# A custom Array created using forwardable (that can also access the PApplet pre, | |
# post and draw loops by extending our new LibraryProxy class. Also has access | |
# to custom background(int), fill(int) and stroke(int) methods. | |
class CustomArray < LibraryProxy | |
extend Forwardable | |
def_delegators(:@objs, :each, :<<) | |
include Enumerable | |
attr_reader :app | |
# We must initialize class with the PApplet instance | |
def initialize(app) | |
@app = app | |
@objs = [] | |
end | |
def add_object(mx, my, x, y, speed) | |
self << Particle.new(x.to_i, y.to_i, mx, my, Sketch::UNIT, speed, 1, 1) | |
end | |
# Access the processing post loop (gets called after draw) | |
def post | |
each do |obj| | |
update_x obj | |
next unless obj.y >= Sketch::UNIT || obj.x <= 0 | |
obj.ydir *= -1 | |
obj.y += obj.ydir | |
end | |
end | |
def update_x(obj) | |
obj.x += obj.speed * obj.xdir | |
return if (0..Sketch::UNIT).cover? obj.x | |
obj.xdir *= -1 | |
obj.x += obj.xdir | |
obj.y += obj.ydir | |
end | |
# We need this to fulfill the contract of implementing abstract methods of | |
# LibraryProxy which is an alias for Java::ProcessingCore::AbstractLibrary | |
def pre | |
end | |
# Access the processing draw loop here, using our custom background and fill | |
# note: use of 'app' to access ellipse functionality as would otherwise be | |
# required for background and fill | |
def draw | |
background(0) | |
fill(255) | |
each do |obj| | |
app.ellipse(obj.mx + obj.x, obj.my + obj.y, 6, 6) | |
end | |
end | |
end | |
# The Particle object | |
Particle = Struct.new(:x, :y, :mx, :my, :size, :speed, :xdir, :ydir) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A minimalist sketch that demonstrates a possible approach to creating a custom | |
# array of objects using forwardable. Also demonstrates how to use LibraryProxy. | |
load_library :library_proxy # loads the JRubyArt LibraryProxy abstract class | |
require_relative 'custom_array' # loads our custom 'library' class | |
UNIT = 40 | |
def setup | |
size 640, 360 | |
wide_count = width / UNIT | |
height_count = height / UNIT | |
custom_array = CustomArray.new(self) | |
height_count.times do |i| | |
wide_count.times do |j| | |
custom_array.add_object(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8)) | |
end | |
end | |
no_stroke | |
end | |
# does nothing here see custom_array.rb | |
def draw | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sketch requires JRubyArt video branch (jruby_art-0.2.6.pre ?)