Page 1 of 1

MouseLeave message to be sent when object moves away

Posted: Sat Oct 04, 2014 7:14 am
by andrewferguson
Hi,

I have an object that has a mouseLeave handler. This handler is run when the mouse moves off the object. However if I keep the mouse still and move the object with the "move" command, mouseLeave is only sent when the user moves their mouse a tiny bit after the object have been moved from under the mouse pointer.

I really need the mouseLeave message to be sent immediately when the object moves away (by the move command) from under the mouse pointer.

Does anyone know how to do this?

Thanks

Re: MouseLeave message to be sent when object moves away

Posted: Sat Oct 04, 2014 4:23 pm
by dunbarx
Hi,

This is similar to a few recent threads that deal with like issues.

No messages are sent during a move. It is necessary to set up a recursive handler after the "move" command line (pseudo):

Code: Select all

...
move whatever
checkForMouseLeave
...

on checkForMouseLeave
put "Still Inside!!!"
if the mouseLoc is not within the rect of whateverControl then
put "Just Left"
exit to top
send checkForMouseLeave in 1
end checkForMouseLeave
Fun stuff.

Craig Newman

Re: MouseLeave message to be sent when object moves away

Posted: Sat Oct 04, 2014 4:45 pm
by FourthWorld
One way to do that would be to poll for the state you're looking for (in your case, whether the mouse is within the object) using a timer, e.g.:

Code: Select all

on mouseUp
   set the label of me to "In"
   set the movespeed to 100
   send "PollForMouseWithin" to me in 10 millisecs
   move me rel 200,200 without waiting
end mouseUp

on PollForMouseWithin
   if not within(me, the mouseLoc) then
      set the label of me to "Out" 
   end if
   if "PollForMouseWithin" is not in the pendingMessages \
         AND the movingControls is not empty then
      send "PollForMouseWithin" to me in 10 millisecs
   end if
end PollForMouseWithin

Re: MouseLeave message to be sent when object moves away

Posted: Sun Oct 05, 2014 7:22 pm
by [-hh]
Yet another solution.

Immediately before the move do
send "mouseLeave" to me in 1 millisecs
This goes 'immediately' into the main loop and is done before the move!

Finally, if needed, immediately after the move do
send "mouseEnter" to me in 1 millisecs

To see what I mean and how it works here a test script. Also drag the button and let mouse go up.

Code: Select all

on mouseUp
  -- set rect of me to (200,200,280,280)
   lock messages # <-- avoids flicker
   send "mouseLeave" to me in 1 millisecs
  --> "move me to (0,0) in 1 second" has after dragging
  --> (with grab) a wrong starting point, correct this:
  move me from the mouseloc to (0,0) in 1 second
  move me to (100,100) in 1 second
  if the mouseloc is within the rect of me
  then send "mouseEnter" to me in 1 millisecs
end mouseUp

on mouseEnter
  if there is no grc "Light0" then createOval
  set backcolor of grc "Light0" to "red"
end mouseEnter

on mouseLeave
  set backcolor of grc "Light0" to "green"
end mouseLeave

on mouseDown
  grab me
end mouseDown
on createOval
  create grc "Light0"
  set style of grc "Light0" to oval
  set opaque of grc "Light0" to true
  set rect of grc "Light0" to (0,0,64,64)
end createOval