> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.6YBN3U1UEY9/rev.964
 * 
 * authors: 
 *   
 *   Pjotor

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 




// Pressing Control-R will render this sketch.

PFont font;
int fontSize = 10;
float ani;

int i = 0; 
color bgc = color(#041b0d);
color fgc = color(#22f075);

float a[];

String title;
String output = "";
String buffer = "";
String[] out_lines = {};
float fontSlot = (fontSize/1.43);
int lineMax;

int pos = 0;
int term = 0;
String flash = "";

void setup() {  // this is run once.   
    noCursor();
    // set the background color
    background(bgc);
    
    // canvas size (Integers only, please.)
    size(400,300); 

      
    // smooth edges
    smooth();
    
    // limit the number of frames per second
//    frameRate(30);
 

    PFont fontA = loadFont("monospace");
    textFont(fontA, fontSize);
} 

void draw() {  // this is run repeatedly.      
    background(bgc);
//    testImage();
    interactivity(125);
//    badTV(45);
}

void interactivity(int amp) {
    if( mousePressed ) {
        noFill();
        stroke(fgc);
        ani = abs(sin((frameCount*1.5)/float(amp/2))+.5);
        strokeCap(SQUARE);
        strokeWeight(1);
        line(mouseX-(15*ani), mouseY, mouseX+(15*ani), mouseY);
        line(mouseX, mouseY-(15*ani), mouseX, mouseY+(15*ani));

    drawPolygon(mouseX,mouseY,5,ani*40);

        ellipse(mouseX, mouseY, ani * 70 + 15, ani * 70 + 15);

        strokeWeight(ani * (amp/15) );
        arc(mouseX, mouseY, ani * 60 + 15, ani  * 60 + 15, 
        ani * PI/4, ani * PI/2 );
        strokeWeight(ani * (amp/10) );
        arc(mouseX, mouseY, ani * 70 + 15, ani * 70 + 15, 
        ani * PI*1.25, ani * TWO_PI-PI/4 ); 
        strokeWeight(ani * (amp/9) );
        arc(mouseX, mouseY, ani * 70, ani * 70, 
        ani * TWO_PI-PI/2, ani * TWO_PI );        
    }
    

}

void drawPolygon(int x, int y, int sides, int size) {
    for(int i = 0; i<sides; i++) {
      int i2 = i+1 > sides ? 0 : i+1;
      line( x+(sin(TWO_PI/sides*i) * size), y+(cos(TWO_PI/sides*i) * size), x + (sin(TWO_PI/sides*i2) * size), y + (cos(TWO_PI/sides*i2) * size) );
    }
}

void testImage(){
 background(#041b0d);
// textFont(font);
 fill(#22f075);

/*
 textAlign(CENTER);
 text(" - Anmälningsterminal #" + term + " - \n" +
      settings.get("location") + " " + 
       year() + "-" + nf(month(),2) + "-" + nf(day(),2) + " " +
       nf(hour(),2) + ":" + nf(minute(),2) + ":" +  nf(second(),2), width/2, 30);
 
 textAlign(LEFT);
 text("Förening: Solnedgång \n" + d
      "Evenemang: Skymningsland \n" +
      "Period: " + settings.get("dates") + " \n"
      , 60, 100);
 getData();
*/

 input(20, height-10);
}

void badTV(int amp){
 loadPixels();
   distort(min(amp,255));
   scanlines();
 updatePixels();
}

int ir(int a, int b){ return int(random(a,b)); }
int limit(int test, int minInt, int maxInt){ return min(max(test, minInt), maxInt); }

void distort(int amount)
{
 color px;
 int rr,bb,gg,ran;
 for(int y = 1; y < height; y+=2)
 {
   for(int x = 0; x < width; x++)
   {
     px = pixels[x+y*width];
     ran = ir(amount/4,amount);
     rr = ((px >> 16) & 0xff) + ran;
     gg = ((px >> 8) & 0xff) + ran;
     bb = (px & 0xff) + ran;
     pixels[x+y*width] = color(rr, gg, bb);
   }
 }
}

void scanlines(){
 for(int y = 0; y < height; y+=2)
 {
   for(int x = 0; x < width; x++)
   {
     pixels[x+y*width] = color(0);
   }
 }
}


void input(int x, int y){
  /* print the input line */
  lineMax = int(width/fontSlot);
  text("> " + output + ((millis() % 500 > 250) ? " " : "_"),x,y); //(fontSize*.5));
}

void keyPressed()
{
    if( key == CODED ) {
  switch(keyCode)
  {
  case ESC:
    key = 0;
    break;
  case ALT:
    key = 0;
    break;
  case TAB:
    key = 0;
    break;      
  }
      }
}

void keyTyped(){
//    println("typed " + int(key) + " " + keyCode);
if( key == CODED ) {
  if(key == RETURN || key == ENTER){
    parseCommands("" + buffer);  // parse commands
  } 
  else if(key == BACKSPACE) { // manipulates the input string when we delete
    if(buffer.length()>0) buffer = buffer.substring(0,buffer.length()-1);
    inputHandler();
  } 
} else { //default
    buffer += "" + key;
    inputHandler();
}
}

void inputHandler()
{
  if(pos == 151 || pos == 152){
    output = buffer.replaceAll("(.)","*");
  } else {
    if( buffer.length() > lineMax ) { // "scroll" the input line
      output = buffer.substring(buffer.length()-lineMax,buffer.length());
    } else {
      output = buffer;
    }
  }
}

void getData(){
  text(title, 60, 200);
  rect(60, 210, width-120, 3);
  printLines();
}

void printLines(){
  String[] o = {};
  
  for(int i = 0; i<out_lines.length; i++){
    if( out_lines[i].length() > int(width/fontSlot) )          
    {
      String[][] m = matchAll(out_lines[i],"(.{0,"+int(width/fontSlot)+"})");
      String[] br = {};
      for(int j = 0; j<m.length; j++) if( m[j][0].length()>0) br = append(br, m[j][0]);
      br[0] = br[0];
      o = concat(o,br);
    } else {
      o = append(o, out_lines[i]);
    }
  }  
  
  if(o.length > 12) o = subset(o,0,12);
  text(join(o,"\n"),60,240);
  if(flash.length() > int(width/fontSlot)) flash = flash.substring(0,int(width/fontSlot));
  text(flash,60,height-60); 
}