Simple textinput class

class TextArea{
  float x,y,w,h,bx,by,textSize = 12,spacing = 2,txoff,tyoff,lxpos;
  int last,lines,xpos,ypos;
  String [] textArea;
  String text = "",blankLine="",tempLine = "",label = "";
  boolean update,blink,visible = true,vScroll,hScroll;
  boolean mdown,toggle,border,bgFill,localTheme,setMouse;
  char currentKey;
  int col;
  PVector mouse;
  PGraphics canvas,parentCanvas;
  
  TextArea(float x_,float y_,float w_,float h_){
    x = x_;
    bx = x;
    y = y_;
    by = y;
    w = w_;
    h = h_;
    
    //textSize(textSize);
    canvas = createGraphics((int) w,(int) h);
    println(w,h,x,y,canvas.width);
    //while(textWidth(blankLine)<w-textWidth("")
    
  };
  
  TextArea(float x_,float y_,float w_,float h_,String Label){
    label = Label;
    x = x_;
    bx = x;
    y = y_;
    by = y;
    w = w_;
    h = h_;
    
    canvas = createGraphics((int) w,(int) h);
    println(w,h,x,y,canvas.width);
    //textSize(textSize);
    //while(textWidth(blankLine)<w-textWidth("")
    
  };
  
  void draw(){
    logic();
    getCursor();
    //getKey();
    fill(255);
    strokeWeight(0);
    rect(x,y,w,h);
    stroke(0);
    strokeWeight(2);
    lxpos = 0;
    if(textArea!=null&&ypos<textArea.length&&xpos<=textArea[ypos].length())lxpos = textWidth(textArea[ypos].substring(0,xpos));
    if(frameCount%30==0&&blink)blink = false;
    else if(frameCount%60==0&&!blink)blink = true;
    if(blink)line(x+lxpos,y+ypos*(textSize+spacing),x+lxpos,y+ypos*(textSize+spacing)+textSize);
    if(label!=null){
      fill(col);
      text(label,x,y+ypos*(textSize+spacing));
    }
    
    if(textArea!=null){
      for(int i=0;i<textArea.length;i++){
        fill(0,0,255,50);
        noStroke();
        rect(x,y+(textSize+spacing)*i,w,textSize);
      }
      for(int i=0;i<textArea.length;i++){
          String s = textArea[i];
          fill(col);
          text(s,x,y+(textSize+spacing)*i+textSize);
      }
    }
  };
  
  void draw(boolean k){
    if(parentCanvas==null)draw(canvas);
    else draw(parentCanvas);
  };
  
  void draw(PGraphics canvas){
    if(parentCanvas ==null){
      
      logic(true);
      getCursor(true);
      //getKey();
      
      if(canvas!=null){
        
        canvas.beginDraw();
        canvas.fill(255);
        canvas.strokeWeight(0);
        canvas.rect(0,0,w,h);
        canvas.stroke(0);
        canvas.strokeWeight(2);
        if(label!=null){
          canvas.fill(col);
          canvas.text(label,0,0+(textSize));
          //println(label);
        }
        textSize(textSize);
        lxpos = 0;
        if(textArea!=null&&ypos<textArea.length&&xpos<=textArea[ypos].length())lxpos = textWidth(textArea[ypos].substring(0,xpos));
        if(frameCount%30==0&&blink)blink = false;
        else if(frameCount%60==0&&!blink)blink = true;
        if(blink)canvas.line(0+lxpos,0+ypos*(textSize+spacing),0+lxpos,0+ypos*(textSize+spacing)+textSize);
        
        if(textArea!=null){
          
          canvas.fill(0,0,255,50);
          canvas.noStroke();
          for(int i=0;i<textArea.length;i++){
            
            canvas.rect(0,0+(textSize+spacing)*i,w,textSize);
          }
          canvas.fill(col);
          canvas.textSize(textSize);
          for(int i=0;i<textArea.length;i++){
              String s = textArea[i];
              
              canvas.text(s,0+txoff,0+(textSize+spacing)*i+textSize+tyoff);
          }
        }
        canvas.endDraw();
        image(canvas,x,y);
        //println(canvas.width);
      }
    }else{
      draw(parentCanvas,true);
    }
  };
  
