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
mouseDrag (Read 495 times)
mouseDrag
Feb 24th, 2009, 11:04pm
 
In the code below, I need the white rectangle and the word to drag with the mouse only when the mouse is over the rectangle and only while pressed. At the moment, I can only drag down and to the right, and it frequently misses the drag entirely.

Eventually I'll create a class for this (I'll need to have a few of these on the screen at a time), but any thoughts on what I'm missing here?

-----

String word1 = "drag me";
float rdmX = random(40, 650);
float rdmY = random(40, 500);

void setup() {
 size(800,600);
}

void draw() {
 background(0);

 PFont font;
 font = loadFont("TimesNewRomanPSMT-48.vlw");
 textFont(font);
 textSize(48);
 fill(255);
 rect(rdmX, rdmY, (word1.length()*24), 55);
 rectMode(CENTER);
 fill(0);
 text(word1, rdmX, (rdmY + 15));
 textAlign(CENTER);

}

void mouseDragged() {
 if ((mouseX > rdmX) && (mouseX < rdmX+(word1.length()*24)) && (mouseY > rdmY) && (mouseY < rdmY + 55)) {
   rdmX = mouseX;
   rdmY = mouseY;
 }
}




Re: mouseDrag
Reply #1 - Feb 24th, 2009, 11:27pm
 
you're drawing the text rectangle in CENTER mode but your condition for dragging is checking as if it was drawn in CORNERS mode. so it's only valid in lower right quadrant of the rectangle which is why you can only drag it down and to the right.
Re: mouseDrag
Reply #2 - Feb 24th, 2009, 11:42pm
 
Yeah, I've been going back and forth on that one. If I leave it as is, it only checks the bottom corner and that's the only place I can take it. But I need the box and the text to be centred with respect to each other.

Alternatively, I can declare the limits of the box when it appears, but then the drag is only in effect on the box's initial position, rather than updating to its new position.

So I gather I need to either get the drag to follow the CENTER of the box, or have the declared limits update every frame. But how?
Re: mouseDrag
Reply #3 - Feb 24th, 2009, 11:54pm
 
change the check then.

you need to accept the dragging when the mouse is within plus or minus half the width and within plus or minus half the height of the box centre.

Code:

if ((mouseX > rdmx - hwidth) &&
   (mouseX < rdmx + hwidth) &&
   (mouseY > rdmy - hheight) &&
   (mouseY < rdmy + hheight)) {
 // inside box
}


given that the text doesn't change (does it?) then word1.length() * 24 won't change so calculate it once rather than every single time. what do people have against local variables? 8) ditto the height, assign it once and use the variable rather than littering the code with '55'.

i haven't checked any of this 8)

(edited for 'the the' and smileys)
Re: mouseDrag
Reply #4 - Feb 25th, 2009, 12:59am
 
This is perfect, thank you! I'll never speak ill of local variables again.
Page Index Toggle Pages: 1