상속: OpenRA.Widgets.Widget
예제 #1
0
        public override void DrawOuter()
        {
            if (!IsVisible())
            {
                return;
            }

            UpdateSmoothScrolling();

            var rb = RenderBounds;
            var scrollbarHeight = rb.Height - 2 * ScrollbarWidth;

            // Scroll thumb is only visible if the content does not fit within the panel bounds
            var thumbHeight = 0;
            var thumbOrigin = rb.Y + ScrollbarWidth;

            if (ContentHeight > rb.Height)
            {
                thumbHeight  = Math.Max(MinimumThumbSize, scrollbarHeight * rb.Height / ContentHeight);
                thumbOrigin += (int)((scrollbarHeight - thumbHeight) * currentListOffset / (rb.Height - ContentHeight));
            }

            switch (ScrollBar)
            {
            case ScrollBar.Left:
                backgroundRect = new Rectangle(rb.X + ScrollbarWidth, rb.Y, rb.Width + 1, rb.Height);
                upButtonRect   = new Rectangle(rb.X, rb.Y, ScrollbarWidth, ScrollbarWidth);
                downButtonRect = new Rectangle(rb.X, rb.Bottom - ScrollbarWidth, ScrollbarWidth, ScrollbarWidth);
                scrollbarRect  = new Rectangle(rb.X, rb.Y + ScrollbarWidth - 1, ScrollbarWidth, scrollbarHeight + 2);
                thumbRect      = new Rectangle(rb.X, thumbOrigin, ScrollbarWidth, thumbHeight);
                break;

            case ScrollBar.Right:
                backgroundRect = new Rectangle(rb.X, rb.Y, rb.Width - ScrollbarWidth + 1, rb.Height);
                upButtonRect   = new Rectangle(rb.Right - ScrollbarWidth, rb.Y, ScrollbarWidth, ScrollbarWidth);
                downButtonRect = new Rectangle(rb.Right - ScrollbarWidth, rb.Bottom - ScrollbarWidth, ScrollbarWidth, ScrollbarWidth);
                scrollbarRect  = new Rectangle(rb.Right - ScrollbarWidth, rb.Y + ScrollbarWidth - 1, ScrollbarWidth, scrollbarHeight + 2);
                thumbRect      = new Rectangle(rb.Right - ScrollbarWidth, thumbOrigin, ScrollbarWidth, thumbHeight);
                break;

            case ScrollBar.Hidden:
                backgroundRect = new Rectangle(rb.X, rb.Y, rb.Width + 1, rb.Height);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            WidgetUtils.DrawPanel(Background, backgroundRect);

            if (ScrollBar != ScrollBar.Hidden)
            {
                var upHover = Ui.MouseOverWidget == this && upButtonRect.Contains(Viewport.LastMousePos);
                upDisabled = thumbHeight == 0 || currentListOffset >= 0;

                var downHover = Ui.MouseOverWidget == this && downButtonRect.Contains(Viewport.LastMousePos);
                downDisabled = thumbHeight == 0 || currentListOffset <= Bounds.Height - ContentHeight;

                var thumbHover = Ui.MouseOverWidget == this && thumbRect.Contains(Viewport.LastMousePos);
                WidgetUtils.DrawPanel(ScrollBarBackground, scrollbarRect);
                ButtonWidget.DrawBackground(Button, upButtonRect, upDisabled, upPressed, upHover, false);
                ButtonWidget.DrawBackground(Button, downButtonRect, downDisabled, downPressed, downHover, false);

                if (thumbHeight > 0)
                {
                    ButtonWidget.DrawBackground(Button, thumbRect, false, HasMouseFocus && thumbHover, thumbHover, false);
                }

                var upOffset   = !upPressed || upDisabled ? 4 : 4 + ButtonDepth;
                var downOffset = !downPressed || downDisabled ? 4 : 4 + ButtonDepth;

                var upArrowImageName = WidgetUtils.GetStatefulImageName(DecorationScrollUp, upDisabled, upPressed, upHover);
                var upArrowImage     = ChromeProvider.GetImage(Decorations, upArrowImageName) ?? ChromeProvider.GetImage(Decorations, DecorationScrollUp);
                WidgetUtils.DrawRGBA(upArrowImage,
                                     new float2(upButtonRect.Left + upOffset, upButtonRect.Top + upOffset));

                var downArrowImageName = WidgetUtils.GetStatefulImageName(DecorationScrollDown, downDisabled, downPressed, downHover);
                var downArrowImage     = ChromeProvider.GetImage(Decorations, downArrowImageName) ?? ChromeProvider.GetImage(Decorations, DecorationScrollDown);
                WidgetUtils.DrawRGBA(downArrowImage,
                                     new float2(downButtonRect.Left + downOffset, downButtonRect.Top + downOffset));
            }

            var drawBounds = backgroundRect.InflateBy(-BorderWidth, -BorderWidth, -BorderWidth, -BorderWidth);

            Game.Renderer.EnableScissor(drawBounds);

            // ChildOrigin enumerates the widget tree, so only evaluate it once
            var co = ChildOrigin;

            drawBounds.X -= co.X;
            drawBounds.Y -= co.Y;

            foreach (var child in Children)
            {
                if (child.Bounds.IntersectsWith(drawBounds))
                {
                    child.DrawOuter();
                }
            }

            Game.Renderer.DisableScissor();
        }
예제 #2
0
        public static void BindButtonIcon(ButtonWidget button)
        {
            var icon = button.Get <ImageWidget>("ICON");

            var hasActiveImage         = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName + "-active") != null;
            var hasActiveDisabledImage = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName + "-active-disabled") != null;
            var hasActivePressedImage  = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName + "-active-pressed") != null;
            var hasActiveHoverImage    = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName + "-active-hover") != null;

            var hasImage         = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName) != null;
            var hasDisabledImage = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName + "-disabled") != null;
            var hasPressedImage  = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName + "-pressed") != null;
            var hasHoverImage    = ChromeProvider.GetImage(icon.ImageCollection, icon.ImageName + "-hover") != null;

            icon.GetImageName = () =>
            {
                var isActive   = button.IsHighlighted();
                var isDisabled = button.IsDisabled();
                var isPressed  = button.Depressed;
                var isHovered  = Ui.MouseOverWidget == button;

                var baseName  = button.IsHighlighted() ? icon.ImageName + "-active" : icon.ImageName;
                var stateName = WidgetUtils.GetStatefulImageName(baseName, isDisabled, isPressed, isHovered);

                if (isActive)
                {
                    if (isDisabled)
                    {
                        return(hasActiveDisabledImage ? stateName : hasActiveImage?baseName : icon.ImageName);
                    }
                    else if (isPressed)
                    {
                        return(hasActivePressedImage ? stateName : hasActiveImage?baseName : icon.ImageName);
                    }
                    else if (isHovered)
                    {
                        return(hasActiveHoverImage ? stateName : hasActiveImage?baseName : icon.ImageName);
                    }
                    else
                    {
                        return(hasActiveImage ? baseName : icon.ImageName);
                    }
                }
                else
                {
                    if (isDisabled)
                    {
                        return(hasDisabledImage ? stateName : baseName);
                    }
                    else if (isPressed)
                    {
                        return(hasPressedImage ? stateName : baseName);
                    }
                    else if (isHovered)
                    {
                        return(hasHoverImage ? stateName : baseName);
                    }
                    else
                    {
                        return(baseName);
                    }
                }
            };
        }
예제 #3
0
        void BindButtonIcon(ButtonWidget button)
        {
            var icon = button.Get <ImageWidget>("ICON");

            icon.GetImageName = () => button.IsDisabled() ? icon.ImageName + "-disabled" : icon.ImageName;
        }
예제 #4
0
        public override void DrawOuter()
        {
            if (!IsVisible())
            {
                return;
            }

            var rb = RenderBounds;

            var scrollbarHeight = rb.Height - 2 * ScrollbarWidth;

            var thumbHeight = ContentHeight == 0 ? 0 : Math.Max(MinimumThumbSize, (int)(scrollbarHeight * Math.Min(rb.Height * 1f / ContentHeight, 1f)));
            var thumbOrigin = rb.Y + ScrollbarWidth + (int)((scrollbarHeight - thumbHeight) * (-1f * currentListOffset / (ContentHeight - rb.Height)));

            if (thumbHeight == scrollbarHeight)
            {
                thumbHeight = 0;
            }

            backgroundRect = new Rectangle(rb.X, rb.Y, rb.Width - ScrollbarWidth + 1, rb.Height);
            upButtonRect   = new Rectangle(rb.Right - ScrollbarWidth, rb.Y, ScrollbarWidth, ScrollbarWidth);
            downButtonRect = new Rectangle(rb.Right - ScrollbarWidth, rb.Bottom - ScrollbarWidth, ScrollbarWidth, ScrollbarWidth);
            scrollbarRect  = new Rectangle(rb.Right - ScrollbarWidth, rb.Y + ScrollbarWidth - 1, ScrollbarWidth, scrollbarHeight + 2);
            thumbRect      = new Rectangle(rb.Right - ScrollbarWidth, thumbOrigin, ScrollbarWidth, thumbHeight);

            var upHover = Ui.MouseOverWidget == this && upButtonRect.Contains(Viewport.LastMousePos);

            upDisabled = thumbHeight == 0 || currentListOffset >= 0;

            var downHover = Ui.MouseOverWidget == this && downButtonRect.Contains(Viewport.LastMousePos);

            downDisabled = thumbHeight == 0 || currentListOffset <= Bounds.Height - ContentHeight;

            var thumbHover = Ui.MouseOverWidget == this && thumbRect.Contains(Viewport.LastMousePos);

            WidgetUtils.DrawPanel(Background, backgroundRect);
            WidgetUtils.DrawPanel(Background, scrollbarRect);
            ButtonWidget.DrawBackground(Button, upButtonRect, upDisabled, upPressed, upHover, false);
            ButtonWidget.DrawBackground(Button, downButtonRect, downDisabled, downPressed, downHover, false);

            if (thumbHeight > 0)
            {
                ButtonWidget.DrawBackground(Button, thumbRect, false, HasMouseFocus && thumbHover, thumbHover, false);
            }

            var upOffset   = !upPressed || upDisabled ? 4 : 4 + ButtonDepth;
            var downOffset = !downPressed || downDisabled ? 4 : 4 + ButtonDepth;

            WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", upPressed || upDisabled ? "up_pressed" : "up_arrow"),
                                 new float2(upButtonRect.Left + upOffset, upButtonRect.Top + upOffset));
            WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", downPressed || downDisabled ? "down_pressed" : "down_arrow"),
                                 new float2(downButtonRect.Left + downOffset, downButtonRect.Top + downOffset));

            var drawBounds = backgroundRect.InflateBy(-1, -1, -1, -1);

            Game.Renderer.EnableScissor(drawBounds);

            drawBounds.Offset((-ChildOrigin).ToPoint());
            foreach (var child in Children)
            {
                if (child.Bounds.IntersectsWith(drawBounds))
                {
                    child.DrawOuter();
                }
            }

            Game.Renderer.DisableScissor();
        }
