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.
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