Click and/or drag ?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Click and/or drag ?
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.
Except the behavior button and the info field at the bottom.
- Attachments
-
- drip_drop4.livecode.zip
- (5.04 KiB) Downloaded 50 times
Re: Click and/or drag ?
Thank you very much Klaus
Now I understand how to use dragEnter and dragLeave messages, in order to show visual effects during drag process:
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
Re: Click and/or drag ?
Well, I wanted to leave a tiny bit of work up to you!richmond62 wrote: ↑Sat Mar 02, 2024 5:47 pmIndeed that works.
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.
-
- Livecode Opensource Backer
- Posts: 9455
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Click and/or drag ?
Maybe we should leave a bit of work up to the OP.
Re: Click and/or drag ?
As I can see from his script above, he already did it.
Re: Click and/or drag ?
Meant to answer a lot sooner but work/real life getting in the way.Zax wrote: ↑Sat Mar 02, 2024 1:40 pmSorry 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.
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
Re: Click and/or drag ?
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.
Re: Click and/or drag ?
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.
-
- VIP Livecode Opensource Backer
- Posts: 4028
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Click and/or drag ?
If you use Klaus' stack "drip_drop4" then in the behvior script try
It respects the transparencies of Klaus' graphics and also drags the image from where you clicked.
Kind regards
Bernd
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
Re: Click and/or drag ?
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
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.
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 41 times
Last edited by stam on Mon Mar 04, 2024 9:56 am, edited 1 time in total.
Re: Click and/or drag ?
Waow, impressive works Stam!
I think this subscribe method could be a major improvement for Beleaguered Castle.
Thank you so share this great idea.
I think this subscribe method could be a major improvement for Beleaguered Castle.
Thank you so share this great idea.
Re: Click and/or drag ?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 4028
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Click and/or drag ?
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
This is a wonderful example of publish and subscribe.
BTW LC IDE uses a similar mechanism all over the place.
Kind regards
Bernd
-
- VIP Livecode Opensource Backer
- Posts: 265
- Joined: Sat Aug 16, 2008 9:48 am
- Location: Stockholm, Sweden
- Contact:
Re: Click and/or drag ?
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!
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!
Andreas Bergendal
Independent app and system developer
Free LC dev tools: https://github.com/wheninspace
WhenInSpace: https://wheninspace.se/en
Independent app and system developer
Free LC dev tools: https://github.com/wheninspace
WhenInSpace: https://wheninspace.se/en