tags:

views:

87

answers:

2

This is my code... curX and curY are my current X and Y coordinates while tmpX and tmpY are relative X and Y values (how much the mouse has moved).

curX:= curX+tmpX;
curY:= curY+tmpY;

I use these values to move/control my cursor-shaped form.
How can I keep the cursors within the screen?

I tried limiting the values until Screen.Height and Screen.Width...here's the code.

if(curX>Screen.Width) then
  curX:=Screen.Width;
if(curY>Screen.Height) then
  curY:=Screen.Height;

but it didn't work.


Solved it! (Sort of):

curX:= max(0, min((curX+tmpX), Screen.Width));
curY:= max(0, min((curY+tmpY), Screen.Height));

The only issue I have is that (0,0) is apparently not the upper left most (very close though, just a couple of pixels off I think) and (Screen.Width, Screen.Height) is not the upper right most (also close, the cursor disappears at the right most, though I think is one is supposed to behave that way).

+2  A: 

You can create periodic boundary conditions by calculating (curX+tmpX) mod (screenSizeX) or limit the curX values with curX:= min(curX+tmpX, screenSizeX).

zoli2k
Thanks for answering. For some strange and amusing reason when I use the mod solution and move the mouse to the rightmost, the cursors disappears at the right and comes out the left and when I move it to bottom it comes out at the top (though this doesn't happen when I move it to the leftmost or top). Is there any way to make this consistent on all sides?
Dian
This is the "periodic" boundary condition. You should use the second solution with min() function.
zoli2k
Yeah, thanks, I used min and max (because min alone didn't stop the cursor from disappearing from top and left). The weird thing is that I do not know the proper coordinates for the boundaries. :D
Dian
+1  A: 

IF you limit it properly, it won't go outside your range. Post your code if you want to know why it's broken.

var1 := Inc(var1,amt);
var2 := Inc(var1,amt);
if var1 > limit1 then
    var1 := limit1;
if var2 > limit2 then
    var2 := limit2;

You must be careful to know if your limit is off all the screens in your system, not just the current screen. not everybody has one screen only. Some people have multiple screens.

Warren P
It's quite similar to my solution, but I don't know why it didn't work. The cursor still disappeared.Thanks for answering though, I'll keep that multiple screen thing in mind.
Dian
Try subtracting a bit from your limits. Remember that when the cursor is at the far extent of the screen, the position you are setting is the visible tip of the cursor, which is probably the top left corner. Ergo, you see nothing. Or one pixel which is the tip of the cursor.
Warren P