Пример #1
0
        private void DrawNodes()
        {
            Event e = Event.current;

            if (e.type == EventType.Repaint)
            {
                portConnectionPoints.Clear();
                nodeWidths.Clear();
            }

            //Selected node is hashed before and after node GUI to detect changes
            int nodeHash = 0;

            System.Reflection.MethodInfo onValidate = null;
            if (selectedNode != null)
            {
                onValidate = selectedNode.GetType().GetMethod("OnValidate");
                if (onValidate != null)
                {
                    nodeHash = selectedNode.GetHashCode();
                }
            }

            BeginZoomed(position, zoom);

            Vector2 mousePos = Event.current.mousePosition;

            if (e.type != EventType.Layout)
            {
                hoveredNode = null;
                hoveredPort = null;
            }

            //Save guiColor so we can revert it
            Color guiColor = GUI.color;

            for (int n = 0; n < graph.nodes.Count; n++)
            {
                while (graph.nodes[n] == null)
                {
                    graph.nodes.RemoveAt(n);
                }
                if (n >= graph.nodes.Count)
                {
                    return;
                }
                Node node = graph.nodes[n];

                NodeEditor nodeEditor = NodeEditor.GetEditor(node);
                NodeEditor.portPositions = new Dictionary <NodePort, Vector2>();

                //Get node position
                Vector2 nodePos = GridToWindowPositionNoClipped(node.position);

                GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000)));

                GUIStyle style = NodeEditorResources.styles.nodeBody;
                GUI.color = nodeEditor.GetTint();
                GUILayout.BeginVertical(new GUIStyle(style));
                GUI.color = guiColor;
                EditorGUI.BeginChangeCheck();

                //Draw node contents
                nodeEditor.OnNodeGUI();

                //Apply
                nodeEditor.serializedObject.ApplyModifiedProperties();

                //If user changed a value, notify other scripts through onUpdateNode
                if (EditorGUI.EndChangeCheck())
                {
                    if (NodeEditor.onUpdateNode != null)
                    {
                        NodeEditor.onUpdateNode(node);
                    }
                }

                if (e.type == EventType.Repaint)
                {
                    nodeWidths.Add(node, nodeEditor.GetWidth());

                    foreach (var kvp in NodeEditor.portPositions)
                    {
                        Vector2 portHandlePos = kvp.Value;
                        portHandlePos += node.position;
                        Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
                        portConnectionPoints.Add(kvp.Key, rect);
                    }
                }

                GUILayout.EndVertical();

                if (e.type != EventType.Layout)
                {
                    //Check if we are hovering this node
                    Vector2 nodeSize   = GUILayoutUtility.GetLastRect().size;
                    Rect    windowRect = new Rect(nodePos, nodeSize);
                    if (windowRect.Contains(mousePos))
                    {
                        hoveredNode = node;
                    }

                    //Check if we are hovering any of this nodes ports
                    //Check input ports
                    foreach (NodePort input in node.Inputs)
                    {
                        //Check if port rect is available
                        if (!portConnectionPoints.ContainsKey(input))
                        {
                            continue;
                        }
                        Rect r = GridToWindowRect(portConnectionPoints[input]);
                        if (r.Contains(mousePos))
                        {
                            hoveredPort = input;
                        }
                    }
                    //Check all output ports
                    foreach (NodePort output in node.Outputs)
                    {
                        //Check if port rect is available
                        if (!portConnectionPoints.ContainsKey(output))
                        {
                            continue;
                        }
                        Rect r = GridToWindowRect(portConnectionPoints[output]);
                        if (r.Contains(mousePos))
                        {
                            hoveredPort = output;
                        }
                    }
                }

                GUILayout.EndArea();
            }

            EndZoomed(position, zoom);

            //If a change in hash is detected in the selected node, call OnValidate method.
            //This is done through reflection because OnValidate is only relevant in editor,
            //and thus, the code should not be included in build.
            if (selectedNode != null)
            {
                if (onValidate != null && nodeHash != selectedNode.GetHashCode())
                {
                    onValidate.Invoke(selectedNode, null);
                }
            }
        }