  void draw(PGraphics scene,boolean b1){
    
    if(scene !=null){
      
      logic(true);
      if(toggle)getCursor(true);
      
      
      if(canvas!=null&&visible){
        canvas.beginDraw();
        canvas.fill(255);
        canvas.strokeWeight(0);
        canvas.rect(0,0,w,h);
        canvas.stroke(0);
        canvas.strokeWeight(2);
        if(label!=null){
          canvas.textSize(textSize);
          canvas.fill(0);
          
          canvas.text(label,0,0+(textSize));
        }
        textSize(textSize);
        //lxpos = 0;
        if(textArea!=null&&xpos>-1&&ypos>-1&&ypos<textArea.length
        &&xpos<=textArea[ypos].length())
        lxpos = textWidth(textArea[ypos].substring(0,xpos));
        if(frameCount%30==0&&blink)blink = false;
        else if(frameCount%30==0&&!blink)blink = true;
        if(blink)canvas.line(lxpos,0+ypos*(textSize+spacing),0+lxpos,0+ypos*(textSize+spacing)+textSize);
        
        if(textArea!=null){
          canvas.fill(0,0,255,50);
          canvas.noStroke();
          for(int i=0;i<textArea.length;i++){
            
            canvas.rect(0,0+(textSize+spacing)*i,w,textSize);
          }
          canvas.fill(col);
          canvas.textSize(textSize);
          for(int i=0;i<textArea.length;i++){
              String s = textArea[i];
              
              canvas.text(s,0+txoff,0+(textSize+spacing)*i+textSize+tyoff);
          }
        }
        canvas.endDraw();
        scene.beginDraw();
        scene.image(
        canvas,x,y);
        scene.endDraw();
        image(scene,0,0);
      }
    }
  };
  
  void update(){
    if(update){
      if(ypos==last){
        
      }else{
        int l = text.length()-1;
        textArea[last] = textArea[last]+text.charAt(l);
      }
      
      update = false;
    }
  };
  
  void logic(){
    if(textArea ==null){
      textArea = new String[1];
      textArea[0] = "";
      
    }
    if(textArea!=null&&textArea.length>0){
      lines = textArea.length-1;
      last = textArea[lines].length()-1;
    }
    
    if(pos()&&mousePressed)toggle = true;
    if(pos()&&!mousePressed)toggle = false;
      
    if(vScroll){
      
    }
    if(hScroll){
      
    }
    //if(!pos&&mousePressed)toggle=false;
    //if(
  };
  
  void logic(boolean b1){
    if(textArea ==null){
      textArea = new String[1];
      textArea[0] = "";
      
    }
    if(textArea!=null&&textArea.length>0){
      lines = textArea.length-1;
      last = textArea[lines].length()-1;
    }
    if(label!=null&&toggle)label = null;
    
    //if(pos(mouse))println(mouse.x,mouse.y);
    if(pos(mouse)&&mousePressed)toggle = true;
    if(!pos(mouse)&&mousePressed)toggle = false;
      
    if(vScroll){
      
    }
    if(hScroll){
      
    }
    //if(!pos&&mousePressed)toggle=false;
    //if(
  };
  
  void logic(PVector mouse){
    if(mouse!=null){
      if(textArea ==null){
        textArea = new String[1];
        textArea[0] = "";
        
      }
      if(textArea!=null&&textArea.length>0){
        lines = textArea.length-1;
        last = textArea[lines].length()-1;
      }
      
      if(pos(mouse)&&mousePressed)toggle = true;
      if(pos(mouse)&&!mousePressed)toggle = false;
        
      if(vScroll){
        
      }
      if(hScroll){
        
      }
    }
    //if(!pos&&mousePressed)toggle=false;
    //if(
  };
  
