public void DrawBackground(IUserInterfaceRenderContext renderContext, RenderElementDisplay display, Rectangle clientDest)
        {
            if (display.Style.Background == null)
            {
                return;
            }

            // This draws the background behind the border - a problem for borders which have transparency on their outer edges
            // but important for borders which have transparency on their inner edges. Oh what to do...
            //Rectangle backgroundRect = new Rectangle(
            //                                new Point(dest.X - display.Region.BorderToContentOffset.Left,
            //                                    dest.Y - display.Region.BorderToContentOffset.Top),
            //                                display.Region.BorderRect.Size);

            // Here we make a compromise and set the background rectangle to be slightly smaller than
            // the border rectangle
            var borderLayout = display.Style.Border.ToLayoutBox();

            Rectangle backgroundRect = new Rectangle(
                clientDest.X - display.Region.BorderToContentOffset.Left + borderLayout.Left / 2,
                clientDest.Y - display.Region.BorderToContentOffset.Top + borderLayout.Top / 2,
                display.BorderRect.Width - borderLayout.Width / 2,
                display.BorderRect.Height - borderLayout.Height / 2);

            styleRenderer.DrawBackground(
                renderContext.SpriteBatch,
                display.Style.Background,
                backgroundRect);
        }
Пример #2
0
        public void CompleteRendering(Rectangle parentContentDest,
                                      IUserInterfaceRenderContext renderContext, IRenderElement widget)
        {
            var animation = widget.Display.Animation;
            var display   = widget.Display;

            display.Animation.Buffer.RenderContext.SpriteBatch.End();

            // Now use the parent render target.
            renderContext.GraphicsDevice.SetRenderTarget(renderContext.RenderTarget);

            var screenRect = animation.AnimatedBorderRect;

            screenRect.X += parentContentDest.X;
            screenRect.Y += parentContentDest.Y;

            renderContext.SpriteBatch.Draw(
                animation.RenderTarget,
                screenRect,
                null,
                animation.Color,
                0,
                Vector2.Zero,
                SpriteEffects.None,
                animation.LayerDepth);
        }
Пример #3
0
        public override void DoLayout(IUserInterfaceRenderContext renderContext, Size size)
        {
            int desty = 0;

            for (int y = 0; y < Rows; y++)
            {
                int destx = 0;

                for (int x = 0; x < Columns; x++)
                {
                    var item = ChildAt(x, y);

                    if (item == null)
                    {
                        destx += columnWidths[x];
                        continue;
                    }

                    item.Display.ContentRect = new Rectangle(
                        destx + item.Display.Region.MarginToContentOffset.Left,
                        desty + item.Display.Region.MarginToContentOffset.Top,
                        SizeAt(x, y).Width,
                        SizeAt(x, y).Height);

                    destx += columnWidths[x];
                }

                desty += rowHeights[y];
            }

            Width  = columnWidths.Sum();
            Height = rowHeights.Sum();
        }
Пример #4
0
        private bool AnimateExit(RenderElementDisplay display, IUserInterfaceRenderContext renderContext, RenderElementAnimator animation)
        {
            animation.Alpha -=
                (float)renderContext.GameTime.ElapsedGameTime.TotalSeconds
                / transitionTime;

            if (animation.Alpha <= 0)
            {
                animation.Alpha     = 0;
                animation.IsVisible = false;
                return(true);
            }

            const float shrinkMax = 0.1f;
            float       shrink    = shrinkMax * MathF.Pow(1 - animation.Alpha, 0.8f);

            float leftRightMargin = shrink * display.BorderRect.Width;
            float topBottomMargin = shrink * display.BorderRect.Height;

            animation.AnimatedBorderRect = new Rectangle(
                display.BorderRect.X + (int)leftRightMargin,
                display.BorderRect.Y + (int)leftRightMargin,
                display.BorderRect.Width - (int)(2 * leftRightMargin),
                display.BorderRect.Height - (int)(2 * leftRightMargin));

            return(false);
        }
Пример #5
0
        private void RefreshContent(IUserInterfaceRenderContext renderContext, int maxWidth)
        {
            if (!dirty && content != null)
            {
                if (maxWidth > content.Size.Width)
                {
                    return;
                }

                dirty = true;
            }

            if (dirty || lastContentMaxWidth != maxWidth)
            {
                if (Props.Text == null)
                {
                    content = null;
                }
                else
                {
                    layoutOptions.Font     = Style.Font;
                    layoutOptions.MaxWidth = maxWidth;

                    content = renderContext.CreateContentLayout(Props.Text, layoutOptions, Props.PerformLocalization);
                    content.Options.ReadSlowly = Props.ReadSlowly;

                    content.AnimationComplete += () => Props.AnimationComplete?.Invoke(evt.Reset(this));
                }

                lastContentMaxWidth = maxWidth;
                dirty = false;
            }
        }
