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.
Page Index Toggle Pages: 1
dragging a rect (Read 984 times)
dragging a rect
Oct 22nd, 2007, 6:00pm
 
hello.
how can i track down where on an object the mouse i located? i would like to be able to click it and drag it without it automatically snapping to the mouseX and Y.

int rectX, rectY;
int rectSize = 260;
int maxSize = 270;
int minSize = 260;

color rectColor;
color rectColor2;

int Alpha = 255;


boolean rectOver = false;

void setup()
{
 size(325, 330);
 colorMode(RGB,255,255,255,100);
}

void draw()
{
 update(mouseX, mouseY);
 
 
 rectColor = color(25,65,245,Alpha);
 rectColor2 = color(122,248,149,Alpha);
 
 if((mousePressed) && (rectOver)){
 rectX = mouseX;
 rectY = mouseY;}
 
 else{
 rectX = maxSize/2 + 30;
 rectY = maxSize/2 + 30;
}


 rectMode(CENTER);
 noStroke();
 background(255);

 fill(rectColor);
 
 rect(rectX, rectY, rectSize, rectSize);
 
}

void update(int x, int y)
{

 if ( overRect(rectX-(rectSize/2), rectY-(rectSize/2), rectSize, rectSize) ) {
   rectOver = true;
   rectSize += 1;
 }
 else {
   rectOver = false;
   rectSize -= 1;
 }
 if ( rectSize > maxSize){
   rectSize= maxSize;    
 }

 if ( rectSize < minSize ){
   rectSize= minSize;
 }

}

boolean overRect(int x, int y, int width, int height)
{
 if (mouseX >= x && mouseX <= x+width &&
   mouseY >= y && mouseY <= y+height) {
   return true;
 }
 else {
   return false;
 }
}

Re: dragging a rect
Reply #1 - Oct 22nd, 2007, 8:20pm
 
use pmouseX and pmouseY :

lines #28 - #30
Code:
if((mousePressed) && (rectOver)){
rectX += mouseX - pmouseX;
rectY += mouseY - pmouseY;}
Re: dragging a rect
Reply #2 - Oct 22nd, 2007, 10:12pm
 
hi,

when you click the rect, the clicked pixel has a certain distance to rectX and rectY. Save the difference, and add it to the positioning of the rect. Something like this:

rectX = mouseX+offsetX;
rectY = mouseY+offsetY;

As you don't want to update the offset variables every frame, use the mouse events instead of your if (mousePressed) in the draw loop.
void mousePressed() {
 println("Mouse has been pressed");
 // check overRect() here
 // update the offsets here... get the difference of mouseX and rectX, same for Y.
}

you could introduce a new boolean variable mouseRectDown to hold the status of your mouse button. set it to true in mousePressed() and to false in mouseReleased(). Now, only update the rects position if  mosueRectDown is true.


Re: dragging a rect
Reply #3 - Oct 23rd, 2007, 10:54am
 
so far i have this. i just cant figure out how to make it update only when the mouseRectDown is true.

int rectX, rectY;  
int rectSize = 260;
int maxSize = 270;
int minSize = 260;

color rectColor;
color rectColor2;

boolean mouseRectDown;

int offsetX;
int offsetY;


boolean rectOver = false;

void setup()
{
 size(325, 330);
}

void draw()
{
 update(mouseX, mouseY);
 
 
 rectColor = color(25,65,245);
 rectColor2 = color(122,248,149);
 
 if(mouseRectDown){
 if(mousePressed){
 rectX = mouseX - offsetX;
 rectY= mouseY - offsetY;
 }}
 
 else{
 rectX = maxSize/2 + 30;
 rectY = maxSize/2 + 30;

}


 rectMode(CENTER);
 noStroke();
 background(255);

 fill(rectColor);
 
 rect(rectX, rectY, rectSize, rectSize);
 
}

void update(int x, int y)
{

 if ( overRect(rectX-(rectSize/2), rectY-(rectSize/2), rectSize, rectSize) ) {
   rectOver = true;
   rectSize += 1;
 }  
 else {
   rectOver = false;
   rectSize -= 1;
 }
 if ( rectSize > maxSize){
   rectSize= maxSize;    
 }

 if ( rectSize < minSize ){
   rectSize= minSize;
 }

}

boolean overRect(int x, int y, int width, int height)  
{
 if (mouseX >= x && mouseX <= x+width &&  
   mouseY >= y && mouseY <= y+height) {
   return true;
 }  
 else {
   return false;
 }
}

void mousePressed() {
 println("Mouse has been pressed");
 // check overRect() here
if(rectOver){
offsetX = mouseX - rectX;
offsetY = mouseY - rectY;

mouseRectDown = true;

}}

void mouseReleased() {
 println("Mouse has been released");

 mouseRectDown = false;

}
Re: dragging a rect
Reply #4 - Oct 23rd, 2007, 2:18pm
 
hi,

your code does exactly that, doesn't it? Well done! It updates the POSITION only when mouseRectDown is true. Or do you mean the update() method? you need to call update() every frame, as you want that mouseOver-behaviour. I'd call it updateSize() or something, to make it clearer.

There are some extra lines in your code that you actualy don't need. E.g. the if (mousePressed) in your draw-loop.

Re: dragging a rect
Reply #5 - Oct 23rd, 2007, 5:57pm
 
okay. thank you very much:)
what i have i mind now is to be able to drag and drop it. so that the position is only "updated" when im dragging it. how would one be able to do that?

Slm
Re: dragging a rect
Reply #6 - Oct 23rd, 2007, 6:11pm
 
well, your code resets the position of the rect as soon as the mouse is released (as soon as mouseRectDown is not true). If you go trhough your draw loop and understand every line, it will be easy for you to know what you need to change to get the desired dropping behaviour.
Re: dragging a rect
Reply #7 - Oct 23rd, 2007, 7:05pm
 
okay then:) thank you very much for your help you saved my day! ill try and do it!

Slm
Page Index Toggle Pages: 1