public GUIDropDown(RectTransform rectT, string text = "", int elementCount = 4, string style = "", bool selectMultiple = false, bool dropAbove = false) : base(style, rectT)
        {
            HoverCursor  = CursorState.Hand;
            CanBeFocused = true;

            this.selectMultiple = selectMultiple;

            button = new GUIButton(new RectTransform(Vector2.One, rectT), text, Alignment.CenterLeft, style: "GUIDropDown")
            {
                OnClicked = OnClicked
            };
            GUI.Style.Apply(button, "", this);
            button.TextBlock.SetTextPos();

            Anchor listAnchor = dropAbove ? Anchor.TopCenter : Anchor.BottomCenter;
            Pivot  listPivot  = dropAbove ? Pivot.BottomCenter : Pivot.TopCenter;

            listBox = new GUIListBox(new RectTransform(new Point(Rect.Width, Rect.Height * MathHelper.Clamp(elementCount, 2, 10)), rectT, listAnchor, listPivot)
            {
                IsFixedSize = false
            }, style: null)
            {
                Enabled = !selectMultiple
            };
            if (!selectMultiple)
            {
                listBox.OnSelected = SelectItem;
            }
            GUI.Style.Apply(listBox, "GUIListBox", this);
            GUI.Style.Apply(listBox.ContentBackground, "GUIListBox", this);

            if (button.Style.ChildStyles.ContainsKey("dropdownicon"))
            {
                icon = new GUIImage(new RectTransform(new Vector2(0.6f, 0.6f), button.RectTransform, Anchor.CenterRight, scaleBasis: ScaleBasis.BothHeight)
                {
                    AbsoluteOffset = new Point(5, 0)
                }, null, scaleToFit: true);
                icon.ApplyStyle(button.Style.ChildStyles["dropdownicon"]);
            }

            currentHighestParent = FindHighestParent();
            currentHighestParent.GUIComponent.OnAddedToGUIUpdateList += AddListBoxToGUIUpdateList;
            rectT.ParentChanged += (RectTransform newParent) =>
            {
                currentHighestParent.GUIComponent.OnAddedToGUIUpdateList -= AddListBoxToGUIUpdateList;
                if (newParent != null)
                {
                    currentHighestParent = FindHighestParent();
                    currentHighestParent.GUIComponent.OnAddedToGUIUpdateList += AddListBoxToGUIUpdateList;
                }
            };
        }
Esempio n. 2
0
        public GUITextBox(RectTransform rectT, string text = "", Color?textColor       = null, ScalableFont font = null,
                          Alignment textAlignment          = Alignment.Left, bool wrap = false, string style     = "", Color?color = null, bool createClearButton = false)
            : base(style, rectT)
        {
            HoverCursor  = CursorState.IBeam;
            CanBeFocused = true;

            this.color = color ?? Color.White;
            frame      = new GUIFrame(new RectTransform(Vector2.One, rectT, Anchor.Center), style, color);
            GUI.Style.Apply(frame, style == "" ? "GUITextBox" : style);
            textBlock = new GUITextBlock(new RectTransform(Vector2.One, frame.RectTransform, Anchor.CenterLeft), text, textColor, font, textAlignment, wrap, playerInput: true);
            GUI.Style.Apply(textBlock, "", this);
            CaretEnabled  = true;
            caretPosDirty = true;

            new GUICustomComponent(new RectTransform(Vector2.One, frame.RectTransform), onDraw: DrawCaretAndSelection);

            int clearButtonWidth = 0;

            if (createClearButton)
            {
                var clearButton = new GUIButton(new RectTransform(new Vector2(0.6f, 0.6f), frame.RectTransform, Anchor.CenterRight, scaleBasis: ScaleBasis.BothHeight)
                {
                    AbsoluteOffset = new Point(5, 0)
                }, style: "GUICancelButton")
                {
                    OnClicked = (bt, userdata) =>
                    {
                        Text = "";
                        frame.Flash(Color.White);
                        return(true);
                    }
                };
                textBlock.RectTransform.MaxSize = new Point(frame.Rect.Width - clearButton.Rect.Height - clearButton.RectTransform.AbsoluteOffset.X * 2, int.MaxValue);
                clearButtonWidth = (int)(clearButton.Rect.Width * 1.2f);
            }

            if (this.style != null && this.style.ChildStyles.ContainsKey("textboxicon"))
            {
                icon = new GUIImage(new RectTransform(new Vector2(0.6f, 0.6f), frame.RectTransform, Anchor.CenterRight, scaleBasis: ScaleBasis.BothHeight)
                {
                    AbsoluteOffset = new Point(5 + clearButtonWidth, 0)
                }, null, scaleToFit: true);
                icon.ApplyStyle(this.style.ChildStyles["textboxicon"]);
                textBlock.RectTransform.MaxSize = new Point(frame.Rect.Width - icon.Rect.Height - clearButtonWidth - icon.RectTransform.AbsoluteOffset.X * 2, int.MaxValue);
            }
            Font = textBlock.Font;

            Enabled = true;

            rectT.SizeChanged += () =>
            {
                if (icon != null)
                {
                    textBlock.RectTransform.MaxSize = new Point(frame.Rect.Width - icon.Rect.Height - icon.RectTransform.AbsoluteOffset.X * 2, int.MaxValue);
                }
                caretPosDirty = true;
            };
            rectT.ScaleChanged += () =>
            {
                if (icon != null)
                {
                    textBlock.RectTransform.MaxSize = new Point(frame.Rect.Width - icon.Rect.Height - icon.RectTransform.AbsoluteOffset.X * 2, int.MaxValue);
                }
                caretPosDirty = true;
            };
        }