Exemple #1
0
        /// <summary>
        /// Creates the user buttons.
        /// </summary>
        /// <param name="layout">The location to add the buttons.</param>
        /// <param name="onPressed">The handler to call when any button is pressed.</param>
        private void CreateUserButtons(PGridLayoutGroup layout,
                                       PUIDelegates.OnButtonPressed onPressed)
        {
            var buttonPanel = new PPanel("Buttons")
            {
                Alignment = TextAnchor.LowerCenter, Spacing = 7, Direction = PanelDirection.
                                                                             Horizontal, Margin = new RectOffset(5, 5, 0, 10)
            };
            int i = 0;

            // Add each user button
            foreach (var button in buttons)
            {
                string key     = button.key;
                var    bgColor = button.backColor;
                var    fgColor = button.textColor ?? PUITuning.Fonts.UILightStyle;
                var    db      = new PButton(key)
                {
                    Text    = button.text, ToolTip = button.tooltip, Margin = BUTTON_MARGIN,
                    OnClick = onPressed, Color = bgColor, TextStyle = fgColor
                };
                // Last button is special and gets a pink color
                if (bgColor == null)
                {
                    if (++i >= buttons.Count)
                    {
                        db.SetKleiPinkStyle();
                    }
                    else
                    {
                        db.SetKleiBlueStyle();
                    }
                }
                buttonPanel.AddChild(db);
            }
            layout.AddComponent(buttonPanel.Build(), new GridComponentSpec(2, 0)
            {
                ColumnSpan = 2
            });
        }
Exemple #2
0
        /// <summary>
        /// Builds a row selection prefab object for this combo box.
        /// </summary>
        /// <param name="style">The text style for the entries.</param>
        /// <param name="entryColor">The color for the entry backgrounds.</param>
        /// <returns>A template for each row in the dropdown.</returns>
        private GameObject BuildRowPrefab(TextStyleSetting style, ColorStyleSetting entryColor)
        {
            var im        = ItemMargin;
            var rowPrefab = PUIElements.CreateUI(null, "RowEntry");
            // Background of the entry
            var bgImage = rowPrefab.AddComponent <KImage>();

            UIDetours.COLOR_STYLE_SETTING.Set(bgImage, entryColor);
            UIDetours.APPLY_COLOR_STYLE.Invoke(bgImage);
            // Checkmark for the front of the entry
            var isSelected = PUIElements.CreateUI(rowPrefab, "Selected");
            var fgImage    = isSelected.AddComponent <Image>();

            fgImage.color          = style.textColor;
            fgImage.preserveAspect = true;
            fgImage.sprite         = PUITuning.Images.Checked;
            // Button for the entry to select it
            var entryButton = rowPrefab.AddComponent <KButton>();

            PButton.SetupButton(entryButton, bgImage);
            UIDetours.FG_IMAGE.Set(entryButton, fgImage);
            // Tooltip for the entry
            rowPrefab.AddComponent <ToolTip>();
            // Text for the entry
            var textContainer = PUIElements.CreateUI(rowPrefab, "Text");

            PUIElements.AddLocText(textContainer, style).SetText(" ");
            // Configure the entire layout in 1 statement! (jk this is awful)
            var group = rowPrefab.AddComponent <RelativeLayoutGroup>();

            group.AnchorYAxis(isSelected).OverrideSize(isSelected, CheckSize).SetLeftEdge(
                isSelected, fraction: 0.0f).SetMargin(isSelected, im).AnchorYAxis(
                textContainer).SetLeftEdge(textContainer, toRight: isSelected).SetRightEdge(
                textContainer, 1.0f).SetMargin(textContainer, new RectOffset(0, im.right,
                                                                             im.top, im.bottom)).LockLayout();
            rowPrefab.SetActive(false);
            return(rowPrefab);
        }
Exemple #3
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);
        }
Exemple #4
0
        public GameObject Build()
        {
            var flexW = new Vector2(1.0f, 0.0f);

            if (Parent == null)
            {
                throw new InvalidOperationException("Parent for dialog may not be null");
            }
            var dialog     = PUIElements.CreateUI(null, Name);
            var dComponent = dialog.AddComponent <PDialogComp>();
            int i          = 0;

            PUIElements.SetParent(dialog, Parent);
            // Background
            dialog.AddComponent <Image>().color = PUITuning.Colors.DialogBackground;
            dialog.AddComponent <Canvas>();
            new PPanel("Header")
            {
                // Horizontal title bar
                Spacing = 3, Direction = PanelDirection.Horizontal, FlexSize = flexW
            }.SetKleiPinkColor().AddChild(new PLabel("Title")
            {
                // Title text, expand to width
                Text = Title, FlexSize = flexW, DynamicSize = true
            }).AddChild(new PButton(DIALOG_KEY_CLOSE)
            {
                // Close button
                Sprite     = PUITuning.Images.Close, Margin = new RectOffset(3, 3, 3, 3),
                SpriteSize = new Vector2f(16.0f, 16.0f), OnClick = dComponent.DoButton
            }.SetKleiBlueStyle()).AddTo(dialog);
            // Buttons
            var buttonPanel = new PPanel("Buttons")
            {
                Alignment = TextAnchor.LowerCenter, Spacing = 5, Direction = PanelDirection.
                                                                             Horizontal, Margin = new RectOffset(5, 5, 5, 5)
            };

            // Add each user button
            foreach (var button in buttons)
            {
                string key = button.key;
                var    db  = new PButton(key)
                {
                    Text    = button.text, ToolTip = button.tooltip, Margin = BUTTON_MARGIN,
                    OnClick = dComponent.DoButton
                };
                // Last button is special and gets a pink color
                if (++i >= buttons.Count)
                {
                    db.SetKleiPinkStyle();
                }
                else
                {
                    db.SetKleiBlueStyle();
                }
                buttonPanel.AddChild(db);
            }
            // Body, make it fill the flexible space
            new PPanel("BodyAndButtons")
            {
                Alignment = TextAnchor.MiddleCenter, Spacing = 5, Direction = PanelDirection.
                                                                              Vertical, Margin = new RectOffset(10, 10, 10, 5), FlexSize = Vector2.one,
                BackColor = DialogBackColor
            }.AddChild(Body).AddChild(buttonPanel).AddTo(dialog);
            // Lay out components vertically
            BoxLayoutGroup.LayoutNow(dialog, new BoxLayoutParams()
            {
                Alignment = TextAnchor.UpperCenter, Margin = new RectOffset(1, 1, 1, 1),
                Spacing   = 1.0f, Direction = PanelDirection.Vertical
            }, Size);
            dialog.AddComponent <GraphicRaycaster>();
            dComponent.dialog  = this;
            dComponent.sortKey = SortKey;
            OnRealize?.Invoke(dialog);
            return(dialog);
        }