Пример #2
0
        private void DrawNodes()
        {
            Event e = Event.current;

            if (e.type == EventType.Layout)
            {
                selectionCache = new List <UnityEngine.Object>(Selection.objects);
            }
            if (e.type == EventType.Repaint)
            {
                portConnectionPoints.Clear();
                nodeWidths.Clear();
            }

            //Active node is hashed before and after node GUI to detect changes
            int nodeHash = 0;

            System.Reflection.MethodInfo onValidate = null;
            if (Selection.activeObject != null && Selection.activeObject is Siccity.XNode.Node)
            {
                onValidate = Selection.activeObject.GetType().GetMethod("OnValidate");
                if (onValidate != null)
                {
                    nodeHash = Selection.activeObject.GetHashCode();
                }
            }

            BeginZoomed(position, zoom);

            Vector2 mousePos = Event.current.mousePosition;

            if (e.type != EventType.Layout)
            {
                hoveredNode = null;
                hoveredPort = null;
            }

            List <UnityEngine.Object> preSelection = preBoxSelection != null ? new List <UnityEngine.Object>(preBoxSelection) : new List <UnityEngine.Object>();

            //Save guiColor so we can revert it
            Color guiColor = GUI.color;

            for (int n = 0; n < graph.nodes.Count; n++)
            {
                // Skip null nodes. The user could be in the process of renaming scripts, so removing them at this point is not advisable.
                if (graph.nodes[n] == null)
                {
                    continue;
                }
                if (n >= graph.nodes.Count)
                {
                    return;
                }
                Siccity.XNode.Node node = graph.nodes[n];

                NodeEditor nodeEditor = NodeEditor.GetEditor(node);
                NodeEditor.portPositions = new Dictionary <Siccity.XNode.NodePort, Vector2>();

                //Get node position
                Vector2 nodePos = GridToWindowPositionNoClipped(node.position);

                GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000)));

                bool selected = selectionCache.Contains(graph.nodes[n]);

                if (selected)
                {
                    GUIStyle style          = new GUIStyle(NodeEditorResources.styles.nodeBody);
                    GUIStyle highlightStyle = new GUIStyle(NodeEditorResources.styles.nodeHighlight);
                    highlightStyle.padding = style.padding;
                    style.padding          = new RectOffset();
                    GUI.color = nodeEditor.GetTint();
                    GUILayout.BeginVertical(new GUIStyle(style));
                    GUI.color = NodeEditorPreferences.GetSettings().highlightColor;
                    GUILayout.BeginVertical(new GUIStyle(highlightStyle));
                }
                else
                {
                    GUIStyle style = NodeEditorResources.styles.nodeBody;
                    GUI.color = nodeEditor.GetTint();
                    GUILayout.BeginVertical(new GUIStyle(style));
                }

                GUI.color = guiColor;
                EditorGUI.BeginChangeCheck();

                //Draw node contents
                nodeEditor.OnNodeGUI();

                //Apply
                nodeEditor.serializedObject.ApplyModifiedProperties();

                //If user changed a value, notify other scripts through onUpdateNode
                if (EditorGUI.EndChangeCheck())
                {
                    if (NodeEditor.onUpdateNode != null)
                    {
                        NodeEditor.onUpdateNode(node);
                    }
                }

                if (e.type == EventType.Repaint)
                {
                    nodeWidths.Add(node, nodeEditor.GetWidth());

                    foreach (var kvp in NodeEditor.portPositions)
                    {
                        Vector2 portHandlePos = kvp.Value;
                        portHandlePos += node.position;
                        Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
                        portConnectionPoints.Add(kvp.Key, rect);
                    }
                }

                GUILayout.EndVertical();
                if (selected)
                {
                    GUILayout.EndVertical();
                }

                if (e.type != EventType.Layout)
                {
                    //Check if we are hovering this node
                    Vector2 nodeSize   = GUILayoutUtility.GetLastRect().size;
                    Rect    windowRect = new Rect(nodePos, nodeSize);
                    if (windowRect.Contains(mousePos))
                    {
                        hoveredNode = node;
                    }

                    //If dragging a selection box, add nodes inside to selection
                    if (currentActivity == NodeActivity.DragGrid)
                    {
                        Vector2 startPos = GridToWindowPositionNoClipped(dragBoxStart);
                        Vector2 size     = mousePos - startPos;
                        if (size.x < 0)
                        {
                            startPos.x += size.x; size.x = Mathf.Abs(size.x);
                        }
                        if (size.y < 0)
                        {
                            startPos.y += size.y; size.y = Mathf.Abs(size.y);
                        }
                        Rect r = new Rect(startPos, size);
                        if (windowRect.Overlaps(r))
                        {
                            preSelection.Add(node);
                        }
                    }

                    //Check if we are hovering any of this nodes ports
                    //Check input ports
                    foreach (Siccity.XNode.NodePort input in node.Inputs)
                    {
                        //Check if port rect is available
                        if (!portConnectionPoints.ContainsKey(input))
                        {
                            continue;
                        }
                        Rect r = GridToWindowRect(portConnectionPoints[input]);
                        if (r.Contains(mousePos))
                        {
                            hoveredPort = input;
                        }
                    }
                    //Check all output ports
                    foreach (Siccity.XNode.NodePort output in node.Outputs)
                    {
                        //Check if port rect is available
                        if (!portConnectionPoints.ContainsKey(output))
                        {
                            continue;
                        }
                        Rect r = GridToWindowRect(portConnectionPoints[output]);
                        if (r.Contains(mousePos))
                        {
                            hoveredPort = output;
                        }
                    }
                }

                GUILayout.EndArea();
            }

            if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid)
            {
                Selection.objects = preSelection.ToArray();
            }
            EndZoomed(position, zoom);

            //If a change in hash is detected in the selected node, call OnValidate method.
            //This is done through reflection because OnValidate is only relevant in editor,
            //and thus, the code should not be included in build.
            if (nodeHash != 0)
            {
                if (onValidate != null && nodeHash != Selection.activeObject.GetHashCode())
                {
                    onValidate.Invoke(Selection.activeObject, null);
                }
            }
        }
