Click and/or drag ?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Click and/or drag ?

Post by Klaus » Sat Mar 02, 2024 8:00 pm

And here a slightly enhanced version, where you can drag and drop anything onto anything.
Except the behavior button and the info field at the bottom.
Attachments
drip_drop4.livecode.zip
(5.04 KiB) Downloaded 33 times

Zax
Posts: 474
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Click and/or drag ?

Post by Zax » Sun Mar 03, 2024 8:17 am

Thank you very much Klaus :D :D :D

Now I understand how to use dragEnter and dragLeave messages, in order to show visual effects during drag process:

Code: Select all

on mouseDown 
   export snapshot from rect (the rect of me) of this cd to img "dragimage"
   set the dragdata["text"] to the name of me
   set the dragimage to 1013
end mouseDown

on dragenter
   if dragdata["text"] <> the name of me then
      set the dragaction to "copy"
      set the blendLevel of me to 0
      set the showName of me to true
   end if
end dragenter

on dragLeave
   set the blendLevel of me to 50
   set the showName of me to false
end dragLeave

on dragdrop
   put ("Control:" & CR & the dragdata["text"] & CR & "was dropped onto:" & CR & the name of me) into fld "where"
end dragdrop

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Click and/or drag ?

Post by Klaus » Sun Mar 03, 2024 10:28 am

richmond62 wrote:
Sat Mar 02, 2024 5:47 pm
Indeed that works. 8)

The disadvantage with your stack is that the end-user gets visual feedback when s/he performs a drag, but the "drag image" does not remain on the target.
Well, I wanted to leave a tiny bit of work up to you! 8)

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

Re: Click and/or drag ?

Post by richmond62 » Sun Mar 03, 2024 11:39 am

Maybe we should leave a bit of work up to the OP.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Click and/or drag ?

Post by Klaus » Sun Mar 03, 2024 11:43 am

As I can see from his script above, he already did it. :)

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

Re: Click and/or drag ?

Post by stam » Sun Mar 03, 2024 12:51 pm

Zax wrote:
Sat Mar 02, 2024 1:40 pm
Sorry if I wasn't not accurate. I would like the visual effect to be shown before the mouse is released, in order to show that the dragged element can be dropped onto the target (here, the pale blue circle).

This could be done with a loop during grab but I'm afraid it would make the movement jerky - I currently have 96 possible targets on a card.
Meant to answer a lot sooner but work/real life getting in the way.

I can see you now have a solution. I probably misunderstood your original intent and can see that you want an indication when your dragged object intersects the target.
But what I understood was that you wanted all possible targets to glow or some such when you start the drag, in which case a publish/subscribe model would have seemed more appropriate.

This isn't built into LC but should be easy to implement. The broadcasting event doesn't know who the subscribers are and the subscribed object don't care who the broadcaster is, they just call a pre-specified handler of their own when the message is received, bypassing built in object hierarchy.

In your use-case then, all potential targets for dragging to would immediately highlight with a graphic effect as soon as you start dragging.
On releasing a further message would broadcast to clear the highlights from all subscribers.

I've been meaning to create my own version of a publish-subscribe model (I had purchased Andre Garzia's book where he shared a stack for this a few years ago but for the life of me I can't find this now and thought it would be fun to try and reconstruct from memory and logic).
Mind you I have no idea how well this would work with 96 subscribers ;)

Zax
Posts: 474
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Click and/or drag ?

Post by Zax » Sun Mar 03, 2024 1:37 pm

richmond62 wrote:
Sun Mar 03, 2024 11:39 am
Maybe we should leave a bit of work up to the OP.
The OP has enough work right now :? ... trying to build a MacOS apps and files launcher! I'll show the result, if it not too crapy.

@ stam: a publish/subscribe model could be interesting, but at this time it seems too complex for me as I don't know enough about this kind of stuff.

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

Re: Click and/or drag ?

Post by stam » Sun Mar 03, 2024 4:14 pm

Zax wrote:
Sun Mar 03, 2024 1:37 pm
@ stam: a publish/subscribe model could be interesting, but at this time it seems too complex for me as I don't know enough about this kind of stuff.
Actually you wouldn’t believe how simple it is…
I thew together a quick stack today and was pleasantly surprised… but dragged into family stuff now, so can’t post something just yet.

I’ll clean it up post with some explanation when I get a chance.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Click and/or drag ?

Post by bn » Sun Mar 03, 2024 9:46 pm

If you use Klaus' stack "drip_drop4" then in the behvior script try

Code: Select all

on mouseDown
   export snapshot from me to img "dragimage" as png
   set the dragdata["text"] to the name of me
   put item 1 of the mouseLoc - the left of me into tHoriz
   put item 2 of the mouseLoc - the top of me into tVert
   set the dragImageOffset to tHoriz, tVert
   set the dragimage to 1013
end mouseDown

It respects the transparencies of Klaus' graphics and also drags the image from where you clicked.

Kind regards
Bernd

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

Re: Click and/or drag ?

Post by stam » Mon Mar 04, 2024 3:50 am

