How can I detect changes to GraphView, Nodes and Edges?

Hey!

I’m building a dialogue editing system using GraphView, and I’m trying to make it save any changes automatically. To do this I need to detect a bunch of situations, and save in response to them:

Situations I’ve been able to catch:

  • A node has been added (I’m explicitly adding it in code, so that’s easy)

  • A node has been deleted (Using DetachFromPanelEvent)

Situations I’m unable to catch:

  • A node has been moved (dragged).

  • An Edge has been added.

  • An Edge has been deleted.

Any tips or info on how I could achieve detecting those is greatly appreciated. I really don’t want to downgrade to a manual saving system as that risks having a lot of work going unsaved by mistake.

You’ve probably figured this out already, but for the sake of other people searching for answers on this forum: The key to all this is the GraphView.graphViewChanged callback. You want to do something like:

    MyGraphView() {
        graphViewChanged = OnGraphChange;
    }

    private GraphViewChange OnGraphChange(GraphViewChange change)
    {
        if (change.edgesToCreate != null) {
            foreach (Edge edge in change.edgesToCreate)
            {
                ...
            }
        }

        if (change.elementsToRemove != null)
        {
            foreach (GraphElement e in change.elementsToRemove)
            {
                if (e.GetType() == typeof(Edge) {
                    ...
                }
            }
        }

        if (change.movedElements != null)
        {
            foreach (GraphElement e in change.movedElements)
            {
                if (e.GetType() == typeof(Node))
                {
                    ...
                }
            }
        }

        return change;
    }