Auto connecting links on drag'n'drop

Hey all,

If the answer is totally obvious then sorry to waste everyone’s time. But, if not…

In our application we are fairly certain that after a user drops a node onto the canvas they are going to want to link it up to one of the other nodes already on the canvas. We have an auto-linking capability already, but I want something a little more useful.

When the user drags a node out from the toolbox and enters the canvas, I’d like the user to be able to see what links would connect if the node were to be dropped in that location… maybe even using some configurable value (a sort of port gravity) so that as they drag the image of the node closer to some existing node and a connection is possible then, zing! and the two nodes seem connected… if the user doesn’t let go of the mouse button from that initial drag and drags out of this radius the connection disappears indicating that no auto connection would happen.

Since the node as it’s being dragged is not a part of the canvas I didn’t see how this could work… so maybe I’d have to temporarily add it to the canvas and then make some sort of custom tool…? Help!

Ah, you have already identified one critical feature – that the node act as if it were already in the document, and that the easiest way to do that is go ahead and actually add it.
That feature is implemented by setting GoView.ExternalDragDropsOnEnter and GoView.DragsRealtime to true.
The other implementation feature you need you can implement using an override of GoToolDragging.DoDragging. Look for all the nearby nodes (GoDocument.PickObjectsInRectangle) and call GoPort.IsValidLink on each pair of ports, ones in the nodes being dragged (in the Selection) and ones in the nodes that are nearby. When IsValidLink is true, create a link, hook it up between the two ports, and add the link to the view, and remember it. When the tool stops, or in ClearDragSelection, remove all those temporary links.
Caveat: I haven’t actually tried this…

Argh, yeah, that’s what I figured. ( =

We are using GoDiagram as the view in an MVC… the controller is wired up to events in Go to effect changes in the model (y’know, OnObjectGotSelection, etc)… Which probably means that we’d have to temporarily suspend the eventing while doing this drag so as not to mung our model up. Hmm… I’m still better off then when I wasn’t sure it would work at all, though.

Thanks! You are, as always, super helpful!