Example #1
0
        public void CopyParamActions(EditableConversationNode editable, ConversationNode node)
        {
            node.ParamActions = new List <SetParamAction>();

            for (int i = 0; i < editable.ParamActions.Count; i++)
            {
                if (editable.ParamActions[i].ParamActionType == EditableSetParamAction.eParamActionType.Int)
                {
                    EditableSetIntParamAction setIntEditable = editable.ParamActions[i] as EditableSetIntParamAction;

                    SetIntParamAction setInt = new SetIntParamAction();
                    setInt.ParameterName = setIntEditable.ParameterName;
                    setInt.Value         = setIntEditable.Value;
                    node.ParamActions.Add(setInt);
                }
                else if (editable.ParamActions[i].ParamActionType == EditableSetParamAction.eParamActionType.Bool)
                {
                    EditableSetBoolParamAction setBoolEditable = editable.ParamActions[i] as EditableSetBoolParamAction;

                    SetBoolParamAction setBool = new SetBoolParamAction();
                    setBool.ParameterName = setBoolEditable.ParameterName;
                    setBool.Value         = setBoolEditable.Value;
                    node.ParamActions.Add(setBool);
                }
            }
        }
        /* -- Deleting connection -- */

        public void DeleteConnection()
        {
            if (m_connectionDeleteParent != null && m_connectionDeleteChild != null)
            {
                // Remove parent relationship
                m_connectionDeleteChild.parents.Remove(m_connectionDeleteParent);

                // Remove child relationship
                if (m_connectionDeleteParent is EditableSpeechNode)
                {
                    if (m_connectionDeleteChild is EditableOptionNode)
                    {
                        (m_connectionDeleteParent as EditableSpeechNode).Options.Remove((m_connectionDeleteChild as EditableOptionNode));
                    }
                    else if (m_connectionDeleteChild is EditableSpeechNode)
                    {
                        (m_connectionDeleteParent as EditableSpeechNode).Speech = null;
                    }
                }
                else
                {
                    (m_connectionDeleteParent as EditableOptionNode).Speech = null;
                }
            }

            // m_ConnectionDeleteA, m_connectionDeleteB
            m_connectionDeleteParent = null;
            m_connectionDeleteChild  = null;
        }
        //---------------------------------
        // Constructor
        //---------------------------------

        public UISpeechNode(EditableConversationNode infoNode, Vector2 pos) : base(infoNode, pos)
        {
            if (defaultNodeStyle == null || defaultNodeStyle.normal.background == null)
            {
                defaultNodeStyle = new GUIStyle();
                defaultNodeStyle.normal.background = DialogueEditorUtil.MakeTextureForNode(Width, Height, DefaultColor);
            }
            if (selectedNodeStyle == null || selectedNodeStyle.normal.background == null)
            {
                selectedNodeStyle = new GUIStyle();
                selectedNodeStyle.normal.background = DialogueEditorUtil.MakeTextureForNode(Width, Height, SelectedColor);
            }
            if (npcNameStyle == null)
            {
                npcNameStyle = new GUIStyle();
                npcNameStyle.normal.textColor = new Color(0.8f, 0.8f, 0.8f, 1);
                npcNameStyle.wordWrap         = true;
                npcNameStyle.stretchHeight    = false;
                npcNameStyle.alignment        = TextAnchor.MiddleCenter;
                npcNameStyle.clipping         = TextClipping.Clip;
            }

            currentBoxStyle = defaultNodeStyle;

            CreateRect(pos, Width, Height);
        }
        public static void GetConnectionDrawInfo(Rect originRect,
                                                 EditableConversationNode connectionTarget, out Vector2 start, out Vector2 end)
        {
            float offset = 12;

            Vector2 origin = new Vector2(originRect.x + originRect.width / 2, originRect.y + originRect.height / 2);
            Vector2 target;

            if (connectionTarget is EditableSpeechNode)
            {
                target = new Vector2(
                    connectionTarget.EditorInfo.xPos + UISpeechNode.Width / 2,
                    connectionTarget.EditorInfo.yPos + UISpeechNode.Height / 2);

                origin.x -= offset;
                target.x -= offset;
            }
            else
            {
                target = new Vector2(
                    connectionTarget.EditorInfo.xPos + UIOptionNode.Width / 2,
                    connectionTarget.EditorInfo.yPos + UIOptionNode.Height / 2);

                origin.x += offset;
                target.x += offset;
            }

            start = origin;
            end   = target;
        }
        public static bool IsPointerNearConnection(List <UINode> uiNodes, Vector2 mousePos, out EditableConnection connection)
        {
            EditableConversationNode parent = null;
            EditableConversationNode child  = null;

            if (IsPointerNearConnection(uiNodes, mousePos, out parent, out child))
            {
                EditableConversationNode.eNodeType type = child.NodeType;
                for (int i = 0; i < parent.Connections.Count; i++)
                {
                    if (type == EditableConversationNode.eNodeType.Speech)
                    {
                        EditableSpeechConnection con = parent.Connections[i] as EditableSpeechConnection;
                        if (con.Speech == child)
                        {
                            connection = parent.Connections[i];
                            return(true);
                        }
                    }
                    else if (type == EditableConversationNode.eNodeType.Option)
                    {
                        EditableOptionConnection con = parent.Connections[i] as EditableOptionConnection;
                        if (con.Option == child)
                        {
                            connection = parent.Connections[i];
                            return(true);
                        }
                    }
                }
            }

            connection = null;
            return(false);
        }
        public static bool IsPointerNearConnection(List <UINode> uiNodes, Vector2 mousePos,
                                                   out EditableConversationNode par, out EditableConversationNode child)
        {
            par   = null;
            child = null;
            Vector2     start, end;
            float       minDistance = float.MaxValue;
            const float MIN_DIST    = 6;

            for (int i = 0; i < uiNodes.Count; i++)
            {
                List <EditableConnection> connections = uiNodes[i].Info.Connections;

                for (int j = 0; j < connections.Count; j++)
                {
                    if (connections[j] is EditableSpeechConnection)
                    {
                        EditableSpeechConnection speechCon = connections[j] as EditableSpeechConnection;
                        GetConnectionDrawInfo(uiNodes[i].rect, speechCon.Speech, out start, out end);

                        float distance = MinimumDistanceBetweenPointAndLine(start, end, mousePos);
                        if (distance < minDistance)
                        {
                            minDistance = distance;
                            par         = uiNodes[i].Info;
                            child       = speechCon.Speech;
                        }
                    }
                    else if (connections[j] is EditableOptionConnection)
                    {
                        EditableOptionConnection optionCon = connections[j] as EditableOptionConnection;
                        GetConnectionDrawInfo(uiNodes[i].rect, optionCon.Option, out start, out end);

                        float distance = MinimumDistanceBetweenPointAndLine(start, end, mousePos);
                        if (distance < minDistance)
                        {
                            minDistance = distance;
                            par         = uiNodes[i].Info;
                            child       = optionCon.Option;
                        }
                    }
                }
            }

            if (minDistance < MIN_DIST)
            {
                return(true);
            }
            else
            {
                par   = null;
                child = null;
                return(false);
            }
        }
        //---------------------------------
        // Constructor
        //---------------------------------

        public UIOptionNode(EditableConversationNode infoNode, Vector2 pos) : base(infoNode, pos)
        {
            if (defaultNodeStyle == null || defaultNodeStyle.normal.background == null)
            {
                defaultNodeStyle = new GUIStyle();
                defaultNodeStyle.normal.background = DialogueEditorUtil.MakeTextureForNode(Width, Height, DefaultColor);
            }
            if (selectedNodeStyle == null || selectedNodeStyle.normal.background == null)
            {
                selectedNodeStyle = new GUIStyle();
                selectedNodeStyle.normal.background = DialogueEditorUtil.MakeTextureForNode(Width, Height, SelectedColor);
            }

            currentBoxStyle = defaultNodeStyle;

            CreateRect(pos, Width, Height);
        }
        //---------------------------------
        // Constructor
        //---------------------------------

        public UINode(EditableConversationNode infoNode, Vector2 pos)
        {
            Info = infoNode;

            if (titleStyle == null)
            {
                titleStyle                  = new GUIStyle();
                titleStyle.alignment        = TextAnchor.MiddleCenter;
                titleStyle.fontStyle        = FontStyle.Bold;
                titleStyle.normal.textColor = Color.white;
            }
            if (textStyle == null)
            {
                textStyle = new GUIStyle();
                textStyle.normal.textColor = Color.white;
                textStyle.wordWrap         = true;
                textStyle.stretchHeight    = false;
                textStyle.clipping         = TextClipping.Clip;
            }
        }
