How to Access Class Properties in While Loop

You should get rid of the while loop and call a function from draw.

Then your draw is short and nice and you have
discrete blocks of code.

like here

[edited]



Button[] buttons = new Button[5];
boolean displayed = false;
int LightGray = 209;
int DarkGray = 45;

boolean exitLoop = false;

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

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

  AddButtons();
}

void draw() {
  background(100);

  for (int i=0; i<buttons.length; i++) {
    buttons[i].display();
  }

  EventHandler();
  test();
}

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

void AddButtons() {

  byte labelBoxWidth = 95;//95
  byte BoxHeight = 60;//60

  int xBoxLabelStart = 57;
  int yBoxLabelStart = 157;
  byte boxDistance = 75;//75

  byte xLabelStartOffset = 12;
  byte yLabelStartOffset = 42;

  byte labelFontSize = 32;
  byte sceneFontSize = 44;

  //color SelectedFill = color(38, 125, 247);//color(209, 34, 200);

  buttons[0] = new Button(xBoxLabelStart, 80, 60, 60, "1", 10, 20, sceneFontSize, true, DarkGray, LightGray);
  buttons[1] = new Button(xBoxLabelStart, yBoxLabelStart, labelBoxWidth, BoxHeight, "Tst1", xLabelStartOffset + 5, yLabelStartOffset, labelFontSize, false, DarkGray, LightGray);
  buttons[2] = new Button(xBoxLabelStart, yBoxLabelStart + boxDistance, labelBoxWidth, BoxHeight, "Tst2", xLabelStartOffset - 6, yLabelStartOffset, labelFontSize - 2, false, DarkGray, LightGray);
  buttons[3] = new Button(xBoxLabelStart, yBoxLabelStart + 2 * boxDistance, labelBoxWidth, BoxHeight, "Tst3", xLabelStartOffset - 2, yLabelStartOffset, labelFontSize, false, DarkGray, LightGray);
  buttons[4] = new Button(xBoxLabelStart, yBoxLabelStart + 3 * boxDistance, labelBoxWidth, BoxHeight, "Tst4", xLabelStartOffset + 4, yLabelStartOffset, labelFontSize, false, DarkGray, LightGray);
}

void EventHandler() {
  if (buttons[0].isClicked) {
    // test();
    //  println("here");
    //buttons[0].isClicked =
    //  ! buttons[0].isClicked;

    exitLoop = false;
  } else exitLoop = true;
}

void test() {

  if (exitLoop)
    return;

  // do sth
  print(".");
}//func

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

class Button {

  int x; //x coordinate
  int y; //y coordinate
  int w; //button width
  int h; //button height

  int cr; //corner radius
  String label; //text on button
  PFont labelFont;//the font to use
  PFont sceneNumberFont; //the font for the scene number
  int textX;//offset for the text X value
  int textY;//offset for text Y value
  int fillUp;//fill color when button is up
  int fillOver; //fill color when button is over
  int lightGray;//stroke color
  int fontSize; //font size
  boolean sceneNumber;//is the label the scene number?
  boolean isClicked;
  boolean on;
  boolean isMouseOver;
  boolean changed;
  int ScnNumY = 46;
  int ScnNumX = 2;
  color selectedFill = color(38, 125, 247);//color(209, 34, 200);

  Button(int x_, int y_, int w_, int h_, String label_, int textX_, int textY_, int fontSize_, boolean sceneNumber_, int fillUp_, int fillOver_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;
    label = label_;
    textX = textX_;//additional offset
    textY = textY_;
    fontSize = fontSize_;
    fillUp = fillUp_;
    fillOver = fillOver_;
    lightGray = 209;
    labelFont = createFont("NotoSans-Regular-32.vlw", 14);
    sceneNumberFont = createFont("Ramabhadra-44-Basic.vlw", 14);
    sceneNumber = sceneNumber_;
    isClicked = false;
    on = false;
    isMouseOver = false;
    changed = false;
  }

  void display() {
    pushMatrix();
    translate(x, y);

    stroke(lightGray);
    strokeWeight(3);

    mouseOver();

    if (isClicked) {
      On();
    } else {
      Off();
    }

    popMatrix();
  }

  boolean isMouseOver() {
    if ((mouseX>=x) && (mouseX<=x+w) && (mouseY>=y) && (mouseY<=y+h)) {
      //MouseOver = true;
      return true;
    } else {
      //MouseOver = false;
      return false;
    }
  }//end isMouseOver

  void mouseOver() {
    if (!isMouseOver()) {
      isMouseOver = false;
    } else {
      isMouseOver = true;
    }
  }

  void On() {

    if (!sceneNumber) fill(selectedFill);
    else fill(selectedFill);

    rect(0, 0, w, h, 10);

    if (!sceneNumber) {
      fill(fillOver);
      textFont(labelFont, fontSize);
      text(label, textX, textY);
    } else {
      fill(fillOver);
      textFont(sceneNumberFont, 44);
      if (Integer.parseInt(label) < 10 )text(Integer.parseInt(label), ScnNumX + 16, ScnNumY);
      else if (Integer.parseInt(label) < 20) text(Integer.parseInt(label), ScnNumX + 4, ScnNumY);
      else text(Integer.parseInt(label), ScnNumX + 5, ScnNumY);
    }
  }//end void On

  void Off() {

    if (!isMouseOver) {
      if (!sceneNumber) fill(fillUp);
      else fill(fillOver);
    } else {
      fill(70);
    }
    rect(0, 0, w, h, 10);

    if (!sceneNumber) {
      fill(fillOver);
      textFont(labelFont, fontSize);
      text(label, textX, textY);
    } else {
      if (!isMouseOver) fill(fillUp);
      else fill(fillOver);
      textFont(sceneNumberFont, 44);
      if (Integer.parseInt(label) < 10 )text(Integer.parseInt(label), ScnNumX + 16, ScnNumY);
      else if (Integer.parseInt(label) < 20) text(Integer.parseInt(label), ScnNumX + 4, ScnNumY);
      else text(Integer.parseInt(label), ScnNumX + 5, ScnNumY);
    }
  }//end void Off
}//end class

void mouseReleased() {
  int i = 0;
  int buttonClicked = 0;

  //figure out which button is currently clicked
  for ( Button buttons : buttons ) {
    if (buttons.isClicked) {
      buttonClicked = i;
    }
    i++;
  }
  //println("Button clicked is: " + buttonClicked);

  //Select the one clicked, and deselect the one that is already selected! Works a treat!!! :)
  for (int j=0; j<buttons.length; j++) {
    if (buttons[j].isMouseOver) {
      buttons[j].isClicked = !buttons[j].isClicked;
      if (buttonClicked != j) buttons[buttonClicked].isClicked = false;
      println("button " + j +": " + buttons[j].isClicked);
    }
  }
}

1 Like