예제 #5
0
        public static void TextInputPrompt(
            string title, string prompt, string initialText,
            Action <string> onAccept, Action onCancel = null,
            string acceptText = null, string cancelText = null,
            Func <string, bool> inputValidator = null)
        {
            var          panel = Ui.OpenWindow("TEXT_INPUT_PROMPT");
            Func <bool>  doValidate = null;
            ButtonWidget acceptButton = null, cancelButton = null;

            //
            // Title
            //
            panel.Get <LabelWidget>("PROMPT_TITLE").GetText = () => title;

            //
            // Prompt
            //
            panel.Get <LabelWidget>("PROMPT_TEXT").GetText = () => prompt;

            //
            // Text input
            //
            var input = panel.Get <TextFieldWidget>("INPUT_TEXT");
            var isValid = false;

            input.Text       = initialText;
            input.IsValid    = () => isValid;
            input.OnEnterKey = () =>
            {
                if (acceptButton.IsDisabled())
                {
                    return(false);
                }

                acceptButton.OnClick();
                return(true);
            };
            input.OnEscKey = () =>
            {
                if (cancelButton.IsDisabled())
                {
                    return(false);
                }

                cancelButton.OnClick();
                return(true);
            };
            input.TakeKeyboardFocus();
            input.CursorPosition = input.Text.Length;
            input.OnTextEdited   = () => doValidate();

            //
            // Buttons
            //
            acceptButton = panel.Get <ButtonWidget>("ACCEPT_BUTTON");
            if (!string.IsNullOrEmpty(acceptText))
            {
                acceptButton.GetText = () => acceptText;
            }

            acceptButton.OnClick = () =>
            {
                if (!doValidate())
                {
                    return;
                }

                Ui.CloseWindow();
                onAccept(input.Text);
            };

            cancelButton = panel.Get <ButtonWidget>("CANCEL_BUTTON");
            if (!string.IsNullOrEmpty(cancelText))
            {
                cancelButton.GetText = () => cancelText;
            }

            cancelButton.OnClick = () =>
            {
                Ui.CloseWindow();
                if (onCancel != null)
                {
                    onCancel();
                }
            };

            //
            // Validation
            //
            doValidate = () =>
            {
                if (inputValidator == null)
                {
                    return(true);
                }

                isValid = inputValidator(input.Text);
                if (isValid)
                {
                    acceptButton.Disabled = false;
                    return(true);
                }

                acceptButton.Disabled = true;
                return(false);
            };

            doValidate();
        }