Last active
August 29, 2015 13:57
-
-
Save nataliefreed/9633499 to your computer and use it in GitHub Desktop.
Draggable objects and handler example for Processing 2.0 Natalie Freed, March 2014
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
/* | |
Draggable objects and handler example for Processing 2.0 | |
Natalie Freed, March 2014 | |
*/ | |
DraggableHandler circles; | |
void setup() | |
{ | |
size(500, 500); | |
circles = new DraggableHandler(this); | |
circles.addToEnd(new DraggableCircle(3*width/4, height/2, 30, color(0, 100, 0, 150))); | |
circles.addToEnd(new DraggableCircle(width/2, height/2, 20, color(0, 0, 100, 150))); | |
circles.addToEnd(new DraggableCircle(width/4, height/2, 50, color(255, 0, 255, 150))); | |
} | |
void draw() | |
{ | |
background(100); | |
circles.display(); //DraggableHandlers are automatically updated | |
} | |
//To make a draggable object that is not a circle, use the following class | |
//as a template. Write your own display and isOver functions that work | |
//for your shape. You can add your own functions if you want it to be draggable | |
//and do other things, like move on its own or be affected by gravity. | |
//You can add more actions to the update function, and it will be called automatically by the handler. | |
class DraggableCircle implements Draggable | |
{ | |
PVector center; | |
int radius; | |
boolean dragged; | |
int fillColor; | |
PVector clickOffset; //offset of cursor from center | |
DraggableCircle(int centerX, int centerY, int tempRadius, int tempColor) | |
{ | |
center = new PVector(centerX, centerY); | |
radius = tempRadius; | |
fillColor = tempColor; | |
dragged = false; | |
PVector clickOffset = new PVector(0, 0); | |
} | |
void display() | |
{ | |
noStroke(); | |
fill(fillColor); | |
ellipse(center.x, center.y, radius*2, radius*2); | |
} | |
boolean isOver(int x, int y) | |
{ | |
//return true if distance between point and center is smaller than radius | |
if (dist(center.x, center.y, x, y) <= radius) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
void update() //move with mouse | |
{ | |
if (dragged) | |
{ | |
if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) //don't let it get away! | |
{ | |
center.set(PVector.add(new PVector(mouseX, mouseY), clickOffset)); | |
} | |
} | |
} | |
void setDragged(int fromX, int fromY) | |
{ | |
dragged = true; | |
clickOffset = PVector.sub(center, new PVector(fromX, fromY)); | |
} | |
void releaseDragged() | |
{ | |
dragged = false; | |
} | |
} | |
//You don't need to change anything below here! | |
//Here in case you want to peek under the hood, but | |
//there's some slightly weird stuff. | |
interface Draggable | |
{ | |
void update(); | |
void display(); | |
void setDragged(int fromX, int fromY); | |
void releaseDragged(); | |
boolean isOver(int x, int y); | |
} | |
public class DraggableHandler | |
{ | |
ArrayList<Draggable> draggables; | |
Draggable currentDragged; | |
DraggableHandler(PApplet app) | |
{ | |
draggables = new ArrayList<Draggable>(); | |
currentDragged = null; | |
app.registerMethod("draw", this); | |
app.registerMethod("mouseEvent", this); | |
} | |
void addToEnd(Draggable d) | |
{ | |
draggables.add(d); | |
} | |
void addToFront(Draggable d) | |
{ | |
draggables.add(0, d); //add to index 0, shift all others | |
} | |
void remove(Draggable d) | |
{ | |
draggables.remove(d); | |
} | |
void display() | |
{ | |
for (int i=draggables.size()-1;i>=0;i--) //draw backwards so first will be on top | |
{ | |
draggables.get(i).display(); | |
} | |
} | |
public void draw() //this draw is automatically called at the end of each draw() | |
{ | |
for (int i=0;i<draggables.size();i++) | |
{ | |
draggables.get(i).update(); | |
} | |
} | |
public void mouseEvent(MouseEvent event) | |
{ | |
int x = event.getX(); | |
int y = event.getY(); | |
switch (event.getAction()) { | |
case MouseEvent.PRESS: | |
// choose top draggable to drag if clicked on | |
if (currentDragged == null) | |
{ | |
for (int i=0;i<draggables.size();i++) | |
{ | |
if (draggables.get(i).isOver(x, y)) | |
{ | |
currentDragged = draggables.get(i); | |
currentDragged.setDragged(x, y); | |
break; | |
} | |
} | |
} | |
break; | |
case MouseEvent.RELEASE: | |
// release any dragged objects | |
if (currentDragged != null) | |
{ | |
currentDragged.releaseDragged(); | |
currentDragged = null; | |
} | |
break; | |
case MouseEvent.CLICK: | |
// | |
break; | |
case MouseEvent.DRAG: | |
// | |
break; | |
case MouseEvent.MOVE: | |
// | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment