Need help with buttons or rollovers
in
Programming Questions
•
5 months ago
Hi there, I am new to Processing, so hopefully my problem is simple to answer! I found a sketch here in the forum that showed how to use multiple buttons and a rollover. For my project I need to use something similar, in the final sketch there will be many different rollovers or buttons displaying a fair amount of information each, because of this I need to make it so the text with additional information appears and disappears. So far I can make it appear by clicking but I cant make it disappear. It thought that by calling drawData(); in draw() the screen would refresh and the additional information would disappear. Can anyone point me in a good direction?
Here is the code that I have so far,
Elisabeth
- int btnLength = 4;
- RectButton[] rectBt;
- boolean locked;
- int y2 = 15;
- int h2 = 12;
- void setup() {
- size(400,200);
- rectBt = new RectButton[btnLength];
- color col1 = #ff0000;
- color col2 = #ffff00;
- for(int i=0; i<btnLength; i++) {
- rectBt[i] = new RectButton(width/2, y2, 100, h2, col1, col2);
- y2 = y2 + 15;;
- }
- }
- void draw() {
- drawData();
- for(int i=0; i<btnLength; i++) {
- rectBt[i].display();
- rectBt[i].update();
- }
- }
- void drawData()
- {
- if (mousePressed ) {
- for(int i=0; i<btnLength; i++) {
- if (rectBt[i].down()){
- textAlign (CENTER);
- text ( "SHOW"+ i, (rectBt[i].x + rectBt[i].w/2 +50), rectBt[i].y);
- println(i);
- };
- }
- }
- }
- class Button {
- float x, y, w, h;
- color basecolor, highlightcolor;
- color currentcolor;
- boolean hover = false;
- boolean down = false;
- void update() {
- if (hover()) {
- currentcolor = highlightcolor;
- }
- else {
- currentcolor = basecolor;
- }
- }
- boolean down() {
- if (hover) {
- locked = true;
- return true;
- }
- else {
- locked = false;
- return false;
- }
- }
- boolean hover() {
- return true;
- }
- boolean hoverRect(float x, float y, float width, float height) {
- if (mouseX >= x && mouseX <= x+width &&
- mouseY >= y && mouseY <= y+height) {
- return true;
- }
- else {
- return false;
- }
- }
- }
- class RectButton extends Button {
- RectButton(float ix, float iy, float iw, float ih, color icolor, color ihighlight) {
- x = ix;
- y = iy;
- w = iw;
- h = ih;
- basecolor = icolor;
- highlightcolor = ihighlight;
- currentcolor = basecolor;
- }
- boolean hover() {
- if ( hoverRect(x, y, w, h) ) {
- hover = true;
- return true;
- }
- else {
- hover = false;
- return false;
- }
- }
- void display() {
- stroke(255);
- fill(currentcolor);
- rect(x, y, w, h);
- }
- }
1