Example #1
0
        // Change the value and hide the dropdown.
        private void OnSelectItem(PreciseManeuverDropdownItem item)
        {
            if (!item.Toggle.isOn)
            {
                item.Toggle.isOn = true;
            }

            Value = item.Index;
            Hide();
        }
 private void setChooserOption(PreciseManeuverDropdownItem item)
 {
     if (item.index == 0)
     {
         m_saverControl.replaceTextComponentWithTMPro(item.GetComponentInChildren <Text> ())?.Invoke(m_saverControl.newPresetLocalized);
     }
     else
     {
         m_saverControl.replaceTextComponentWithTMPro(item.GetComponentInChildren <Text> ())?.Invoke(presetCache[item.index - 1]);
     }
 }
Example #3
0
        private void SetChooserOption(PreciseManeuverDropdownItem item)
        {
            if (!(item is PreciseManeuverPagerItem))
            {
                return;
            }
            var pageritem = item as PreciseManeuverPagerItem;

            m_Control.ReplaceTextComponentWithTMPro(pageritem.NodeIdx.GetComponent <Text> ())?.
            Invoke(m_Control.getLocalizedNodeln(pageritem.Index + 1));
            m_Control.ReplaceTextComponentWithTMPro(pageritem.NodeTime.GetComponent <Text> ())?.
            Invoke(m_Control.getManeuverTime(pageritem.Index));
            m_Control.ReplaceTextComponentWithTMPro(pageritem.NodedV.GetComponent <Text> ())?.
            Invoke(m_Control.getManeuverDV(pageritem.Index));
            m_Control.ReplaceTextComponentWithTMPro(pageritem.dVLabel.GetComponent <Text> ());
        }
Example #4
0
        private void setChooserOption(PreciseManeuverDropdownItem item)
        {
            if (!(item is PreciseManeuverPagerItem))
            {
                return;
            }
            var pageritem = item as PreciseManeuverPagerItem;

            m_pagerControl.replaceTextComponentWithTMPro(pageritem.nodeidx.GetComponent <Text> ())?.
            Invoke(m_pagerControl.getManeuverNodeLocalized() + "\n" + (pageritem.index + 1).ToString());
            m_pagerControl.replaceTextComponentWithTMPro(pageritem.nodetime.GetComponent <Text> ())?.
            Invoke(m_pagerControl.getManeuverTime(pageritem.index));
            m_pagerControl.replaceTextComponentWithTMPro(pageritem.nodedv.GetComponent <Text> ())?.
            Invoke(m_pagerControl.getManeuverDV(pageritem.index));
            m_pagerControl.replaceTextComponentWithTMPro(pageritem.dvlabel.GetComponent <Text> ());
        }
