Constraining the pointer within a rect

David Glasgow david at dvglasgow.wanadoo.co.uk
Thu Aug 17 05:36:02 EDT 2006


Greetings

This is my annual mailing on this topic.  I won't bore you with why I 
need to do this deprecated act, but I do.

Basically I need the pointer to refuse to move outside a rect, and just 
'bounce' against the edge.  No recoil or anything fancy, just stop 
until the user moves elsewhere with the rect.  I had a script that 
worked in a standalone in 1.1.1.  It was inelegant, but effectively.  
It became painfully slow and jerky in 2.x.

I posted to this list, and someone came up with a demon script (a 
variant of what follows). Can't remember who it was, but thanks again.  
I tested it in an empty stack, and it worked.  Months later I come to 
actually implement it in my project, and can I get it to work?

The script is as follows:

on mouseMove X,Y
   --if button "constrain" is visible then
     put the rect of fld "container" into SrcImageRect
     put globalloc((item 1 of SrcImageRect) & "," & (item 2 of 
SrcImageRect)), globalloc((item 3 of SrcImageRect) & "," & (item 4 of 
SrcImageRect)) into Fred
     put min (item 3 of Fred - 4, max (item 1 of Fred + 4, X)) into X
     put min (item 4 of Fred - 4, max (item 2 of Fred + 4, Y)) into Y
     get the localloc of (X,Y)
     set the screenMouseLoc to it
   --end if
end mouseMove


The explanation which accompanied the original script is :

 >    If the mouseLoc is within the specified rectangle, the mouse's X 
co-ord
 > will be less than item 3 of the rect, and greater than item 1 of the  
rect. Thus,
 > max (item 1 of rect, the mouse X) will always result in a number *at 
least*
 > as large as that item 1, and min (item 3 of rect, the mouse Y) will 
always
 > result in a number *no greater than* that item 3. Plug the results of 
the max into
 > a min (sorry about how that sounds, but you get the idea, right?), 
and you
 > end up with a number that *must* be somewhere between item 1 of the
 > rect and item 3 of the rect.Something similar applies to the mouse's 
Y co-ord, and items 2 and
 > 4 of the rect.

There are 2 issues.  The first is that if I uncomment the 'if then', 
whatever the visible of the button "constrain", nothing happens.  
Pointer moves freely about the screen.

Commenting out the 'if then' results in a pointer which won't enter the 
stack window (the field "container" is much smaller than the stack), 
but jumps to a localloc of  121,-2.

I have modified the original script by adding in the globalloc bit to 
get round a bug using the rect directly (thank you Mr Jimmieson).  I 
think this was present in 2.2, and doesn't seem to be completely fixed 
in 2.7.2.  if I miss the globalloc conversion  out, the pointer dances 
around somewhere between the origin of the screen and the stack.

I have a hunch I am not properly disentangling local and global locs, 
but I can't see where.

Best Wishes,

David Glasgow
Carlton Glasgow Partnership

http://www.i-psych.co.uk


More information about the use-livecode mailing list