Page 1 of 1

mouseMove before mouseDown [FIXED, hilite and/or drag object

Posted: Fri Jul 17, 2015 3:00 am
by mattmaier
I've got a text field that represents an object. I want to either click on it, hiliting it, or drag it, repositioning it. However, I keep getting mouseMove messages before mouseDown, regardless of the fact that the mouse isn't moving. This is a script that sort of works. It's got a couple problems. The most interesting one is that it allows me to "sneak up" on the object. If I click and then drag quickly the object follows, but if I drag slowly the object stays put.

There's a short forum conversation about the mouseMove/mouseDown weirdness. That can't possibly be the correct behavior, right?
http://forums.livecode.com/viewtopic.php?f=9&t=18691

Code: Select all

on mouseMove pX,pY
   if the mouse is down then
      set the cOldX of me to the cNewX of me
      set the cOldY of me to the cNewY of me
      if the cOldX of me is empty then set the cOldX of me to pX
      if the cOldY of me is empty then set the cOldY of me to pY
      set the cNewX of me to pX
      set the cNewY of me to pY
      put pX - the cOldX of me into tDiffX
      put pY - the cOldY of me into tDiffY
      if abs(tDiffX) < 5 and abs(tDiffY) < 5 then
         userGraphNetSel(the cRecKey of me)
      else
         put the loc of me into tOldLoc
         put item 1 of tOldLoc + tDiffX into tNewObjX
         put item 2 of tOldLoc + tDiffY into tNewObjY
         userDrag(the long name of me, tNewObjX, tNewObjY, the cRecKey of me)
         set the cDragging of me to true
      end if
   end if
end mouseMove
on mouseUp
   if the cDragging of me is true then 
      userMoveNode(the cRecKey of me, the loc of me)
      set the cDragging of me to false
   end if
end mouseUp

Re: mouseMove before mouseDown

Posted: Sat Jul 18, 2015 3:17 am
by PBH
The mouseMove command seems to be blocking the mouseUp, I tested LC 5.5 too and it causes the same problem, so probably a long standing issue. In a simple experiment I found that I could get the mouseUp message to fire reliably by adding an 'exit mouseMove' step to the end of the mouseMove handler, I can't guarantee it will work here, but you could easily try it…

Code: Select all

on mouseMove pX,pY
   if the mouse is down then
      set the cOldX of me to the cNewX of me
      set the cOldY of me to the cNewY of me
      if the cOldX of me is empty then set the cOldX of me to pX
      if the cOldY of me is empty then set the cOldY of me to pY
      set the cNewX of me to pX
      set the cNewY of me to pY
      put pX - the cOldX of me into tDiffX
      put pY - the cOldY of me into tDiffY
      if abs(tDiffX) < 5 and abs(tDiffY) < 5 then
         userGraphNetSel(the cRecKey of me)
      else
         put the loc of me into tOldLoc
         put item 1 of tOldLoc + tDiffX into tNewObjX
         put item 2 of tOldLoc + tDiffY into tNewObjY
         userDrag(the long name of me, tNewObjX, tNewObjY, the cRecKey of me)
         set the cDragging of me to true
      end if
   end if
   exit mouseMove ## Workaround ##
end mouseMove

on mouseUp
   if the cDragging of me is true then 
      userMoveNode(the cRecKey of me, the loc of me)
      set the cDragging of me to false
   end if
end mouseUp
HTH,

Paul

mouseMove before mouseDown

Posted: Sun Jul 19, 2015 5:51 pm
by mattmaier
I posted this in the open forum but didn't get any traction.

I've got a text field that represents an object. I want to either click on it, hiliting it, or drag it, repositioning it. However, I keep getting mouseMove messages before mouseDown, regardless of the fact that the mouse isn't moving. This is a script that sort of works. It's got a couple problems. The most interesting one is that it allows me to "sneak up" on the object. If I click and then drag quickly the object follows, but if I drag slowly the object stays put.

There's a short forum conversation about the mouseMove/mouseDown weirdness. That can't possibly be the correct behavior, right? Why would the mouseMove message be sent when the mouse isn't moving?
http://forums.livecode.com/viewtopic.php?f=9&t=18691

This is interfering with my script because I have to manually calculate whether or not the mouse is actually moving and the obvious way of doing that doesn't seem to work.

Code: Select all

on mouseMove pX,pY
   if the mouse is down then
      set the cOldX of me to the cNewX of me
      set the cOldY of me to the cNewY of me
      if the cOldX of me is empty then set the cOldX of me to pX
      if the cOldY of me is empty then set the cOldY of me to pY
      set the cNewX of me to pX
      set the cNewY of me to pY
      put pX - the cOldX of me into tDiffX
      put pY - the cOldY of me into tDiffY
      if abs(tDiffX) < 5 and abs(tDiffY) < 5 then
         userGraphNetSel(the cRecKey of me)
      else
         put the loc of me into tOldLoc
         put item 1 of tOldLoc + tDiffX into tNewObjX
         put item 2 of tOldLoc + tDiffY into tNewObjY
         userDrag(the long name of me, tNewObjX, tNewObjY, the cRecKey of me)
         set the cDragging of me to true
      end if
   end if
end mouseMove
on mouseUp
   if the cDragging of me is true then 
      userMoveNode(the cRecKey of me, the loc of me)
      set the cDragging of me to false
   end if
end mouseUp

Re: mouseMove before mouseDown

Posted: Sun Jul 19, 2015 7:45 pm
by PBH
mattmaier wrote:I posted this in the open forum but didn't get any traction.
I responded with a suggested solution to your original post…

http://forums.livecode.com/viewtopic.php?f=8&t=24857

Paul :)

