draggable image

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: draggable image

Post by dunbarx » Mon Feb 21, 2022 6:27 pm

OK.

Hardly a finished product. Click on a control in stack "clone" and drag the resultant copy to stack "clone2".
Clone.livecode.zip
(1.42 KiB) Downloaded 79 times
Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: draggable image

Post by richmond62 » Mon Feb 21, 2022 7:47 pm

Ah, missed something:
I would like to click on a control, select it, create a draggable image of that control, move to a new location, release the mouse and create a copy of the control at the new location.
"a draggable image" rather than a clone of the control. :?

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: draggable image

Post by marksmithhfx » Mon Feb 21, 2022 8:43 pm

richmond62 wrote:
Mon Feb 21, 2022 7:47 pm
Ah, missed something:
I would like to click on a control, select it, create a draggable image of that control, move to a new location, release the mouse and create a copy of the control at the new location.
"a draggable image" rather than a clone of the control. :?
Hi Richmond,

At the time I wrote that I didn't know about clone. So I was assuming you would have to make a snapshot, drag that around, though snapshot alone is not enough. You then have to save the snapshot somewhere, make an image, set the image to the text of the snapshot, and in the end it looks terrible. It has weird borders, the text in the image is fuzzy, it's really not very attractive. Then I discovered clone (actually, I discovered that in your example). Make a clone, set it to the mouseLoc and away you go. I'm not saying this is the solution, just that, at this point in the journey if you are trying to create a control of some sort (my personal example has nothing to do with LC controls, its more like a sticky note control), this works pretty well. But I had to try all of these neat ideas you guys threw at me in order to discover it. Your example was very helpful.

Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: draggable image

Post by marksmithhfx » Mon Feb 21, 2022 9:58 pm

dunbarx wrote:
Mon Feb 21, 2022 5:17 pm
Mark.

Whoa. Both handlers are in the card script. I said this in the very first post. I would not even propose that every control have it locally. I am not a barbarian.

Craig
I think I prefer the mouseDown to go directly to the object it is intending to effect (ie. clone / copy) rather than to have to sort out if it's the right action in the right place or not (as in your... if "card" is not in the target... ). If I want the mouseDown to target the card (I do that a lot) then I put it in the card, and it's not confused with some other object.

If you like having the handlers all in one place, you can also move the mouseMove handler from the card into the original object (the one being cloned) and this works as well. In that case you can make myNewControl a local variable since both handlers are now in the same script (keep gHaveNewControl global or a custom property since it is initialized in the preOpenCard handler). What really surprises me is that the mouseMove handler works when placed in the original object. By the time it is cloned there is no handler in the clone, so that can't be where the functionality is coming from. I'll upload a version that has both handlers in the original control, and no mouseMove in the card so all the handlers are located in the same place. There is some advantage to doing that from a documentation perspective... you don't have to look anywhere else to find the handlers or vars that are controlling the action. I haven't tried your latest upload yet. Will check it out tomorrow.

Mark
Attachments
Drag and Drop2.livecode.zip
(1.92 KiB) Downloaded 75 times
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: draggable image

Post by dunbarx » Tue Feb 22, 2022 12:54 am

Mark.

Whether or not to have a single handler in the card script or to have lots of them locally in certain controls, is, in my opinion, is not just a matter of style. It is a matter of robust design. Certainly either way will work, but I do not see an advantage in local handlers. And you have to add the appropriate handlers in every new control you make, and tailor them to the type of control they are, since "last control" no longer applies.

If I wanted to exempt certain controls from being cloned, I would set some property in those controls that is validated in the overarching card handler.

Maybe all this is really a matter of style.

As for "mouseDown" in the card itself, not part of any control, I can see how that might be useful, but then only another small addendum to the handlers I posted is needed to address that scenario.


Craig

stam
Posts: 2635
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: draggable image

Post by stam » Tue Feb 22, 2022 9:10 am

Would this not be better delivered as a behaviour (I.e.assign behaviour to the clone) rather than a card script?
Best of both options above?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: draggable image

Post by dunbarx » Tue Feb 22, 2022 3:07 pm

Stam.

Why assign a behavior to the clone? The issue, I believe, is a "mouseDown" handler on the card or not. Whether a few card script handlers along with use of "the target" and "last control" are a better method than having each control have its own handlers.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: draggable image

Post by jacque » Tue Feb 22, 2022 6:43 pm

I haven't looked at any of the download stacks so I may be repeating the something that was already done.