Пример #6
0
        private bool AnimateEntry(RenderElementDisplay display, IUserInterfaceRenderContext renderContext, RenderElementAnimator animation)
        {
            animation.Alpha +=
                (float)renderContext.GameTime.ElapsedGameTime.TotalSeconds
                / transitionTime;

            if (animation.Alpha >= 1)
            {
                animation.Alpha = 1;
                animation.AnimatedBorderRect = display.BorderRect;

                return(true);
            }

            const float shrinkMax = 0.1f;
            float       shrink    = shrinkMax * MathF.Pow(1 - animation.Alpha, 3);

            float leftRightMargin = shrink * display.BorderRect.Width;
            float topBottomMargin = shrink * display.BorderRect.Height;

            animation.AnimatedBorderRect = new Rectangle(
                display.BorderRect.X + (int)leftRightMargin,
                display.BorderRect.Y + (int)leftRightMargin,
                display.BorderRect.Width - (int)(2 * leftRightMargin),
                display.BorderRect.Height - (int)(2 * leftRightMargin));

            Log.Debug($"Alpha: {animation.Alpha} BorderRect: {display.BorderRect} AnimatedBorderRect: {animation.AnimatedBorderRect}");

            return(false);
        }
Пример #7
0
            public Size CalcIdealSize(IUserInterfaceRenderContext renderContext, Size maxSize, IList <IRenderElement> children)
            {
                int idealCrossSize = 0;
                int idealAxisSize  = 0;

                foreach (var item in children)
                {
                    if (!item.Display.IsVisible)
                    {
                        continue;
                    }

                    var itemBox = item.Display.Region.MarginToContentOffset;

                    var itemMaxSize = LayoutMath.ItemContentMaxSize(itemBox, maxSize);

                    item.Display.Region.IdealContentSize
                        = item.CalcIdealContentSize(renderContext, maxSize);

                    var itemIdealContentSize = item.Display.Region.CalcConstrainedContentSize(maxSize);
                    var itemIdealMarginSize  = itemBox.Expand(itemIdealContentSize);

                    idealCrossSize = Math.Max(idealCrossSize, CrossAxis(itemIdealMarginSize));
                    idealAxisSize += MainAxis(itemIdealMarginSize);
                }

                return(SetSize(idealAxisSize, idealCrossSize));
            }
Пример #8
0
        public void Draw(IUserInterfaceRenderContext renderContext)
        {
            contentLayoutOptions.Font     = Font;
            contentLayoutOptions.MaxWidth = renderContext.Area.Width;

            var validItems = items.Where(kvp => kvp.Value.Visible &&
                                         !string.IsNullOrWhiteSpace(kvp.Value.Text));

            int totalWidth = 0;
            int count      = 0;

            foreach (var itemKvp in validItems)
            {
                var item = itemKvp.Value;

                InitializeContent(renderContext, itemKvp.Key, item);

                totalWidth += item.Content.Size.Width + item.ButtonContent.Size.Width;
                count++;
            }

            totalWidth += (count - 1) * Margin;

            int x = (renderContext.Area.Width - totalWidth) / 2;

            foreach (var itemKvp in validItems)
            {
                DrawContent(renderContext, itemKvp.Value.ButtonContent, ref x);
                DrawContent(renderContext, itemKvp.Value.Content, ref x);

                x += Margin;
            }
        }
Пример #9
0
        private static void DrawContent(IUserInterfaceRenderContext renderContext, IContentLayout content, ref int x)
        {
            content.Draw(
                new Vector2(x, renderContext.Area.Height - content.Size.Height),
                renderContext.SpriteBatch);

            x += content.Size.Width;
        }
Пример #10
0
        public UIContext()
        {
            renderContext = CommonMocks.RenderContext().Object;

            scene = new UserInterfaceSceneDriver(
                renderContext,
                CommonMocks.StyleConfigurator().Object,
                CommonMocks.FontProvider().Object);
        }
Пример #11
0
        public override void DoLayout(IUserInterfaceRenderContext renderContext, Size size)
        {
            PerformLayout(renderContext, size);

            foreach (var item in Children.Where(x => x.Display.IsVisible))
            {
                item.DoLayout(renderContext, item.Display.ContentRect.Size);
            }
        }
 public void DrawFrame(IUserInterfaceRenderContext renderContext, RenderElementDisplay display, Rectangle clientDest)
 {
     styleRenderer.DrawFrame(
         renderContext.SpriteBatch,
         display.Style.Border,
         new Rectangle(
             new Point(clientDest.X - display.Region.BorderToContentOffset.Left,
                       clientDest.Y - display.Region.BorderToContentOffset.Top),
             display.BorderRect.Size));
 }
