Sunniva-project1-documentation

 

TextBox textBox1;
StringList trialSpeedTime;
int backSpaceCounter;

import rita.*;

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

String [] colors = {"5,0,10","7,0,20","5,0,25", //purple-blue
            "2,1,25","0,10,20","0,20,10", //blue-green
          "5,20,5","15,15,0","30,15,1"}; //green-yellow

void setup(){
    size(1512,982);//1512, 982
    background(189,169,209);
    textBox1 = new TextBox(526,258,739,506); //500,245,700,478
    trialSpeedTime = new StringList();
    backSpaceCounter = millis();
    String portName = Serial.list()[1];
    println(Serial.list());
    myPort = new Serial(this, portName, 9600);
    PImage img;
    img = loadImage("background.png");
    image(img, 0, 0, width, height); //*0.95
}

void draw(){
    textBox1.draw();
}

class TextBox{
    TextBox(int x, int y, int boxWidth, int boxHeight){
        this.x = x;
        this.y = y;
        this.boxWidth = boxWidth;
        this.boxHeight = boxHeight;
    }


    void draw(){
        drawBox();
        drawText();
        checkkeyReleased();
        checkKeyPressed();
        recordEvent();
      }

      void drawBox(){
        stroke(205);
        fill(244,243,236);
        rect(x, y, boxWidth, boxHeight);
    }

    void drawText(){
        textAlign(LEFT, TOP);
        textSize(16);
        fill(0);
        PFont font;
        font = createFont("Arial", 25);
        textFont(font);
        text(textValue + getCursor(), x + 5, y+10, boxWidth-15,boxHeight-10);
    }
   
   void checkKeyPressed(){
     if (keyPressed && c != key && str(c).toUpperCase()!=str(key).toUpperCase()){
            keyCounter = millis();
            c = key;
            if (key == BACKSPACE){
            textValue = textValue.substring(0,textValue.length()-1);
            backSpace += 1;
            println(backSpace);
            if (backSpace > 3){ //CHECK IF DELETE IS PRESSED TOO MANY TIMES
            println("many delete!");
            manyDelete = true;}
            else {
            manyDelete = false;
            }
          }
            else if ((c >= ' ' && keyCode != 16)){
               textValue+=(str(key));
               keyTime = keyCounter - previousKeyCounter;
               checkKeySpeed();
               previousKeyCounter = keyCounter;
               keyCounter = 0;}
     }
    }
    

    void recordEvent(){ 
      backSpaceCounter1 = millis();
      if ((backSpaceCounter1 - prev_backSpaceCounter)>5000){
            prev_backSpaceCounter = backSpaceCounter1;
            print(backSpace,backSpaceCounter1, prev_backSpaceCounter);
            
            //check typed contents
            String[] checkText = split(textValue,' ');
            for (int i = 0; i <(checkText.length); i++) {
              if (checkText[i].length() > 0 && RiTa.isPunct(checkText[i])== false){
              println(checkText[i],RiTa.hasWord(checkText[i]),RiTa.hasWord(checkText[i].substring(0,checkText[i].length()-1)));
               if ((RiTa.hasWord(checkText[i]) == false) 
                 && RiTa.hasWord(checkText[i].substring(0,checkText[i].length()-1)) == false){
                  misSpell+=1;
                 }              
              }
            }
            println(misSpell,checkText.length);
           // check misspell
                if (checkText.length != 0 && misSpell > checkText.length/2){
                  println("Too Many MISSPELLING");
                  manyError = true;}
                else {
                  manyError = false;
                }
            //initialize
            misSpell = 0;
            backSpace = 0;
            backSpaceCounter1 = 0;
            //change Lighting
            checkLight();
               }
              }


    void checkkeyReleased(){
         if(!keyPressed){
         c = 0;
       }
    }

    void checkKeySpeed(){
         trialSpeedTime.append(str(keyTime));
         println((keyTime),trialSpeedTime);
         int size = trialSpeedTime.size(); //check speed each 10 character(seconds/char)
         if (size>9){
           trialSpeedTime.remove(0);
           keyTotalTime = 0;
           for (int i = 0; i < 9; i++){
             float item = float(trialSpeedTime.get(i)); 
             keyTotalTime += item;
           }
           keySpeed = keyTotalTime / 10;
           println("Speed =" + keySpeed + "s/character");
           if (keySpeed > 300){ //CHECK IF SPEED IS FAST
             fastSpeed = false;
           println("TOO SLOW");}
           else
             fastSpeed = true;
         }
         }
    
    
    void checkLight(){
      //NOT A AND (B OR C) --> Y 
      if ((fastSpeed == false) && (manyError == true | manyDelete == true) && (confuseLevel<9)){
        confuseLevel += 1;
        //light
        println("CONFUSE LEVEL is" + confuseLevel);}
      else if(confuseLevel>0){
         confuseLevel -= 1;}
      myPort.write(colors[confuseLevel]);
      println(colors[confuseLevel]);
    }


    String getCursor() {
      return hovering() && (frameCount>>4 & 1) == 0 ? "|" : "";
    }

    boolean hovering(){
        return mouseX >= x && mouseX <= x + boxWidth && mouseY >= y && mouseY <= y + boxHeight;
    }
    
    //private String[] lines = loadStrings("IncludedWords.txt");
    private int x, y, boxWidth, boxHeight;
    private float keyCounter, previousKeyCounter, keyTime,keySpeed, keyTotalTime, backSpace,backSpaceCounter1,prev_backSpaceCounter;
    private float misSpell;
    private int confuseLevel = 0;
    private String textValue = "";
    private char c;
    private boolean fastSpeed = true, manyDelete = false, manyError = false;
}