/
PAppletJythonDriver.java
79 lines (68 loc) · 1.8 KB
/
PAppletJythonDriver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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.PyObject;
import org.python.core.PyStringMap;
import org.python.util.PythonInterpreter;
import processing.core.PApplet;
import processing.core.PConstants;
@SuppressWarnings("serial")
abstract public class PAppletJythonDriver extends PApplet {
abstract protected void populateBuiltins();
abstract protected void setNonPrimitives();
private static final PyObject NODRAW = new PyObject();
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(builtins);
populateBuiltins();
}
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
public void setup() {
final PyObject setup = interp.get("setup");
if (setup != null) {
try {
setup.__call__();
} catch (PyException e) {
if (e.getCause() instanceof RendererChangeException) {
throw (RendererChangeException)e.getCause();
} else {
throw e;
}
}
}
}
@Override
public void draw() {
if (draw == null) {
draw = interp.get("draw");
if (draw == null) {
draw = NODRAW;
}
}
if (draw == NODRAW) {
setNonPrimitives();
super.draw();
} else {
draw.__call__();
}
}
}