Created
August 2, 2021 16:03
-
-
Save manuelborst/1025479ab37dc619e7b1d3c5e9fcb059 to your computer and use it in GitHub Desktop.
Drag and Drop Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Mouse Functions. | |
* | |
* Click on the box and drag it across the screen. | |
*/ | |
float bx; | |
float by; | |
int boxSize = 75; | |
boolean overBox = false; | |
boolean locked = false; | |
float xOffset = 0.0; | |
float yOffset = 0.0; | |
void setup() { | |
size(640, 360); | |
bx = width/2.0; | |
by = height/2.0; | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment