Bad performance UI [fixed]

I wrote a these three basic UI element classes. as a sort substitute to passing as reference I'm using this trick with the dummy data type classes to allow me to assign each UI element what Im calling their 'incumbency', in other other words, so that I can define all of their physical and functionality characteristics as well what they're actually controlling in setup, and then just run their exe() method and have them do it all internally. Problem is I created a menu screen that has 14 of these elements and now it doesn't go above 22 fps for me. it's my only sketch that does so. should I just dump it and go for something like p5 control or is it salvageable?

class number{
  public float n;
  number(){}
}
class bool{
  public boolean b;
  bool(){}
}
class letter{
  public char l;
  letter(){}
}

class slider{
  float x, y, w, h, min, max;
  number incumbency;
  slider(float x, float y, float w, float h, number i, float min, float max){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.min = min;
    this.max = max;
    incumbency = i;
  }
  void exe(){
   fill(dim);
   rect(x,y,w,h);
   if(mouseX>x && mouseX<(x+w) && mouseY>y && mouseY<(y+h)){
     fill(brighter);
    if(mousePressed){
      incumbency.n = map(mouseX, x+4, x+w-4, min, max);
      incumbency.n = constrain(incumbency.n, min, max);
      }
    }
    else{fill(bright);}
    rect(map(incumbency.n, min, max, x+4, x+w-4)-4, y-1, 8, h+2);
  }
}

class button{
  int wait=0;
  float x, y, h, w, min, max, step;
  number incumbency;
  bool b;
  letter l;
  char type, set;
 button(float x_, float y_, float size_, char s, number i, float step_, float min, float max){
    x=x_;
    y=y_;
    h=size_;
    w=size_;
    type = s;
    incumbency = i;
    step = step_;
    this.min = min;
    this.max = max;
  }
  button(float x_, float y_, float w_, float h_, char s, bool b_){
    x=x_;
    y=y_;
    h=h_;
    w=w_;
    type = s;
    b = b_;
  }
  button(float x_, float y_, float w_, float h_, char s, letter l, char set){
    x=x_;
    y=y_;
    h=h_;
    w=w_;
    type = s;
    this.l = l;
    this.set = set;
  }
  void exe(){
    if(mouseX>x && mouseX<(x+w) && mouseY>y && mouseY<(y+h)){
      fill(bright);
      if(mousePressed && wait==0){
        if(type=='p'){
          if(incumbency.n <= max-step)incumbency.n += step;
          else incumbency.n = min;
        }
        else if(type=='m'){
          if(incumbency.n >= min+step)incumbency.n -= step;
          else incumbency.n = max;
        }
        else if(type=='t'){if(b.b)b.b=false; else b.b=true;}
        else if(type=='c'){l.l = set;}
        wait=15;
      }
    }
    else{ fill(dim);}
    if(wait>0)wait--;
    rect(x,y,w,h);
    fill(0);
    if(type != 'c' && type != 't'){
      if(type == 'p'){
      rect(x+(0.4*w), y+0.1*h, 0.2*w, 0.8*h);
      }
      rect(x+(0.1*w), y+0.4*h, 0.8*w, 0.2*h);
    }

  }
}

class checkbox{
  int wait=0;
  float x, y, size, set, store;
  number incumbency;
  bool b;
  public boolean checked=false;
  checkbox i0, i1, i2;
  checkbox(float x_, float y_, float size_, bool b){
    x=x_;
    y=y_;
    size=size_;
    this.b = b;
  }
  checkbox(float x_, float y_, float size_, number i, float set){
    x=x_;
    y=y_;
    size=size_;
    incumbency = i;
    store = incumbency.n;
    this.set = set;
  }
  void setInterference(checkbox i0){this.i0 = i0;}
  void setInterference(checkbox i0, checkbox i1){this.i0 = i0;this.i1 = i1;}
  void setInterference(checkbox i0, checkbox i1, checkbox i2){this.i0 = i0;this.i1 = i1;this.i2 = i2;}
  void exe(){
  if(mouseX>x && mouseX<(x+size) && mouseY>y && mouseY<(y+size)){
    fill(bright);
    if(wait==0  && mousePressed){
      if(checked){
        checked=false;
        if(incumbency != null){
          if(incumbency.n == set)incumbency.n = store;
        }
        if(b != null){b.b=false;}
      }
      else{
        checked=true;
        if(incumbency != null){
          if(incumbency.n != set){store = incumbency.n; incumbency.n = set;}
        }
        if(b != null){b.b=true;}
        if(i0 != null){i0.checked = false;}
        if(i1 != null){i1.checked = false;}
        if(i2 != null){i2.checked = false;}
      }
      wait=30;
    }
  }
  else {fill(dim);}
  if(wait>0)wait--;
  if(checked){fill(brighter);}
  rect(x,y,size,size);
  }
}
Tagged:

Answers

  • actually I found that the renderer P2D is what was causing the slowness. oh well. feel free to use this if you'd like, it works great!

Sign In or Register to comment.