Пример #13
0
        public static void CalcIdealSize(IRenderElement item, IUserInterfaceRenderContext renderContext, Size parentMaxSize)
        {
            Size maxSize = ConstrainMaxSize(parentMaxSize, item.Style.Size);

            Size idealSize = item.CalcIdealContentSize(renderContext, maxSize);

            idealSize = ConstrainSize(idealSize, item.Style.Size);

            item.Display.Region.IdealContentSize = idealSize;
        }
        public void UpdateAnimation(IUserInterfaceRenderContext renderContext, IRenderElement element)
        {
            var animator = element.Display.Animation;

            bool finished = animator.Transition?.Update(element.Display, renderContext) ?? false;

            if (finished)
            {
                AdvanceTransitionState(element);
            }
        }
Пример #15
0
        public bool Update(RenderElementDisplay display, IUserInterfaceRenderContext renderContext)
        {
            var animator = display.Animation;

            animator.IsVisible           = display.IsVisible;
            animator.AnimatedContentRect = display.ContentRect;
            animator.AnimatedBorderRect  = display.BorderRect;
            animator.Alpha = 1;

            return(true);
        }
Пример #16
0
            /// <summary>
            /// Updates the ideal size property for each child.
            /// Returns the total ideal margin size along the main axis.
            /// </summary>
            /// <param name="renderContext"></param>
            /// <param name="mySize"></param>
            /// <param name="children"></param>
            /// <returns></returns>
            private int CalcChildrenIdealSizes(IUserInterfaceRenderContext renderContext,
                                               Size mySize,
                                               IList <IRenderElement> children)
            {
                foreach (var item in children)
                {
                    LayoutMath.CalcIdealSize(item, renderContext, mySize);

                    item.Display.Region.SetContentSize(item.Display.Region.IdealContentSize);
                }

                return(children.Sum(x => MainAxis(x.Display.Region.IdealMarginSize)));
            }
Пример #17
0
        private void InitializeContent(IUserInterfaceRenderContext renderContext, Buttons key, InstructionItem item)
        {
            if (item.ButtonContent == null)
            {
                item.ButtonContent = renderContext.CreateContentLayout(
                    "{Button " + key + "} ", contentLayoutOptions, performLocalization: false);
            }

            if (item.Content == null)
            {
                item.Content = renderContext.CreateContentLayout(item.Text, contentLayoutOptions);
            }
        }
Пример #18
0
        public override Size CalcIdealContentSize(IUserInterfaceRenderContext renderContext, Size maxSize)
        {
            Array.Clear(columnWidths, 0, columnWidths.Length);
            Array.Clear(rowHeights, 0, rowHeights.Length);

            for (int y = 0; y < Rows; y++)
            {
                for (int x = 0; x < Columns; x++)
                {
                    var item = ChildAt(x, y);

                    SetSizeAt(x, y, Size.Empty);

                    if (item == null)
                    {
                        continue;
                    }

                    var itemBox = item.Display.Region.MarginToContentOffset;

                    var itemMaxSize = LayoutMath.ItemContentMaxSize(itemBox, maxSize);

                    item.Display.Region.IdealContentSize
                        = item.CalcIdealContentSize(renderContext, maxSize);

                    var itemIdealSize = item.Display.Region.CalcConstrainedContentSize(maxSize);

                    SetSizeAt(x, y, itemIdealSize);

                    columnWidths[x] = Math.Max(columnWidths[x], itemIdealSize.Width + itemBox.Width);
                    rowHeights[y]   = Math.Max(rowHeights[y], itemIdealSize.Height + itemBox.Height);
                }
            }

            for (int i = 0; i < Columns; i++)
            {
                columnWidths[i] = Math.Min(maxSize.Width, columnWidths[i]);
            }

            for (int i = 0; i < Rows; i++)
            {
                rowHeights[i] = Math.Min(maxSize.Height, rowHeights[i]);
            }

            var idealWidth  = columnWidths.Sum();
            var idealHeight = rowHeights.Sum();

            return(new Size(idealWidth, idealHeight));
        }
Пример #19
0
        public UserInterfaceSceneDriver(
            IUserInterfaceRenderContext renderContext,
            IStyleConfigurator styles,
            IFontProvider fonts,
            IUserInterfaceAudio audio = null)
        {
            this.renderContext = renderContext;

            desktop = new Desktop(fonts, styles);

            desktop.Audio = audio;

            Desktop.UnhandledEvent += Desktop_UnhandledEvent;

            uiInput.UIAction += desktop.OnUserInterfaceAction;
        }
Пример #20
0
        public bool Update(RenderElementDisplay display, IUserInterfaceRenderContext renderContext)
        {
            var animation = display.Animation;

            animation.IsVisible = true;

            if (animation.State == AnimationState.TransitionIn)
            {
                return(AnimateEntry(display, renderContext, animation));
            }
            else if (animation.State == AnimationState.TransitionOut)
            {
                return(AnimateExit(display, renderContext, animation));
            }

            return(true);
        }
