Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bricktower works!

  • Loading branch information...
commit 42311ae343a9171d2a8a5fe2dcbef4066f4a1c3b 1 parent 98268e0
@jdf authored
View
1  .classpath
@@ -6,5 +6,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/processing-core"/>
<classpathentry kind="lib" path="C:/jython2.5.2b1core/jython.jar" sourcepath="C:/jython2.5.2b1src/src"/>
+ <classpathentry combineaccessrules="false" kind="src" path="/processing-opengl"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
3  .gitignore
@@ -6,4 +6,5 @@ kinetic-type/
jython.jar
jython_processing_demo
examples.pde
-
+generated/**
+*.py.class
View
4 buildtime/jycessing/build/Binding.java
@@ -5,19 +5,17 @@
import java.util.ArrayList;
public class Binding {
- private final String interpreterName;
private final String name;
private final ArrayList<PolymorphicMethod> methods = new ArrayList<PolymorphicMethod>();
private Global global = null;
public Binding(final String interpreterName, final String name) {
this.name = name;
- this.interpreterName = interpreterName;
}
public String toString() {
final StringBuilder sb = new StringBuilder();
- sb.append(interpreterName).append(".set(\"").append(name).append("\", ");
+ sb.append("builtins.__setitem__(\"").append(name).append("\", ");
sb.append(global == null ? "new PyObject() {" : global.getInitializerPrefix());
if (methods.size() > 0) {
sb
View
60 buildtime/jycessing/build/DriverGenerator.java
@@ -1,12 +1,19 @@
package jycessing.build;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -14,14 +21,10 @@
@SuppressWarnings("serial")
public class DriverGenerator {
- private static final String[] APPLET_OWNED_METHODS = new String[] { "loadFont", "size",
- "frameRate", "sin", "cos", "tan", "sqrt", "millis" };
+ final String BAD_METHOD = "^(init|handleDraw|draw|parse[A-Z].*)$";
- private static final Set<String> BAD_METHODS = new HashSet<String>(Arrays
- .asList("init"));
-
- private static final Set<String> BAD_FIELDS = new HashSet<String>(Arrays
- .asList("screen"));
+ private static final Set<String> BAD_FIELDS = new HashSet<String>(Arrays.asList(
+ "screen", "args", "recorder"));
private static final Set<String> ALL_APPLET_METHODS = Collections
.unmodifiableSet(new HashSet<String>() {
@@ -36,6 +39,7 @@
});
final Map<String, Binding> bindings = new HashMap<String, Binding>();
+ final List<Field> nonPrimitives = new ArrayList<Field>();
public DriverGenerator() {
}
@@ -50,9 +54,8 @@ private Binding findOrCreateBinding(final String name) {
private void maybeAdd(final Method m) {
final String name = m.getName();
final int mods = m.getModifiers();
- if (!Modifier.isPublic(mods) || Modifier.isStatic(mods)
- || !PolymorphicMethod.shouldAdd(m) || BAD_METHODS.contains(name)
- || !ALL_APPLET_METHODS.contains(name)) {
+ if (!Modifier.isPublic(mods) || !PolymorphicMethod.shouldAdd(m)
+ || name.matches(BAD_METHOD) || !ALL_APPLET_METHODS.contains(name)) {
return;
}
findOrCreateBinding(name).add(m);
@@ -64,23 +67,50 @@ private void maybeAdd(final Field f) {
if (!Modifier.isPublic(mods) || Modifier.isStatic(mods) || BAD_FIELDS.contains(name)) {
return;
}
- findOrCreateBinding(name).add(f);
+ if (f.getType() == char.class || !f.getType().isPrimitive()) {
+ nonPrimitives.add(f);
+ } else {
+ findOrCreateBinding(name).add(f);
+ }
}
- public void generateDriver() {
+ public String getBindings() {
for (final Method m : PApplet.class.getDeclaredMethods()) {
maybeAdd(m);
}
for (final Field f : PApplet.class.getDeclaredFields()) {
maybeAdd(f);
}
+ final StringBuilder sb = new StringBuilder();
for (final Binding b : bindings.values()) {
- System.out.println(b);
+ sb.append(b.toString());
}
+ return sb.toString();
+ }
+ public static String getText(final Reader r) throws IOException {
+ final BufferedReader reader = new BufferedReader(r);
+ final StringBuilder sb = new StringBuilder();
+ String line;
+ try {
+ while ((line = reader.readLine()) != null) {
+ sb.append(line).append("\n");
+ }
+ return sb.toString();
+ } finally {
+ reader.close();
+ }
}
- public static void main(final String[] args) {
- new DriverGenerator().generateDriver();
+ public static void main(final String[] args) throws Exception {
+ final DriverGenerator gen = new DriverGenerator();
+
+ final String template = getText(new FileReader("template/DriverImpl.java"));
+ final String bindings = gen.getBindings();
+ final String withBindings = template.replace("%BINDINGS%", bindings);
+
+ final FileWriter out = new FileWriter("generated/jycessing/DriverImpl.java");
+ out.write(withBindings);
+ out.close();
}
}
View
8 buildtime/jycessing/build/PolymorphicMethod.java
@@ -80,7 +80,13 @@ public String toString() {
private void append(final Signature signature, final StringBuilder sb) {
if (!signature.isVoid()) {
- sb.append("return ").append(TypeUtil.pyConversionPrefix(signature.getReturnType()));
+ try {
+ final String prefix = TypeUtil.pyConversionPrefix(signature.getReturnType());
+ sb.append("return ").append(prefix);
+ } catch (RuntimeException e) {
+ System.err.println(name);
+ throw e;
+ }
}
sb.append(name).append('(');
for (int i = 0; i < arity; i++) {
View
8 examples.py/3D/Form/BrickTower/bricktower.py
@@ -1,25 +1,21 @@
"""
-/**
* 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
+brick = Cube(brickWidth, brickHeight, brickDepth)
def setup():
- size(640, 360, P3D)
- brick = Cube(brickWidth, brickHeight, brickDepth)
+ size(640, 360, OPENGL)
def draw():
background(0)
View
4 examples.py/3D/Form/BrickTower/cube.py
@@ -44,7 +44,7 @@ def __init__(self, w, h, d):
def create(self):
for i in range(6):
- beginShape(QUADS);
+ beginShape(QUADS)
for j in range(4):
- vertex(vertices[j+4*i][0], vertices[j+4*i][1], vertices[j+4*i][2])
+ vertex(self.vertices[j+4*i][0], self.vertices[j+4*i][1], self.vertices[j+4*i][2])
endShape()
View
38 src/jycessing/PAppletJythonDriver.java
@@ -1,29 +1,48 @@
package jycessing;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+import org.python.core.Py;
import org.python.core.PyException;
-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;
+import processing.core.PConstants;
@SuppressWarnings("serial")
-public class PAppletJythonDriver extends PApplet {
+abstract public class PAppletJythonDriver extends PApplet {
+
+ abstract protected void populateBuiltins();
+
+ abstract protected void setNonPrimitives();
private static final PyObject NODRAW = new PyObject();
- final PythonInterpreter interp;
+ protected final PyStringMap builtins;
+ protected final PythonInterpreter interp;
PyObject draw;
public PAppletJythonDriver(final PythonInterpreter interp) {
+ interp.getSystemState();
+ this.builtins = (PyStringMap)interp.getSystemState().getBuiltins();
this.interp = interp;
- initializeStatics(interp);
-
+ initializeStatics(builtins);
+ populateBuiltins();
}
- public static void initializeStatics(final PythonInterpreter interp) {
- interp.set("P3D", new PyString(PApplet.P3D));
- interp.set("TWO_PI", new PyFloat(PApplet.TWO_PI));
+ public static void initializeStatics(final PyStringMap builtins) {
+ for (final Field f : PConstants.class.getDeclaredFields()) {
+ final int mods = f.getModifiers();
+ if (Modifier.isPublic(mods) || Modifier.isStatic(mods)) {
+ try {
+ builtins.__setitem__(f.getName(), Py.java2py(f.get(null)));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
@Override
@@ -51,6 +70,7 @@ public void draw() {
}
}
if (draw == NODRAW) {
+ setNonPrimitives();
super.draw();
} else {
draw.__call__();
View
16 src/jycessing/Runner.java
@@ -7,7 +7,6 @@
import java.io.Reader;
import org.python.core.Py;
-import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.InteractiveConsole;
@@ -40,23 +39,12 @@ public static void main(final String[] args) throws Exception {
final String text = wrap(new FileReader(pathname));
Py.initPython();
- final ProcessingLocals locals = new ProcessingLocals();
final InteractiveConsole interp = new InteractiveConsole();
- interp.setLocals(locals);
final String path = new File(pathname).getCanonicalFile().getParent();
Py.getSystemState().path.insert(0, new PyString(path));
try {
- locals.__setitem__(new PyString("__file__"), new PyString(pathname));
- final PApplet applet = new PAppletJythonDriver(interp);
-
- final K k = new K();
- interp.set("b", new JavaWrapper(k) {
- @Override
- public PyObject __call__(final PyObject[] args, final String[] keywords) {
- System.err.println("Ow!");
- return Py.None;
- }
- });
+ interp.getLocals().__setitem__(new PyString("__file__"), new PyString(pathname));
+ final PApplet applet = new DriverImpl(interp);
interp.exec(text);
PApplet.runSketch(new String[] { "Test" }, applet);
} catch (Throwable t) {
View
26 template/DriverImpl.java
@@ -0,0 +1,26 @@
+package jycessing;
+
+import org.python.util.PythonInterpreter;
+import org.python.core.*;
+import processing.core.*;
+import java.io.*;
+
+@SuppressWarnings("serial")
+public class DriverImpl extends PAppletJythonDriver {
+
+ public DriverImpl(final PythonInterpreter interp) {
+ super(interp);
+ }
+
+ @Override
+ protected void populateBuiltins() {
+ %BINDINGS%
+ }
+
+ @Override
+ protected void setNonPrimitives() {
+ interp.set("key", key);
+ interp.set("keyEvent", keyEvent);
+ }
+
+}
View
6 test.py
@@ -1,6 +1,2 @@
def setup():
- print b
- print dir(b)
- b.boom()
- b()
- noloop()
+ noLoop()
Please sign in to comment.
Something went wrong with that request. Please try again.