Пример #3
0
        //private EntityActionNode eaNode;
        //private EntityConditionNode ecNode;
        void RenderDialogNodeGUI(NodeEditor nodeEditor, XNode.Node node)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.BeginVertical();
            dn = node as DialogNode;
            if (dn != null)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                if (dn.showDialogText = EditorGUILayout.Foldout(dn.showDialogText, "Dialog"))
                {
                    EditorGUILayout.LabelField("Dialog");
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    dn.dialog.text = EditorGUILayout.TextArea(dn.dialog.text, EditorStyles.wordWrappedLabel, GUILayout.MinHeight(60), GUILayout.MaxWidth(180));
                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndVertical();
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                if (dn.Options = EditorGUILayout.Foldout(dn.Options, "Options"))
                {
                    EditorGUILayout.LabelField("Talk ID");
                    dn.dialog.TalkID = EditorGUILayout.IntSlider(dn.dialog.TalkID, 0, 6);
                    dn.dialog.clip   = (AudioClip)EditorGUILayout.ObjectField("Audio", dn.dialog.clip, typeof(AudioClip), false);
                    if (dn.dialog.hasEvent = EditorGUILayout.Toggle("Event", dn.dialog.hasEvent))
                    {
                        dn.dialog.eventID = EditorGUILayout.IntField("Event ID", dn.dialog.eventID);
                    }
                    dn.dialog.isEntry = EditorGUILayout.Toggle("Is Entry", dn.dialog.isEntry);
                }
                EditorGUILayout.EndVertical();
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                if (dn.showDialogCondition = EditorGUILayout.Foldout(dn.showDialogCondition, "Conditions"))
                {
                    if (dn.dialog.conditions == null)
                    {
                        dn.dialog.conditions = new DialogObject.DialogCondition();
                    }
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.Space();
                    EditorGUILayout.BeginVertical();
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    dn.dialog.conditions.isOneTime = EditorGUILayout.Toggle("One Shot", dn.dialog.conditions.isOneTime);
                    if (dn.dialog.conditions.isOneTime && string.IsNullOrEmpty(dn.dialog.conditions.oneTimeID))
                    {
                        dn.dialog.conditions.oneTimeID = ToolKit.GetUniqueID();
                    }
                    EditorGUILayout.EndVertical();
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    if (dn.showDialogVar = EditorGUILayout.Foldout(dn.showDialogVar, "Global Var"))
                    {
                        EditorGUILayout.LabelField(dn.dialog.conditions.varID);
                        if (GUILayout.Button("Set Var"))
                        {
                            ObjectPicker.GetObject(ObjectPicker.ObjectType.Variable, dn.SetDialogVar);
                        }
                        dn.dialog.conditions.varState = EditorGUILayout.Toggle("State", dn.dialog.conditions.varState);
                    }
                    EditorGUILayout.EndVertical();

                    EditorGUILayout.EndVertical();
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.EndVertical();

                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
                if (GUILayout.Button("Add", EditorStyles.toolbarButton))
                {
                    dn.AddResponse();
                }
                if (GUILayout.Button("Remove", EditorStyles.toolbarButton))
                {
                    dn.RemoveResponse();
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                {
                    int i = 0;
                    if (dn.selectedResponse > dn.dialog.responses.Length - 1)
                    {
                        dn.selectedResponse = -1;
                    }
                    foreach (DialogObject.DialogResponse response in dn.dialog.responses)
                    {
                        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                        EditorGUILayout.BeginHorizontal();
                        string s = "";
                        if (!string.IsNullOrEmpty(response.response))
                        {
                            s = TruncateLongString(response.response, 16);
                        }
                        else
                        {
                            s = "Response " + i.ToString();
                        }
                        if (GUILayout.Button(s, EditorStyles.foldout))
                        {
                            if (dn.selectedResponse == i)
                            {
                                dn.selectedResponse = -1;
                            }
                            else
                            {
                                dn.selectedResponse = i;
                            }
                        }
                        nodeEditor.OnNodeGUI(i);
                        EditorGUILayout.EndHorizontal();
                        if (dn.selectedResponse == i)
                        {
                            if (dn.dialog.responses != null && dn.dialog.responses.Length > 0 && dn.selectedResponse >= 0)
                            {
                                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                                dn.dialog.responses[dn.selectedResponse].response = EditorGUILayout.TextArea(dn.dialog.responses[dn.selectedResponse].response, EditorStyles.wordWrappedLabel, GUILayout.MinHeight(60), GUILayout.MaxWidth(180));
                                EditorGUILayout.EndVertical();
                                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                                dn.responseOptions = EditorGUILayout.Foldout(dn.responseOptions, "Options");
                                if (dn.responseOptions)
                                {
                                    dn.conditionOptions = false;
                                    dn.dialog.responses[dn.selectedResponse].eventID = EditorGUILayout.IntField("Event", dn.dialog.responses[dn.selectedResponse].eventID);
                                }
                                EditorGUILayout.EndVertical();
                                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                                dn.conditionOptions = EditorGUILayout.Foldout(dn.conditionOptions, "Conditions");
                                if (dn.conditionOptions)
                                {
                                    dn.responseOptions = false;
                                    if (dn.dialog.responses[dn.selectedResponse].conditions == null)
                                    {
                                        dn.dialog.responses[dn.selectedResponse].conditions = new DialogObject.DialogCondition();
                                    }
                                    EditorGUILayout.BeginHorizontal();
                                    EditorGUILayout.Space();
                                    EditorGUILayout.BeginVertical();
                                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                                    dn.dialog.responses[dn.selectedResponse].conditions.isOneTime = EditorGUILayout.Toggle("One Shot", dn.dialog.responses[dn.selectedResponse].conditions.isOneTime);
                                    if (dn.dialog.responses[dn.selectedResponse].conditions.isOneTime && string.IsNullOrEmpty(dn.dialog.responses[dn.selectedResponse].conditions.oneTimeID))
                                    {
                                        dn.dialog.responses[dn.selectedResponse].conditions.oneTimeID = ToolKit.GetUniqueID();
                                    }
                                    EditorGUILayout.EndVertical();
                                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                                    if (dn.showVar = EditorGUILayout.Foldout(dn.showVar, "Global Var"))
                                    {
                                        EditorGUILayout.LabelField(dn.dialog.responses[dn.selectedResponse].conditions.varID);
                                        if (GUILayout.Button("Set Var"))
                                        {
                                            //ObjectPicker.GetObject(ObjectPicker.ObjectType.Variable, dn.SetVar);
                                            Debug.Log("Get/set vars");
                                        }
                                        dn.dialog.responses[dn.selectedResponse].conditions.varState = EditorGUILayout.Toggle("State", dn.dialog.responses[dn.selectedResponse].conditions.varState);
                                    }
                                    EditorGUILayout.EndVertical();
                                    EditorGUILayout.EndVertical();
                                    EditorGUILayout.EndHorizontal();
                                }
                                EditorGUILayout.EndVertical();
                            }
                        }
                        i++;
                        EditorGUILayout.EndVertical();
                    }
                }
                EditorGUILayout.EndVertical();
                EditorGUILayout.EndVertical();
                EditorUtility.SetDirty(dn);
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
        }