Example #9
0
        public void DeleteConnectionChild(EditableConversationNode node)
        {
            if (Connections.Count == 0)
            {
                return;
            }

            if (node.NodeType == eNodeType.Speech && Connections[0] is EditableSpeechConnection)
            {
                EditableSpeechNode toRemove = node as EditableSpeechNode;

                for (int i = 0; i < Connections.Count; i++)
                {
                    EditableSpeechConnection con = Connections[i] as EditableSpeechConnection;
                    if (con.Speech == toRemove)
                    {
                        Connections.RemoveAt(i);
                        return;
                    }
                }
            }
            else if (node is EditableOptionNode && Connections[0] is EditableOptionConnection)
            {
                EditableOptionNode toRemove = node as EditableOptionNode;

                for (int i = 0; i < Connections.Count; i++)
                {
                    EditableOptionConnection con = Connections[i] as EditableOptionConnection;
                    if (con.Option == toRemove)
                    {
                        Connections.RemoveAt(i);
                        return;
                    }
                }
            }
        }
Example #10
0
        private void ReconstructEditableConversation(EditableConversation conversation)
        {
            if (conversation == null)
            {
                conversation = new EditableConversation();
            }

            // Get a list of every node in the conversation
            List <EditableConversationNode> allNodes = new List <EditableConversationNode>();

            for (int i = 0; i < conversation.SpeechNodes.Count; i++)
            {
                allNodes.Add(conversation.SpeechNodes[i]);
            }
            for (int i = 0; i < conversation.Options.Count; i++)
            {
                allNodes.Add(conversation.Options[i]);
            }

            // For every node:
            // Find the children and parents by UID
            for (int i = 0; i < allNodes.Count; i++)
            {
                // New parents list
                allNodes[i].parents = new List <EditableConversationNode>();

                // Get parents by UIDs
                //-----------------------------------------------------------------------------
                // UPDATE:  This behaviour has now been removed. Later in this function,
                //          the child->parent connections are constructed by using the
                //          parent->child connections. Having both of these behaviours run
                //          results in each parent being in the "parents" list twice.
                //
                // for (int j = 0; j < allNodes[i].parentUIDs.Count; j++)
                // {
                //     allNodes[i].parents.Add(conversation.GetNodeByUID(allNodes[i].parentUIDs[j]));
                // }
                //-----------------------------------------------------------------------------

                // Construct the parent->child connections
                //
                // V1.03
                if (conversation.SaveVersion <= (int)eSaveVersion.V1_03)
                {
                    // Construct Connections from the OptionUIDs and SpeechUIDs (which are now deprecated)
                    // This supports upgrading from V1.03 +

                    allNodes[i].Connections  = new List <EditableConnection>();
                    allNodes[i].ParamActions = new List <EditableSetParamAction>();

                    if (allNodes[i].NodeType == EditableConversationNode.eNodeType.Speech)
                    {
                        EditableSpeechNode thisSpeech = allNodes[i] as EditableSpeechNode;

                        // Speech options
                        int count = thisSpeech.OptionUIDs.Count;
                        for (int j = 0; j < count; j++)
                        {
                            int optionUID             = thisSpeech.OptionUIDs[j];
                            EditableOptionNode option = conversation.GetOptionByUID(optionUID);

                            thisSpeech.Connections.Add(new EditableOptionConnection(option));
                        }

                        // Speech following speech
                        {
                            int speechUID             = thisSpeech.SpeechUID;
                            EditableSpeechNode speech = conversation.GetSpeechByUID(speechUID);

                            if (speech != null)
                            {
                                thisSpeech.Connections.Add(new EditableSpeechConnection(speech));
                            }
                        }
                    }
                    else if (allNodes[i] is EditableOptionNode)
                    {
                        int speechUID             = (allNodes[i] as EditableOptionNode).SpeechUID;
                        EditableSpeechNode speech = conversation.GetSpeechByUID(speechUID);

                        if (speech != null)
                        {
                            allNodes[i].Connections.Add(new EditableSpeechConnection(speech));
                        }
                    }
                }
                //
                // V1.10 +
                else
                {
                    // For each node..  Reconstruct the connections
                    for (int j = 0; j < allNodes[i].Connections.Count; j++)
                    {
                        if (allNodes[i].Connections[j] is EditableSpeechConnection)
                        {
                            EditableSpeechNode speech = conversation.GetSpeechByUID(allNodes[i].Connections[j].NodeUID);
                            (allNodes[i].Connections[j] as EditableSpeechConnection).Speech = speech;
                        }
                        else if (allNodes[i].Connections[j] is EditableOptionConnection)
                        {
                            EditableOptionNode option = conversation.GetOptionByUID(allNodes[i].Connections[j].NodeUID);
                            (allNodes[i].Connections[j] as EditableOptionConnection).Option = option;
                        }
                    }
                }
            }

            // For every node:
            // Tell any of the nodes children that the node is the childs parent
            for (int i = 0; i < allNodes.Count; i++)
            {
                EditableConversationNode thisNode = allNodes[i];

                for (int j = 0; j < thisNode.Connections.Count; j++)
                {
                    if (thisNode.Connections[j].ConnectionType == EditableConnection.eConnectiontype.Speech)
                    {
                        (thisNode.Connections[j] as EditableSpeechConnection).Speech.parents.Add(thisNode);
                    }
                    else if (thisNode.Connections[j].ConnectionType == EditableConnection.eConnectiontype.Option)
                    {
                        (thisNode.Connections[j] as EditableOptionConnection).Option.parents.Add(thisNode);
                    }
                }
            }
        }
        //--------------------------------------
        // Load New Asset
        //--------------------------------------

        public void LoadNewAsset(NPCConversation asset)
        {
            CurrentAsset = asset;
            Log("Loading new asset: " + CurrentAsset.name);

            // Clear all current UI Nodes
            uiNodes.Clear();

            // Deseralize the asset and get the conversation root
            EditableConversation conversation = CurrentAsset.DeserializeForEditor();

            if (conversation == null)
            {
                conversation = new EditableConversation();
            }
            ConversationRoot = conversation.GetRootNode();

            // If it's null, create a root
            if (ConversationRoot == null)
            {
                ConversationRoot = new EditableSpeechNode();
                ConversationRoot.EditorInfo.xPos   = (Screen.width / 2) - (UISpeechNode.Width / 2);
                ConversationRoot.EditorInfo.yPos   = 0;
                ConversationRoot.EditorInfo.isRoot = true;
                conversation.SpeechNodes.Add(ConversationRoot);
            }

            // Get a list of every node in the conversation
            List <EditableConversationNode> allNodes = new List <EditableConversationNode>();

            for (int i = 0; i < conversation.SpeechNodes.Count; i++)
            {
                allNodes.Add(conversation.SpeechNodes[i]);
            }
            for (int i = 0; i < conversation.Options.Count; i++)
            {
                allNodes.Add(conversation.Options[i]);
            }

            // For every node:
            // Find the children and parents by UID
            for (int i = 0; i < allNodes.Count; i++)
            {
                // Remove duplicate parent UIDs
                HashSet <int> noDupes = new HashSet <int>(allNodes[i].parentUIDs);
                allNodes[i].parentUIDs.Clear();
                foreach (int j in noDupes)
                {
                    allNodes[i].parentUIDs.Add(j);
                }

                allNodes[i].parents = new List <EditableConversationNode>();
                for (int j = 0; j < allNodes[i].parentUIDs.Count; j++)
                {
                    allNodes[i].parents.Add(conversation.GetNodeByUID(allNodes[i].parentUIDs[j]));
                }

                if (allNodes[i] is EditableSpeechNode)
                {
                    // Speech options
                    int count = (allNodes[i] as EditableSpeechNode).OptionUIDs.Count;
                    (allNodes[i] as EditableSpeechNode).Options = new List <EditableOptionNode>();
                    for (int j = 0; j < count; j++)
                    {
                        (allNodes[i] as EditableSpeechNode).Options.Add(
                            conversation.GetOptionByUID((allNodes[i] as EditableSpeechNode).OptionUIDs[j]));
                    }

                    // Speech following speech
                    (allNodes[i] as EditableSpeechNode).Speech = conversation.
                                                                 GetSpeechByUID((allNodes[i] as EditableSpeechNode).SpeechUID);
                }
                else if (allNodes[i] is EditableOptionNode)
                {
                    (allNodes[i] as EditableOptionNode).Speech =
                        conversation.GetSpeechByUID((allNodes[i] as EditableOptionNode).SpeechUID);
                }
            }

            // For every node:
            // 1: Create a corresponding UI Node to represent it, and add it to the list
            // 2: Tell any of the nodes children that the node is the childs parent
            for (int i = 0; i < allNodes.Count; i++)
            {
                EditableConversationNode node = allNodes[i];

                if (node is EditableSpeechNode)
                {
                    // 1
                    UISpeechNode uiNode = new UISpeechNode(node,
                                                           new Vector2(node.EditorInfo.xPos, node.EditorInfo.yPos));

                    uiNodes.Add(uiNode);

                    // 2
                    EditableSpeechNode speech = node as EditableSpeechNode;
                    if (speech.Options != null)
                    {
                        for (int j = 0; j < speech.Options.Count; j++)
                        {
                            speech.Options[j].parents.Add(speech);
                        }
                    }

                    if (speech.Speech != null)
                    {
                        speech.Speech.parents.Add(speech);
                    }
                }
                else
                {
                    // 1
                    UIOptionNode uiNode = new UIOptionNode(node,
                                                           new Vector2(node.EditorInfo.xPos, node.EditorInfo.yPos));

                    uiNodes.Add(uiNode);

                    // 2
                    EditableOptionNode option = node as EditableOptionNode;
                    if (option.Speech != null)
                    {
                        option.Speech.parents.Add(option);
                    }
                }
            }

            Recenter();
            Repaint();
#if UNITY_EDITOR
            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
#endif
        }
        public static bool IsPointerNearConnection(List <UINode> uiNodes, Vector2 mousePos,
                                                   out EditableConversationNode par, out EditableConversationNode child)
        {
            par   = null;
            child = null;
            UISpeechNode speech;
            UIOptionNode option;
            Vector2      start, end;
            float        minDistance = float.MaxValue;
            const float  MIN_DIST    = 6;

            for (int i = 0; i < uiNodes.Count; i++)
            {
                if (uiNodes[i] is UISpeechNode)
                {
                    speech = uiNodes[i] as UISpeechNode;

                    if (speech.SpeechNode.Options != null && speech.SpeechNode.Options.Count > 0)
                    {
                        for (int j = 0; j < speech.SpeechNode.Options.Count; j++)
                        {
                            GetConnectionDrawInfo(speech.rect, speech.SpeechNode.Options[j], out start, out end);

                            float distance = MinimumDistanceBetweenPointAndLine(start, end, mousePos);
                            if (distance < minDistance)
                            {
                                minDistance = distance;
                                par         = speech.SpeechNode;
                                child       = speech.SpeechNode.Options[j];
                            }
                        }
                    }
                    else if (speech.SpeechNode.Speech != null)
                    {
                        GetConnectionDrawInfo(speech.rect, speech.SpeechNode.Speech, out start, out end);
                        float distance = MinimumDistanceBetweenPointAndLine(start, end, mousePos);
                        if (distance < minDistance)
                        {
                            minDistance = distance;
                            par         = speech.SpeechNode;
                            child       = speech.SpeechNode.Speech;
                        }
                    }
                }
                else if (uiNodes[i] is UIOptionNode)
                {
                    option = uiNodes[i] as UIOptionNode;

                    if (option.OptionNode.Speech != null)
                    {
                        GetConnectionDrawInfo(option.rect, option.OptionNode.Speech, out start, out end);

                        float distance = MinimumDistanceBetweenPointAndLine(start, end, mousePos);
                        if (distance < minDistance)
                        {
                            minDistance = distance;
                            par         = option.OptionNode;
                            child       = option.OptionNode.Speech;
                        }
                    }
                }
            }

            if (minDistance < MIN_DIST)
            {
                return(true);
            }
            else
            {
                par   = null;
                child = null;
                return(false);
            }
        }