Example #5
0
        private PreciseManeuverDropdownItem AddItem(bool selected, PreciseManeuverDropdownItem itemTemplate, int index)
        {
            PreciseManeuverDropdownItem item = CreateItem(itemTemplate);

            item.rectTransform.SetParent(itemTemplate.rectTransform.parent, false);

            item.gameObject.name = "Item " + index;
            item.Index           = index;
            if (item.Toggle != null)
            {
                item.Toggle.isOn = false;
            }
            UpdateDropdownOption?.Invoke(item);

            item.gameObject.SetActive(true);

            return(item);
        }
        private PreciseManeuverDropdownItem AddItem(int index, PreciseManeuverDropdownItem itemTemplate)
        {
            GameObject go = Instantiate(itemTemplate.gameObject);
            PreciseManeuverDropdownItem item = go.GetComponent <PreciseManeuverDropdownItem> ();

            go.name    = gameObject.name + " item " + index.ToString();
            item.Index = index;
            var layout = GetOrAddComponent <LayoutElement> (go);

            layout.preferredHeight = (go.transform as RectTransform).rect.height;

            if (item.Toggle != null)
            {
                item.Toggle.isOn = false;
            }
            UpdateDropdownOption?.Invoke(item);

            go.transform.SetParent(itemTemplate.transform.parent, false);
            go.SetActive(true);

            return(item);
        }
        public void Show()
        {
            if (!IsActive() || !IsInteractable() || m_Dropdown != null || UpdateDropdownOption == null)
            {
                return;
            }

            if (!validTemplate)
            {
                SetupTemplate();
                if (!validTemplate)
                {
                    return;
                }
            }

            m_Template.gameObject.SetActive(true);

            // Instantiate the drop-down template
            m_Dropdown      = Instantiate(Template.gameObject);
            m_Dropdown.name = "Dropdown List";
            m_Dropdown.SetActive(true);

            RectTransform dropdownRectTransform = m_Dropdown.transform as RectTransform;

            dropdownRectTransform.SetParent(Template.parent, false);
            var layout = GetOrAddComponent <VerticalLayoutGroup> (m_Dropdown);

            layout.childForceExpandHeight = false;
            layout.childForceExpandWidth  = true;
            var fitter = GetOrAddComponent <ContentSizeFitter> (m_Dropdown);

            fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
            fitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;

            PreciseManeuverDropdownItem itemPrefab = m_Dropdown.GetComponentInChildren <PreciseManeuverDropdownItem> ();

            itemPrefab.gameObject.SetActive(true);

            Toggle prev = null;

            for (int i = 0; i < OptionCount; ++i)
            {
                PreciseManeuverDropdownItem item = AddItem(i, itemPrefab);
                if (item == null)
                {
                    continue;
                }
                // Automatically set up a toggle state change listener
                item.Toggle.isOn = (Value == i);
                item.Toggle.onValueChanged.AddListener(x => OnSelectItem(item.Index));

                // Select current option
                if (item.Toggle.isOn)
                {
                    item.Toggle.Select();
                }

                // Automatically set up explicit navigation
                if (prev != null)
                {
                    Navigation prevNav   = prev.navigation;
                    Navigation toggleNav = item.Toggle.navigation;
                    prevNav.mode   = Navigation.Mode.Explicit;
                    toggleNav.mode = Navigation.Mode.Explicit;

                    prevNav.selectOnDown   = item.Toggle;
                    prevNav.selectOnRight  = item.Toggle;
                    toggleNav.selectOnLeft = prev;
                    toggleNav.selectOnUp   = prev;

                    prev.navigation        = prevNav;
                    item.Toggle.navigation = toggleNav;
                }
                prev = item.Toggle;
            }

            // Invert anchoring and position if dropdown is partially or fully outside of canvas rect.
            // Typically this will have the effect of placing the dropdown above the button instead of below,
            // but it works as inversion regardless of initial setup.
            Vector3[] corners = new Vector3[4];
            dropdownRectTransform.GetWorldCorners(corners);
            bool          outside = false;
            RectTransform rootCanvasRectTransform = m_RootCanvas.transform as RectTransform;

            for (int i = 0; i < 4; i++)
            {
                Vector3 corner = rootCanvasRectTransform.InverseTransformPoint(corners[i]);
                if (!rootCanvasRectTransform.rect.Contains(corner))
                {
                    outside = true;
                    break;
                }
            }
            if (outside)
            {
                RectTransformUtility.FlipLayoutOnAxis(dropdownRectTransform, 0, false, false);
                RectTransformUtility.FlipLayoutOnAxis(dropdownRectTransform, 1, false, false);
            }

            // Fade in the popup
            SetAlpha(0f);
            AlphaFadeList(0.15f, 1f);

            // Make drop-down template and item template inactive
            Template.gameObject.SetActive(false);
            itemPrefab.gameObject.SetActive(false);
            Destroy(itemPrefab);

            m_Blocker = CreateBlocker(m_RootCanvas);
        }
Example #8
0
 protected virtual void DestroyItem(PreciseManeuverDropdownItem item)
 {
 }
Example #9
0
 protected virtual PreciseManeuverDropdownItem CreateItem(PreciseManeuverDropdownItem itemTemplate)
 {
     return((PreciseManeuverDropdownItem)Instantiate(itemTemplate));
 }