  void getKey(){
    boolean b1 = ypos*(textSize+spacing)<h-(textSize+spacing)*2;
    boolean b2 = lxpos<w;
    if(key!=CODED&&key!=BACKSPACE&&key!=ENTER){
      String s = "";
      s+= key;
      b2 = lxpos<w - textWidth(s);
    }
    if(b2&&!b1&&key!=ENTER)b1 = true;
    //if(!b1&&)b1 = true;
    //println(lxpos,w,b1,b2);
    if(toggle&&b1&&b2){
      textSize(textSize);
      if(key!=CODED&&key!=BACKSPACE&&key!=ENTER){
        currentKey = key;
        
        
        if(textArea==null){
          
          textArea = new String[1];
          textArea[0] = text;
          xpos = text.length();
        }else{
          if(ypos<textArea.length){
            if(xpos==tempLine.length()){
              tempLine += currentKey;
              
              textArea[ypos] = tempLine;
              xpos = textArea[ypos].length();
              setText(textArea);
            }else if(xpos<tempLine.length()){
              String s1 = tempLine.substring(0,xpos);
              String s2 = "";
              if(xpos>2)textArea[ypos].substring(xpos-2,textArea[ypos].length()-1);
              else textArea[ypos].substring(0,textArea[ypos].length()-1);
              
              tempLine = s1+currentKey;
              
              for(int i=xpos;i<textArea[ypos].length();i++){
                char a = textArea[ypos].charAt(i);
                tempLine+= a;
              }
              textArea[ypos] = tempLine;
              xpos =s1.length()+1;
              setText(textArea);
            }
            
          }
        }
        
      }
      else if(key==BACKSPACE){
        
        if(textArea!=null){
          println("backspace",xpos,textArea[ypos].length());
          if(textArea.length==1||ypos==0){
            if(xpos==textArea[0].length()){
              if(xpos>0){
                tempLine = textArea[0].substring(0,textArea[0].length()-1);
                xpos = tempLine.length();
                textArea[0] = tempLine;
              }
              
            }else if(xpos>0){
              tempLine = textArea[0].substring(0,xpos-1);
              xpos = tempLine.length();
              for(int i=xpos+1;i<textArea[0].length();i++){
                tempLine+= textArea[0].charAt(i);
              }
              textArea[0] = tempLine;
            }
          }else{
            
            if(xpos==textArea[ypos].length()||xpos==0){
              
              if(xpos>0){
                
                tempLine = textArea[ypos].substring(0,textArea[ypos].length()-1);
                xpos = tempLine.length();
                textArea[ypos] = tempLine;
              }else{
                String s2 = textArea[ypos].substring(xpos,textArea[ypos].length());
                String[] temp = new String[textArea.length-1];
                //temp[ypos-1] = textArea[ypos-1];
                for(int i=0;i<ypos;i++){
                  temp[i] = textArea[i];
                }
                temp[ypos-1]+= s2;
                for(int i=ypos+1;i<textArea.length;i++){
                  temp[i-1] = textArea[i];
                }
                //
                tempLine = textArea[ypos];
                xpos = textArea[ypos-1].length();
                tempLine += s2;
                for(int i=0;i<textArea[ypos].length();i++){
                  char a = textArea[ypos].charAt(i);
                  tempLine += a;
                }
                textArea = temp;
                ypos --;
              }
              
            }else{
              if(xpos>0){
                tempLine = textArea[ypos].substring(0,xpos-1);
                xpos = tempLine.length();
                for(int i=xpos+1;i<textArea[ypos].length();i++){
                  tempLine+= textArea[ypos].charAt(i);
                }
                textArea[ypos] = tempLine;
              }else{
                tempLine = textArea[ypos-1].substring(0,textArea[ypos-1].length());
                //xpos = tempLine.length();
                for(int i=xpos+1;i<textArea[ypos].length();i++){
                  tempLine+= textArea[ypos].charAt(i);
                }
                textArea[ypos] = tempLine;
              }
            }
          }
        }
        
      }else if(key==ENTER){
        
        if(ypos ==textArea.length-1){
          if(xpos==tempLine.length()){
            String []temp = new String[textArea.length+1];
            for(int i=0;i<temp.length;i++){
              if(i<textArea.length)temp[i] = textArea[i];
              else {
                temp[i] = "";
              }
            }
            tempLine = "";
            textArea = temp;
            setText(textArea);
            ypos = textArea.length-1;
          }else{
            String []temp = new String[textArea.length+1];
            for(int i=0;i<textArea.length;i++){
              temp[i] = textArea[i];
            }
            String s1 = "",s2 = "";
            //for(int i=0;i<ypos-1;i++){
            //  temp[i] = textArea[i];
            //}
            temp[ypos] = textArea[ypos].substring(0,xpos);
            //temp[ypos] = "";
            temp[temp.length-1] = textArea[ypos].substring(xpos,textArea[ypos].length());
            
            
            tempLine = temp[ypos+1];
            //xpos = tempLine.length();
            textArea = temp;
            setText(textArea);
            ypos = textArea.length-1;
            //xpos = 0;
          }
          xpos = 0;
          
        }else{
          String []temp = new String[textArea.length+1];
          
          if(xpos==textArea[ypos].length()){
              
              for(int i=0;i<ypos+1;i++){
                temp[i] = textArea[i];
              }
              temp[ypos+1] = "";
              for(int i=ypos+2;i<temp.length;i++){
                temp[i] = textArea[i-1];
              }
              tempLine = "";
              textArea = temp;
              setText(textArea);
              ypos ++;
              xpos = 0;
        }
        else{
          for(int i=0;i<ypos;i++){
            temp[i] = textArea[i];
          }
          
          for(int i=ypos+1;i<textArea.length+1;i++){
            temp[i] = textArea[i-1];
          }
          
          if(xpos>textArea[ypos].length())xpos = textArea[ypos].length();
          temp[ypos] = textArea[ypos].substring(0,xpos);
          //xpos = temp[ypos].length();
          temp[ypos+1] = textArea[ypos].substring(xpos,textArea[ypos].length());
          for(int i=ypos+2;i<temp.length;i++){
            temp[i] = textArea[i-1];
          }
          tempLine = temp[ypos];
          textArea = temp;
          setText(textArea);
          //xpos = tempLine.length();
          xpos = 0;
          ypos ++;
        }
        
      }
        
        
      }
      if(keyCode==37&&xpos>0){
        xpos--;
      }else if(keyCode==37&&xpos==0&&ypos>0){
        ypos--;
        xpos = textArea[ypos].length();
        tempLine = textArea[ypos];
      }else if(keyCode==39&&xpos<textArea[ypos].length()){
        xpos++;
      }else if(keyCode==39&&xpos==textArea[ypos].length()&&ypos<textArea.length-1){
        xpos = 0;
        ypos++;
        tempLine = textArea[ypos];
      }else if(keyCode==38&&ypos>0){
        ypos--;
        tempLine = textArea[ypos];
        if(tempLine=="")xpos = 0;
        if(xpos>tempLine.length())xpos = tempLine.length();
      }else if(keyCode==40&&ypos<textArea.length-1){
        ypos++;
        tempLine = textArea[ypos];
        if(tempLine=="")xpos = 0;
        if(xpos>tempLine.length())xpos = tempLine.length();
      }
    }
  };
  
