Trouble moving a function into a class

There is a function checkMouse() that I have moved into the Button class, and renamed it checkMouse2. For some reason I can’t figure out, when I try to call it in Button.show(), Button b2 becomes non functional, but Button b1 still works as expected.

Could someone please tell me why b2 doesn’t work when I do this? Are the variables sharing values between class instances or something like that?

I can see that b2.touchBegan never becomes true.

Edit: I added a println statement to setMousePressed(), the isOver() function is using the same values for min & max In the mouse over test. I’m still not sure why though. I don’t see why both instances are using the same values for min & max In the test.

Button b1, b2;
Lamp l1, l2;
boolean mouseReady;
void setup() {
  size(400, 600);
  background(127);
  b1 = new Button(new PVector(width/2, height/4), new PVector(75, 75), true, "Momentary");
  b2 = new Button(new PVector(width/2, height/4*3), new PVector(75, 75), false, "Maintained");
  l1 = new Lamp(new PVector(width/2, height/4), new PVector(width, height/2));
  l2 = new Lamp(new PVector(width/2, height/4*3), new PVector(width, height/2));
  mouseReady = true;
}
void draw() {
  background(127);
  noStroke();

  l1.isOn = b1.switchClosed;
  l2.isOn = b2.switchClosed;

  l1.show();
  l2.show();

  b1.show();
  b2.show();
  println("isOver b1=", isOver(mouseX, mouseY, b1.min, b1.max), 
    "mouseReady=", mouseReady, "b1.buttonReady=", b1.buttonReady, 
    "b1.touchBegan=", b1.touchBegan);
  println("isOver b2=", isOver(mouseX, mouseY, b2.min, b2.max), 
    "mouseReady=", mouseReady, "b2.buttonReady=", b2.buttonReady, 
    "b2.touchBegan=", b2.touchBegan);
}
void checkMouse() {
   if (mousePressed && mouseReady) {
      mouseReady = false;
      b1.setMousePressed();
      b2.setMousePressed();
   }
   if (mousePressed && !mouseReady) {
      b1.updateMouse();
      b2.updateMouse();
   }
   if (!mousePressed && !mouseReady) {
      b1.setMouseReleased();
      b2.setMouseReleased();
      mouseReady = true;
   }
}
class Button {
  PVector loc, sz, min, max;
  boolean mOver, touchBegan, buttonReady, touchCanceled, isMomentary, switchClosed;
  String buttonText;
  Button(PVector loc, PVector sz, boolean isMomentary, String buttonText) {
    this.loc = loc;
    this.sz = sz;
    this.isMomentary = isMomentary;
    this.buttonText = buttonText;
    min = new PVector(loc.x - sz.x/2, loc.y - sz.y/2);
    max = new PVector(loc.x + sz.x/2, loc.y +  sz.y/2);
    mOver = false;
    touchBegan = false;
    buttonReady = true;
    switchClosed = false;
    touchCanceled = false;
  }
  void checkMouse2() {
    if (mousePressed && mouseReady) {
      mouseReady = false;
      setMousePressed();
    }
    if (mousePressed && !mouseReady) {
      updateMouse();
    }
    if (!mousePressed && !mouseReady) {
      setMouseReleased();
      mouseReady = true;
    }
  }
  void setMousePressed() {
    mOver = isOver(mouseX, mouseY, min, max);
    println("isOver() called for box at y", loc.y);
    if (mOver) {
      if (buttonReady && !touchCanceled) {
        touchBegan = true;
        buttonReady = false;
        if (isMomentary) {
          toggle();
        }
      }
    }
  }
  void updateMouse() {
    mOver = isOver(mouseX, mouseY, min, max);
    if (touchBegan && !mOver) {
      touchCanceled = true;
    }
  }
  void setMouseReleased() {
    if (isMomentary) {
      // "momentary"
      touchBegan = false;
      touchCanceled = false;
      buttonReady = true;
    } else {
      // "maintained"
      if (touchBegan && !touchCanceled) {
        toggle();
      }
      touchBegan = false;
      touchCanceled = false;
      buttonReady = true;
    }
  }
  void update() {
    //buttonText = getStatus();
  }
  void toggle() {
    switchClosed = !switchClosed;
  }
  void show() {
    /////////////////
    //checkMouse();
    checkMouse2();
    /////////////////
    stroke(255, 0, 0);
    if (mOver && touchBegan && !touchCanceled) {
      strokeWeight(3);
    } else {
      strokeWeight(1);
      stroke(0, 200, 0);
    }
    if (touchBegan) {
      fill(233, 234, 226);
    } else {
      fill(251, 251, 248);
    }
    pushMatrix();
    translate(loc.x, loc.y);
    drawBox();
    showLabel();
    popMatrix();
  }
  void drawBox() {
    rectMode(CENTER);
    rect(0, 0, sz.x, sz.y);
  }
  void showLabel() {
    fill(0);
    textAlign(CENTER, CENTER);
    text(buttonText, 0, 0);
  }
}
boolean isOver(float tx, float ty, PVector a, PVector b) {
  if (tx > a.x && ty > a.y && tx < b.x && ty < b.y) {
    return true;
  } else {
    return false;
  }
}
class Lamp {
  PVector loc, sz;
  boolean isOn;
  Lamp(PVector loc, PVector sz) {
    this.loc = loc;
    this.sz = sz;
    isOn = false;
  }
  void show() {
    if (isOn) {
      fill(240, 240, 0);
    } else {
      fill(20, 20, 0);
    }
    rectMode(CENTER);
    strokeWeight(5);
    stroke(255);
    pushMatrix();
    translate(loc.x, loc.y);
    rect(0, 0, sz.x, sz.y);
    popMatrix();
  }
}
1 Like

When I do this:

  b2.show();
  b1.show();

Then button b1 does not work as you are expecting.

It is too late for me to look further into this but that may help in the troubleshooting.

Got it. I had to put the mouse over test in a different place and now it works fine.

void checkMouse2() {
      mOver = isOver(mouseX, mouseY, min, max);
      if(mOver) {
         if (mousePressed && mouseReady) {
            mouseReady = false;
            setMousePressed();
         }
         if (mousePressed && !mouseReady) {
            updateMouse();
         }
         if (!mousePressed && !mouseReady) {
            setMouseReleased();
            mouseReady = true;
         }
      }
   }
   void setMousePressed() {
      if (buttonReady && !touchCanceled) {
         touchBegan = true;
         buttonReady = false;
         if (isMomentary) {
            toggle();
         }
      }
   }
1 Like