/**
     * Called when the suggestion is chosen/accepted
     */
    public void SubmitChoiceToParent()
    {
        if (transform.GetComponentInParent <InputFieldOverrideScript>())
        {
            InputFieldOverrideScript parentInput = transform.GetComponentInParent <InputFieldOverrideScript>();

            parent.AddTag(transform.Find("Text").GetComponent <Text>().text);
            //parent.AddTag(transform.GetComponent<Dropdown>().captionText.text);
            parentInput.Select();
            parentInput.OnSelect(null);
            parentInput.UpdateCursor();
        }
        else if (transform.GetComponentInParent <TMPInputFieldOverrideScript>())
        {
            TMPInputFieldOverrideScript parentInput = transform.GetComponentInParent <TMPInputFieldOverrideScript>();

            transform.parent.GetComponentInParent <AutofillTMP>().AddTag(transform.Find("Text").GetComponent <TextMeshProUGUI>().text);
            //parent.AddTag(transform.GetComponent<Dropdown>().captionText.text);
            parentInput.Select();
            parentInput.OnSelect(null);
            parentInput.UpdateCursor();
        }
        else
        {
            Transform tfPath = transform;

            string path = transform.name;
            while (transform.parent != null)
            {
                tfPath = tfPath.parent;
                path   = tfPath.name + "/" + path;
            }
            Debug.Log("There is no overridescript for: " + path);
        }
    }
