// Use this for initialization
 void Start()
 {
     if (transform.name.EndsWith("Default") || transform.name.StartsWith("Character"))
     {
         isCharacterButton = true;
     }
     if (manager == null)
     {
         if (isCharacterButton)
         {
             manager = transform.parent.parent.GetComponentInChildren <DialogueManagerScript> ();
         }
         else
         {
             manager = transform.GetComponentInParent <DialogueManagerScript> ();
         }
     }
     scrollRectTransform = manager.transform.parent.GetComponent <RectTransform>();
     scrollScrollRect    = scrollRectTransform.transform.GetComponent <DragOverrideScript> ();
     dialogue            = transform.GetComponentInParent <DialogueEntryScript> ();
     rt    = dialogue.transform.GetComponent <RectTransform>();
     dMPos = 0;
     //entries = new List<DialogueEntryScript> ();
     entries = manager.entries;
     UpdateEntryList();
     drag = false;
 }
 /**
  * Empty right now. May not use this but if I do it'll be when adding in.
  */
 public void AddEntry(DialogueEntryScript entry)
 {
     entry.index = entries.Count;
     print(entries.Count);
     entries.Add(entry);
     //mostRecentEntry = entry;
 }
    /**
     * Removes the dialogue entry specified by entry
     */
    public void RemoveDialogue(GameObject entry)
    {
        DialogueEntryScript temp = entry.GetComponent <DialogueEntryScript>();

        //If you're removing the last child, disable the dropdown
        if (temp.parentEntry != null)
        {
            if (temp.parentEntry.children.Count <= 1)
            {
                Toggle t = temp.parentEntry.transform.GetComponentInChildren <Toggle>();
                t.isOn = false;
            }
            temp.parentEntry.RemoveChild(temp);
        }
        mostRecentEntry = temp.parentEntry;

        //Remove the dialogue and it's children from the entries list
        //if (temp.index > 0 && entries.Count > (temp.index + GetFinalChildIndex(temp) - temp.index + 1)) {
        print(temp.index + ", " + entries.Count + ", " + (temp.index + GetFinalChildIndex(temp) - temp.index + 1));
        entries.RemoveRange(temp.index, GetFinalChildIndex(temp) - temp.index + 1);
        //}
        //GameObject.Destroy (entry); //This is handled in the below line
        GetComponentInChildren <HistoryFieldManagerScript>().RemovePanelEntry(entry);

        int i = 0;

        foreach (DialogueEntryScript des in entries)
        {
            entries[i].index = i;
            i++;
        }
    }
 /**
  * Returns the index of the specified Dialogue's children.
  */
 public int GetFinalChildIndex(DialogueEntryScript des)
 {
     while (des.children != null && des.children.Count > 0)
     {
         des = des.children[des.children.Count - 1];
     }
     return(des.index);
 }
    /**
     * Adds a new dialogue by a character. Adds as child/sibling to the mostRecentEntry (this will change in the future)
     */
    public DialogueEntryScript AddNewEntry(Text characterName)
    {
        if (adding)
        {
            throw new System.Exception("Please wait until current dialogue option is added");
        }
        adding = true;
        Transform entryParent;

        if (mostRecentEntry != null)
        {
            if (mostRecentEntry.characterName.Equals(characterName.text))
            {
                entryParent = mostRecentEntry.transform.parent;
            }
            else
            {
                entryParent = mostRecentEntry.childrenParent;
            }
        }
        else
        {
            entryParent = baseParent;
        }
        entryParent.parent.gameObject.SetActive(true);

        GameObject entryObj;

        if (characterName.text.Equals("Patient"))
        {
            entryObj = Resources.Load(GlobalData.resourcePath + "/Prefabs/DialogueEntryLeft") as GameObject;
        }
        else
        {
            entryObj = Resources.Load(GlobalData.resourcePath + "/Prefabs/DialogueEntryRight") as GameObject;
        }

        //GameObject entryObj = Resources.Load (GlobalData.resourcePath + "/Prefabs/DialogueEntry") as GameObject;
        entryObj = Instantiate(entryObj, entryParent);
        DialogueEntryScript entry = entryObj.GetComponent <DialogueEntryScript>();

        entry.characterName = characterName.text;
        entry.index         = entries.Count;
        entryObj.GetComponentInChildren <Image>().color = charColors[characterName.text];

        /*
         * Testing
         */
        entry.charColor = charColors[characterName.text];
        entries.Add(entry);
        mostRecentEntry = entry;
        //mostRecentEntry.parentEntry.transform.GetComponentInChildren<Toggle> ().isOn = true;
        Debug.Log("Entries: " + string.Join("----", entries.Select(x => x.characterName + ", " + x.index + ", " + x.transform.GetSiblingIndex()).ToArray()));
        adding = false;
        return(entry);
    }
    /**
     * If clicked
     */
    public void OnPointerDown(PointerEventData data)
    {
        if (data.button.Equals(PointerEventData.InputButton.Right) || data.button.Equals(PointerEventData.InputButton.Middle))
        {
            return;
        }
        if (hover)
        {
            drag = true;
            if (isCharacterButton)
            {
                dMPos                  = Input.mousePosition.y - rt.position.y;
                dialogue               = (Instantiate(transform.gameObject, manager.baseParent) as GameObject).GetComponent <DialogueEntryScript> ();
                rt                     = dialogue.transform.GetComponent <RectTransform>();
                dialogue.parentEntry   = null;
                dialogue.characterName = transform.Find("CharacterNameValue").GetComponent <TextMeshProUGUI> ().text;
                dialogue.index         = manager.entries.Count;
                transformIndex         = dialogue.transform.GetSiblingIndex();
                UpdateEntryList();
                if (!dialogue.transform.GetComponent <LayoutElement> ())
                {
                    dialogue.gameObject.AddComponent <LayoutElement> ();
                }
                if (!dialogue.transform.GetComponent <CanvasGroup> ())
                {
                    dialogue.gameObject.AddComponent <CanvasGroup> ();
                }
                dialogue.transform.GetComponent <CanvasGroup> ().alpha = 1.0f;
            }
            else
            {
                transformIndex = dialogue.transform.GetSiblingIndex();
                Debug.Log(transformIndex);
                dialogue.transform.GetComponent <CanvasGroup> ().alpha = .5f;
                dMPos = Input.mousePosition.y - rt.position.y;
            }
            //Comment this line out to grab the objects by their center.


            //Spawn placeholder
            placeholder = Instantiate(Resources.Load(GlobalData.resourcePath + "/Prefabs/Placeholder") as GameObject, dialogue.transform.parent);
            placeholder.transform.SetSiblingIndex(dialogue.transform.GetSiblingIndex());
            placeholder.name = "placeholder";

            dialogue.transform.SetParent(manager.baseParent);
            dialogue.transform.SetAsLastSibling();
            dialogue.transform.GetComponent <LayoutElement> ().ignoreLayout = true;
        }
    }
