Page 1 of 1
Drag and Drop Question
Posted: Thu Oct 07, 2010 5:06 pm
by interactbooks
I'm a bit new to the LiveCode environment so please bear with me. In the script below I am allowing a user to drag around any image object placed on the card. The intent is to show the user the location of the image as they are dragging it around. The dragStart works but the dragDrop doesn't seem to be invoked. I assume the problem is because this message needs to be associated with the actual object. The problem I am having is that these images aren't present till the user imports them from their file system. So I'm not sure how to write a method for an object that doesn't exist yet (in other words how do I write a generic method for any image on the screen that would get invoked). I'm sure I'm missing something obvious.
Code: Select all
on dragStart
if word 1 of the name of mouseControl = "image" then
grab mouseControl
end if
End dragStart
on dragDrop
put location of mouseControl into field currentLocation
end dragDrop
Re: Drag and Drop Question
Posted: Thu Oct 07, 2010 5:21 pm
by Klaus
Hi interactbooks,
if yu will let the user only drag images around on the SAME card, the you could use this generic script, maybe in the card script:
Code: Select all
on mousedown
if word 1 of the name of the target <> "image" then
exit mousedown
end if
grab the target
end mousedown
on mouseup
if word 1 of the name of the target <> "image" then
exit mouseup
end if
put the loc of the target into fld "currentlocation"
## ALWAYS put Quotes around names!
end mouseup
Best
Klaus
Re: Drag and Drop Question
Posted: Fri Oct 08, 2010 3:57 am
by interactbooks
Thanks for the response Klaus. Do you know if there might be a way I can show the location information as the user is dragging the image around? Right now it displays the location only after the mouseUp event occurs.
Re: Drag and Drop Question
Posted: Fri Oct 08, 2010 6:38 am
by WaltBrown
Hi interactBooks.
I tried this in a circle object and it worked OK. I create a field named "fLoc" and had it follow the mouse around as it dragged the circle. Note that when you put this into a card script you have to deal with the change in screenMouseLoc, which gives stack relative coordinates when the script is in the object, but screen relative coordinates when the script is in the card or stack.
Walt
Code: Select all
on mouseDown
grab the target
end mouseDown
on mouseMove
local tMouse
show fld "fLoc"
put the screenMouseLoc into tMouse
set the Loc of fld "fLoc" to (item 1 of tMouse + 50),(item 2 of tMouse - 50)
put tMouse into fld "fLoc"
end mouseMove
on mouseLeave
hide fld "fLoc"
end mouseLeave
on mouseEnter
show fld "fLoc"
put the screenMouseLoc into fld "fLoc"
end mouseEnter
Re: Drag and Drop Question
Posted: Fri Oct 08, 2010 6:58 am
by WaltBrown
Polished a bit for card script use rather than object script use, and following Klaus' example, only acts on objects with the word "Circle" in their name:
on mouseLeave
if not(the name of the target contains "Circle") then exit mouseLeave
hide fld "fLoc"
end mouseLeave
on mouseEnter
if not(the name of the target contains "Circle") then exit mouseEnter
show fld "fLoc"
end mouseEnter
on mouseDown
if not(the name of the target contains "Circle") then exit mouseDown
grab the target
end mouseDown
on mouseMove
local tMouse
if not(the name of the target contains "Circle") then exit mouseMove
put the loc of the target into tMouse
set the loc of fld "fLoc" to (item 1 of tMouse + 50),(item 2 of tMouse - 20)
put tMouse into fld "fLoc"
end mouseMove
Re: Drag and Drop Question
Posted: Fri Oct 08, 2010 7:38 am
by Regulae
Hi there,
I tried the following:
Code: Select all
global grabInProgress
on mousedown
if word 1 of the name of the target <> "image" then
exit mousedown
end if
put "yes" into grabInProgress
grab the target
end mousedown
on mouseMove x,y
put x,y into moveLoc
if grabInProgress = "Yes" then
put the loc of the target into fld "currentlocation"
-- put the bottomRight of the target into imageBR
-- set the topLeft of fld "currentlocation" to ((item 1 of imageBR + 2)),((item 2 of imageBR + 2))
end if
end mouseMove
on mouseup
if word 1 of the name of the target <> "image" then
exit mouseup
end if
put "no" into grabInProgress
-- hide fld "currentlocation"
end mouseup
... which updates fld “currentlocation” as you move the image. If you want the field to “track” the image, uncomment the commented lines. These make the field move with the image, just below and to the right of its bottom right corner. I hadn't really worked with "grab" before, so Klaus's script is quite an inspiration.
Edit: A tidier line to reposition fld "currentlocation" is:
Code: Select all
set the topLeft of fld "currentlocation" to the bottomRight of the target
Regards,
Michael
Re: Drag and Drop Question
Posted: Fri Oct 08, 2010 11:14 am
by Klaus
Hi all,
some useful hints:
Code: Select all
on mouseLeave
if not(the name of the target contains "Circle") then
exit mouseLeave
end if
hide fld "fLoc"
end mouseLeave
,,,
This will also accept objects named "My Circle", "Circle and square" etc.
So you better script:
...
if the short name of the target = "circle" then
...
"mousemove" will also provide two parameters, as Reguale already showed in his script, so no extra checking of "the mouseloc" etc is necessary!
Code: Select all
on mousemove x,y
### do whatever you want to
put x & "," & y into fld "current mouseloc"
end mousemove
Best
Klaus
Re: Drag and Drop Question
Posted: Sat Oct 09, 2010 12:29 pm
by WaltBrown
The only reason I used "Circle", Klaus, was to show that the squares in my demo were NOT affected by the script, regardless of their layer position.
Walt
Re: Drag and Drop Question
Posted: Sat Oct 09, 2010 2:33 pm
by interactbooks
Thank you everyone for your responses, it's great to see such a vibrant community!