/**
     * 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> ();
            }
        }
    }
 public void AddCharacter()
 {
     dms.AddNewEntry(transform.Find("CharacterNameValue").GetComponent <TextMeshProUGUI>());
 }