How do I enable all objects to be dragged?
My question is; How do I enable all objects to be dragged around the screen?
float Dist;
boolean hover = false;
boolean locked = false;
float difx = 0.0;
float dify = 0.0;
void setup() {
size(300, 300);
ellipseMode(CENTER);
particles = new ArrayList();
smooth();
Dist = (50);
}
void draw() {
background(255);
for (int i=0; i<particles.size(); i++) {
Particle p = (Particle) particles.get(i);
//p.run();
//p.gravity();
//p.move();
p.display();
}
for (int i=particles.size()-1; i>=0; i--) {
Particle p1 = (Particle) particles.get(i);
for (int j=0; j < particles.size(); j++) {
Particle p2 = (Particle) particles.get(j);
if (dist(p1.x, p1.y, p2.x, p2.y) < Dist) {
line(p1.x, p1.y, p2.x, p2.y);
}
}
}
for (int i=particles.size()-1; i>=0; i--) {
Particle p1 = (Particle) particles.get(i);
if (mouseX > p1.x-10 && mouseX < p1.x+10 && mouseY > p1.y-10 && mouseY < p1.y+10) { // MOUSE OVER
hover = true;
if(!locked);
}
else {
hover = false;
}
}
}
/*
if (particles.size() > 100) {
particles.remove(0);
} */
void mousePressed() {
if (mouseButton == RIGHT)
particles.add(new Particle());
for (int i=particles.size()-1; i>=0; i--) {
Particle p1 = (Particle) particles.get(i);
if(hover) {
locked = true;
fill(255);
}
else {
locked = false;
}
difx = mouseX-p1.x;
dify = mouseY-p1.y;
}
}
void mouseDragged() {
if (mouseButton == LEFT)
for (int i=particles.size()-1; i>=0; i--) {
Particle p1 = (Particle) particles.get(i);
if(locked) {
p1.x = mouseX-difx;
p1.y = mouseY-dify;
}
}
}
void mouseReleased() {
locked = false;
}
void keyPressed() {
switch(key) {
case '=':
Dist+=20;
break;
case '-':
Dist-=20;
break;
}
}
//////////////////////////////////// || CLASS
// A simple Particle Class
class Particle {
float x;
float y;
float xspeed;
float yspeed;
Particle() {
x = mouseX;
y = mouseY;
xspeed = random(-1,1);
yspeed = random(-2,0);
}
void run() {
x = x + xspeed;
y = y + yspeed;
}
void gravity() {
yspeed += 0.1;
}
void move() {
y = y + yspeed;
if (y > height) {
yspeed = yspeed * -0.55;
y = height;
}
}
void display() {
stroke(0);
fill (0, 75);
ellipseMode(CENTER);
ellipse(x, y, 10, 10);
}
}