summaryrefslogtreecommitdiffstats
path: root/Scripts/Fold/Editor/FoldWindow.cs
blob: 3ba556c5fe17e31dc221d6d537187d4b3c230e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class FoldWindow : EditorWindow
{
    const string GraphPath = "Assets/FoldGraph.asset";

    FoldGraphView graphView;
    FoldGraph graphAsset;

    [MenuItem("Tools/yum_food/Fold")]
    public static void Open()
    {
        var window = GetWindow<FoldWindow>();
        window.titleContent = new GUIContent("Fold");
        window.Show();
    }

    void OnEnable()
    {
        graphAsset = LoadOrCreateGraph();
        ConstructGraphView();
        Undo.undoRedoPerformed += OnUndoRedo;
    }

    void OnDisable()
    {
        if (graphView != null)
        {
            rootVisualElement.Remove(graphView);
            graphView.Dispose();
            graphView = null;
        }

        AssetDatabase.SaveAssets();
        Undo.undoRedoPerformed -= OnUndoRedo;
    }

    void OnFocus()
    {
        // If we lost the graph view for any reason, rebuild it. Otherwise, reload to match the asset on disk.
        if (graphView == null)
        {
            graphAsset = LoadOrCreateGraph();
            ConstructGraphView();
        }
        else
        {
            graphView.Reload();
        }
    }

    FoldGraph LoadOrCreateGraph()
    {
        var asset = AssetDatabase.LoadAssetAtPath<FoldGraph>(GraphPath);
        if (asset == null)
        {
            asset = CreateInstance<FoldGraph>();
            AssetDatabase.CreateAsset(asset, GraphPath);
            AssetDatabase.SaveAssets();
        }

        return asset;
    }

    void ConstructGraphView()
    {
        graphView = new FoldGraphView(this, graphAsset)
        {
            name = "Fold Graph"
        };

        graphView.style.flexGrow = 1f;
        graphView.style.width = Length.Percent(100);
        graphView.style.height = Length.Percent(100);
        rootVisualElement.Add(graphView);
    }

    void OnUndoRedo()
    {
        graphView?.Reload();
    }

    public void ShowNotification(string message)
    {
        if (string.IsNullOrEmpty(message))
            return;

        ShowNotification(new GUIContent(message));
    }
}