  void setText(String[] s){
    text = "";
    for(int i=0;i<s.length;i++){
      String s1 = s[i];
      if(s.length>0&&s1.length()>0&&s1.charAt(s1.length()-1)!='\n')s1 += "\n";
      //s[i] = s1;
      text+=s1;
    }
  };
  
  boolean pos(){
    boolean k = false;
    float max = 0;
    if(textArea!=null)max = textArea.length*(textSize+spacing);
    return mouseX>x&&mouseX<x+w&&mouseY>y&&mouseY<y+h;
  };
  
  boolean lpos(){
    boolean k = false;
    float max = textArea.length*(textSize+spacing);
    return mouseX>x&&mouseX<x+w&&mouseY>y&&mouseY<y+max;
  };
  
  boolean pos(PVector m){
    boolean k = false;
    if(m ==null)m = setMouse();
    //float max = textArea.length*(textSize+spacing);
    if(m!=null)return m.x>x&&m.x<x+w&&m.y>y&&m.y<y+h;
    else {
      return false;
      
    }
  };
  
  boolean lpos(PVector m){
    boolean k = false;
    float max = textArea.length*(textSize+spacing);
    return m.x>x&&m.x<x+w&&m.y>y&&m.y<y+max;
  };
  
  int getCursor(){
    int k = -1;
    int kx = -1;
    if(pos()){
      if(mousePressed)toggle = true;
    if(lpos()&&mousePressed){
      //canvas.beginDraw();
      textSize(textSize);
      float max = textArea.length*(textSize+spacing);
      float tw = textWidth(textArea[ypos]);
      k = floor(map(mouseY,y,y+max,0,textArea.length));
      
      if(k<textArea.length){
        
        ypos = k;
        
        String s = "";
        for(int i=0;i<textArea[k].length();i++){
          s += textArea[k].charAt(i);
          float tw1 = textWidth(s)-5;
          if(mouseX<x+tw1&&mouseX>x)break;
          else kx = i;
          if(i==textArea[k].length()-1&&mouseX>x+tw1&&mouseX>x)kx = textArea[k].length();
        }
        //canvas.endDraw();
        if(kx>-1)xpos = kx;
        
      }
      tempLine = textArea[k];
    }}else if(mousePressed)toggle = false;
    
    return k;
  };
  
  int getCursor(boolean b1){
    int k = -1;
    int kx = -1;
    if(pos(mouse)){
      if(mousePressed)toggle = true;
    if(lpos(mouse)&&mousePressed){
      float max = textArea.length*(textSize+spacing);
      float tw = textWidth(textArea[ypos]);
      k = floor(map(mouse.y,y,y+max,0,textArea.length));
      
      
      if(k<textArea.length){
        //int 
        ypos = k;
        textSize(textSize);
        String s = "";
        for(int i=0;i<textArea[k].length();i++){
          s += textArea[k].charAt(i);
          float tw1 = textWidth(s)-5;
          if(mouse.x<x+tw1&&mouse.x>x)break;
          else kx = i;
          
          if(i==textArea[k].length()-1&&mouse.x>x+tw1&&mouse.x>x)kx = textArea[k].length();
        }
        if(kx>-1)xpos = kx;
        
      }
      tempLine = textArea[k];
    }}else if(mousePressed)toggle = false;
    
    return k;
  };
  
  int getCursor(PVector mouse){
    int k = -1;
    int kx = -1;
    if(pos(mouse)){
      if(mousePressed)toggle = true;
    if(lpos(mouse)&&mousePressed){
      float max = textArea.length*(textSize+spacing);
      float tw = textWidth(textArea[ypos]);
      k = floor(map(mouse.y,y,y+max,0,textArea.length));
      
      
      if(k<textArea.length){
        
        ypos = k;
        textSize(textSize);
        String s = "";
        for(int i=0;i<textArea[k].length();i++){
          s += textArea[k].charAt(i);
          float tw1 = textWidth(s)-5;
          
          if(mouse.x<x+tw1&&mouse.x>x)break;
          else kx = i;
          
          if(i==textArea[k].length()-1&&mouse.x>x+tw1&&mouse.x>x)kx = textArea[k].length();
        }
        if(kx>-1)xpos = kx;
      }
      tempLine = textArea[k];
    }}else if(mousePressed)toggle = false;
    
    return k;
  };
  
  PVector setMouse(){
    if(mouse==null){
      mouse = new PVector(mouseX,mouseY);
    }
    else {
      
      mouse.x = mouseX;
      mouse.y = mouseY;
    }
    return mouse;
  };
  
  public void addLine() {
    String []temp = new String[textArea.length];
    for(int i=0;i<textArea.length;i++) {
      temp[i] = textArea[i];
    }
    tempLine = "";
    textArea = temp;
    ypos = textArea.length-1;
    textArea[ypos] = "";
    xpos = 0;

  };
  
  public void addLineAt(int a) {
    String []temp = new String[textArea.length];
    for(int i=0;i<a;i++) {
      temp[i] = textArea[i];
    }
    temp[a] = "";
    tempLine = "";
    for(int i=a;i<textArea.length;i++) {
      temp[i] = textArea[i-1];
    }
    textArea = temp;
    ypos = textArea.length-1;
    textArea[ypos] = "";
    xpos = 0;

  };
  
  
  public void addLineAt(int a,int j) {
    String []temp = new String[textArea.length];
    for(int i=0;i<a;i++) {
      temp[i] = textArea[i].substring(0,j);
    }
    temp[a] = "";
    for(int i=xpos;i<textArea[ypos].length();i++) {
      temp[a] += textArea[ypos].charAt(i);
    }
    tempLine = temp[a];
    for(int i=a;i<textArea.length;i++) {
      temp[i] = textArea[i-1];
    }
    textArea = temp;
    ypos = textArea.length-1;
    textArea[ypos] = "";
    xpos = 0;

  };
  
  //public float
  
  
  
};
TextArea t1,t2;
PGraphics pg;
void settings(){
  size(600,600);
};
void setup(){
  
  t1 = new TextArea(50,50,200,400);
  t2 = new TextArea(60+200,50,200,400,"Click to add text");
  pg = createGraphics(200,200);
  t1.parentCanvas = pg;
  t1.setMouse = true;
};

void draw(){
  background(0,255,0);
  t1.setMouse();
  t1.draw(true);
  t2.draw(true);
  fill(0);
  text(frameRate,100,100);
};

void keyPressed(){
  t1.getKey();
  t2.getKey();
};

has been added to the library Iā€™m currently working on, will update github later;
upcoming features paste from clipboard, highlight and copy text;

2 Likes