Example #1
0
        public override GameObject Build()
        {
            var label = PUIElements.CreateUI(null, Name);

            // Background
            if (BackColor.a > 0)
            {
                label.AddComponent <Image>().color = BackColor;
            }
            // Add foreground image
            if (Sprite != null)
            {
                ImageChildHelper(label, this);
            }
            // Add text
            if (!string.IsNullOrEmpty(Text))
            {
                TextChildHelper(label, TextStyle ?? PUITuning.Fonts.UILightStyle, Text);
            }
            // Add tooltip
            if (!string.IsNullOrEmpty(ToolTip))
            {
                label.AddComponent <ToolTip>().toolTip = ToolTip;
            }
            label.SetActive(true);
            // Icon and text are side by side
            var lp = new BoxLayoutParams()
            {
                Spacing   = IconSpacing, Direction = PanelDirection.Horizontal, Margin = Margin,
                Alignment = TextAlignment
            };

            if (DynamicSize)
            {
                label.AddComponent <BoxLayoutGroup>().Params = lp;
            }
            else
            {
                BoxLayoutGroup.LayoutNow(label, lp);
            }
            label.SetFlexUISize(FlexSize);
            InvokeRealize(label);
            return(label);
        }
Example #2
0
        public GameObject Build()
        {
            var panel = PUIElements.CreateUI(Name);

            if (BackColor.a > 0.0f)
            {
                panel.AddComponent <Image>().color = BackColor;
            }
            panel.layer = LayerMask.NameToLayer("UI");
            // Add children
            foreach (var child in children)
            {
                PUIElements.SetParent(child.Build(), panel);
            }
            // Add layout component
            var args = new BoxLayoutParams()
            {
                Direction = Direction, Alignment = Alignment, Spacing = Spacing,
                Margin    = Margin
            };

            // Gotta love freezable layouts
            if (DynamicSize)
            {
                var lg = panel.AddComponent <BoxLayoutGroup>();
                lg.Params         = args;
                lg.flexibleWidth  = FlexSize.x;
                lg.flexibleHeight = FlexSize.y;
            }
            else
            {
                BoxLayoutGroup.LayoutNow(panel, args).SetFlexUISize(FlexSize);
            }
            OnRealize?.Invoke(panel);
            return(panel);
        }
Example #3
0
        public GameObject Build()
        {
            var textField = PUIElements.CreateUI(Name);
            // Background
            var style = TextStyle ?? PUITuning.Fonts.TextLightStyle;

            textField.AddComponent <Image>().color = style.textColor;
            // Text box with rectangular clipping area
            var textArea = PUIElements.CreateUI("Text Area", true, false);

            textArea.AddComponent <Image>().color = BackColor;
            PUIElements.SetParent(textArea, textField);
            var mask = textArea.AddComponent <RectMask2D>();
            // Scrollable text
            var textBox = PUIElements.CreateUI("Text");

            PUIElements.SetParent(textBox, textArea);
            // Text to display
            var textDisplay = textBox.AddComponent <TextMeshProUGUI>();

            textDisplay.alignment             = TextAlignment;
            textDisplay.autoSizeTextContainer = false;
            textDisplay.enabled         = true;
            textDisplay.color           = style.textColor;
            textDisplay.font            = style.sdfFont;
            textDisplay.fontSize        = style.fontSize;
            textDisplay.fontStyle       = style.style;
            textDisplay.maxVisibleLines = 1;
            // Text field itself
            var onChange = OnTextChanged;

            textField.SetActive(false);
            var textEntry = textField.AddComponent <TMP_InputField>();

            textEntry.textComponent = textDisplay;
            textEntry.textViewport  = textArea.rectTransform();
            textField.SetActive(true);
            textEntry.text   = Text ?? "";
            textDisplay.text = Text ?? "";
            ConfigureTextEntry(textEntry).onDeselect.AddListener((text) => {
                onChange?.Invoke(textField, (text ?? "").TrimEnd());
            });
            // Add tooltip
            if (!string.IsNullOrEmpty(ToolTip))
            {
                textField.AddComponent <ToolTip>().toolTip = ToolTip;
            }
            mask.enabled = true;
            // Lay out - TMP_InputField does not support auto layout so we have to do a hack
            var minSize = new Vector2(MinWidth, LayoutUtility.GetPreferredHeight(textBox.
                                                                                 rectTransform()));

            textArea.SetMinUISize(minSize).SetFlexUISize(Vector2.one);
            var lp = new BoxLayoutParams()
            {
                Direction = PanelDirection.Horizontal, Alignment = TextAnchor.MiddleLeft,
                Margin    = new RectOffset(1, 1, 1, 1)
            };

            if (DynamicSize)
            {
                var layout = textField.AddComponent <BoxLayoutGroup>();
                layout.Params         = lp;
                layout.flexibleWidth  = FlexSize.x;
                layout.flexibleHeight = FlexSize.y;
                textField.SetMinUISize(minSize);
            }
            else
            {
                BoxLayoutGroup.LayoutNow(textField, lp, new Vector2(MinWidth, 0.0f)).
                SetFlexUISize(FlexSize);
            }
            OnRealize?.Invoke(textField);
            return(textField);
        }
