In this homework, I learned how to create a custom class called Button. With this custom class, I managed to add custom actions like when you click on the button, there will be a cross X on that button indicates that this color is being selected.
Here's the source code class Button:
color selectedColor = 0; int selectedX = 0, selectedY = 0; class Button { int x, y; // The x- and y-coordinates int size; // Dimension (width and height) color baseColor; // Default gray value color overColor; // Value when mouse is over the button color pressColor; // Value when mouse is over and pressed boolean over = false; // True when the mouse is over boolean pressed = false; // True when the mouse is over and pressed Button(int xp, int yp, int s, color b, color o, color p) { x = xp; y = yp; size = s; baseColor = b; overColor = o; pressColor = p; } // Updates the over field every frame void update() { if ((mouseX >= x) && (mouseX <= x+size) && (mouseY >= y) && (mouseY <= y+size)) { over = true; } else { over = false; } } boolean press() { if (over == true) { pressed = true; selectedX = x; selectedY = y; selectedColor = pressColor; return true; } else { pressed = false; return false; } } void release() { //pressed = false; // Set to false when the mouse is released } void display() { if (pressed == true) { fill(pressColor); } else if (over == true) { fill(overColor); } else { fill(baseColor); } strokeWeight(1); stroke(255); rect(x, y, size, size); if (selectedX == x && selectedY == y) { fill(255); line(x+5, y+5, x+35, y+35); line(x+5, y+35, x+35, y+5); } } }
沒有留言:
張貼留言