Hi, I’ve got a sketch with a separate class file for buttons. I’m making an app, and I want to be able to click a button and run a void for serial communication. The void needs to be in its own loop so it can wait for serial data, or respond to the user exiting the function with a button click.
When you run the sketch and click button 1, the top button, it enters the loop as expected. But it never “hears” the update to the class property isClicked once it’s in the loop. In fact it never even finishes the class routines as it does not change the color to blue as the other buttons do.
I am a newbie here, so it might be an obvious dumb mistake, but how can I get out of the loop by pressing button 1 again?
I want to click button 1 to enter the loop, and click button 1 to exit the same loop. A toggle button.
Thanks for any insights.
Main Sketch snippet:
Button[] buttons = new Button[5];
boolean displayed = false;
int LightGray = 209;
int DarkGray = 45;
void setup(){
size(800,600);
AddButtons();
}
void draw(){
background(100);
for (int i=0; i<buttons.length; i++){
buttons[i].display();
}
EventHandler();
}
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){
buttons[0].isClicked = false;
test();
}
}
void test(){
boolean exitLoop = false;
while (!exitLoop) {
if(buttons[0].isClicked) exitLoop = true;
println("in the loop!");
}//while
println("out of loop!");
}//void changeScene
Separate tab named Button, with the following code:
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 = loadFont("NotoSans-Regular-32.vlw");
sceneNumberFont = loadFont("Ramabhadra-44-Basic.vlw");
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);
}
}
}
Finally, I am using a syntax I found that I don’t fully understand. Can anyone explain this to me? I believe it iterates through all the button objects, but I can’t seem to find a reference to explain it clearly. And I hate using code I don’t understand (but it works).
for( Button buttons : buttons ){
}
Thank you kind internet geniuses!
Mike