I’ve added the following manipulators to the GraphView
:
this.AddManipulator(new ContentZoomer());
this.AddManipulator(new SelectionDragger());
this.AddManipulator(new ContentDragger());
this.AddManipulator(new RectangleSelector());
Then I’ve added a manipulator to add a Contextual Menu to add a node, like so:
this.AddManipulator(CreateContextualMenu("Title"));
private ContextualMenuManipulator CreateContextualMenu(string contextualMenuText)
{
return new ContextualMenuManipulator(menuEvent =>
menuEvent.menu.AppendAction(contextualMenuText, actionEvent =>
AddElement(CreateNode(actionEvent.eventInfo.mousePosition)), DropdownMenuAction.AlwaysEnabled));
}
The CreateNode
method receives the mouse position and inserts the node at the correct place, until I drag the graph view (due to the content dragger) and try to create a new node.
I’ve inserted a log to see the position and it seems that the actionEvent.eventInfo.mousePosition
(and others) only goes up to the current window size, and therefore instead of adding on the current position it creates in the closest to the window size (which in terms of graph position, it might be in a position that isn’t showing after I drag).
The contextual menu is where I’ve clicked to add a node, and the node on the left is where it was added.
I’ve tried adding a MouseMove
/Up
/DownEvent
to both the GraphView
and GridBackground
but it still does the same.
I’ve also tried using Mouse.current.position.ReadValue()
but the same happens.
This works like a charm, but I'll add that you should cache the localMousePosition of the event before modifying the event, otherwise in some versions of graphview it goes back to (0,0): Vector2 localMousePos = evt.localMousePosition; evt.menu.AppendAction(...) //And other possible manipulations... Vector2 actualGraphPosition = viewTransform.matrix.inverse.MultiplyPoint(localMousePos );
– Guillemot