Пример #7
0
 // Use this for initialization
 void Start()
 {
     if (!transform.parent.name.Equals("Entries"))
     {
         parentEntry = null;
     }
     else
     {
         parentEntry = transform.parent.GetComponentInParent <DialogueEntryScript>();
     }
     children = new List <DialogueEntryScript>();
     if (parentEntry != null)
     {
         parentEntry.GetComponentInChildren <Toggle>().isOn = true;
         parentEntry.children.Add(this);
         parentEntry.UpdateChildren();
     }
 }
Пример #8
0
 /**
  * Removes a child from the list of children (does not delete the game object)
  */
 public void RemoveChild(DialogueEntryScript des)
 {
     children.Remove(des);
 }
    /**
     * Runs every frame to determine position of dragged dialogue and updating placeholder position
     */
    void Update()
    {
        if (drag)
        {
            if (rt)
            {
                rt.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y - dMPos, 0f);
            }
            //Count the number of entries this dialogue is below

            rt.transform.GetComponent <LayoutElement> ().preferredWidth = placeholder.transform.GetComponent <RectTransform> ().rect.width;
            float vPos = 0f;
            vPos = transform.position.y;

            int pos = 0;

            foreach (DialogueEntryScript entry in entries)
            {
                if (!entry.transform.Equals(dialogue) && entry.transform.Find("ParentDialogue").position.y > vPos)
                {
                    if (!entry.transform.IsChildOf(dialogue.transform))
                    {
                        pos++;
                    }
                }
            }

            Debug.Log("Pos:" + pos + ", Dialogue idx: " + dialogue.index);
            DialogueEntryScript desBelow;

            //If it's the only child, disable the parent's dropdown while moving
            if (dialogue.parentEntry != null && dialogue.parentEntry.children.Count == 1)
            {
                //dialogue.transform.SetParent (dialogue.parentEntry.transform.parent);
                dialogue.transform.SetParent(manager.baseParent);
                dialogue.transform.SetAsLastSibling();
                dialogue.parentEntry.transform.GetComponentInChildren <Toggle> ().isOn = false;
            }

            //If the entry is at the bottom of the list
            if (pos == entries.Count - manager.GetFinalChildIndex(dialogue) + dialogue.index - 1)
            {
                Debug.Log("Setting as last child");

                //adjust pos for the number of children.
                if (pos > dialogue.index)
                {
                    pos += manager.GetFinalChildIndex(dialogue) - dialogue.index;
                }
                desBelow = manager.GetDialogueOfIndex(pos);
                if (desBelow.characterName.Equals(dialogue.characterName))
                {
                    placeholder.transform.SetParent(desBelow.transform.parent);
                    if (desBelow != dialogue)
                    {
                        desBelow.transform.GetComponentInChildren <Toggle> ().isOn = false;
                    }
                }
                else
                {
                    placeholder.transform.SetParent(desBelow.transform.parent);
                }

                placeholder.transform.SetAsLastSibling();
                if (placeholder.transform.parent == dialogue.transform.parent)
                {
                    placeholder.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex() - 1);
                }
                //placeholder.transform.SetSiblingIndex (pos - GetNewPlaceholderPos ());
            }
            else                 //entry within list

            //If you're moving it downward, adjust pos for the number of children.
            {
                if (pos > dialogue.index)
                {
                    pos += manager.GetFinalChildIndex(dialogue) - dialogue.index;
                }
                int movingUp = 0;
                //Assign the desBelow accurately
                if (pos >= dialogue.index)                  //&& dialogue.parentEntry != manager.GetDialogueOfIndex (pos + 1).parentEntry) {
                {
                    desBelow = manager.GetDialogueOfIndex(pos + 1);
                }
                else
                {
                    desBelow = manager.GetDialogueOfIndex(pos);
                    movingUp = 1;
                }
                Debug.Log(pos);
                int tempPos = pos;
                Debug.Log("POS: " + tempPos + ", " + movingUp);

                /*
                 * List<DialogueEntryScript> siblings = entries.FindAll ((DialogueEntryScript obj) => obj.transform.parent == placeholder.transform.parent);
                 * int accountForChildren = 0;
                 * foreach (DialogueEntryScript Des in siblings) {
                 *      if (Des.index < pos) {
                 *              int finalChildIndex;
                 *              if (pos > (finalChildIndex = manager.GetFinalChildIndex (Des))) { //make sure pos is actually above children too
                 *                      accountForChildren += (finalChildIndex - Des.index);
                 *                      if (dialogue.parentEntry != null && dialogue.parentEntry.transform.IsChildOf (Des.transform)) {
                 *                              accountForChildren--;
                 *                      }
                 *              }
                 *      }
                 * }
                 * pos -= accountForChildren;*/

                //Debug.Log ("desbelow: " + desBelow.dialogue + ",Index: " + desBelow.index);

                //Check if the parent of desBelow is of the same character as dialogue. If so, set dialogue as sibling to parent
                if (tempPos - movingUp == dialogue.index)
                {
                    Debug.Log("Option 0");
                    if (dialogue.parentEntry == null)
                    {
                        placeholder.transform.SetParent(manager.baseParent);
                    }
                    else if (dialogue.parentEntry.children.Count > 1)
                    {
                        placeholder.transform.SetParent(dialogue.parentEntry.childrenParent);
                    }
                    else
                    {
                        placeholder.transform.SetParent(dialogue.parentEntry.transform.parent);
                    }
                }
                else if (tempPos > 0 && !manager.GetDialogueOfIndex(tempPos - movingUp).characterName.Equals(dialogue.characterName))
                {
                    Debug.Log("Option 1");
                    int isChildOf = 0;
                    if (dialogue.parentEntry == manager.GetDialogueOfIndex(tempPos - movingUp))
                    {
                        isChildOf = 1;
                    }
                    if (manager.GetDialogueOfIndex(tempPos - movingUp).children.Count >= 1 + isChildOf)
                    {
                        placeholder.transform.SetParent(manager.GetDialogueOfIndex(tempPos - movingUp).childrenParent);
                    }
                    else
                    {
                        placeholder.transform.SetParent(manager.GetDialogueOfIndex(tempPos - movingUp).transform.parent);
                    }
                }
                else if (desBelow.parentEntry != null && desBelow.parentEntry.characterName.Equals(dialogue.characterName) && tempPos == desBelow.parentEntry.index + movingUp)
                {
                    Debug.Log("Option 2");
                    placeholder.transform.SetParent(desBelow.parentEntry.transform.parent);
                }
                else
                {
                    Debug.Log("Option 3");
                    placeholder.transform.SetParent(desBelow.transform.parent);
                }

                int localPosition = 0;
                Debug.Log("movingUP: " + movingUp);
                for (int i = 0; i < placeholder.transform.parent.childCount; i++)
                {
                    Transform child = placeholder.transform.parent.GetChild(i);
                    if (child.name.Equals("placeholder") || child == dialogue.transform)
                    {
                        continue;
                    }
                    if (pos - movingUp >= child.GetComponent <DialogueEntryScript>().index)
                    {
                        localPosition++;
                    }
                }
                Debug.Log(localPosition);
                placeholder.transform.SetSiblingIndex(localPosition);

                /*
                 * int index = manager.GetDialogueOfIndex (pos).transform.GetSiblingIndex ();
                 * if (placeholder.transform.IsChildOf (manager.GetDialogueOfIndex (pos).transform.parent) && placeholder.transform.GetSiblingIndex () <= index) {
                 *      pos = index;
                 *      pos--;
                 * } else {
                 *      pos = index;
                 * }*/
                //Debug.Log ("POS: " + pos + ",NEW placeholder POS: " + GetNewPlaceholderPos ());

                //Set local transform index
                //placeholder.transform.SetSiblingIndex (pos - GetNewPlaceholderPos ());
            }


            //Used for scrolling. Hold dragable near top/bottom to scroll
            Vector3[] corners = new Vector3[4];
            scrollRectTransform.GetWorldCorners(corners);
            if (vPos > corners[1].y - 40 && scrollScrollRect.verticalNormalizedPosition < 1.0f)               //Scrolling up
            {
                scrollScrollRect.verticalNormalizedPosition += .02f;
            }
            else if (vPos < corners[0].y + 40 && scrollScrollRect.verticalNormalizedPosition > 0.0f)                 //Scrolling down
            {
                scrollScrollRect.verticalNormalizedPosition -= .02f;
            }

            //When let go
            if (!Input.GetMouseButton(0))               //&& false) {
            {
                drag = false;

                if (isCharacterButton)
                {
                    if (pos > 0)
                    {
                        manager.mostRecentEntry = manager.GetDialogueOfIndex(pos - 1);
                    }
                    Destroy(dialogue.gameObject);

                    /*try {
                     *      dialogue = manager.AddNewEntry (transform.Find ("CharacterNameValue").GetComponent<TextMeshProUGUI> ());
                     * } catch (Exception e) {
                     *      Debug.Log (e.Message);
                     * }*/
                    scrollScrollRect.verticalNormalizedPosition = 0.0f;
                    dialogue = manager.AddNewEntry(transform.Find("CharacterNameValue").GetComponent <TextMeshProUGUI> ());
                    scrollScrollRect.verticalNormalizedPosition = 0.0f;

                    //DISABLE BRANCHING
                    hover = false;
                }

                dialogue.transform.GetComponent <CanvasGroup> ().alpha          = 1.0f;
                dialogue.transform.GetComponent <LayoutElement> ().ignoreLayout = false;

                pos = 0;
                //if (isCharacterButton) { //Use the mouse position, not the dialogue entry position, for comparisons
                //foreach (DialogueEntryScript entry in entries) {
                //if (!entry.transform.Equals (dialogue) && entry.transform.Find ("ParentDialogue").position.y > Input.mousePosition.y) {
                //if (!entry.transform.IsChildOf (dialogue.transform))
                //pos++;
                //}
                //}
                //} else { //Use the dialogue entry position for comparisons
                foreach (DialogueEntryScript entry in entries)
                {
                    if (!entry.transform.Equals(dialogue) && entry.transform.Find("ParentDialogue").position.y > transform.position.y)
                    {
                        if (!entry.transform.IsChildOf(dialogue.transform))
                        {
                            pos++;
                        }
                    }
                }
                //}
                if (pos > dialogue.index)
                {
                    pos += manager.GetFinalChildIndex(dialogue) - dialogue.index;
                }
                Destroy(placeholder);
                placeholder = null;

                manager.MoveChildTo(dialogue.transform.gameObject, pos, dialogue.index);

                if (!EventSystem.current.IsPointerOverGameObject())
                {
                    //OnPointerExit (null);
                }
                if (!hover)
                {
                    cursor.sprite = cursorPicture;
                }
                dialogue = transform.GetComponentInParent <DialogueEntryScript> ();
                rt       = dialogue.gameObject.GetComponent <RectTransform> ();
            }
        }
    }
    /**
     * Moves a dialogue from one position to another
     * pos = index the dialogue is moving to
     * beforePos = the index the dialogue started at
     */
    public void MoveChildTo(GameObject dialogue, int pos, int beforePos)
    {
        DialogueEntryScript movingDES = dialogue.GetComponent <DialogueEntryScript>();

        Debug.Log("Index we're moving: " + movingDES.index + ", newpos:" + pos + ", beforepos:" + beforePos);
        Debug.Log("Entries: " + string.Join("----", entries.Select(x => x.characterName + ", idx: " + x.index + ", display: " + x.dialogue).ToArray()));


        int movingUp = 0;
        DialogueEntryScript newParent;

        if (pos == 0)
        {
            newParent = null;
        }
        else
        {
            if (pos <= movingDES.index)
            {
                movingUp = 1;
            }
            if (!GetDialogueOfIndex(pos - movingUp).characterName.Equals(movingDES.characterName))   // && GetDialogueOfIndex (pos).children.Count > 0) {
            {
                newParent = GetDialogueOfIndex(pos - movingUp);
                GetDialogueOfIndex(pos - movingUp).transform.GetComponentInChildren <Toggle>().isOn = true;
                //Debug.Log ("Becoming child");
            }
            else
            {
                newParent = GetDialogueOfIndex(pos - movingUp).parentEntry;
                //pos = pos + GetFinalChildIndex (movingDES) - movingDES.index;
            }
        }

        //NESTED remove the following line to restore nesting
        newParent = null;

        //newParent = GetDialogueOfIndex (pos).parentEntry;

        DialogueEntryScript oldParent = movingDES.parentEntry;


        List <DialogueEntryScript> rangeToMove = entries.GetRange(beforePos, GetFinalChildIndex(entries[beforePos]) - beforePos + 1);

        //Debug.Log("RangeToMove: " + string.Join("----", rangeToMove.Select(x => x.characterName + ", idx: " + x.index + ", display: " + x.dialogue).ToArray()));
        //Debug.Log ("Pos: " + pos);

        entries.RemoveRange(beforePos, rangeToMove.Count);
        int tempPos = pos;

        if (pos > 0)
        {
            if (newParent == null)
            {
                Debug.Log(GetFinalChildIndex(GetDialogueOfIndex(pos - movingUp)));
                Debug.Log(GetDialogueOfIndex(pos - movingUp).index);
                pos += GetFinalChildIndex(GetDialogueOfIndex(pos - movingUp)) - GetDialogueOfIndex(pos - movingUp).index;
            }
            else if (GetDialogueOfIndex(pos - movingUp).characterName.Equals(movingDES.characterName))     // && GetDialogueOfIndex (pos).children.Count > 0) {
            {
                pos += GetFinalChildIndex(GetDialogueOfIndex(pos - movingUp)) - GetDialogueOfIndex(pos - movingUp).index;
            }
            if (oldParent != null && oldParent.transform.IsChildOf(GetDialogueOfIndex(tempPos - movingUp).transform))
            {
                if (newParent == null || !newParent.transform.IsChildOf(GetDialogueOfIndex(tempPos - movingUp).transform))
                {
                    pos--;
                }
            }
        }
        if (pos > movingDES.index)   //If moving down, adjust position to account for dialogue's children
        {
            pos = pos - rangeToMove.Count + 1;
        }

        Debug.Log("Pos: " + pos);
        entries.InsertRange(pos, rangeToMove);
        //entries.Insert (pos, temp);

        int i = 0;

        foreach (DialogueEntryScript des in entries)
        {
            entries[i].index = i;
            i++;
        }
        Debug.Log("Entries: " + string.Join("----", entries.Select(x => x.characterName + ", idx: " + x.index + ", display: " + x.dialogue).ToArray()));

        movingDES.parentEntry = newParent;
        if (oldParent != null)
        {
            oldParent.children.Remove(movingDES);
            //oldParent.UpdateChildren ();
        }
        if (newParent != null)
        {
            dialogue.transform.SetParent(newParent.childrenParent);

            //newParent.UpdateChildren ();
        }
        else
        {
            dialogue.transform.SetParent(baseParent);
        }

        //Set sibling transform index accordingly with reference to it's children
        int position = 0;

        for (i = 0; i < dialogue.transform.parent.childCount; i++)
        {
            Debug.Log(dialogue.transform.parent.GetChild(i).name);

            if (dialogue.transform.parent.GetChild(i).name.Equals("placeholder"))
            {
                continue;
            }
            if (movingDES.index > dialogue.transform.parent.GetChild(i).GetComponent <DialogueEntryScript>().index)
            {
                position++;
            }
        }
        if (newParent != null)
        {
            newParent.children.Insert(position, movingDES);
        }
        dialogue.transform.SetSiblingIndex(position);

        //If the moved dialogue was placed as a sibling above those of another character
        //As in, character foo's dialogue being placed as a sibling above character bar's dialogue
        //so therefore bar's dialogue should become children of foo's dialogue
        DialogueEntryScript temp = null;
        int siblingPosition      = position + 1;

        while (movingDES.transform.parent.childCount > siblingPosition)
        {
            if (movingDES.transform.parent.GetChild(siblingPosition) != null)
            {
                temp = movingDES.transform.parent.GetChild(siblingPosition).GetComponent <DialogueEntryScript>();
            }
            if (temp != null && !temp.characterName.Equals(movingDES.characterName))
            {
                movingDES.GetComponentInChildren <Toggle>().isOn = true;
                temp.parentEntry = movingDES;
                temp.parentEntry.children.Remove(temp);
                temp.transform.SetParent(movingDES.childrenParent);
                temp.transform.SetAsLastSibling();
                siblingPosition--;
            }
            temp = null;
            siblingPosition++;
        }
        movingDES.UpdateChildren();
    }
 /**
  * For other scripts to remove entries from the list
  */
 public void RemoveFromEntryList(DialogueEntryScript t)
 {
     entries.Remove(t);
 }
 /**
  * Empty right now. May not use this but if I do it'll be when adding in.
  */
 public void AddEntry(DialogueEntryScript entry)
 {
     entry.index = entries.Count;
     //print(entries.Count);
     entries.Add(entry);
 }