Dragging multiple objects separately
in
Programming Questions
•
2 years ago
Hi everybody,
I'm traying to make a tree structure where every node can be dragged anywhere. I'm having a problem about the dragging part. First of all if I keep the mouse pressed anywhere and I move it over a node it will drag it wherever I put the mouse, but I want that to happen only if the mouse is first pressed over the node. Becuase of this, if I'm already dragging a node and the mouse pass over another node then I'll be dragging two nodes, or three, and so on. I thought about using a global boolean var but it doesn't seem to work..here a piece of code...
void mouseDragged(){
if(locked){
dragging=true;
}
}
void mouseReleased(){
dragging=false;
}
void draw(){
background(0,0,0);
//displaying the root node outsied the for loop so that dragging
//will not affect this node
nodes[0].display();
for (int i = 1; i < numNodes; i++) {
nodes[i].checkPos();
nodes[i].display();
if( dist(nodes[i].xpos, nodes[i].ypos, mouseX, mouseY) < nodes[i].radius){
if( !locked){
locked =true;
if(dragging){
nodes[i].c = color(90,90,90);
nodes[i].xpos = mouseX;
nodes[i].ypos = mouseY;
}
}else{
nodes[i].c=color(255,255,255);
locked=false;
}
}
};
}
Maybe this code won't make any sense to somebody but it's just one of the solutions I tried...too many if condition =D
PS = I didn't report the setup part and the class Node
I'm traying to make a tree structure where every node can be dragged anywhere. I'm having a problem about the dragging part. First of all if I keep the mouse pressed anywhere and I move it over a node it will drag it wherever I put the mouse, but I want that to happen only if the mouse is first pressed over the node. Becuase of this, if I'm already dragging a node and the mouse pass over another node then I'll be dragging two nodes, or three, and so on. I thought about using a global boolean var but it doesn't seem to work..here a piece of code...
void mouseDragged(){
if(locked){
dragging=true;
}
}
void mouseReleased(){
dragging=false;
}
void draw(){
background(0,0,0);
//displaying the root node outsied the for loop so that dragging
//will not affect this node
nodes[0].display();
for (int i = 1; i < numNodes; i++) {
nodes[i].checkPos();
nodes[i].display();
if( dist(nodes[i].xpos, nodes[i].ypos, mouseX, mouseY) < nodes[i].radius){
if( !locked){
locked =true;
if(dragging){
nodes[i].c = color(90,90,90);
nodes[i].xpos = mouseX;
nodes[i].ypos = mouseY;
}
}else{
nodes[i].c=color(255,255,255);
locked=false;
}
}
};
}
Maybe this code won't make any sense to somebody but it's just one of the solutions I tried...too many if condition =D
PS = I didn't report the setup part and the class Node
1