I just completed this little project to a point where it appears to be working well and I'm pretty excited about it. Its another take on live scripting using beanshell.org. I looked at
similar work by FloHimself from several years ago to solve problems I was having with getting context.
Quote:public class Console {
PApplet p;
Interpreter i;
char tog = '`';
boolean active = true;
boolean clear = false;
String in = "";
int x, y, w, h;
int m = 5;
PFont f = createFont("SansSerif.plain", 10, false);
String allowed =
"`~1!2@3#4$5%6^7&8*9(0)-_=+\\|qQwWeErRtTyYuUiIoOpP[{]}aAsSdDfFgGfFgGhHjJkKlL;:\'\"zZxXcCvVbBnNmM,<.>/ ";
Console(PApplet p) {
this.p = p;
i = new Interpreter();
try{
i.eval("import processing.core.*;");
i.set("p", p);
i.eval("importObject(p); setAccessibility(true);");
}catch(EvalError e){}
p.registerKeyEvent(this);
p.registerDraw(this);
x = 0;
y = 0;
w = width;
h = height;
this.f = f;
textFont(f);
}
void keyEvent(KeyEvent e) {
if(e.getID()==e.KEY_TYPED) {
if(clear) { clear(); }
char c = e.getKeyChar();
if(c==tog) active = !active;
if(active&&c!=tog) {
if(c=='\u0008')
in = (in.length()>0) in.substring(0, in.length()-1) : "";
else if(c=='\n') submit(in);
else if(allowed.indexOf(c)!=-1) in += c;
}
}
}
void submit(String in) {
try{i.eval(in); clear();}
catch ( TargetError e ) {
Throwable t = e.getTarget();
this.in = t.toString();
}
catch(EvalError e) {
this.in = e.toString();
}
clear = true;
}
void clear() {in = ""; clear = false;}
void consoleOut(Object s) {
pushMatrix();
pushStyle();
textFont(f);
if(g.getClass().getName()!="processing.core.PGraphicsJava2D")
textMode(SCREEN);
fill(0);
String in = s.toString();
text(in, x+m-1, y+m, w-m, h);
text(in, x+m+1, y+m, w-m, h);
text(in, x+m, y+m-1, w-m, h);
text(in, x+m, y+m+1, w-m, h);
fill(255);
text(in, x+m, y+m, w-m, h);
popStyle();
popMatrix();
}
void draw() {
if(active) {
consoleOut(">"+in);
}
else if(in!="") clear();
}
}
Help me test it by copying this class into your project and setting it up like so:
Code:
Console c;
void setup() {
c=new Console(this);
}
void draw(){
}
I found it worked with this simple setup, just declaring a global field to hold the console and making the instance in setup. I found I didn't need to import anything from beanshell but you do need to
download the main beanshell jar and put it in a folder called "code" in your sketch folder. Hopefully that font will work for people. If not you may have to tweak that part.
The tilde or tick key under escape (`/~) enables and disables the console. The code is fairly rough so the only indication you get is a one pixel black line at the top of the sketch when its enabled. You should be able to enter Processing/Java looking code and you can even leave out most class/type stuff as beanshell will take care of it. It appears you are given full access to the fields, methods, and classes of the sketch, but I haven't tested it thoroughly yet. I believe you can define classes and whatnot too. I haven't researched beanshell a whole lot yet.
I don't really know why I wrote my own input handling stuff. It could have been easily done with one of the UI libraries, but it was a good experience. Anyway, tell me how it works.
Edit: tweked console code (Apr 9
th, 2010, 10:02am)