CLOSED - user types "Codes" - special strings, if correct something happens!

Hey!
What do i need to change to get it working?
And make it so i can have multiple codes! (i know its just string list, but if u can do it for me :slight_smile: )

int chr = 0;
String code = "a";
String input = "";
int stage = 0; //-1: sucess |0 - no | 1 - yes

void setup() {
  size(600, 600);
}

void draw() {
  background(0);
  if (stage == 1) {
    background(255, 0, 0);
    chr = 0;
    stage = 0;
  } else if(stage == -1) {
    background(0,255,0);
    chr = 0;
    stage = 0;
  }
}

void keyPressed() {
  if (key == ENTER) {
    checkCodes();
  } else {
    input = input + key;
    chr++;
  }
  
  println("INPUT: " + input + " CODE: " + code);
}

void checkCodes() {
  if(input==code) {
    stage = -1;
    println("Code " + code + " Sucessfull!");
  } else {
    stage = 1;
    println("DENY");
  }
  chr = 0;
  input = "";
}

pls explain me what mistake i made, so i know for next time. Its probably something minor, but still
thanks!

1 Like

hi
new code… a bit changed, but still doesnt work

String codes[] = new String[1];
String input = "";
int stage = 0;
void setup() {
  size(600,600);
  codes[0] = "code";
}

void draw() {
  background( (stage == 1)? color(0,255,0) : color(255,0,0) );
  if(stage == 0) {
    background(0);
  }
}

void keyPressed() {
  if(key == ENTER) {
    checkCode();
  } else {
    input += key;
    println(input);
  }
}

void checkCode() {
  for(int i = 0; i < codes.length; i++) {
    if(codes[i] == input) {
      stage = 1;
    } else {
      stage = -1;
    }
  }
}

well i found this on refrence:

	
String str1 = "CCCP";
String str2 = "CCCP";
// Tests to see if 'str1' is equal to 'str2'
if (str1.equals(str2) == true) {
  println("Equal");  // They are equal, so this line will print
} else {
  println("Not equal");  // This line will not print
}

well it works now!

here is the code if u need it:

String codes[] = new String[1];
String input = "";
int stage = 0;
void setup() {
  size(600,600);
  codes[0] = "code";
}

void draw() {
  println(stage);
}

void keyPressed() {
  if(stage != 0) {
    stage = 0;
    input = "";
  }
  
  if(key == ENTER) {
    checkCode();
  } else {
    input += key;
    println(input);
  }
}

void checkCode() {
  for(int i = 0; i < codes.length; i++) {
    if (codes[i].equals(input) == true) {
      stage = 1;
      break;
    }
  }
}

ok end version;

//array of "Codes"
String codes[] = new String[2];
//what user types
String input = "";
void setup() {
  size(600,600);
  //defying codes:
  codes[0] = "code";
  codes[1] = "code2";
}

void draw() {
}
//if user presses a key
void keyPressed() {
  if(key == ENTER) {
    checkCode();
  } else {
    input += key;
    println(input);
  }
}
//checking codes: 
void checkCode() {
  //Looping throuh codes
  for(int i = 0; i < codes.length; i++) {
    if (codes[i].equals(input) == true) {
      input = "";
      triggerCode(i);
      break;
    }
  }
}
//triggering codes
void triggerCode(int ID) {
  //comment out next line if u dont want consloe say "trigger..."
  println("code #" + ID + " triggered!");
  //just add another:
  /*
  case(X)
  >code
  break;
  */
  switch(ID) {
    case(0):
    break;
    
    case(1):
    break;
  }
}
1 Like

same as

