Skip to content

Instantly share code, notes, and snippets.

@monkstone
Last active April 18, 2016 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/a7bc0e81947b800e91d635d1916ea20f to your computer and use it in GitHub Desktop.
Save monkstone/a7bc0e81947b800e91d635d1916ea20f to your computer and use it in GitHub Desktop.
HYPE example translated for JRubyArt
/**
* Here is an example of a more tricky sketch to convert, first I exported to pure java (none that obfusctating processing
* crap), interestingly we can use a jdk8 lambda in place of a anonymous class for callback this is probably one way
* forward, although to be honest it may actually pay to create a JRuby extension to get more efficient operation.
*/
import processing.core.*;
import hype.*;
import hype.extended.layout.HGridLayout;
import hype.extended.behavior.HMagneticField;
import hype.extended.behavior.HSwarm;
public class MagneticField extends PApplet {
HDrawablePool pool, poolSwarm;
HMagneticField field;
HSwarm swarm;
int numMagnets = 10;
@Override
public void setup() {
H.init(this);
H.background(0xff000000);
field = new HMagneticField();
for (int i = 0; i < numMagnets; i++) {
if ((int) random(2) == 0) {
field.addPole((int) random(width), (int) random(height), 3); // x, y, north polarity / strength = 3 / repel
} else {
field.addPole((int) random(width), (int) random(height), -3); // x, y, south polarity / strength = -3 / attract
}
}
pool = new HDrawablePool(2500);
pool.autoAddToStage()
.add(new HShape("arrow.svg").enableStyle(false).anchorAt(H.CENTER))
.layout(new HGridLayout().startX(-60).startY(-60).spacing(16, 16).cols(50))
.onCreate((Object obj) -> {
HDrawable d = (HDrawable) obj;
d.noStroke().anchor(-20, -20);
field.addTarget(d);
})
.requestAll();
swarm = new HSwarm().addGoal(width / 2, height / 2).speed(7).turnEase(0.03f).twitch(20);
poolSwarm = new HDrawablePool(numMagnets);
poolSwarm.autoAddToStage()
.add(new HRect(5))
.onCreate((Object obj) -> {
HDrawable d = (HDrawable) obj;
d.noStroke().noFill().loc((int) random(width), (int) random(height)).visibility(false);
swarm.addTarget(d);
})
.requestAll();
}
@Override
public void draw() {
int i = 0;
for (HDrawable d : poolSwarm) {
HMagneticField.HPole p = field.pole(i);
p.x = d.x();
p.y = d.y();
++i;
}
H.drawStage();
}
@Override
public void settings() {
size(640, 640);
}
static public void main(String[] passedArgs) {
String[] appletArgs = new String[]{"MagneticField"};
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
load_library :hype
include_package 'hype'
def settings
size(640, 640)
end
def setup
sketch_title('Method Chaining')
H.init(self)
H.background(color('#242424')) # background is a static java method and should be called this way
rect1 = HRect.new(100)
rect1.rounding(10) # set corner rounding
rect1.stroke_weight(6) # set stroke weight
rect1.stroke(color('#000000'), 150) # set stroke color and alpha
rect1.fill(color('#FF6600')) # set fill color
rect1.anchor_at(H::CENTER) # set where anchor point is / key point for rotation and positioning
rect1.rotation(45) # set rotation of the rect
rect1.loc(100, height / 2) # set x and y location
H.add(rect1)
# here's the same code / with method chaining
rect2 = HRect.new(100)
rect2.rounding(10)
.stroke_weight(6)
.stroke(color('#000000'), 150)
.fill(color('#FF9900'))
.anchor_at(H::CENTER)
.rotation(45)
.loc(247, height / 2)
H.add(rect2)
# here's the same code / minus the hard returns and indentation (tabs are bad)
rect3 = HRect.new(100)
rect3.rounding(10).stroke_weight(6).stroke(color('#000000'), 150).fill(color('#FFCC00')).anchor_at(H::CENTER).rotation(45).loc(394, height / 2)
H.add(rect3)
H.draw_stage # paint the stage
# here is the non HYPE version / basic processing syntax
push_matrix
stroke_weight(6)
stroke(color('#000000'), 150)
fill(color('#FF3300'))
translate(width - 100, (height / 2))
rotate(45.radians)
rect(0, 0, 100, 100, 10, 10, 10, 10)
pop_matrix
stroke_weight(1)
stroke(color('#0095a8'))
line(0, height / 2, width, height / 2)
# no_loop # we are not looping
end
# def draw # unecessary draw loop
# end
@monkstone
Copy link
Author

Note using color('#0095a8') in place of #0095a8 constants need :: not . use 45.radians instead of radians(45). Also put hype library in sensible folder eg sketchbook3/libraries/hype and make sure to rename the HYPE.jar ie hype.jar in my case. Please submit your examples to https://github.com/ruby-processing/samples4ruby-processing3. Also we need an explicit settings for size declaration (vanilla processing hides this). PS gist screwed up indentation when using tabbing (so use spaces instead you know it makes sense). PS such method chaining (of PApplet) may be of dubious benefit in JRubyArt, for useful method chaining see Vec3D and Vec2D classes, where it makes perfect sense. You may wish to take a look a toxiclibs gem as an alternative, this library seem to be intimately tied to the processing framework, and may be a bit confused as a result, toxiclibs libraries are virtually completely implementation agnostic and very well designed..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment