Beispiel #1
0
 private void _contextMenu_OnOptionSelected(object sender, int e)
 {
     _label.Text   = _items[e];
     SelectedIndex = e;
     Engine.UI.Remove <ComboboxContextMenu>();
     OnOptionSelected?.Invoke(this, e);
 }
Beispiel #2
0
        public GameObject Build()
        {
            var        combo = PUIElements.CreateUI(null, Name);
            var        style = TextStyle ?? PUITuning.Fonts.UILightStyle;
            var        entryColor = EntryColor ?? PUITuning.Colors.ButtonBlueStyle;
            RectOffset margin = Margin, im = ItemMargin;
            // Background color
            var bgImage        = combo.AddComponent <KImage>();
            var backColorStyle = BackColor ?? PUITuning.Colors.ButtonBlueStyle;

            UIDetours.COLOR_STYLE_SETTING.Set(bgImage, backColorStyle);
            PButton.SetupButtonBackground(bgImage);
            // Need a LocText (selected item)
            var selection = PUIElements.CreateUI(combo, "SelectedItem");

            if (MinWidth > 0)
            {
                selection.SetMinUISize(new Vector2(MinWidth, 0.0f));
            }
            var selectedLabel = PUIElements.AddLocText(selection, style);
            // Vertical flow panel with the choices
            var contentContainer = PUIElements.CreateUI(null, "Content");

            contentContainer.AddComponent <VerticalLayoutGroup>().childForceExpandWidth = true;
            // Scroll pane with items is laid out below everything else
            var pullDown = new PScrollPane("PullDown")
            {
                ScrollHorizontal = false, ScrollVertical = true, AlwaysShowVertical = true,
                FlexSize         = Vector2.right, TrackSize = 8.0f, BackColor = entryColor.
                                                                                inactiveColor
            }.BuildScrollPane(combo, contentContainer);

            // Add a black border (Does not work, covered by the combo box buttons...)
#if false
            var pdImage = pullDown.GetComponent <Image>();
            pdImage.sprite = PUITuning.Images.BoxBorder;
            pdImage.type   = Image.Type.Sliced;
#endif
            pullDown.rectTransform().pivot = new Vector2(0.5f, 1.0f);
            // Initialize the drop down
            var comboBox = combo.AddComponent <PComboBoxComponent>();
            comboBox.CheckColor       = style.textColor;
            comboBox.ContentContainer = contentContainer.rectTransform();
            comboBox.EntryPrefab      = BuildRowPrefab(style, entryColor);
            comboBox.MaxRowsShown     = MaxRowsShown;
            comboBox.Pulldown         = pullDown;
            comboBox.SelectedLabel    = selectedLabel;
            comboBox.SetItems(Content);
            comboBox.SetSelectedItem(InitialItem);
            comboBox.OnSelectionChanged = (obj, item) => OnOptionSelected?.Invoke(obj.
                                                                                  gameObject, item as T);
            // Inner component with the pulldown image
            var image = PUIElements.CreateUI(combo, "OpenImage");
            var icon  = image.AddComponent <Image>();
            icon.sprite = PUITuning.Images.Contract;
            icon.color  = style.textColor;
            // Button component
            var dropButton = combo.AddComponent <KButton>();
            PButton.SetupButton(dropButton, bgImage);
            UIDetours.FG_IMAGE.Set(dropButton, icon);
            dropButton.onClick += comboBox.OnClick;
            // Add tooltip
            PUIElements.SetToolTip(selection, ToolTip);
            combo.SetActive(true);
            // Button gets laid out on the right, rest of space goes to the label
            // Scroll pane is laid out on the bottom
            var layout = combo.AddComponent <RelativeLayoutGroup>();
            layout.AnchorYAxis(selection).SetLeftEdge(selection, fraction:
                                                      0.0f).SetRightEdge(selection, toLeft: image).AnchorYAxis(image).SetRightEdge(
                image, fraction: 1.0f).SetMargin(selection, new RectOffset(margin.left,
                                                                           im.right, margin.top, margin.bottom)).SetMargin(image, new RectOffset(0,
                                                                                                                                                 margin.right, margin.top, margin.bottom)).OverrideSize(image, ArrowSize).
            AnchorYAxis(pullDown, 0.0f).OverrideSize(pullDown, Vector2.up);
            layout.LockLayout();
            if (DynamicSize)
            {
                layout.UnlockLayout();
            }
            // Disable sizing on the pulldown
            pullDown.AddOrGet <LayoutElement>().ignoreLayout = true;
            // Scroll pane is hidden right away
            pullDown.SetActive(false);
            layout.flexibleWidth  = FlexSize.x;
            layout.flexibleHeight = FlexSize.y;
            OnRealize?.Invoke(combo);
            return(combo);
        }
Beispiel #3
0
    public IEnumerator DisplayOptionList(string[] options, OnOptionSelected onOptionSelected = null)
    {
        int index = 0;

        gameObject.SetActive(true);
        dialogOptionPanel.SetActive(true);

        foreach (Transform child in dialogOptionPanel.transform)
        {
            Destroy(child.gameObject);
        }

        for (int i = 0; i < options.Length; i++)
        {
            GameObject optionObj = GameObject.Instantiate(optionPrefab, dialogOptionPanel.transform);
            optionObj.GetComponentInChildren <Text>().text = options[i];
            if (i == 0)
            {
                optionObj.GetComponent <Image>().enabled = true;
            }
        }

        // Wait for the next frame to reset the Input
        yield return(null);

        int selectedIndex = -1;

        while (selectedIndex < 0)
        {
            if (Input.GetKeyUp("up"))
            {
                dialogOptionPanel.transform.GetChild(index).GetComponent <Image>().enabled = false;
                index = Mathf.Max(index - 1, 0);
                dialogOptionPanel.transform.GetChild(index).GetComponent <Image>().enabled = true;
            }
            else if (Input.GetKeyUp("down"))
            {
                dialogOptionPanel.transform.GetChild(index).GetComponent <Image>().enabled = false;
                index = Mathf.Min(index + 1, options.Length - 1);
                dialogOptionPanel.transform.GetChild(index).GetComponent <Image>().enabled = true;
            }
            else if (Input.GetKeyUp("space"))
            {
                selectedIndex = index;
            }
            else if (Input.GetKeyUp("escape"))
            {
                dialogOptionPanel.SetActive(false);
                gameObject.SetActive(false);
                yield break;
            }
            yield return(null);
        }

        dialogOptionPanel.SetActive(false);
        gameObject.SetActive(false);

        if (onOptionSelected != null)
        {
            onOptionSelected(options[index]);
        }
    }
Beispiel #4
0
 private void Label_MouseClick(object sender, MouseEventArgs e)
 {
     OnOptionSelected?.Invoke(this, (int)((Label)sender).Tag);
 }
 public void SendOptionsSelectedEvent(bool result)
 {
     this.result = result;
     eventFired  = true;
     OnOptionSelected.Invoke(this, result);
 }