コード例 #1
0
    /// <summary>
    /// Defines behavior for selecting right-click context items. Modify this function if you wish to create custom nodes and context menu behaviors.
    /// </summary>
    public override void ContextCallback(object obj)
    {
        switch (obj.ToString())
        {
        case "dialogueNode":
            numberOfStates++;
            DialogueNode.Create(new Rect(mousePos.x, mousePos.y, 350, 350), numberOfStates);
            break;

        case "dialogueChoice":
            numberOfChoiceStates++;
            DialogueChoiceNode.Create(new Rect(mousePos.x, mousePos.y, 100, 50), numberOfChoiceStates);
            break;

        case "deleteNode":
            Node node = NodeAtPosition(mousePos);
            if (node != null)
            {
                if (node.GetType() == typeof(DialogueNode))
                {
                    numberOfStates--;
                }
                else if (node.GetType() == typeof(DialogueChoiceNode))
                {
                    numberOfChoiceStates--;
                }

                // Find all nodes with higher state values than the deleted node and subtract 1 from their state.
                foreach (Node successorNode in nodeCanvas.nodes)
                {
                    // Ignore choice states
                    if (successorNode.state != -2 && successorNode.state > node.state)
                    {
                        successorNode.state--;
                        successorNode.name = "State " + successorNode.state;
                    }
                }

                nodeCanvas.nodes.Remove(node);

                if (node.GetType() == typeof(DialogueChoiceNode))
                {
                    _nodeCanvas.choiceNodes.Remove(node);
                    _nodeCanvas.choiceNodeNames.Remove(node.name);
                }

                // Iterate over all the other nodes and update start/final states
                foreach (Node editorNode in Node_Editor.editor.nodeCanvas.nodes)
                {
                    Node_Editor.editor.RecalculateFrom(editorNode);
                }

                node.OnDelete();
            }

            break;
        }
    }