package processing.core; /** * * @author Martin Prout */ public abstract class Proxy { private final PApplet app; /** * Useful accessors */ public int width, height; /** * * @param app Applet */ public Proxy(PApplet app) { this.app = app; this.width = app.width; this.height = app.height; setActive(true); } /** * Extending classes must implement */ public abstract void pre(); /** * Extending classes must implement */ public abstract void draw(); /** * Extending classes must implement */ public abstract void post(); private void setActive(boolean active) { if (active) { this.app.registerMethod("pre", this); this.app.registerMethod("draw", this); this.app.registerMethod("post", this); this.app.registerMethod("dispose", this); } else { this.app.unregisterMethod("pre", this); this.app.unregisterMethod("draw", this); this.app.unregisterMethod("post", this); } } /** * Simple signature * @param col */ public void background(int col) { this.app.background(col); } /** * Simple signature * @param col */ public void fill(int col) { this.app.fill(col); } /** * Simple signature * @param col */ public void stroke(int col) { this.app.stroke(col); } /** * Access applet if we must * @return */ public PApplet app() { return this.app; } /** * required for processing */ public void dispose() { setActive(false); } }
# Demonstrates possible syntax for creating a custom array of objects. UNIT = 40 attr_reader :custom_array 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 def draw end # The Particle object Particle = Struct.new(:x, :y, :mx, :my, :size, :speed, :xdir, :ydir) require 'forwardable' # The custom Array (that can access pre, post and draw loops by reflection) # Also Proxy has access to background(int), fill(int) and stroke(int) class CustomArray < Java::ProcessingCore::Proxy extend Forwardable def_delegators(:@objs, :each, :<<) include Enumerable attr_reader :app 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, UNIT, speed, 1, 1) end def post each do |obj| update_x obj next unless obj.y >= 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..UNIT).cover? obj.x obj.xdir *= -1 obj.x += obj.xdir obj.y += obj.ydir end def pre end def draw background(0) fill(255) each do |obj| app.ellipse(obj.mx + obj.x, obj.my + obj.y, 6, 6) end end end
JRubyArt is a development branch of ruby-processing, that is currently going its own way (ie somewhat different implementation)
No comments:
Post a Comment