Permalink
Browse files

feh

  • Loading branch information...
1 parent 6d6e780 commit 98268e0629311f54f2ff846dfc47d4c42fb13b6c @jdf committed Aug 1, 2010
View
@@ -5,4 +5,5 @@ python-hack.jar
kinetic-type/
jython.jar
jython_processing_demo
+examples.pde
@@ -5,20 +5,20 @@
import java.util.ArrayList;
public class Binding {
- private final String localsName;
+ private final String interpreterName;
private final String name;
private final ArrayList<PolymorphicMethod> methods = new ArrayList<PolymorphicMethod>();
private Global global = null;
- public Binding(final String localsName, final String name) {
+ public Binding(final String interpreterName, final String name) {
this.name = name;
- this.localsName = localsName;
+ this.interpreterName = interpreterName;
}
public String toString() {
final StringBuilder sb = new StringBuilder();
- sb.append(localsName).append(".__setitem__(\"").append(name).append("\", ");
- sb.append("new PyObject() {\n");
+ sb.append(interpreterName).append(".set(\"").append(name).append("\", ");
+ sb.append(global == null ? "new PyObject() {" : global.getInitializerPrefix());
if (methods.size() > 0) {
sb
.append("\tpublic PyObject __call__(final PyObject[] args, final String[] kws) {\n");
@@ -36,7 +36,7 @@ public String toString() {
sb.append("\t\t}\n\t}\n");
}
if (global != null) {
- sb.append(global.toString());
+ sb.append(global.getBody());
}
sb.append("}");
sb.append(");\n");
@@ -11,8 +11,6 @@
import java.util.Set;
import processing.core.PApplet;
-import processing.core.PGraphics;
-import processing.core.PImage;
@SuppressWarnings("serial")
public class DriverGenerator {
@@ -44,42 +42,35 @@ public DriverGenerator() {
private Binding findOrCreateBinding(final String name) {
if (!bindings.containsKey(name)) {
- bindings.put(name, new Binding("locals", name));
+ bindings.put(name, new Binding("interp", name));
}
return bindings.get(name);
}
private void maybeAdd(final Method m) {
final String name = m.getName();
- if (!Modifier.isPublic(m.getModifiers()) || !PolymorphicMethod.shouldAdd(m)
- || BAD_METHODS.contains(name) || !ALL_APPLET_METHODS.contains(name)) {
+ final int mods = m.getModifiers();
+ if (!Modifier.isPublic(mods) || Modifier.isStatic(mods)
+ || !PolymorphicMethod.shouldAdd(m) || BAD_METHODS.contains(name)
+ || !ALL_APPLET_METHODS.contains(name)) {
return;
}
findOrCreateBinding(name).add(m);
}
private void maybeAdd(final Field f) {
final String name = f.getName();
- if (!Modifier.isPublic(f.getModifiers()) || BAD_FIELDS.contains(name)) {
+ final int mods = f.getModifiers();
+ if (!Modifier.isPublic(mods) || Modifier.isStatic(mods) || BAD_FIELDS.contains(name)) {
return;
}
findOrCreateBinding(name).add(f);
}
public void generateDriver() {
- for (final Method m : PGraphics.class.getDeclaredMethods()) {
+ for (final Method m : PApplet.class.getDeclaredMethods()) {
maybeAdd(m);
}
- for (final Method m : PImage.class.getDeclaredMethods()) {
- maybeAdd(m);
- }
- for (final String mn : APPLET_OWNED_METHODS) {
- for (final Method m : PApplet.class.getDeclaredMethods()) {
- if (m.getName().equals(mn)) {
- maybeAdd(m);
- }
- }
- }
for (final Field f : PApplet.class.getDeclaredFields()) {
maybeAdd(f);
}
@@ -9,37 +9,43 @@ public Global(final Field field) {
this.field = field;
}
- public String toString() {
- final StringBuilder sb = new StringBuilder();
+ public String getInitializerPrefix() {
final Class<?> k = field.getType();
- if (k == int.class || k == long.class || k == float.class || k == double.class) {
- sb.append("\tpublic PyFloat __float__() {\n");
- sb.append(String.format("\t\treturn new PyFloat(%s);\n", field.getName()));
- sb.append("\t}\n\n");
- sb.append("\tpublic PyObject __int__() {\n");
- sb.append(String.format("\t\treturn new PyInteger((int)%s);\n", field.getName()));
- sb.append("\t}\n\n");
- sb.append("\tpublic PyObject __long__() {\n");
- sb.append(String.format("\t\treturn new PyLong((long)%s);\n", field.getName()));
- sb.append("\t}\n\n");
- } else if (k == boolean.class) {
- sb.append("\tpublic boolean __nonzero__() {\n");
- sb.append(String.format("\t\treturn %s;\n", field.getName()));
- sb.append("\t}\n\n");
+ if (k == int.class) {
+ return "new PyInteger(0) {";
} else if (k == char.class) {
// noop
+ } else if (k == long.class) {
+ return "new PyLong(0) {";
+ } else if (k == float.class || k == double.class) {
+ return "new PyFloat(0f) {";
+ } else if (k == boolean.class) {
+ return "new PyBoolean(false) {";
} else if (k.isPrimitive()) {
throw new RuntimeException("You've got to put in a converter for field "
+ field.getName() + " of type " + k);
- } else {
- sb.append("\tpublic Object __tojava__(Class<?> c) {\n");
- sb.append(String.format("\t\treturn %s;\n", field.getName()));
- sb.append("\t}\n\n");
}
- sb.append("\tpublic PyString __repr__() {\n");
- sb.append(String.format("\t\treturn new PyString(String.valueOf(%s));\n", field
- .getName()));
- sb.append("\t}\n\n");
- return sb.toString();
+ return "new JavaWrapper(" + field.getName() + ") {";
+ }
+
+ public String getBody() {
+ final Class<?> k = field.getType();
+ final String name = field.getName();
+ if (k == int.class) {
+ return String.format("\tpublic int getValue() { return %s; }\n", name);
+ } else if (k == long.class) {
+ return String.format(
+ "\tpublic BigInteger getValue() { return BigInteger.valueOf(%s); }\n", name);
+ } else if (k == float.class || k == double.class) {
+ return String.format("\tpublic double getValue() { return %s; }\n", name);
+ } else if (k == boolean.class) {
+ return String.format("\tpublic int getValue() { return %s ? 1 : 0; }\n", name);
+ } else if (k == char.class || k == String.class) {
+ // noop
+ } else if (k.isPrimitive()) {
+ throw new RuntimeException("You've got to put in a converter for field " + name
+ + " of type " + k);
+ }
+ return "";
}
}
@@ -0,0 +1,55 @@
+"""
+/**
+ * Brick Tower
+ * by Ira Greenberg.
+ *
+ * 3D castle tower constructed out of individual bricks.
+ * Uses the PVector and Cube classes.
+ */
+"""
+from cube import Cube
+bricksPerLayer = 16.0
+brickLayers = 18.0
+brick = None
+brickWidth = 60
+brickHeight = 25
+brickDepth = 25
+radius = 175.0
+angle = 0
+
+def setup():
+ size(640, 360, P3D)
+ brick = Cube(brickWidth, brickHeight, brickDepth)
+
+def draw():
+ background(0)
+ (tempX, tempY, tempZ) = (0,0,0)
+ fill(182, 62, 29)
+ noStroke()
+ # Add basic light setup
+ lights()
+ translate(width/2, height*1.2, -380)
+ # Tip tower to see inside
+ rotateX(radians(-45))
+ # Slowly rotate tower
+ rotateY(frameCount * PI/600)
+ for i in range(brickLayers):
+ # Increment rows
+ tempY-=brickHeight
+ # Alternate brick seams
+ angle = 360.0 / bricksPerLayer * i/2
+ for j in range(bricksPerLayer):
+ tempZ = cos(radians(angle))*radius
+ tempX = sin(radians(angle))*radius
+ pushMatrix()
+ translate(tempX, tempY, tempZ)
+ rotateY(radians(angle))
+ # Add crenelation
+ if (i==brickLayers-1):
+ if (j%2 == 0):
+ brick.create()
+ else:
+ # Create main tower
+ brick.create()
+ popMatrix()
+ angle += 360.0/bricksPerLayer
Binary file not shown.
@@ -0,0 +1,50 @@
+class Cube:
+ def __init__(self, w, h, d):
+ self.w = w
+ self.h = h
+ self.d = d
+ self.vertices = [
+ # Cube composed of 6 quads
+ # Front
+ (-w/2, -h/2, d/2),
+ (w/2, -h/2, d/2),
+ (w/2, h/2, d/2),
+ (-w/2, h/2, d/2),
+
+ # Left
+ (-w/2, -h/2, d/2),
+ (-w/2, -h/2, -d/2),
+ (-w/2, h/2, -d/2),
+ (-w/2, h/2, d/2),
+
+ # Right
+ (w/2, -h/2, d/2),
+ (w/2, -h/2, -d/2),
+ (w/2, h/2, -d/2),
+ (w/2, h/2, d/2),
+
+ # Back
+ (-w/2, -h/2, -d/2),
+ (w/2, -h/2, -d/2),
+ (w/2, h/2, -d/2),
+ (-w/2, h/2, -d/2),
+
+ # Top
+ (-w/2, -h/2, d/2),
+ (-w/2, -h/2, -d/2),
+ (w/2, -h/2, -d/2),
+ (w/2, -h/2, d/2),
+
+ # Bottom
+ (-w/2, h/2, d/2),
+ (-w/2, h/2, -d/2),
+ (w/2, h/2, -d/2),
+ (w/2, h/2, d/2)
+ ]
+
+ def create(self):
+ for i in range(6):
+ beginShape(QUADS);
+ for j in range(4):
+ vertex(vertices[j+4*i][0], vertices[j+4*i][1], vertices[j+4*i][2])
+ endShape()
@@ -0,0 +1,45 @@
+"""
+ Kinetic Type
+ by Zach Lieberman.
+ Adapted to Python by Jonathan Feinberg
+ Using push() and pop() to define the curves of the lines of type.
+"""
+
+words = [
+ "sometimes it's like", "the lines of text", "are so happy",
+ "that they want to dance", "or leave the page or jump",
+ "can you blame them?", "living on the page like that",
+ "waiting to be read..."
+ ]
+
+def setup():
+ size(640, 360, P3D)
+ textFont(loadFont("Univers-66.vlw"), 1.0)
+ fill(255)
+ frameRate(1000)
+
+def draw():
+ background(0)
+ pushMatrix()
+ translate(-200, -50, -450)
+ rotateY(0.3)
+
+ # Now animate every line object & draw it...
+ for i in range(len(words)):
+ f1 = sin((i + 1.0) * (millis() / 10000.0) * TWO_PI)
+ f2 = sin((8.0 - i) * (millis() / 10000.0) * TWO_PI)
+ line = words[i]
+ pushMatrix()
+ translate(0.0, i*75, 0.0)
+ for j in range(len(line)):
+ if j != 0:
+ translate(textWidth(line[j - 1]) * 75, 0.0, 0.0)
+ rotateY(f1 * 0.005 * f2)
+ pushMatrix()
+ scale(75.0)
+ text(line[j], 0.0, 0.0)
+ popMatrix()
+ popMatrix()
+ popMatrix()
+ scale(20)
+ text("%d fps"%int(frameRate), .5, 1)
Binary file not shown.
@@ -0,0 +1,13 @@
+package jycessing;
+
+import org.python.core.PyObjectDerived;
+import org.python.core.PyType;
+
+public class JavaWrapper extends PyObjectDerived {
+
+ public JavaWrapper(final Object k) {
+ super(PyType.fromClass(k.getClass(), false));
+ javaProxy = k;
+ }
+
+}
@@ -4,31 +4,31 @@
import org.python.core.PyFloat;
import org.python.core.PyObject;
import org.python.core.PyString;
-import org.python.core.PyStringMap;
+import org.python.util.PythonInterpreter;
import processing.core.PApplet;
@SuppressWarnings("serial")
public class PAppletJythonDriver extends PApplet {
- final PyObject setup;
- final PyObject draw;
+ private static final PyObject NODRAW = new PyObject();
+ final PythonInterpreter interp;
+ PyObject draw;
- public PAppletJythonDriver(final PyStringMap locals) {
- setup = locals.__finditem__("setup");
- draw = locals.__finditem__("draw");
-
- initializeStatics(locals);
+ public PAppletJythonDriver(final PythonInterpreter interp) {
+ this.interp = interp;
+ initializeStatics(interp);
}
- public static void initializeStatics(final PyStringMap locals) {
- locals.__setitem__("P3D", new PyString(PApplet.P3D));
- locals.__setitem__("TWO_PI", new PyFloat(PApplet.TWO_PI));
+ public static void initializeStatics(final PythonInterpreter interp) {
+ interp.set("P3D", new PyString(PApplet.P3D));
+ interp.set("TWO_PI", new PyFloat(PApplet.TWO_PI));
}
@Override
public void setup() {
+ final PyObject setup = interp.get("setup");
if (setup != null) {
try {
setup.__call__();
@@ -45,10 +45,15 @@ public void setup() {
@Override
public void draw() {
if (draw == null) {
+ draw = interp.get("draw");
+ if (draw == null) {
+ draw = NODRAW;
+ }
+ }
+ if (draw == NODRAW) {
super.draw();
} else {
draw.__call__();
}
}
-
}
Oops, something went wrong.

0 comments on commit 98268e0

Please sign in to comment.