Example #4
0
        public override GameObject Build()
        {
            var button = PUIElements.CreateUI(null, Name);
            // Background
            var kImage    = button.AddComponent <KImage>();
            var trueColor = Color ?? PUITuning.Colors.ButtonPinkStyle;

            kImage.colorStyleSetting = trueColor;
            kImage.color             = trueColor.inactiveColor;
            kImage.sprite            = PUITuning.Images.ButtonBorder;
            kImage.type = Image.Type.Sliced;
            // Set on click event
            var kButton = button.AddComponent <KButton>();
            var evt     = OnClick;

            if (evt != null)
            {
                kButton.onClick += () => {
                    evt?.Invoke(button);
                }
            }
            ;
            kButton.additionalKImages = new KImage[0];
            kButton.soundPlayer       = PUITuning.ButtonSounds;
            kButton.bgImage           = kImage;
            // Add foreground image since the background already has one
            if (Sprite != null)
            {
                kButton.fgImage = ImageChildHelper(button, this);
            }
            // Set colors
            kButton.colorStyleSetting = trueColor;
            // Add text
            if (!string.IsNullOrEmpty(Text))
            {
                TextChildHelper(button, TextStyle ?? PUITuning.Fonts.UILightStyle, Text);
            }
            // Add tooltip
            if (!string.IsNullOrEmpty(ToolTip))
            {
                button.AddComponent <ToolTip>().toolTip = ToolTip;
            }
            button.SetActive(true);
            // Icon and text are side by side
            var lp = new BoxLayoutParams()
            {
                Spacing   = IconSpacing, Direction = PanelDirection.Horizontal, Margin = Margin,
                Alignment = TextAlignment
            };

            if (DynamicSize)
            {
                button.AddComponent <BoxLayoutGroup>().Params = lp;
            }
            else
            {
                BoxLayoutGroup.LayoutNow(button, lp);
            }
            button.SetFlexUISize(FlexSize);
            InvokeRealize(button);
            return(button);
        }
Example #5
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);
        }
Example #6
0
        public override GameObject Build()
        {
            var checkbox = PUIElements.CreateUI(null, Name);
            // Background
            var trueColor = CheckColor ?? PUITuning.Colors.ComponentLightStyle;
            // Checkbox background
            var checkBack = PUIElements.CreateUI(checkbox, "CheckBox");

            checkBack.AddComponent <Image>().color = BackColor;
            // Checkbox border
            var checkBorder = PUIElements.CreateUI(checkBack, "CheckBorder");
            var borderImg   = checkBorder.AddComponent <Image>();

            borderImg.sprite = PUITuning.Images.CheckBorder;
            borderImg.color  = trueColor.activeColor;
            borderImg.type   = Image.Type.Sliced;
            // Checkbox foreground
            var imageChild = PUIElements.CreateUI(checkBorder, "CheckMark", true, PUIAnchoring.
                                                  Center, PUIAnchoring.Center);
            var img = imageChild.AddComponent <Image>();

            img.sprite         = PUITuning.Images.Checked;
            img.preserveAspect = true;
            // Determine the checkbox size
            var actualSize = CheckSize;

            if (actualSize.x <= 0.0f || actualSize.y <= 0.0f)
            {
                var rt = imageChild.rectTransform();
                actualSize.x = LayoutUtility.GetPreferredWidth(rt);
                actualSize.y = LayoutUtility.GetPreferredHeight(rt);
            }
            imageChild.SetUISize(CheckSize, false);
            // Add foreground image since the background already has one
            if (Sprite != null)
            {
                ImageChildHelper(checkbox, this);
            }
            // Add text
            if (!string.IsNullOrEmpty(Text))
            {
                TextChildHelper(checkbox, TextStyle ?? PUITuning.Fonts.UILightStyle, Text);
            }
            // Add tooltip
            if (!string.IsNullOrEmpty(ToolTip))
            {
                checkbox.AddComponent <ToolTip>().toolTip = ToolTip;
            }
            // Toggle
            var mToggle = checkbox.AddComponent <MultiToggle>();
            var evt     = OnChecked;

            if (evt != null)
            {
                mToggle.onClick += () => {
                    evt?.Invoke(checkbox, mToggle.CurrentState);
                }
            }
            ;
            mToggle.play_sound_on_click   = true;
            mToggle.play_sound_on_release = false;
            mToggle.states       = GenerateStates(trueColor);
            mToggle.toggle_image = img;
            mToggle.ChangeState(InitialState);
            checkbox.SetActive(true);
            // Lay out the checkbox using anchors only
            checkBack.SetUISize(new Vector2(CheckSize.x + 2.0f * CHECKBOX_MARGIN, CheckSize.y +
                                            2.0f * CHECKBOX_MARGIN), true);
            imageChild.SetUISize(CheckSize);
            // Icon and text are side by side
            var lp = new BoxLayoutParams()
            {
                Margin = Margin, Spacing = Math.Max(IconSpacing, 0), Alignment = TextAnchor.
                                                                                 MiddleLeft
            };

            if (DynamicSize)
            {
                checkbox.AddComponent <BoxLayoutGroup>().Params = lp;
            }
            else
            {
                BoxLayoutGroup.LayoutNow(checkbox, lp);
            }
            checkbox.SetFlexUISize(FlexSize);
            InvokeRealize(checkbox);
            return(checkbox);
        }
