We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Move a selected point using mouseX and mouseY
Page Index Toggle Pages: 1
Move a selected point using mouseX and mouseY (Read 460 times)
Move a selected point using mouseX and mouseY
Jan 22nd, 2009, 10:34am
 
I want to be able to select a point (x,y coordinate), than drag it and after releasing the mousebutton, make these mouseX and mouseY coordinates the new coordinates of the point. This is working now, though I have the feeling I can do it in a better/easier way. Also I wonder if it is possible to see the point during dragging. At the moment I only see the point (at new location) after I let go of the mousebutton, because I do not update the coordinates in the void mouseDraggged()...

Q1. Should I combine the mouseDragged() en mouseReleased() voids as I did now? Is there not an easier way, since I use the same loop two times (both in dragged as in released void)

Q2. How can I see the movement of the point during dragging

Thanks in advance!


Code so far:
Quote:
void mouseDragged(){
 for (int pc=1; pc<(totalNodes+1); pc++){
   if (check==0){
     if (pc<=normalNodes){
       if ((mouseX >= (originXgrids-4+(scalePG*n[pc].x)))&&(mouseY <= (originYPrimalGrid+4-(scalePG*n[pc].y)))&&
             (mouseX <= (originXgrids+4+(scalePG*n[pc].x)))&&(mouseY >= (originYPrimalGrid-4-(scalePG*n[pc].y)))){
         check++;
         selectedQ = true;
         n[pc].select(selectedQ);
       }
     } else {
         if ((mouseX >= (originXgrids-4+(scalePG*fn[pc-(normalNodes+1)].x)))&&(mouseY <= (originYPrimalGrid+4-(scalePG*fn[pc-(normalNodes+1)].y)))&&
               (mouseX <= (originXgrids+4+(scalePG*fn[pc-(normalNodes+1)].x)))&&(mouseY >= (originYPrimalGrid-4-(scalePG*fn[pc-(normalNodes+1)].y)))){          
           check++;
           selectedQ = true;
           fn[pc-(normalNodes+1)].select(selectedQ);                  
         }
       }
   }
 }
}

void mouseReleased() {
 float x = mouseX-originXgrids;
 float y = originYPrimalGrid-mouseY;
 for (int pc=1; pc<(totalNodes+1); pc++){
   if (pc<=normalNodes && n[pc].selected()){
     n[pc].x = x/scalePG;
     n[pc].y = y/scalePG;
     selectedQ = false;
     n[pc].select(selectedQ);
   } else if (pc>normalNodes && fn[pc-(normalNodes+1)].selected()){        
       fn[pc-(normalNodes+1)].x = x/scalePG;
       fn[pc-(normalNodes+1)].y = y/scalePG;
       selectedQ = false;
       fn[pc-(normalNodes+1)].select(selectedQ);
     }
 }
 check = 0;
}
Re: Move a selected point using mouseX and mouseY
Reply #1 - Jan 22nd, 2009, 12:51pm
 
hi,

here's a quick (uncommented) snippet

Quote:
Node dragNode = null;
ArrayList nodes;
int nodeCount = 50;

void setup() {
  size(500, 500);
  smooth();
  nodes = new ArrayList();
  for (int i = 0; i < nodeCount; i++) {
    Node n = new Node(random(width), random(height), random(8, 13));
    nodes.add(n);
  }
}

void draw() {
  background(0xffaabbcc);
  for (int i = 0; i < nodes.size(); i++) {
    Node n = (Node) nodes.get(i);
    n.draw();
  }
}

void mousePressed() {
  dragNode = null;
  Node n;
  for (int i = 0; i < nodes.size(); i++) {
    n = (Node) nodes.get(i);
    if (n.contains(mouseX, mouseY)) {
      dragNode = n;
    }
  }
}

void mouseDragged() {
  if (dragNode != null) {
    dragNode.x = mouseX;
    dragNode.y = mouseY;
  }


}

void mouseReleased() {
  dragNode = null;
}

class Node {
  float x, y, r;
  Node(float x, float y, float r) {
    this.x = x;
    this.y = y;
    this.r = r;
  }
  void draw() {
    ellipse(x, y, r * 2f, r * 2f);
  }
  boolean contains(float x, float y) {
    float dx = this.x - x;
    float dy = this.y - y;
    return sqrt(dx * dx + dy * dy) <= r;
  }
}




hope this helps
frank
Re: Move a selected point using mouseX and mouseY
Reply #2 - Jan 22nd, 2009, 3:01pm
 
Thanks Frank!

It is working very well, and the script is even a lot more clean and clear now.
One more question: I am actually working with a 3D model as basis(hence the OPENGL), and I create 2D grids of it (hence the question about only moving in X and Y direction.) Now I wonder if it is possible to extend the script to 3D?

Thanks again...
Page Index Toggle Pages: 1