This commit is contained in:
Casey Reas
2011-09-06 05:22:47 +00:00
parent 3b84c790cd
commit 9775dedaad
17 changed files with 89 additions and 311 deletions
@@ -1,220 +0,0 @@
/**
* Buttons.
*
* Click on one of the shapes to change
* the background color. This example
* demonstates a class for buttons.
*/
color currentcolor;
CircleButton circle1, circle2, circle3;
RectButton rect1, rect2;
boolean locked = false;
void setup()
{
size(640, 360);
smooth();
color baseColor = color(102);
currentcolor = baseColor;
// Define and create circle button
color buttoncolor = color(204);
color highlight = color(153);
ellipseMode(CENTER);
circle1 = new CircleButton(30, 100, 100, buttoncolor, highlight);
// Define and create circle button
buttoncolor = color(204);
highlight = color(153);
circle2 = new CircleButton(130, 110, 24, buttoncolor, highlight);
// Define and create circle button
buttoncolor = color(153);
highlight = color(102);
circle3 = new CircleButton(130, 140, 24, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(102);
highlight = color(51);
rect1 = new RectButton(150, 20, 100, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(51);
highlight = color(0);
rect2 = new RectButton(90, 20, 50, buttoncolor, highlight);
}
void draw()
{
background(currentcolor);
stroke(255);
update(mouseX, mouseY);
circle1.display();
circle2.display();
circle3.display();
rect1.display();
rect2.display();
}
void update(int x, int y)
{
if(locked == false) {
circle1.update();
circle2.update();
circle3.update();
rect1.update();
rect2.update();
}
else {
locked = false;
}
if(mousePressed) {
if(circle1.pressed()) {
currentcolor = circle1.basecolor;
}
else if(circle2.pressed()) {
currentcolor = circle2.basecolor;
}
else if(circle3.pressed()) {
currentcolor = circle3.basecolor;
}
else if(rect1.pressed()) {
currentcolor = rect1.basecolor;
}
else if(rect2.pressed()) {
currentcolor = rect2.basecolor;
}
}
}
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void update()
{
if(over()) {
currentcolor = highlightcolor;
}
else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if(over) {
locked = true;
return true;
}
else {
locked = false;
return false;
}
}
boolean over()
{
return true;
}
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
boolean overCircle(int x, int y, int diameter)
{
float disX = x - mouseX;
float disY = y - mouseY;
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
}
else {
return false;
}
}
}
class CircleButton extends Button
{
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overCircle(x, y, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
ellipse(x, y, size, size);
}
}
class RectButton extends Button
{
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
+6 -6
View File
@@ -31,7 +31,7 @@ void draw() {
void mouseReleased() {
for(int i=0; i<num; i++) {
handles[i].release();
handles[i].releaseEvent();
}
}
@@ -71,8 +71,8 @@ class Handle {
}
if(otherslocked == false) {
over();
press();
overEvent();
pressEvent();
}
if(press) {
@@ -80,7 +80,7 @@ class Handle {
}
}
void over() {
void overEvent() {
if(overRect(boxx, boxy, size, size)) {
over = true;
} else {
@@ -88,7 +88,7 @@ class Handle {
}
}
void press() {
void pressEvent() {
if(over && mousePressed || locked) {
press = true;
locked = true;
@@ -97,7 +97,7 @@ class Handle {
}
}
void release() {
void releaseEvent() {
locked = false;
}
@@ -13,8 +13,8 @@ int topWidth, bottomWidth; // The width of the top and bottom images
void setup() {
size(640, 360);
noStroke();
hs1 = new HScrollbar(0, 20, width, 20, 3*5+1);
hs2 = new HScrollbar(0, height-20, width, 20, 3*5+1);
hs1 = new HScrollbar(0, height*0.33, width, 20, 3*5+1);
hs2 = new HScrollbar(0, height*0.66, width, 20, 3*5+1);
top = loadImage("seedTop.jpg");
topWidth = top.width;
bottom = loadImage("seedBottom.jpg");
@@ -45,15 +45,15 @@ void draw() {
class HScrollbar {
int swidth, sheight; // width and height of bar
int xpos, ypos; // x and y position of bar
float xpos, ypos; // x and y position of bar
float spos, newspos; // x position of slider
int sposMin, sposMax; // max and min values of slider
float sposMin, sposMax; // max and min values of slider
int loose; // how loose/heavy
boolean over; // is the mouse over the slider?
boolean locked;
float ratio;
HScrollbar (int xp, int yp, int sw, int sh, int l) {
HScrollbar (float xp, float yp, int sw, int sh, int l) {
swidth = sw;
sheight = sh;
int widthtoheight = sw - sh;
@@ -68,7 +68,7 @@ class HScrollbar {
}
void update() {
if(over()) {
if(overEvent()) {
over = true;
} else {
over = false;
@@ -87,11 +87,11 @@ class HScrollbar {
}
}
int constrain(int val, int minv, int maxv) {
float constrain(float val, float minv, float maxv) {
return min(max(val, minv), maxv);
}
boolean over() {
boolean overEvent() {
if(mouseX > xpos && mouseX < xpos+swidth &&
mouseY > ypos && mouseY < ypos+sheight) {
return true;
@@ -101,10 +101,10 @@ class HScrollbar {
}
void display() {
fill(255);
fill(204);
rect(xpos, ypos, swidth, sheight);
if(over || locked) {
fill(153, 102, 0);
fill(0, 0, 0);
} else {
fill(102, 102, 102);
}