Skip to content

Commit a7522d7

Browse files
committed
Preprocessor functional!
1 parent 7aa7be5 commit a7522d7

File tree

7 files changed

+118
-17
lines changed

7 files changed

+118
-17
lines changed

BUILD.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Read in and concatenate user code
66
Search & replace user-readable PApplet instance variables (mouseX, mouseY, pmouseX, pmouseY, mouseButton, mousePressed, key, keyCode, keyPressed)
77
with __applet__.[var]
88
Check if user code defines setup(); if not, surround with setup() function definition
9+
Make sure user isn't misdefining things?
910
Process user globals?
1011
prepend prepend.py
1112
write to file

release/PythonMode.zip

-222 KB
Binary file not shown.

resources/prepend.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ def __mul__(a, b):
5959
__applet__ = PythonPApplet()
6060
g = globals()
6161

62+
getmousePressed = __applet__.getmousePressed
63+
getkeyPressed = __applet__.getkeyPressed
64+
6265
for method in PythonPApplet.staticMethods:
6366
g[method] = PApplet.__dict__[method]
6467

68+
def get_inst(method):
69+
return lambda *args: PApplet.__dict__[method](__applet__, *args)
70+
6571
for method in PythonPApplet.instanceMethods:
66-
g[method] = lambda *args: PApplet.__dict__[method](__applet__, *args)
72+
g[method] = get_inst(method)
6773

68-
del monkeypatch_method, RealPVector, PApplet, PythonPApplet, g
74+
del monkeypatch_method, g, __sub__, __add__, __mul__, method, get_inst

src/info/sansgills/mode/python/PythonBuild.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.io.PrintWriter;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
57

68
import processing.app.Base;
79
import processing.app.Library;
@@ -85,16 +87,37 @@ public void build() throws Exception{
8587
}
8688
}
8789

90+
//Some regexes
91+
//A hack, but much less work than a full parser, and we don't need to do very much
92+
private static Pattern getPressed; //replace mousePressed / keyPressed with
93+
private static Pattern instanceVars; //replace 'mouseX' with __applet__.mouseX, etc.
94+
95+
static{
96+
getPressed = Pattern.compile("(?<!def\\s{1,100})(mousePressed|keyPressed)");
97+
instanceVars = Pattern.compile("(mouseX|mouseY|pmouseX|pmouseY|mouseButton|keyCode|key)");
98+
}
99+
88100

89101

90102
/*
91103
* Turn .pde into valid python
92104
*/
93105
private String preprocess(StringBuilder program){
106+
System.out.println("Preprocessing");
94107
try{
108+
String temp;
109+
110+
Matcher regex = getPressed.matcher(program);
111+
112+
temp = regex.replaceAll("get$1()");
113+
114+
regex = instanceVars.matcher(temp);
115+
116+
temp = regex.replaceAll("__applet__.$1");
95117

118+
program = new StringBuilder(temp);
119+
96120
program.insert(0, prepend);
97-
program.append(append);
98121

99122
}catch(Exception e){
100123

src/info/sansgills/mode/python/PythonRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ private String[] buildArgs(boolean present){
6565

6666
String[] out = args.toArray(new String[0]);
6767

68-
{//debugging
68+
/*{//debugging
6969
String cmd = "";
7070
for (String c : out) {
7171
cmd += c + " ";
7272
}
7373
System.out.println("command: " + cmd);
74-
}
74+
}*/
7575

7676
return out;
7777
}

src/info/sansgills/mode/python/wrapper/ProcessingJythonWrapper.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import java.io.File;
44
import java.util.Arrays;
55

6-
import org.python.core.Py;
7-
import org.python.core.PyObject;
8-
import org.python.core.PyString;
9-
import org.python.core.PySystemState;
6+
import org.python.core.*;
107
import org.python.util.InteractiveConsole;
118

129
/**
@@ -31,6 +28,10 @@
3128
public class ProcessingJythonWrapper {
3229

3330
//EVERYTHING IS STATIC
31+
32+
static final String[] sketchFunctions = { "setup", "draw", "mousePressed",
33+
"mouseReleased", "mouseClicked", "mouseWheel", "mouseMoved",
34+
"mouseDragged", "keyPressed", "keyReleased", "keyTyped" };
3435

3536
static InteractiveConsole interp; // python interpreter to feed things to
3637
static PySystemState sys; // for modifying python classpath, as yet
@@ -64,7 +65,7 @@ public static void run(String scriptPath, String[] params){
6465
sys.add_package("processing.opengl");
6566

6667
try {
67-
//run the script we were given; should define a PythonPApplet subclass and create an instance
68+
//run the script we were given
6869
interp.execfile(scriptPath);
6970

7071
//get the applet we constructed
@@ -77,6 +78,15 @@ public static void run(String scriptPath, String[] params){
7778
throw new Exception("Couldn't construct applet.");
7879
}
7980

81+
//go through functions the sketch might have defined and give them to the applet
82+
for (String name : sketchFunctions){
83+
PyFunction function = (PyFunction) interp.get(name);
84+
if(function != null){
85+
System.out.println("found "+name);
86+
constructedApplet.inject(name, function);
87+
}
88+
}
89+
8090
//run the sketch!
8191
PythonPApplet.runSketch(params, constructedApplet);
8292

src/info/sansgills/mode/python/wrapper/PythonPApplet.java

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package info.sansgills.mode.python.wrapper;
22

3+
import java.util.HashMap;
4+
35
import processing.core.PApplet;
6+
import processing.event.MouseEvent;
7+
48
import org.python.core.*;
59

610
/**
@@ -37,17 +41,15 @@ public class PythonPApplet extends PApplet {
3741
"createGraphics", "createImage", "createInput", "createOutput",
3842
"createReader", "createShape", "createWriter", "cursor", "curve",
3943
"curveDetail", "curvePoint", "curveTangent", "curveTightness",
40-
"curveVertex", "directionalLight", "draw", "ellipse",
44+
"curveVertex", "directionalLight", "ellipse",
4145
"ellipseMode", "emissive", "endCamera", "endContour", "endRaw",
4246
"endRecord", "endShape", "exit", "fill", "filter", "frameRate",
43-
"frustum", "get", "hint", "hue", "image", "imageMode",
44-
"keyPressed", "keyReleased", "keyTyped", "lerpColor",
47+
"frustum", "get", "hint", "hue", "image", "imageMode", "lerpColor",
4548
"lightFalloff", "lightSpecular", "lights", "line", "loadBytes",
4649
"loadFont", "loadImage", "loadJSONArray", "loadJSONObject",
4750
"loadPixels", "loadShader", "loadShape", "loadStrings",
4851
"loadTable", "loadXML", "loop", "millis", "modelX", "modelY",
49-
"modelZ", "mouseClicked", "mouseDragged", "mouseMoved",
50-
"mousePressed", "mouseReleased", "mouseWheel", "noCursor",
52+
"modelZ", "noCursor",
5153
"noFill", "noLights", "noLoop", "noSmooth", "noStroke", "noTint",
5254
"noise", "noiseDetail", "noiseSeed", "normal", "ortho", "parseXML",
5355
"perspective", "point", "pointLight", "popMatrix", "popStyle",
@@ -68,14 +70,73 @@ public class PythonPApplet extends PApplet {
6870

6971

7072

73+
private HashMap<String, PyFunction> sketchFunctions;
74+
75+
public PythonPApplet(){
76+
super();
77+
78+
sketchFunctions = new HashMap<String, PyFunction>();
79+
}
80+
81+
public void inject(String name, PyFunction function){
82+
sketchFunctions.put(name, function);
83+
}
84+
85+
//woo
86+
87+
@Override
88+
public void setup(){
89+
if(sketchFunctions.containsKey("setup")) sketchFunctions.get("setup").__call__();
90+
}
91+
@Override
92+
public void draw(){
93+
if(sketchFunctions.containsKey("draw")) sketchFunctions.get("draw").__call__();
94+
}
95+
@Override
96+
public void mousePressed(){
97+
if(sketchFunctions.containsKey("mousePressed")) sketchFunctions.get("mousePressed").__call__();
98+
}
99+
@Override
100+
public void mouseReleased(){
101+
if(sketchFunctions.containsKey("mouseReleased")) sketchFunctions.get("mouseReleased").__call__();
102+
}
103+
@Override
104+
public void mouseClicked(){
105+
if(sketchFunctions.containsKey("mouseClicked")) sketchFunctions.get("mouseClicked").__call__();
106+
}
107+
@Override
108+
public void mouseMoved(){
109+
if(sketchFunctions.containsKey("mouseMoved")) sketchFunctions.get("mouseMoved").__call__();
110+
}
111+
@Override
112+
public void mouseDragged(){
113+
if(sketchFunctions.containsKey("mouseDragged")) sketchFunctions.get("mouseDragged").__call__();
114+
}
115+
@Override
116+
public void mouseWheel(){ //TODO fix
117+
if(sketchFunctions.containsKey("mouseWheel")) sketchFunctions.get("mouseWheel").__call__();
118+
}
119+
@Override
120+
public void keyPressed(){
121+
if(sketchFunctions.containsKey("keyPressed")) sketchFunctions.get("keyPressed").__call__();
122+
}
123+
@Override
124+
public void keyReleased(){
125+
if(sketchFunctions.containsKey("keyReleased")) sketchFunctions.get("keyReleased").__call__();
126+
}
127+
@Override
128+
public void keyTyped(){
129+
if(sketchFunctions.containsKey("keyTyped")) sketchFunctions.get("keyTyped").__call__();
130+
}
71131

72132

73133
// workaround for naming conflicts
74-
public boolean getMousePressed(){
134+
// not properly camelcased because regex, and noone is going to see it anyway
135+
public boolean getmousePressed(){
75136
return mousePressed;
76137
}
77138

78-
public boolean getKeyPressed(){
139+
public boolean getkeyPressed(){
79140
return keyPressed;
80141
}
81142
}

0 commit comments

Comments
 (0)