Zax wrote:
Sun Mar 03, 2024 1:37 pm
@ stam: a publish/subscribe model could be interesting, but at this time it seems too complex for me as I don't know enough about this kind of stuff.
As it's actually quite simple, I've tidied it up and attach a short publish/subscribe library along with an example stack that shows how this can be used. This doesn't attempt to do the clever drag/drop actions others mention above, just to hilite good or bad destinations for dragging.

The library is simple, with 4 public commands:

Code: Select all

local sEventsA

command subscribe pEvent, pCallback, pTarget
    if pTarget is empty then put the long id of the target into pTarget
    put empty into sEventsA[pEvent][pTarget][pCallback]
end subscribe

command broadcast pEvent, pData
    repeat for each key tTarget in sEventsA[pEvent]
        repeat for each key tCallback in sEventsA[pEvent][tTarget]
            dispatch tCallback to tTarget with pData
        end repeat
    end repeat
end broadcast

command unsubscribe pEvent, pCallback, pTarget
    if pCallback is empty then
        delete variable sEventsA[pEvent][pTarget]
    else
        repeat for each key tCallback in sEventsA[pEvent][pTarget]
            if tCallback is pCallback then delete variable  sEventsA[pEvent][pTarget][tCallback]
        end repeat
    end if
end unsubscribe

command removeAllSubscriptions
    put empty into sEventsA
end removeAllSubscriptions

on libraryStack
    insert the script of me into back
end libraryStack
This is based loosely on a stack that was bundled with Andre Garzia's LiveCode Advanced App Architecture. I bought this eBook long ago, but can't for the life of me locate the bundled stack now, so this is recreated roughly from memory. Any kudos should go to him, any blame should be directed at me!

The example stack attached assumes this library is in the same folder as itself. At preOpenCard, it loads the library into the backscripts so it's available in any context, and it subscribes all graphics to custom messages dragHasStarted and dragHasEnded, specifying callbacks for both.

There are roundRect and oval graphics on the card that can all be dragged with Jacque's code (set in the behavior button). If you drag an oval, all ovals glow blue (for example to signify a good destination), other graphics glow red ("bad" destination), and vice versa if you drag a roundRect.

This is because when dragging starts, the custom message 'dragHasStarted' is broadcast and affects all subscribers to this message, without the broadcaster having any awareness of the subscribers and vice versa.

pubSub example.jpg

You can remove all subscriptions to remove all glow effects, or (re)subscribe all graphics to the dragHasStarted and dragHasEnded messages to get the glow effects. Brief explanation of concepts and the 4 handlers to use are included in card 2.

While this was mostly an exercise for myself, as I've been meaning to get this clear in my own head for some time, you may also find this useful.

Stam
Attachments
pubSub example.zip
(5.51 KiB) Downloaded 29 times
Last edited by stam on Mon Mar 04, 2024 9:56 am, edited 1 time in total.

Zax
Posts: 474
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Click and/or drag ?

Post by Zax » Mon Mar 04, 2024 8:00 am

Waow, impressive works Stam!
I think this subscribe method could be a major improvement for Beleaguered Castle.

Thank you so share this great idea. :D

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

Re: Click and/or drag ?

Post by stam » Mon Mar 04, 2024 12:25 pm

There is a role for the publish/subscribe model in LiveCode and even though people have occasionally asked about it, I don't think there's been a good post about it on the forums. The library I shared isn't perfect - no error checking etc - but it is quite simple (mainly so I could digest it myself!) and it works.

The essence of this library is that it's just an intermediary - a handler sends it a message (the 'broadcast') and the broker has a list of controls (the 'subscribers') that have provided the name of a an internal handler of theirs to be executed when the library receives the message.

It's not really a broadcast and the subscribers aren't really listening for the broadcast, as may be the case in other programming languages, but the appearance and end-effect is the same.

I'm sure this can be improved on - because I couldn't find anything similar, I've created a GitHub repo for this. If anyone has improvements to add, GitHub provides mechanisms to incorporate successive changes - please do a pull request if you have improvements to add!

https://github.com/stam66/skPubSub

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Click and/or drag ?

Post by bn » Mon Mar 04, 2024 7:01 pm

Thank you Stam,

This is a wonderful example of publish and subscribe.
BTW LC IDE uses a similar mechanism all over the place.

Kind regards
Bernd

SWEdeAndy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 261
Joined: Sat Aug 16, 2008 9:48 am
Location: Stockholm, Sweden
Contact:

Re: Click and/or drag ?

Post by SWEdeAndy » Thu Mar 07, 2024 9:58 pm

Thanks Stam, this is really helpful in understanding the publish/subscribe model!

I too bought Andre Garzia's Advanced App Architecture eBook some years ago, and although I learned a lot from it, I could not really get my head around his publish/subscribe example. I think it was due to the fact that in order to make the example simple enough to include in the book, it became, somewhat paradoxically, so simple that a publish/subscribe model for it seemed like a massive overkill. I couldn't at the time see the benefit of it.

Your example, while still simple, effectively demonstrates the benefit when the number of subscribers is scaled up, and I can finally see the power and elegance of this model. Thanks again! :D
Andreas Bergendal
Independent app and system developer
WhenInSpace: https://wheninspace.se

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”