Пример #21
0
            public void PerformLayout(
                IUserInterfaceRenderContext renderContext,
                Size mySize,
                IRenderElementStyle myStyle,
                IList <IRenderElement> children)
            {
                int idealSizeOfAllChildren = CalcChildrenIdealSizes(renderContext, mySize, children);

                int extraSpace = MainAxis(mySize) - idealSizeOfAllChildren;

                GrowFlexItems(children, ref extraSpace);
                ContractItems(children, mySize, ref extraSpace);

                PositionChildren(mySize, myStyle, children);
                ApplyAlignment(mySize, myStyle, children);

                DistributeExtraSpace(myStyle, children, ref extraSpace);
            }
Пример #22
0
        public override Size CalcIdealContentSize(IUserInterfaceRenderContext renderContext, Size maxSize)
        {
            UpdateChildLists();

            maxSize = LayoutMath.ConstrainMaxSize(maxSize, Style.Size);

            switch (Direction)
            {
            case FlexDirection.Column:
            case FlexDirection.ColumnReverse:
                return(vertical.CalcIdealSize(renderContext, maxSize, layoutChildren));

            case FlexDirection.Row:
            case FlexDirection.RowReverse:
                return(horizontal.CalcIdealSize(renderContext, maxSize, layoutChildren));

            default:
                throw new InvalidOperationException();
            }
        }
Пример #23
0
        private void PerformLayout(IUserInterfaceRenderContext renderContext, Size size)
        {
            UpdateChildLists();

            switch (Direction)
            {
            case FlexDirection.Column:
            case FlexDirection.ColumnReverse:
                vertical.PerformLayout(renderContext, size, Style, layoutChildren);
                break;

            case FlexDirection.Row:
            case FlexDirection.RowReverse:
                horizontal.PerformLayout(renderContext, size, Style, layoutChildren);
                break;

            default:
                throw new InvalidOperationException();
            }
        }
Пример #24
0
        public IUserInterfaceRenderContext PrepRenderState(IRenderElement widget, IUserInterfaceRenderContext renderContext)
        {
            var animation = widget.Display.Animation;
            var display   = widget.Display;

            if (InitializeRenderTarget(display, renderContext.GraphicsDevice))
            {
                if (display.Animation.Buffer.RenderContext == null)
                {
                    display.Animation.Buffer.RenderContext = new UserInterfaceRenderContext(
                        (UserInterfaceRenderContext)renderContext,
                        new SpriteBatch(renderContext.GraphicsDevice),
                        animation.RenderTarget);
                }

                display.Animation.Buffer.RenderContext.RenderTarget = animation.RenderTarget;
            }

            var newRenderContext = display.Animation.Buffer.RenderContext;

            newRenderContext.GameTime = renderContext.GameTime;

            newRenderContext.GraphicsDevice.SetRenderTarget(animation.RenderTarget);
            newRenderContext.GraphicsDevice.Clear(new Color(0, 0, 0, 0));
            newRenderContext.GraphicsDevice.BlendState = blendState;

            newRenderContext.SpriteBatch.Begin();

            widget.Display.Animation.Buffer.ContentDestination = new Rectangle(
                display.Region.MarginToContentOffset.Left,
                display.Region.MarginToContentOffset.Top,
                widget.Display.Animation.RenderTarget.Width - display.Region.MarginToContentOffset.Right,
                widget.Display.Animation.RenderTarget.Height - display.Region.MarginToContentOffset.Bottom);

            return(newRenderContext);
        }
Пример #25
0
 public override void Update(IUserInterfaceRenderContext renderContext)
 {
     content?.Update(renderContext.GameTime);
 }
Пример #26
0
 public override void Draw(IUserInterfaceRenderContext renderContext, Rectangle clientArea)
 {
     content?.Draw(clientArea.Location.ToVector2(), renderContext.SpriteBatch);
 }
Пример #27
0
        public override Size CalcIdealContentSize(IUserInterfaceRenderContext renderContext, Size maxSize)
        {
            RefreshContent(renderContext, maxSize.Width);

            return(content?.Size ?? Size.Empty);
        }
Пример #28
0
 public override void DoLayout(IUserInterfaceRenderContext renderContext, Size size)
 {
 }
Пример #29
0
 public override void Draw(IUserInterfaceRenderContext renderContext, Rectangle clientArea)
 => renderContext.DrawChild(clientArea, child);
Пример #30
0
 public override Size CalcIdealContentSize(IUserInterfaceRenderContext renderContext, Size maxSize)
 => child.CalcIdealMarginSize(renderContext, maxSize);