Esempio n. 2
0
    /**
     * Creates the dropdown of suggested tags
     * This is called when the inputfield value is changed
     */
    public void DisplayAutoCompleteResults()
    {
        if (tagObjs == null)
        {
            Start();
        }

        //Clear previous options
        if (dd != null)
        {
            dd.ClearOptions();
            //dd = tagInputField.parent.GetComponentInChildren<Dropdown> ();
        }
        if (SuggestionsParent == null)
        {
            SuggestionsParent = tagInputField.Find("Suggestions/Viewport/Content");
        }
        //SuggestionsParent.gameObject.SetActive(true);\
        tagInputField.Find("CloseSuggestions")?.gameObject.SetActive(true);
        tagInputField.Find("Suggestions").gameObject.SetActive(true);
        tagInputField.Find("Suggestions").GetComponent <CanvasGroup>().blocksRaycasts = false;
        if (SuggestionsParent.childCount > 0)
        {
            tagObjs.Clear();
            foreach (Transform obj in SuggestionsParent.GetComponentsInChildren <Transform>())
            {
                if (obj.name != SuggestionsParent.name)
                {
                    Destroy(obj.gameObject);
                }
            }
        }

        //Get the most recent tag entry
        input = tagInputField.GetComponent <TMPInputFieldOverrideScript>();
        string tagInput = input.text;

        if (tagInput.EndsWith("; ") && !downPressed)   //TagInput only ends with this after successfully entering a tag.
                                                       //We don't want another runthrough, so return;
        {
            tagObjs.Clear();
            justAdded = false; //We successfully averted after adding. Set this to false
            tagInputField.Find("CloseSuggestions")?.gameObject.SetActive(false);
            tagInputField.Find("Suggestions").gameObject.SetActive(false);
            return;
        }

        //Since adding tags means updating the input text, this automatically gets called again
        //We return so that we don't get any undesired effects.
        if (justAdded)
        {
            justAdded = false;
            return;
        }

        List <string> selectedTags = new List <string>(Regex.Split(tagInput, "; "));

        if (selectedTags.Count > 0)
        {
            tagInput = selectedTags[selectedTags.Count - 1]; //Gets the tag that's being input currently
            tagInput = tagInput.TrimStart();
            //Debug.Log (selectedTags.Count + "," + enteredTags.Count);
        }

        //If there are less tags input than there are entered tags, the user selected and deleted tags
        //So we remove the ones they deleted from the entered tags list
        if (selectedTags.Count < enteredTags.Count || (selectedTags.Count == enteredTags.Count && !tagInput.EndsWith(";")))
        {
            List <string> tagsToRemove = new List <string>();
            foreach (string s in enteredTags)
            {
                if (!selectedTags.Contains(s))
                {
                    tagsToRemove.Add(s);
                }
            }
            foreach (string s in tagsToRemove)
            {
                enteredTags.Remove(s);
            }
            isDeleting = false; //deleting was already taken care of.

            //To not display results if the user has nothing typed in (after deleting)
            if (!downPressed && input.text.Equals(""))
            {
                return;
            }
        }

        //To prevent looping which I encountered. Not currently used (preventLooping is false)
        nolooppls++;
        if (nolooppls > 100 && preventLooping)
        {
            input.text = "";
            nolooppls  = 0;
            tagInput   = "";
            return;
        }

        //If the user pressed the end tag key or is deleting a tag
        bool endCharPressed = tagInput.EndsWith(";");

        if (endCharPressed)
        {
            tagInput = tagInput.Remove(tagInput.Length - 1);
        }
        if (endCharPressed && enteredTags.Count > 0)
        {
            /*
             *          input.text = "";
             *          Debug.Log ("Deleting:" + tagInput);
             *          enteredTags.Remove (enteredTags[0]);
             *
             *          return;
             */
        }
        //Debug.Log (string.Join (",", enteredTags.ToArray ()));
        //Debug.Log (isDeleting + "," + justAdded + "," + tagInput);
        if (enteredTags.Contains(tagInput))
        {
            Debug.Log(input.text + "....");
            Debug.Log(Regex.Matches(input.text.ToLower(), "\\b" + tagInput.ToLower() + "((;)|$)").Count);

            //If the user is deleting a tag
            if (Regex.Matches(input.text.ToLower(), "\\b" + tagInput.ToLower() + "((;)|$)").Count == 1)
            {
                enteredTags.Remove(tagInput);
                isDeleting = true;
                Debug.Log("Deleting..." + tagInput);
                selectedTags[selectedTags.Count - 1] = "";
                input.text = string.Join("; ", selectedTags.ToArray()) + tagInput;
                return;
            }
            else if (!endCharPressed)     //If the user is trying to add a duplicate tag
            {
                Debug.Log("Cannot add two duplicate tags!");
                isDeleting = true;
                selectedTags[selectedTags.Count - 1] = "";
                input.text = string.Join("; ", selectedTags.ToArray()) + tagInput;
                return;
            }
        }


        //Find any matching tags from the list of all possible tags. Remove any that have already been entered
        List <string> matchingTags = tags.FindAll(s => s.StartsWith(tagInput, true, null));

        foreach (string tag in enteredTags)
        {
            if (matchingTags.Contains(tag))
            {
                matchingTags.Remove(tag);
            }
        }

        //Ways to detect that a tag has been entered
        if (matchingTags.Count == 1 && !enteredTags.Contains(matchingTags[0]))
        {
            if (endCharPressed || tagInput.ToLower().Equals(matchingTags[0].ToLower())) //If end char pressed or entered tag matches last available
            {
                if (isDeleting)                                                         //if deleting, do not add duplicate tag
                {
                    isDeleting = false;
                }
                else
                {
                    Debug.Log("Matching tag found");
                    enteredTags.Add(matchingTags[0]);
                    selectedTags[selectedTags.Count - 1] = matchingTags[0];
                    justAdded = true;
                    if (this.isDynamic == false)
                    {
                        input.text = string.Join("; ", selectedTags.ToArray()) + "";
                    }
                    else
                    {
                        input.text = string.Join("; ", selectedTags.ToArray()) + "; ";
                    }
                    updateCursor(input);
                    tagObjs.Clear();
                    tagInputField.Find("CloseSuggestions")?.gameObject.SetActive(false);
                    tagInputField.Find("Suggestions").gameObject.SetActive(false);
                    return;
                }
            }
        }

        //If it gets here, deleting a tag has been handled. Safe to set false (Useful when more than one available tag after deleting)
        isDeleting = false;

        if (matchingTags.Count == 0)
        {
            tagInputField.Find("Suggestions").GetComponent <GraphicRaycaster>().enabled = false;
        }
        else
        {
            tagInputField.Find("Suggestions").GetComponent <GraphicRaycaster>().enabled = true;
        }

        if (endCharPressed)
        {
            if (!string.IsNullOrWhiteSpace(tagInput) && matchingTags.Count > 0)   //If there are more than one matching tags and end char pressed, select the top tag
            {
                Debug.Log("Matching tag found");
                matchingTags.Sort();
                enteredTags.Add(matchingTags[0]);
                selectedTags[selectedTags.Count - 1] = matchingTags[0];
                justAdded = true;
                if (this.isDynamic == false)
                {
                    input.text = string.Join("; ", selectedTags.ToArray()) + "";
                }
                else
                {
                    input.text = string.Join("; ", selectedTags.ToArray()) + "; ";
                }
                updateCursor(input);
                Debug.Log(matchingTags[0]);
                tagObjs.Clear();
                tagInputField.Find("CloseSuggestions")?.gameObject.SetActive(false);
                tagInputField.Find("Suggestions").gameObject.SetActive(false);
                return;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(tagInput))
                {
                    Debug.Log("No Matching tags found. Please enter another");
                    GameObject.Find("GaudyBG").GetComponent <DataScript>().ShowMessage("No Matching tags found. Please enter another");
                }
                selectedTags[selectedTags.Count - 1] = "";
                if (name == "SaveCaseBG")
                {
                    input.text = string.Join("; ", selectedTags.ToArray()) + tagInput;
                }
                return;
            }
        }

        //To not display results if the user has nothing typed in (after deleting)
        if (!downPressed && input.text.Equals(""))
        {
            return;
        }

        downPressed = false;

        //If no matching tag or end char has not been pressed, then show the available tags
        int i   = 0;
        int max = 4;

        //boolSwitch is true currently
        if (boolSwitch)   //boolSwitch was used to test an actual dropdown vs my custom one. Keeping it like this for now
        {
            if (selectedTagPos > matchingTags.Count - 1)
            {
                selectedTagPos = -1;
            }
            tagObjs.Clear();
            matchingTags.Sort();
            tagInputField.Find("Suggestions").GetComponent <CanvasGroup>().blocksRaycasts = true;
            foreach (string s in matchingTags)
            {
                GameObject tr            = Resources.Load(GlobalData.resourcePath + "/Prefabs/TagsValue") as GameObject;
                GameObject tagSuggestion = Instantiate(tr, SuggestionsParent);
                //tagSuggestion.GetComponent<LayoutElement> ().ignoreLayout = true;
                tagSuggestion.transform.Find("Text").GetComponent <TextMeshProUGUI>().text = s;
                tagSuggestion.name = "Label: " + s;
                tagSuggestion.GetComponent <AutoCompleteEntryScript>().Initiate(i, false);
                //Rect r = tagSuggestion.GetComponent<RectTransform> ().rect;
                //Rect tagInputFieldRect = tagInputField.GetComponent<RectTransform> ().rect;

                //r.x = -tagInputFieldRect.width / 2 + r.width / 2;
                //r.y = tagInputFieldRect.y - (tagInputFieldRect.height * i) - r.height / 2;
                //tagSuggestion.transform.localPosition = r.position;
                //r.width = tagInputFieldRect.width;
                i++;
                tagObjs.Add(tagSuggestion.GetComponent <AutoCompleteEntryScript>());
                if (i > max)
                {
                    //Uncomment this break line in order to set a max number of suggested tags
                    ;//break;
                }
            }
            //tagInputField.GetComponentInChildren<ScrollRect> ().verticalScrollbar.value = 1;
            //SuggestionsParent.parent.parent.Find("Scrollbar Vertical").GetComponent<Scrollbar> ().value = 1;
            updateCursor(input);
        }
        else
        {
            dd.Hide();
            dd.ClearOptions();
            dd.AddOptions(matchingTags);
            //dd.transform.gameObject.SetActive (true);
            dd.RefreshShownValue();

            dd.Show();
            dd.OnDeselect(null);
            input.Select();
            updateCursor(input);
        }
        //input.ForceLabelUpdate ();
    }