Ejemplo n.º 1
0
    void OnGUI()
    {
        GUILayout.BeginVertical();
        scrollPosTree = GUILayout.BeginScrollView(scrollPosTree, GUILayout.Height(currentScrollViewHeight));

        EditorGUIUtility.hierarchyMode = true;
        EditorGUI.indentLevel++;

        GUI.SetNextControlName("DummyControl");
        GUI.Button(new Rect(0, 0, 0, 0), "", GUIStyle.none);

        savedTree = (SerializableTree)EditorGUILayout.ObjectField("Dialogue Tree", savedTree, typeof(SerializableTree), false);

        if (savedTree == null || (lastSavedTree != null && savedTree != lastSavedTree))
        {
            tree  = null;
            nodes = null;
        }
        if (savedTree != null)
        {
            if (GUILayout.Button("Save") && tree != null)
            {
                savedTree.ExportTree(tree);
                EditorUtility.SetDirty(savedTree);
                Debug.Log("Saved tree");
            }

            if (GUILayout.Button("Load"))
            {
                lastSavedTree = savedTree;
                tree          = savedTree.ImportTree();
                if (tree == null)
                {
                    tree = DialogueTester.CreateTestTree();
                    Debug.Log("Created new tree");
                }
            }
        }

        if (savedTree != null && tree != null)
        {
            if (nodes == null)
            {
                nodes  = new Dictionary <int, NodeGUI>();
                nextID = 0;
            }
            NodeGUI.RenderNode(this, tree.root);


            NodeGUI gui = GetNodeAtPoint(Event.current.mousePosition);
            if (gui != null)
            {
                switch (Event.current.type)
                {
                case EventType.MouseDown:
                    if (contextMenuShown)
                    {
                        GUI.FocusControl("DummyControl");
                        dataInEditor     = null;
                        contextMenuShown = false;
                        Event.current.Use();
                    }
                    break;

                case EventType.MouseUp:
                    if (Event.current.button == 1)
                    {
                        GenerateContextMenu(gui);
                        Event.current.Use();
                    }
                    break;
                }
            }

            GUILayout.EndScrollView();
            ResizableSplit();
            scrollPosEditor = GUILayout.BeginScrollView(scrollPosEditor, GUILayout.Height(this.position.height - currentScrollViewHeight));

            NodeGUI focused = GetNodeGUI(GUI.GetNameOfFocusedControl());
            if (focused != null && focused.node != null)
            {
                dataInEditor = focused.node.Data;
                if (focused.node.Data == null)
                {
                    Debug.Log("Data is null... :(");
                }
            }
            if (dataInEditor != null)
            {
                SerializedObject   editor = new SerializedObject(this);
                SerializedProperty data   = editor.FindProperty("dataInEditor");
                EditorGUILayout.PropertyField(data);
            }

            GUILayout.EndScrollView();
            GUILayout.EndVertical();
            Repaint();
        }
    }