Example #7
0
        public override GameObject Build()
        {
            var checkbox = PUIElements.CreateUI(Name);
            // Background
            var trueColor = CheckColor ?? PUITuning.Colors.CheckboxWhiteStyle;
            // Checkbox background
            var checkBack = PUIElements.CreateUI("CheckBox");

            checkBack.AddComponent <Image>().color = BackColor;
            PUIElements.SetParent(checkBack, checkbox);
            // Checkbox border
            var checkBorder = PUIElements.CreateUI("CheckBorder");
            var borderImg   = checkBorder.AddComponent <Image>();

            borderImg.sprite = PUITuning.Images.CheckBorder;
            borderImg.color  = trueColor.activeColor;
            borderImg.type   = Image.Type.Sliced;
            PUIElements.SetParent(checkBorder, checkBack);
            // Checkbox foreground
            var imageChild = PUIElements.CreateUI("CheckMark");
            var img        = imageChild.AddComponent <Image>();

            PUIElements.SetParent(imageChild, checkBorder);
            img.sprite         = PUITuning.Images.Checked;
            img.preserveAspect = true;
            // Limit size if needed
            if (CheckSize.x > 0.0f && CheckSize.y > 0.0f)
            {
                PUIElements.SetSizeImmediate(imageChild, CheckSize);
            }
            else
            {
                PUIElements.AddSizeFitter(imageChild, false);
            }
            // Add foreground image since the background already has one
            if (Sprite != null)
            {
                ImageChildHelper(checkbox, Sprite, SpriteTransform, SpriteSize);
            }
            // Add text
            if (!string.IsNullOrEmpty(Text))
            {
                TextChildHelper(checkbox, TextStyle ?? PUITuning.Fonts.UILightStyle, Text);
            }
            // Add tooltip
            if (!string.IsNullOrEmpty(ToolTip))
            {
                checkbox.AddComponent <ToolTip>().toolTip = ToolTip;
            }
            // Toggle
            var kButton = checkbox.AddComponent <MultiToggle>();
            var evt     = OnChecked;

            if (evt != null)
            {
                kButton.onClick += () => {
                    evt?.Invoke(checkbox, kButton.CurrentState);
                }
            }
            ;
            kButton.play_sound_on_click   = true;
            kButton.play_sound_on_release = false;
            kButton.states       = GenerateStates(trueColor, borderImg);
            kButton.toggle_image = img;
            kButton.ChangeState(InitialState);
            checkbox.SetActive(true);
            BoxLayoutGroup.LayoutNow(checkBorder, new BoxLayoutParams()
            {
                Margin = CHECKBOX_MARGIN
            });
            BoxLayoutGroup.LayoutNow(checkBack);
            // Icon and text are side by side
            var lp = new BoxLayoutParams()
            {
                Margin = Margin, Spacing = Math.Max(IconSpacing, 0), Alignment = TextAnchor.
                                                                                 MiddleLeft
            };

            if (DynamicSize)
            {
                checkbox.AddComponent <BoxLayoutGroup>().Params = lp;
            }
            else
            {
                BoxLayoutGroup.LayoutNow(checkbox, lp);
            }
            PUIElements.AddSizeFitter(checkbox, DynamicSize).SetFlexUISize(FlexSize);
            InvokeRealize(checkbox);
            return(checkbox);
        }