Hardware Reference
In-Depth Information
The constructor for this Button
class is almost the same, except
for the new variable, pressedLastTime .
8
Continued from opposite page.
w = thisW;
basecolor = thisColor;
highlightcolor = thisHighlight;
currentcolor = basecolor;
name = thisName;
pressedLastTime = false;
}
The display() method combines the
functions of update() and display()
from Chapter 9's RFID Writer example,
both changing the color as needed and
drawing the actual button.
// draw the button and its text:
void display() {
// if pressed, change the color:
if (isPressed()) {
currentcolor = highlightcolor;
}
else {
currentcolor = basecolor;
}
fill(currentcolor);
rect(x, y, w, h);
//put the name in the middle of the button:
fill(textColor);
textAlign(CENTER);
text(name, x+w/2, y+h/2);
}
// check to see if the mouse position is inside
// the bounds of the rectangle and sets its current state:
boolean isPressed() {
if (mouseX >= x && mouseX <= x+w &&
mouseY >= y && mouseY <= y+h && mousePressed) {
return true;
}
else {
return false;
}
}
isPressed() checks not only whether
the mouseX and mouseY are within
the button's bounds, but it also uses
mousePressed to indicate whether the
user is touching the screen at all.
//this method is for setting the state of the button
// last time it was checked, as opposed to its
// current state:
void setLastState(boolean state) {
pressedLastTime = state;
}
boolean getLastState() {
return pressedLastTime;
}
}
setLastState() and getLastState()
give you access to the last state of the
button from outside the class.
That's the end of the whole sketch; see
Figure 10-24 for an illustration.
 
Search WWH ::




Custom Search