We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › live scripting console
Page Index Toggle Pages: 1
live scripting console (Read 1766 times)
live scripting console
Apr 7th, 2010, 6:42pm
 
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 9th, 2010, 10:02am)
Re: live scripting console
Reply #1 - Apr 8th, 2010, 7:29am
 
Worked for me, on the simplest test:-
background(0);
Then I couldn't see what I was doing  Grin (well on reflection I could white text).
Seriously what I did to get it to get it to work for me was
1) put the bsh.jar in a library folder, nested in a folder bsh in the sketchbook libraries folder (and imported the library in usual way)
2) The short code in file console.pde (in console folder in sketchbook) and the Console class in Console.pde.

Not quite as cool as the ruby-processing live sketch mode, or the probably more useful watch mode, but well done, top marks for the white text, but on a grey background? Smiley

Could not get window to resize properly though (looked like inner frame resized but the applet would not), so I added size(400, 400); after setup in console.pde to give myself a bit more space to type.
Re: live scripting console
Reply #2 - Apr 8th, 2010, 10:09pm
 
I like the idea of including beanshell with the sketch so users can hack at distributed projects right away. But i found the type / text really hard to read too.

I took a different road from Florians example, have you see that?
http://github.com/fjenett/simplelivecoding
Re: live scripting console
Reply #3 - Apr 9th, 2010, 10:14am
 
Thanks for checking it out guys. I meant to use it alongside code that was already partially developed. Maybe to test functions without following some rules according to structure set up in the program. Or to test functions that still need proper UI / input stuff to be added later.

Here is another simple example:
Quote:
Console c;
Thing[]things = new Thing[3];

void setup() {
  size(400,400);
  c = new Console(this);
  for(int a=0; a<3; a++) things[a] = new Thing();
}

void draw() {
  background(196);
  for(int a=0; a<3; a++) things[a].disp();
}

class Thing {
  float x, y, r;
  color c = color(255);
  
  Thing() {
    x = random(width);
    y = random(height);
    r = random(10,50);
  }
  void highlight() {
    c = color(255,0,0);
  }
  void move() {
    x = random(width);
    y = random(height);
  }
  void change() {
    r = random(10,50);
  }
  void disp() {
    pushStyle();
    fill(c);
    ellipse(x, y, r, r);
    popStyle();
  }
}

//insert Console class definition here



Running this sketch and using the console enter:
things[0].highlight()
things[0].change()
things[0].move()
...etc...

I haven't tried Ruby Processing in a while, but I think I like the idea of a Beanshell approach better. You're example was very smooth fjen. Got it working with no problem and its nice to be able to code in the IDE as opposed to my method.

I'd like to see the possibility of defining classes and re writing overrides and stuff. I haven't been able to figure out how to re define the draw method (for example) with mine. Like I said, mine is still really rough. I'd like to add command history and a cursor. Also the ability to save your dynamic changes would be good, but probably difficult to implement.
Re: live scripting console
Reply #4 - Apr 11th, 2010, 3:21am
 
Partly stimulated by this thread I have updated my jEdit ruby-processing commando file to simplify live editing in ruby-processing via watch. I have published an example of a live edit sequence on my blog http://learning-ruby-processing.blogspot.com/2010/04/live-editing-ruby-processin... Cheesy
Page Index Toggle Pages: 1