Mouse Functions

mouse-functions.pde
/**
 * Mouse Functions. 
 * 
 * Click on the box and drag it across the screen. 
 */
 
float bx;
float by;
int boxSize = 75;
bool overBox = false;
bool locked = false;
float xOffset = 0.0f; 
float yOffset = 0.0f; 

void setup() {
    size(640, 360);
    bx = width / 2.0f;
    by = height / 2.0f;
    rectMode(RADIUS);  
}

void draw() { 
    background(0);
  
    // Test if the cursor is over the box 
    if (mouseX > bx - boxSize && mouseX < bx + boxSize && 
        mouseY > by - boxSize && mouseY < by + boxSize) {

        overBox = true;  

        if (!locked) { 
            stroke(255); 
            fill(153);
        } 

    } else {
        stroke(153);
        fill(153);
        overBox = false;
    }
  
    // Draw the box
    rect(bx, by, boxSize, boxSize);
}

void mousePressed() {
    if (overBox) { 
        locked = true; 
        fill(255, 255, 255);
    } else {
        locked = false;
    }

    xOffset = mouseX - bx; 
    yOffset = mouseY - by; 
}

void mouseDragged() {
    if (locked) {
        bx = mouseX - xOffset; 
        by = mouseY - yOffset; 
    }
}

void mouseReleased() {
    locked = false;
}