Re: mouseMove before mouseDown

Posted: Sun Jul 19, 2015 8:59 pm
by FourthWorld
Posts from duplicate thread merged here. Going forward please do not create duplicate threads.

Re: mouseMove before mouseDown

Posted: Sun Jul 19, 2015 9:21 pm
by sturgis
I'm not sure exactly what you're going for, but heres a quick sample that might give you some ideas.

Code: Select all

local sHilited -- used to keep track of the background color of the field (I used a label field with opaque set to true) 
on mousedown
   if sHilited is empty then put false into sHilited --initialize hilited to false
   wait 200 millisec with messages -- give a pause to make sure the mouse isn't released too quickly
   if the mouse is down then -- if the mouse is still down aftre 200 millisec, 
      grab me -- grabs hold of the control. Keeps it there as long as the mouse is still down
      set the backgroundcolor of me to cyan -- changes the background color to show the control is in drag mode
   end if
end mousedown

on mouseup
-- when the mouse is released, check to see if the object was in drag mode
-- if so, don't update the background color, leave it as either selected (yellow) or unselected (empty)
-- and reset it to the correct state
-- otherwise toggle the boolean sHilited to its opposite, and color the field background accordingly.
   if the backgroundcolor of me is not cyan then put not sHilited into sHilited
   if sHilited then 
      set the backgroundcolor of me to yellow
   else
      set the backgroundcolor of me to empty
   end if
end mouseup

Re: mouseMove before mouseDown [FIXED, hilite and/or drag ob

Posted: Tue Jul 21, 2015 2:42 am
by mattmaier
sturgis, thanks so much! I'm not sure how long it would have taken me to think of combining a wait and grab, but that set me on the right path and now it works correctly :)

This is the final script. It hilites/unhilites and drags properly!

Code: Select all

on mousedown
   if the cHilited of me is empty then set the cHilited of me to false
   wait 100 millisec with messages
   if the mouse is down then
      grab me
      set the backgroundcolor of me to cyan
   end if
end mousedown
on mouseMove
   if the backgroundcolor of me is cyan then userDrag (the long name of me, the loc of me, the cRecKey of me)
end mouseMove
on mouseup
   if the backgroundcolor of me is cyan then 
      userMoveNode (the cRecKey of me, the loc of me)
   else
      set the cHilited of me to not the cHilited of me
   end if
   if the cHilited of me then
      set the backgroundcolor of me to gray
   else
      set the backgroundcolor of me to empty
   end if
end mouseup