Example #10
0
        public void Show()
        {
            if (!IsActive() || !IsInteractable() || m_Dropdown != null)
            {
                return;
            }



            // Get root Canvas.
            var    list       = gameObject.GetComponentsInParent <Canvas> (false);
            Canvas rootCanvas = list.LastOrDefault(c => c.isRootCanvas) ?? list.LastOrDefault();

            if (rootCanvas == null)
            {
                return;
            }


            if (!validTemplate)
            {
                SetupTemplate();
                if (!validTemplate)
                {
                    return;
                }
            }

            m_Template.gameObject.SetActive(true);

            // popupCanvas used to assume the root canvas had the default sorting Layer, next line fixes (case 958281 - [UI] Dropdown list does not copy the parent canvas layer when the panel is opened)
            m_Template.GetComponent <Canvas> ().sortingLayerID = rootCanvas.sortingLayerID;

            // Instantiate the drop-down template
            m_Dropdown      = CreateDropdownList(m_Template.gameObject);
            m_Dropdown.name = "Dropdown List";
            m_Dropdown.SetActive(true);

            // Make drop-down RectTransform have same values as original.
            RectTransform dropdownRectTransform = m_Dropdown.transform as RectTransform;

            dropdownRectTransform.SetParent(m_Template.transform.parent, false);

            // Instantiate the drop-down list items

            // Find the dropdown item and disable it.
            PreciseManeuverDropdownItem itemTemplate = m_Dropdown.GetComponentInChildren <PreciseManeuverDropdownItem>();

            GameObject    content = itemTemplate.rectTransform.parent.gameObject;
            RectTransform contentRectTransform = content.transform as RectTransform;

            itemTemplate.rectTransform.gameObject.SetActive(true);

            // Get the rects of the dropdown and item
            Rect dropdownContentRect = contentRectTransform.rect;
            Rect itemTemplateRect    = itemTemplate.rectTransform.rect;

            // Calculate the visual offset between the item's edges and the background's edges
            Vector2 offsetMin = itemTemplateRect.min - dropdownContentRect.min + (Vector2)itemTemplate.rectTransform.localPosition;
            Vector2 offsetMax = itemTemplateRect.max - dropdownContentRect.max + (Vector2)itemTemplate.rectTransform.localPosition;
            Vector2 itemSize  = itemTemplateRect.size;

            m_Items.Clear();

            Toggle prev = null;

            for (int i = 0; i < OptionsCount; ++i)
            {
                PreciseManeuverDropdownItem item = AddItem(Value == i, itemTemplate, m_Items.Count);
                if (item == null)
                {
                    continue;
                }

                // Automatically set up a toggle state change listener
                item.Toggle.isOn = Value == i;
                item.Toggle.onValueChanged.AddListener(x => OnSelectItem(item));

                // Select current option
                if (item.Toggle.isOn)
                {
                    item.Toggle.Select();
                }

                // Automatically set up explicit navigation
                if (prev != null)
                {
                    Navigation prevNav   = prev.navigation;
                    Navigation toggleNav = item.Toggle.navigation;
                    prevNav.mode   = Navigation.Mode.Explicit;
                    toggleNav.mode = Navigation.Mode.Explicit;

                    prevNav.selectOnDown   = item.Toggle;
                    prevNav.selectOnRight  = item.Toggle;
                    toggleNav.selectOnLeft = prev;
                    toggleNav.selectOnUp   = prev;

                    prev.navigation        = prevNav;
                    item.Toggle.navigation = toggleNav;
                }
                prev = item.Toggle;

                m_Items.Add(item);
            }

            // Reposition all items now that all of them have been added
            Vector2 sizeDelta = contentRectTransform.sizeDelta;

            sizeDelta.y = itemSize.y * m_Items.Count + offsetMin.y - offsetMax.y;
            contentRectTransform.sizeDelta = sizeDelta;

            float extraSpace = dropdownRectTransform.rect.height - contentRectTransform.rect.height;

            if (extraSpace > 0)
            {
                dropdownRectTransform.sizeDelta = new Vector2(dropdownRectTransform.sizeDelta.x, dropdownRectTransform.sizeDelta.y - extraSpace);
            }

            // Invert anchoring and position if dropdown is partially or fully outside of canvas rect.
            // Typically this will have the effect of placing the dropdown above the button instead of below,
            // but it works as inversion regardless of initial setup.
            Vector3[] corners = new Vector3[4];
            dropdownRectTransform.GetWorldCorners(corners);

            RectTransform rootCanvasRectTransform = rootCanvas.transform as RectTransform;
            Rect          rootCanvasRect          = rootCanvasRectTransform.rect;

            for (int axis = 0; axis < 2; axis++)
            {
                bool outside = false;
                for (int i = 0; i < 4; i++)
                {
                    Vector3 corner = rootCanvasRectTransform.InverseTransformPoint(corners[i]);
                    if ((corner[axis] < rootCanvasRect.min[axis] && !Mathf.Approximately(corner[axis], rootCanvasRect.min[axis])) ||
                        (corner[axis] > rootCanvasRect.max[axis] && !Mathf.Approximately(corner[axis], rootCanvasRect.max[axis])))
                    {
                        outside = true;
                        break;
                    }
                }
                if (outside)
                {
                    RectTransformUtility.FlipLayoutOnAxis(dropdownRectTransform, axis, false, false);
                }
            }

            for (int i = 0; i < m_Items.Count; i++)
            {
                RectTransform itemRect = m_Items[i].rectTransform;
                itemRect.anchorMin        = new Vector2(itemRect.anchorMin.x, 0);
                itemRect.anchorMax        = new Vector2(itemRect.anchorMax.x, 0);
                itemRect.anchoredPosition = new Vector2(itemRect.anchoredPosition.x, offsetMin.y + itemSize.y * (m_Items.Count - 1 - i) + itemSize.y * itemRect.pivot.y);
                itemRect.sizeDelta        = new Vector2(itemRect.sizeDelta.x, itemSize.y);
            }

            // Fade in the popup
            AlphaFadeList(m_AlphaFadeSpeed, 0f, 1f);

            // Make drop-down template and item template inactive
            m_Template.gameObject.SetActive(false);
            itemTemplate.gameObject.SetActive(false);

            m_Blocker = CreateBlocker(rootCanvas);
        }