I believe LC's tool palette does this by creating a small new stack on the fly, copying the icon to it, and moving the stack to the target card by following the mouseloc. In this case, since there are no icons, copy the control itself. When the mouse is released, create (or copy in this case) the control to the target card and delete the temporary stack.

I'm guessing LC uses either a handler in one of the many script libraries it has, or assigns a behavior to the temporary stack.

Apologies if this was already suggested.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: draggable image

Post by marksmithhfx » Tue Feb 22, 2022 7:03 pm

stam wrote:
Tue Feb 22, 2022 9:10 am
Would this not be better delivered as a behaviour (I.e.assign behaviour to the clone) rather than a card script?
Best of both options above?
I tried this and while it does, from a design perspective, tidy things up (all the code is now in one place) there is a side effect in that the cloned object retains the link to the behavior (and thus can't be used for its intended purpose. Clicking on it just causes it to try and clone itself). I did try removing the behavior (in the msg box "set the behavior of myClonedObj to empty" works fine) but it doesn't work when executed from the behavior script itself (even when the reference is to a new object, and not the original where the behavior is attached). I hope that is not too confusing. Basically you want to clone an object (that is controlled by a behavior) and remove the behavior link in the clone. Is that possible?
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: draggable image

Post by marksmithhfx » Tue Feb 22, 2022 7:06 pm

jacque wrote:
Tue Feb 22, 2022 6:43 pm
I haven't looked at any of the download stacks so I may be repeating the something that was already done.

I believe LC's tool palette does this by creating a small new stack on the fly, copying the icon to it, and moving the stack to the target card by following the mouseloc. In this case, since there are no icons, copy the control itself. When the mouse is released, create (or copy in this case) the control to the target card and delete the temporary stack.

I'm guessing LC uses either a handler in one of the many script libraries it has, or assigns a behavior to the temporary stack.

Apologies if this was already suggested.
I'm not sure if that would work but it's worth a try. I don't have any more time this week, but maybe next.

Thanks
Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

stam
Posts: 2635
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: draggable image

Post by stam » Tue Feb 22, 2022 7:07 pm

you can assign the behaviour in script, so i'd imagine yes... i think?

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: draggable image

Post by marksmithhfx » Tue Feb 22, 2022 10:57 pm

stam wrote:
Tue Feb 22, 2022 7:07 pm
you can assign the behaviour in script, so i'd imagine yes... i think?
Well, combining some of the suggestions made here I put the "clone me" handler in a behavior script and attached that to all of the objects I want to be clone-able (I guess this is the same as setting a flag on an object that indicates "clone-able = true"). It means you don't have to do any testing for that in the script; if the script is executing you can presume it is for an object you want to be clone-able.

So now there are no handlers in the controls. One behavior script controls all of them and one mouseMove handler in the card script handles the drag. The drag is in the card script for two reasons (1) it uses a "focus on nothing" statement to clear any selections on the card after the drag ends and (2) it removes the behavior script (link) from the clone when the drag is completed. Neither of these could be done from the behaviour script itself (I think focus on nothing only works at the card script level).

I've taken this about as far as I need to at this point, but may look into Jacque's suggestion for dragging across stacks at a later date. It looks interesting and worth a try. If anyone else feels like extending this example in that direction I would be delighted.

And thanks once again. We accomplished what I originally set out to do and I am very pleased about that.

Mark
Attachments
Drag and Drop 5.livecode.zip
(2.07 KiB) Downloaded 77 times
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: draggable image

Post by dunbarx » Tue Feb 22, 2022 11:12 pm

Keep plugging away. There is a lot to digest here. Just so you know, my most recent post drags the clone to other stacks.

Craig

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: draggable image

Post by marksmithhfx » Wed Feb 23, 2022 10:39 am

dunbarx wrote:
Tue Feb 22, 2022 11:12 pm
Keep plugging away. There is a lot to digest here. Just so you know, my most recent post drags the clone to other stacks.
Thanks Craig. I'll give it a look (along with Jacques and Peters suggestions) when I get back to it.

Cheer,
Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: draggable image

Post by dunbarx » Wed Feb 23, 2022 2:56 pm

OK, let me know. In the posted file, there are two stacks. One may drag a clone of a control on the leftmost stack onto the rightmost stack by clicking on that control and, with the mouse down, dragging it to the other stack window.

I probably should have included those instructions. I will do so now.

Craig

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”