if (codes[i].equals(input)) {

1 Like

Here is a minor variation, which works more like secret console codes (e.g. the Konomi code).

You don’t need to press “enter” to activate – codes can be anything, like Enter-Space-Enter, or “123”. The string keeps track of past input – if a code matches, it executes and clears the string buffer.

With this approach you can’t have “code” and “code1” – the first will always execute before you can type the “1”. Instead, if you choose to include enter/return characters in codes, you can have “code\n” and “code1\n”.

/**
 * magic text codes
 * 2019-03 Processing 3.4 
 * discourse.processing.org/t/closed-user-types-codes-special-strings-if-correct-something-happends/9605
 */

String codes[];
String input = "";
color bgcolor;

void setup() {
  size(400, 400);
  textAlign(CENTER);
  textSize(25);
  bgcolor = color(0);
  codes = new String[]{"foo", "bar", "baz", "\n\n\n"};
}

void draw() {
  background(bgcolor);
  text(input, width/2, height-30);
}

void keyPressed() {
  input += key;
  checkCode();
}

void checkCode() {
  for (int i = 0; i < codes.length; i++) {
    if (input.contains(codes[i])) {
      triggerCode(i);
      input="";
      break;
    }
  }
}

void triggerCode(int codeID) {
  switch(codeID) {
  case 0:
    bgcolor=color(255, 0, 0);
    break;
  case 1:
    bgcolor=color(0, 255, 0);
    break;
  case 2:
    bgcolor=color(0, 0, 255);
    break;
  case 3:
    bgcolor=color(0);
    break;
  }
}

Thanks! i found .contains() really useful! you can be more secret and type like: namelolmoney CODE forum reply… so ppl dont get the codes just by backlogging console.

Do you know any way to open console in new window? and if user types something in the CONSOLE window program can respond? like “stop” or “triggerAction_5”?

thanks! (you dont need to research console thing, but i ask if u already know)

and if you remove break; thing, you can also get multiple codes at once. But that makes it more vulnerable to brute force (forcibly trying every combination) but yet again who would brute force a simple program, when they can just look up a few files

When I mentioned console codes, I meant like classic video game consoles – NES, Genesis, etc. https://contra.fandom.com/wiki/Konami_Code

The PDE console isn’t like a terminal console – it is built in to the editor window, and you cannot type into it (it is not like a terminal shell or REPL) – it only displays program output.

It is possible to have a separate sketch window, with PApplet. It is also possible to have that second sketch window create an interactive text typing area – @Chrisr has posted a couple simple versions of this recently, and could maybe provide a link.

does any1 know how to add codes like “getItem<itemID_variable>”

Do you want to type in “getItem1” “getItem2” and have things happen for each? You can already do that by putting those strings in instead of “foo”, “bar”, “baz”, and then checking the String.

Do you mean that you want to handle the numbers dynamically, as a pattern?

If so, instead of String.contains, use match() and define the pattern as a regular expression:

https://processing.org/reference/match_.html

Note that, if you want to support more than 1digit numbers, your pattern will need some final marker after the number, otherwise it will match too early, and getitem1 will prevent you from typing getitem10.

Above you were speaking about a console

here is a simple text field that I can offer

It’s just for entering text, max 55 lines at the moment


String helpText = 
  "My little editor \n\n"
  +"Type text.\n"
  +"Hit enter to go to the next line.\n"
  +"Cursor up to copy previous line.\n"
  +"Use Backspace.\n"
  +"Esc is disabled; use Alt-F4 instead.";

//font
PFont fontEditor; 

// Editor variables 
String[] lines = new String[55];
int lineCounter = 0; // current 
int prevHighestLineCounter = 0; // this is to know how far crs down is allowed to go down  

// cursor sign 
boolean cursorBlinkFlag; 
int timerCursorBlink;

// --------------------------------------------
// Processing Core Functions  

void setup() {
  size(660, 660);
  background(255);

  fontEditor = createFont("ARIAL", 14);
  textFont(fontEditor); 
  lines[lineCounter]="";
}//setup()

void draw() {
  background(255);
  showHelpText(); 
  showTextArray();
}//draw()

// ------------------------------------------
// Inputs Keyboard 

void keyPressed() {
  // eval key 
  if ( keyCode == ENTER || keyCode == RETURN ) {
    if (lineCounter==prevHighestLineCounter) {
      // new line 
      lineCounter++; // pitfall ??? 
      lines[lineCounter]="";
      prevHighestLineCounter = lineCounter;
    }
  } 
  // Backspace and Escape keys
  else if (keyCode==BACKSPACE) {
    lines[lineCounter] = shortenStringByOneSign(lines[lineCounter]);
  } else if (key==ESC) {
    key=0; // kill ESC, stay in the program
  } 
  // CURSOR KEYS
  else if (keyCode==UP) {
    // 
    if (lineCounter>0) {
      lines[lineCounter]=lines[lineCounter-1];
    }
  } else if (keyCode==DOWN) {
    // ignore
  } else if (keyCode==LEFT) {
    // ignore
  } else if (keyCode==RIGHT) {
    // ignore
  }
  // IGNORE 
  else if (keyCode==ALT) {
    // ignore
  } else if (keyCode==CONTROL) {
    // ignore
  } else if (keyCode==SHIFT) {
    // ignore
  } 
  // LETTERS 
  else if (keyCode>30) {
    // normal text input 
    lines[lineCounter] += key;
  } 
  // IGNORE 
  else {
    // ignore
  } //else 
  //
}

// ------------------------------------------
// Minor Tools I. 

void showHelpText() {
  // Yellow box
  fill(#F6FA1C); // Yellow
  stroke(0);     // Black
  rect(width-225, 3, 
    210, 162);

  // Black text
  fill(0); // Black
  textSize(12); 
  text(helpText, 
    width-220, 20);
  textSize(14);
}

void showTextArray() {
  // The text field
  int i=0; 

  // for 
  for (String oneLine : lines) {
    if (oneLine!=null) {

      text(oneLine, 
        9, i*22+20);

      showBlinkingCursor(oneLine, i); 

      i++;
    }//if
  }//for
}//func 

// ------------------------------------------
// Minor Tools II. 

String shortenStringByOneSign( String strIn_) {
  if (strIn_.length()>0)
    return 
      strIn_.substring( 0, strIn_.length()-1 );
  else 
  return "";
}

void showBlinkingCursor(String oneLine, int i) {
  // manage and show the red Blinking Cursor |
  // Is it the current/active line?
  if (i==lineCounter && cursorBlinkFlag) {
    // show text cursor
    fill(255, 0, 0); // RED 
    text("|", 
      9 + textWidth(oneLine), i*22+20);
    // back to normal
    fill(0);
  }//if

  // timer to toggle the flag 
  if (millis()-timerCursorBlink > 330) {
    cursorBlinkFlag  = !cursorBlinkFlag; // toggle
    timerCursorBlink = millis();
  }//if
}
//

here is a similar version where the main program can send answers to the console

  • try to type Hello and dump
  • The console is now also a class of its own.
  • Also the console is better visible as a gray box.
  • You can also place the box on the screen where ever you want it. (The console needs to be given its width and height, that’s a to do)

Chrisir


Console console;

String helpText = 
  "My little console \n\n"
  +"Type text.\n"
  +"Hit enter to go to the next line.\n"
  +"Cursor up to copy previous line.\n"
  +"Use Backspace.\n"
  +"Try to type Hello and dump\n"
  +"Esc is disabled; use Alt-F4 instead.";

// --------------------------------------------
// Processing Core Functions  

void setup() {
  size(660, 660);
  background(255);

  console = new Console(9, height-130);
  //console = new Console(9, height-330);

  console.send("Hello.");
}//setup()

void draw() {
  background(255);

  showHelpText(); 
  console.showTextArrayConsole();
}//draw()

// ------------------------------------------
// Inputs Keyboard 

void keyPressed() {
  // eval key
  //println (keyCode);
  if (keyCode==33) { 
    // PgUp
    // ignore
  } else {
    // eval key
    sendKeyToConsole();
  }//else
  //
}//keyPressed() 

void sendKeyToConsole() {
  if (console.keyPressedConsole()) {
    // return was pressed 
    // eval the last line 
    if (console.lastLineIs("Hello")) {
      console.send("Hello to you, too");
    } else if (console.lastLineIs("dump")) {
      console.send("I dump the console. (dump as file AND as println)");
      console.dump();
    }
    // --------------------------------------
  } else {
    // Any other key than return was pressed 
    // ignore
  }//else
}//sendKeyToConsole() 

// ------------------------------------------
// Minor Tools 

void showHelpText() {
  // Yellow box
  fill(#F6FA1C); // Yellow
  stroke(0);     // Black
  rect(width-225, 3, 
    210, 162);

  // Black text
  fill(0); // Black
  textSize(12); 
  text(helpText, 
    width-220, 20);
  textSize(14);
}

// =======================================================================================

class Console {

  // Pos of box
  float xBoxConsole, yBoxConsole;

  // writing pos inside box
  float xWriteConsole, yWriteConsole;

  int lineSpaceConsole=22; 

  //font
  PFont fontEditor; 

  // Editor variables 
  String[] lines = new String[300];
  int lineCounter1 = 0; // current line number 
  String lastLine="";

  // cursor sign blinks 
  boolean cursorBlinkFlag; 
  int timerCursorBlink;

  DateTimeTool dateTimeTool = new DateTimeTool(); 

  // constr 
  Console( float x_, float y_ ) {
    xBoxConsole=x_;
    yBoxConsole=y_;

    xWriteConsole = xBoxConsole+3;
    yWriteConsole = yBoxConsole+20;

    fontEditor = createFont("ARIAL", 14);
    textFont(fontEditor); 
    lines[lineCounter1]="";
  }// constr   

  void showTextArrayConsole() {
    // The text field

    // box
    fill(190);     // gray
    stroke(0);     // Black
    rect(xBoxConsole, yBoxConsole, 
      width-xBoxConsole*2, 125);

    // Text 
    fill(0); // black 
    // for loop 
    int start=lineCounter1-4;
    if (start<0)
      start=0; 
    int stop=lines.length; 
    int lineCounterForLoop=0;
    for (int i=start; i<stop+1; i++) {

      if ((lineCounterForLoop*lineSpaceConsole+yWriteConsole)>120+yBoxConsole) {
        break;
      }

      if (lines[i]!=null) {

        text(lines[i], 
          xWriteConsole, lineCounterForLoop*lineSpaceConsole+yWriteConsole);

        showBlinkingCursor(lines[i], i, lineCounterForLoop);
      }//if
      lineCounterForLoop++;
    }//for
    //
  }//func

  boolean lastLineIs(String line_) {
    return lastLine.equals(line_);
  }// method

  void send(String line_) {
    lines[lineCounter1]=line_;
    lineCounter1++; // pitfall ???
    lines[lineCounter1]="";
  }//method

  boolean keyPressedConsole() {
    // eval key 
    if ( keyCode == ENTER || keyCode == RETURN ) {
      // new line 
      lastLine=lines[lineCounter1]; 
      lineCounter1++; // pitfall ??? 
      lines[lineCounter1]="";
      return true;
    }//if 
    // Backspace and Escape keys
    else if (keyCode==BACKSPACE) {
      lines[lineCounter1] = shortenStringByOneSign(lines[lineCounter1]);
    } else if (key==ESC) {
      key=0; // kill ESC, stay in the program
    } 
    // CURSOR KEYS
    else if (keyCode==UP) {
      // copy prev line
      if (lineCounter1>0) {
        lines[lineCounter1]=lines[lineCounter1-1];
      }
    } else if (keyCode==DOWN) {
      // ignore
    } else if (keyCode==LEFT) {
      // ignore
    } else if (keyCode==RIGHT) {
      // ignore
    }
    // IGNORE 
    else if (keyCode==ALT) {
      // ignore
    } else if (keyCode==CONTROL) {
      // ignore
    } else if (keyCode==SHIFT) {
      // ignore
    } 
    // LETTERS - the usual input
    else if (keyCode>30) {
      // normal text input 
      lines[lineCounter1] += key;
    } 
    // IGNORE 
    else {
      // ignore
    } //else
    //
    return false; 
    //
  }
  String shortenStringByOneSign( String strIn_) {
    if (strIn_.length()>0)
      return 
        strIn_.substring( 0, strIn_.length()-1 );
    else 
    return strIn_;
  }

  void showBlinkingCursor(String oneLine, int i, int lineCounterForLoop) {
    // manage and show the red Blinking Cursor |
    // Is it the current/active line?
    if (i==lineCounter1 && cursorBlinkFlag) {
      // show text cursor
      fill(255, 0, 0); // RED 
      text("|", 
        xWriteConsole + textWidth(oneLine), lineCounterForLoop*lineSpaceConsole+yWriteConsole);
      // back to normal
      fill(0);
    }//if

    // timer to toggle the flag cursorBlinkFlag
    if (millis()-timerCursorBlink > 330) {
      cursorBlinkFlag  = !cursorBlinkFlag; // toggle
      timerCursorBlink = millis();
    }//if
  }

  void dump() {
    // dump as file AND as println 
    String dateTimeStampShort=dateTimeTool.dateTimeStampShort(); 
    // here he should import a new array without null....
    saveStrings( "result"+dateTimeStampShort+".txt", lines);

    int stop=lines.length; 
    for (int i=0; i<stop; i++) {
      if (lines[i]!=null) {
        println( lines[i] );
      }
    }
  }//method
  //
}//class
//

//------------------------------------------------------

class DateTimeTool {
  String dateTimeStampShort() {
    // short version 
    return getDate() + "_" + getTime();
  }

  String dateTimeStampLong() {
    // long version 
    return getDateLong()
      + " at " 
      + getTimeLong();
  }

  //-----

  String getTime() {
    return leadingZeros(hour()) 
      + leadingZeros(minute()) 
      + leadingZeros(second());
  }

  String getTimeLong() {
    return leadingZeros(hour()) 
      +":"+ leadingZeros(minute()) 
      +":"+ leadingZeros(second());
  }

  String getDate() {
    return leadingZeros(year()) 
      + leadingZeros(month()) 
      + leadingZeros(day());
  }

  String getDateLong() {
    return leadingZeros(year()) 
      +"/"+ leadingZeros(month()) 
      +"/"+ leadingZeros(day());
  }

  //-----

  String leadingZeros(int a) {
    String Buffer;
    Buffer=nf(a, 2);
    return(Buffer);
  }
  //
}//class
//
1 Like

chat bot

I worked further on this - notify me when you need an example

Regards, Chrisir

OMG!!! THIS IS SO AWESOME!!! HOW DO U DO IT???

1 Like

:wink:

I worked further on this - notify me when you need an example

emm i dont understand?

In the meantime I worked further on this sketch

I can post it here or not

Maybe you want to make your own progress first?

Chrisir


// from https://discourse.processing.org/t/closed-user-types-codes-special-strings-if-correct-something-happends

// A Console.  
// The main program can send to console.
// Use commands like: Hello circle rect fill dump quit help show. 
// Search for "Add your" here in the code to see where you can add more commands (3 major occurances with ****). 

// The main class 
Console console;

// help text in yellow box
final String helpText = 
  "My little Console \n\n"
  +"Hit PgUp to toggle this help text.\n"
  +"Type text.\n"
  +"Hit enter to go to the next line.\n"
  +"Cursor up to copy previous line.\n"
  +"Use Backspace.\n"
  +"Try to type Hello and dump\n"
  +"Try circle 33 or circle x y r; circle help.\n"
  +"Try rect 33 or rect x y r; rect help.\n"
  +"Try fill/fill help and quit, show, clear.\n"
  +"Esc is disabled; use Alt-F4 instead.";

// yellow box height
final int heightYellowBox = 234; 

// show the yellow box yes/ no (PgUp)
boolean showYellowHelpBoxFlag = true; 

// when we switch off the yellow box, this text is displayed  
final String errMsgForShowYellowHelpBoxFlag = "Hit PgUp to toggle the yellow box help text.";

// help texts for cmds
final String strCircleHelp            = "circle can be used as: circle, circle radius, circle x y, circle x y r or circle x y w h. Example: circle 111.";
final String strRectHelp              = "rect can be used as: rect, rect radius, rect x y, rect x y r or rect x y w h. Example: rect 111.";
final String strFillHelp              = "fill can be used as: fill (just gives blue), fill GRAY-value (0..255), fill Red Green Blue. Example: fill 111.";
final String strQuestionMarkHelp      = "? can be used instead of help: ?fill or ?rect or ? circle"; 
final String strGeneralHelp1          = "Commands are help, quit, dump, show, fill, rect and circle.";
final String strGeneralHelp2          = "With some Commands, use ?rect etc. to get help for the command.";
final String strShowHelp              = "The command 'show' dumps only the internal list with your draw commands.";
final String strClearHelp             = "The command 'clear' clears the internal list with your draw commands.";

// list of commands 
ArrayList<String> listCmds = new ArrayList(); 

// error 
String errMsg="";

// help 
String helpMsg=""; 

// --------------------------------------------
// Processing Core Functions  

void setup() {
  size(660, 660);
  background(255);

  console = new Console(9, height-130, width-18, 125);
  //  console = new Console(9, height-330, 160, 310);

  console.send("Hello.");
}//setup()

void draw() {
  background(255);

  // show yellow box with help text 
  showHelpText();

  // parse list of commands - draws rect and circles
  parseList();

  // show console 
  console.showTextArrayConsole();

  // show error text in red 
  showErrorMessage(); 

  // show help message
  showHelpMessage();
}//draw()

// ------------------------------------------
// Inputs Keyboard 

void keyPressed() {
  // eval key
  // println (keyCode);

  if (keyCode==33) {
    // PG Up
    showYellowHelpBoxFlag = !showYellowHelpBoxFlag;
    if (!showYellowHelpBoxFlag) {
      errMsg=errMsgForShowYellowHelpBoxFlag;
    }//if
    else {
      if (errMsg.equals(errMsgForShowYellowHelpBoxFlag))
        errMsg="";
    }//else
  } //if
  else 
  {
    // eval key
    sendKeyToConsole();
  }//else
}//keyPressed() 

// ------------------------------------------
// Main Tools ; Inputs 

void sendKeyToConsole() {
  // we send the key to the console. 
  // The return value is true for RETURN or false for any other key. 
  if (console.keyPressedConsole()) {
    // return was pressed 
    // eval the last line
    evalTheLastLine();
  } else {
    // Any other key than return was pressed 
    // Show help during typing  
    evalTheCurrentLine();
  }//else
}//sendKeyToConsole() 

void evalTheLastLine() {
  // Evaluates the last line of the console (after ENTER has been pressed) 

  // reset 
  errMsg="";

  // Delete the help msg (reset) 
  helpMsg="";

  // *********************************************************************
  // Add your commands here **********************************************
  // *********************************************************************

  // We check the last line.
  // We use 
  //      * console.lastLineIs("Hello") for entire line check and 
  //      * console.lastLineStartsWith("circle ") for the beginning of a line (when parameters follow like in circle 120).  
  // 
  // The shorter a command is, the further back in the if..else if.. list it has to be ( console.lastLineStartsWith("circle help") BEFORE console.lastLineStartsWith("circle").
  // Commands like circle that draw on the screen need to be added to the listCmds:  listCmds.add(console.lastLine);   because background is used in draw() and deletes the canvas. 
  // You want to add your commands here and to parseList().
  // 
  if (console.lastLineIs("Hello")||console.lastLineIs("hello")) {
    // Hello 
    console.send("Hello to you, too");
  } else if (console.lastLineIs("show")) {
    // show
    showCmdList();
  } else if (console.lastLineIs("clear")) {
    // show
    console.dump();
    listCmds.clear();
    console.send("I dumped and cleared the draw commands list.");
  } else if (console.lastLineIs("dump")) {
    console.send("I dump the console. (dump as file AND as println)");
    console.dump();
  } else if (console.lastLineIs("help circle")||
    console.lastLineIs("circle help")||
    console.lastLineIs("circle?")||
    console.lastLineIs("?circle")||
    console.lastLineIs("circle ?")||
    console.lastLineIs("? circle")) {
    // we show help for circle 
    console.send(strCircleHelp);
  } else if (console.lastLineIs("help fill")||
    console.lastLineIs("fill help")||
    console.lastLineIs("fill?")||
    console.lastLineIs("?fill")||
    console.lastLineIs("fill ?")||
    console.lastLineIs("? fill")) {
    // we show help for fill 
    console.send(strFillHelp);
  } else if (console.lastLineIs("help rect")||
    console.lastLineIs("rect help")||
    console.lastLineIs("rect?")||
    console.lastLineIs("?rect")||
    console.lastLineIs("rect ?")||
    console.lastLineIs("? rect")) {
    // we show help for rect 
    console.send(strRectHelp);
  } else if (console.lastLineIs("help rect")||
    console.lastLineIs("? help")||
    console.lastLineIs("??")||
    console.lastLineIs("?")||
    console.lastLineIs("? ?")||
    console.lastLineIs("? ?")) {
    // we show help for ?
    console.send(strQuestionMarkHelp);
  }     
  //  
  else if (console.lastLineStartsWith("circle")) {
    // we add this to a list of commands 
    listCmds.add(console.lastLine);
  }//
  else if (console.lastLineStartsWith("rect")) {
    // we add this to a list of commands 
    listCmds.add(console.lastLine);
  }//
  else if (console.lastLineStartsWith("fill")) {
    // we add this to a list of commands 
    listCmds.add(console.lastLine);
  }//
  else if (console.lastLineStartsWith("help")) {
    // 
    // we show help (2 lines) 
    console.send(strGeneralHelp1);
    console.send(strGeneralHelp2);
  }//
  else if (console.lastLineStartsWith("quit")) {
    // 
    console.send("I dump the console. (dump as file AND as println)");
    console.dump();
    exit();
  }//
  else {
    errMsg="Unknown command";
  }
}//evalTheLastLine() 

void evalTheCurrentLine() {
  // eval the line that is typed at the moment (before hitting Enter).
  // eval throughout (not only when Enter has been hit)

  // *********************************************************************
  // Add your help here **********************************************
  // *********************************************************************

  if (console.currentLine.equals("")) {
    helpMsg = "";//reset
  } else if (console.isStringAtTheBeginningOfTheCurrentLine("circle")) {
    helpMsg = strCircleHelp;
  } else if (console.isStringAtTheBeginningOfTheCurrentLine("fill")) {
    helpMsg = strFillHelp;
  } else if (console.isStringAtTheBeginningOfTheCurrentLine("rect")) {
    helpMsg = strRectHelp;
  } else if (console.isStringAtTheBeginningOfTheCurrentLine("show")) {
    helpMsg = strShowHelp;
  } else if (console.isStringAtTheBeginningOfTheCurrentLine("clear")) {
    helpMsg = strClearHelp;
  } else if (console.isStringAtTheBeginningOfTheCurrentLine("help")) {
    // 2 lines 
    helpMsg = strGeneralHelp1
      +"\n"
      +strGeneralHelp2;
  } else if ("?".indexOf(console.currentLine) == 0) {
    helpMsg = strQuestionMarkHelp;
  } 
  // ---
  else {
    helpMsg = ""; // reset
  }//else
}//func 

// --------------------------------------------------------------
// list of commands 

void showCmdList() {
  // 
  for (String lineOfTheForLoop : listCmds) {
    println(lineOfTheForLoop);
  }//for

  String remark1="";
  if (listCmds.size()==0)
    remark1=" List is empty at the moment.";
  console.send("List of Commands are dumped in the processing console."
    +remark1);
}//func 

void parseList() {
  // parsing the cmd list 

  // *********************************************************************
  // Add your PERSISTENT commands here ***********************************
  // *********************************************************************

  fill(255, 0, 0);  // RED 

  for (String lineOfTheForLoop : listCmds) {

    // 1. circle command 
    if (lineOfTheForLoop.indexOf("circle")==0) {
      String line = lineOfTheForLoop.replace("circle", "");
      line = line.trim();
      // empty?
      if (line.equals("")) {
        // 0 params
        ellipse(width/2, height/2, 33, 33); // default circle
      }//if
      else {
        String[] params = split(line, " "); 
        // Different number of params:  
        if (params.length==3) 
          ellipse(float(params[0]), float(params[1]), float(params[2]), float(params[2]));
        else if (params.length==2) 
          ellipse(float(params[0]), float(params[1]), 33, 33);
        else if (params.length==1) 
          ellipse(width/2, height/2, float(params[0]), float(params[0]));
        else if (params.length==4) 
          ellipse(float(params[0]), float(params[1]), float(params[2]), float(params[3]));
        else {
          errMsg = "Circle command says unknown number of parameters("
            +params.length
            +"): " 
            + lineOfTheForLoop;
        }//else
      }//else
    }//if
    //
    // 2. rect command 
    else if (lineOfTheForLoop.indexOf("rect")==0) {
      String line = lineOfTheForLoop.replace("rect", "");
      line = line.trim();
      // empty?
      if (line.equals("")) {
        // 0 params
        rect(width/2, height/2, 33, 33); // default rect
      }//if
      else {
        String[] params = split(line, " "); 
        // Different number of params:  
        if (params.length==3) 
          rect(float(params[0]), float(params[1]), float(params[2]), float(params[2]));
        else if (params.length==2) 
          rect(float(params[0]), float(params[1]), 33, 33);
        else if (params.length==1) 
          rect(width/2, height/2, float(params[0]), float(params[0]));
        else if (params.length==4) 
          rect(float(params[0]), float(params[1]), float(params[2]), float(params[3]));
        else {
          errMsg = "rect command says unknown number of parameters("
            +params.length
            +"): " 
            + lineOfTheForLoop;
        }//else
      }//else
    }//else if
    //
    // 3. fill command 
    else if (lineOfTheForLoop.indexOf("fill")==0) {
      String line = lineOfTheForLoop.replace("fill", "");
      line = line.trim();
      // empty?
      if (line.equals("")) {
        // 0 params
        fill(0, 0, 255); //  default : blue
      }//if
      else {
        String[] params = split(line, " "); 
        // Different number of params:  
        if (params.length==3) 
          fill(int(params[0]), int(params[1]), int(params[2])); // RGB 
        else if (params.length==1) 
          fill(float(params[0])); // GRAY 
        else {
          errMsg = "fill command says unknown number of parameters("
            +params.length
            +"): " 
            + lineOfTheForLoop;
        }//else
      }//else
    }//else if
    //
  }//for
  //
}//func 

// ------------------------------------------
// Minor Tools: Text and messages 

void showHelpText() {
  // Yellow box and text

  if (!showYellowHelpBoxFlag) { 
    return; // leave here
  }

  // Yellow box
  fill(#F6FA1C); // Yellow
  stroke(0);     // Black
  rect(width-225, 3, 
    210, heightYellowBox);

  // Black text
  fill(0); // Black
  textSize(12); 
  text(helpText, 
    width-220, 20);
  textSize(14);
}

void showErrorMessage() {
  // show error msg
  fill(255, 0, 0); // red
  text(errMsg, 
    console.xBoxConsole+10, console.yBoxConsole-23);
}

void showHelpMessage() {
  // show help msg in white

  // when there is no help msg, we leave here 
  if (helpMsg.equals(""))
    return;  // leave here 

  // The box for the help text
  float x = console.xBoxConsole+133; 
  float y = console.yBoxConsole-123; 
  float w = 224; 
  float h = 62;

  // for longer text we can make the box higher 
  if (helpMsg.length()>110) {
    h=82;
  }

  // draw 2 lines as a right angle
  stroke(0);
  // line left <- 
  line(x-1, y+h/2, 
    console.xBoxConsole+4, y+h/2);
  // line down |
  line(console.xBoxConsole+4, y+h/2, 
    console.xBoxConsole+4, console.yBoxConsole-4);

  // draw box
  noFill(); 
  rect(x, y, 
    w, h);

  // draw text
  fill( 0); //black
  text(helpMsg, 
    x+4, y+1, 
    w-2, h+10);
}

// =======================================================================================

class Console {

  // Pos of box
  float xBoxConsole, yBoxConsole;

  // writing pos inside box
  float xWriteConsole, yWriteConsole;

  // w and h 
  float wConsole, hConsole;

  // space between lines 
  int lineSpaceConsole=22; 

  //font
  PFont fontEditor; 

  // Editor variables 
  String[] lines = new String[300];
  int lineCounter1 = 0; // current line number 
  String lastLine="";      // after hitting Enter 
  String currentLine="";   // before hitting Enter

  // cursor sign blinks 
  boolean cursorBlinkFlag; 
  int timerCursorBlink;

  DateTimeTool dateTimeTool = new DateTimeTool(); 

  // constr 
  Console( float x_, float y_, 
    float w_, float h_) {

    xBoxConsole=x_;
    yBoxConsole=y_;

    xWriteConsole = xBoxConsole+3;
    yWriteConsole = yBoxConsole+20;

    wConsole=w_;
    hConsole=h_;

    fontEditor = createFont("ARIAL", 14);
    textFont(fontEditor); 
    lines[lineCounter1]="";
  }// constr   

  void showTextArrayConsole() {
    // The text field

    // box
    fill(190);     // gray
    stroke(0);     // Black
    rect(xBoxConsole, yBoxConsole, 
      wConsole, hConsole);

    // Text 
    fill(0); // black 
    // for loop 
    int start=lineCounter1- int((hConsole-25) / lineSpaceConsole);
    if (start<0)
      start=0; 
    int stop=lines.length; 
    int lineCounterForLoop=0;
    for (int i=start; i<stop+1; i++) {

      if ((lineCounterForLoop*lineSpaceConsole+yWriteConsole) > hConsole-10+yBoxConsole) {
        break;
      }

      if (lines[i]!=null) {

        text(lines[i], 
          xWriteConsole, lineCounterForLoop*lineSpaceConsole+yWriteConsole);

        showBlinkingCursor(lines[i], i, lineCounterForLoop);
      }//if
      lineCounterForLoop++;
    }//for
    //
  }//func

  // ------------------------------------

  boolean lastLineIs(String line_) {
    // equality 
    return lastLine.equals(line_);
  }// method

  boolean lastLineStartsWith(String line_) {
    // Is the param line_ present at start of last line? Rest of line is ignored
    return lastLine.indexOf(line_) == 0;
  }// method

  boolean isStringAtTheBeginningOfTheCurrentLine(String line_) {

    // This is checking if line_ (e.g. circle) is at the start of the line that is cuurently entered (console.currentLine)

    if (line_==null) 
      return false; 

    int lenLine = line_.length();
    lenLine = min(lenLine, currentLine.length());

    String compare1 = currentLine.substring(0, lenLine); 

    return line_.indexOf(compare1) == 0;
  }//func 

  // ------------------------------------

  void send(String line_) {
    lines[lineCounter1]=line_;
    lineCounter1++; // pitfall ???
    lines[lineCounter1]="";
  }//method

  boolean keyPressedConsole() {
    // eval key 
    // The return value is true for return or false for any other key. 
    boolean result=false; 
    if (key==CODED) {
      //CODED
      result=keyPressedConsoleForCodedKeys();
    } else {
      //not CODED
      result=keyPressedConsoleForNotCodedKeys();
    }
    //
    currentLine = lines[lineCounter1]; 
    return result; 
    //
  } // keyPressedConsole() 

  // ----

  boolean keyPressedConsoleForCodedKeys() {
    // Coded
    // CURSOR KEYS
    if (keyCode==UP) {
      // copy prev line
      if (lineCounter1>0) {
        lines[lineCounter1]=lines[lineCounter1-1];
      }
    } else if (keyCode==DOWN) {
      // ignore
    } else if (keyCode==LEFT) {
      // ignore
    } else if (keyCode==RIGHT) {
      // ignore
    }
    // IGNORE 
    else if (keyCode==ALT) {
      // ignore
    } else if (keyCode==CONTROL) {
      // ignore
    } else if (keyCode==SHIFT) {
      // ignore
    }   
    // IGNORE 
    else {
      // ignore
    } //else

    return false;
  }//func

  boolean keyPressedConsoleForNotCodedKeys() {
    // not Coded
    if ( keyCode == ENTER || keyCode == RETURN ) {
      // new line 
      lastLine=lines[lineCounter1]; 
      lineCounter1++; // pitfall ??? 
      lines[lineCounter1]="";
      currentLine=""; // (it's deleted below as well)
      return true;
    }//if 
    // Backspace and Escape keys
    else if (keyCode==BACKSPACE) {
      lines[lineCounter1] = shortenStringByOneSign(lines[lineCounter1]);
    } else if (key==ESC) {
      key=0; // kill ESC, stay in the program
    }
    // LETTERS - the usual input
    else if (keyCode>0||key=='?') {   // was: else if (keyCode>30||key=='?') {
      // normal text input 
      lines[lineCounter1] += key;
    } else {
      // ignore
    }//else 

    return false;
  }//func 

  // ----

  String shortenStringByOneSign( String strIn_) {
    if (strIn_.length()>0)
      return 
        strIn_.substring( 0, strIn_.length()-1 );
    else 
    return strIn_;
  }

  void showBlinkingCursor(String oneLine, int i, int lineCounterForLoop) {
    // manage and show the red Blinking Cursor |
    // Is it the current/active line?
    if (i==lineCounter1 && cursorBlinkFlag) {
      // show text cursor
      fill(255, 0, 0); // RED 
      text("|", 
        xWriteConsole + textWidth(oneLine), lineCounterForLoop*lineSpaceConsole+yWriteConsole);
      // back to normal
      fill(0);
    }//if

    // timer to toggle the flag cursorBlinkFlag
    if (millis()-timerCursorBlink > 330) {
      cursorBlinkFlag  = !cursorBlinkFlag; // toggle
      timerCursorBlink = millis();
    }//if
  }

  void dump() {
    // dump as file AND as println 
    String dateTimeStampShort=dateTimeTool.dateTimeStampShort(); 
    // here he should import a new array without null....
    saveStrings( "result"+dateTimeStampShort+".txt", lines);

    int stop=lines.length; 
    for (int i=0; i<stop; i++) {
      if (lines[i]!=null) {
        println( lines[i] );
      }
    }
  }//method
  //
}//class
//

//------------------------------------------------------

class DateTimeTool {
  String dateTimeStampShort() {
    // short version 
    return getDate() + "_" + getTime();
  }

  String dateTimeStampLong() {
    // long version 
    return getDateLong()
      + " at " 
      + getTimeLong();
  }

  //-----

  String getTime() {
    return leadingZeros(hour()) 
      + leadingZeros(minute()) 
      + leadingZeros(second());
  }

  String getTimeLong() {
    return leadingZeros(hour()) 
      +":"+ leadingZeros(minute()) 
      +":"+ leadingZeros(second());
  }

  String getDate() {
    return leadingZeros(year()) 
      + leadingZeros(month()) 
      + leadingZeros(day());
  }

  String getDateLong() {
    return leadingZeros(year()) 
      +"/"+ leadingZeros(month()) 
      +"/"+ leadingZeros(day());
  }

  //-----

  String leadingZeros(int a) {
    String Buffer;
    Buffer=nf(a, 2);
    return(Buffer);
  }
  //
}//class
//
1 Like