summaryrefslogtreecommitdiff
path: root/Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/SceneGraphEditor.cs
blob: 9fb1c67382eca3bd9c73e97a7312165b3c5da99d (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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using XNode;

namespace XNodeEditor {
    [CustomEditor(typeof(SceneGraph), true)]
    public class SceneGraphEditor : Editor {
        private SceneGraph sceneGraph;
        private bool removeSafely;
        private Type graphType;

        public override void OnInspectorGUI() {
            if (sceneGraph.graph == null) {
                if (GUILayout.Button("New graph", GUILayout.Height(40))) {
                    if (graphType == null) {
                        Type[] graphTypes = NodeEditorReflection.GetDerivedTypes(typeof(NodeGraph));
                        GenericMenu menu = new GenericMenu();
                        for (int i = 0; i < graphTypes.Length; i++) {
                            Type graphType = graphTypes[i];
                            menu.AddItem(new GUIContent(graphType.Name), false, () => CreateGraph(graphType));
                        }
                        menu.ShowAsContext();
                    } else {
                        CreateGraph(graphType);
                    }
                }
            } else {
                if (GUILayout.Button("Open graph", GUILayout.Height(40))) {
                    NodeEditorWindow.Open(sceneGraph.graph);
                }
                if (removeSafely) {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Really remove graph?");
                    GUI.color = new Color(1, 0.8f, 0.8f);
                    if (GUILayout.Button("Remove")) {
                        removeSafely = false;
                        Undo.RecordObject(sceneGraph, "Removed graph");
                        sceneGraph.graph = null;
                    }
                    GUI.color = Color.white;
                    if (GUILayout.Button("Cancel")) {
                        removeSafely = false;
                    }
                    GUILayout.EndHorizontal();
                } else {
                    GUI.color = new Color(1, 0.8f, 0.8f);
                    if (GUILayout.Button("Remove graph")) {
                        removeSafely = true;
                    }
                    GUI.color = Color.white;
                }
            }
        }

        private void OnEnable() {
            sceneGraph = target as SceneGraph;
            Type sceneGraphType = sceneGraph.GetType();
            if (sceneGraphType == typeof(SceneGraph)) {
                graphType = null;
            } else {
                Type baseType = sceneGraphType.BaseType;
                if (baseType.IsGenericType) {
                    graphType = sceneGraphType = baseType.GetGenericArguments() [0];
                }
            }
        }

        public void CreateGraph(Type type) {
            Undo.RecordObject(sceneGraph, "Create graph");
            sceneGraph.graph = ScriptableObject.CreateInstance(type) as NodeGraph;
            sceneGraph.graph.name = sceneGraph.name + "-graph";
        }
    }
}