/// <summary>
        ///   Renders the specified control using the provided graphics interface
        /// </summary>
        /// <param name="control">Control that will be rendered</param>
        /// <param name="graphics">
        ///   Graphics interface that will be used to draw the control
        /// </param>
        public void Render(
            Controls.Desktop.InputControl control, IFlatGuiGraphics graphics
            )
        {
            RectangleF controlBounds = control.GetAbsoluteBounds();

            // Draw the control's frame and background
            graphics.DrawElement(Style, controlBounds);

            using (graphics.SetClipRegion(controlBounds)) {
                string text = control.Text ?? string.Empty;

                // Amount by which the text will be moved within the input box in
                // order to keep the caret in view even when the text is wider than
                // the input box.
                float left = 0;

                // Only scroll the text within the input box when it has the input
                // focus and the caret is being shown.
                if (control.HasFocus)
                {
                    // Find out where the cursor is from the left end of the text
                    RectangleF stringSize = graphics.MeasureString(
                        Style, controlBounds, text.Substring(0, control.CaretPosition)
                        );

                    // TODO: Renderer should query the size of the control's frame
                    //   Otherwise text will be visible over the frame, which might look bad
                    //   if a skin uses a frame wider than 2 pixels or in a different color
                    //   than the text.
                    while (stringSize.Width + left > controlBounds.Width)
                    {
                        left -= controlBounds.Width / 10.0f;
                    }
                }

                // Draw the text into the input box
                controlBounds.X += left;
                graphics.DrawString(Style, controlBounds, control.Text);

                // If the input box is in focus, also draw the caret so the user knows
                // where characters will be inserted into the text.
                if (control.HasFocus)
                {
                    if (control.MillisecondsSinceLastCaretMovement % 500 < 250)
                    {
                        graphics.DrawCaret(
                            "input.normal", controlBounds, control.Text, control.CaretPosition
                            );
                    }
                }
            }

            // Let the control know that we can provide it with additional informations
            // about how its text is being rendered
            control.OpeningLocator = this;
            this.graphics          = graphics;
        }
Example #2
0
        /// <summary>Determines the height of a row displayed in the list</summary>
        /// <param name="bounds">
        ///     Boundaries of the control, should be in absolute coordinates
        /// </param>
        /// <returns>The height of a single row in the list</returns>
        public float GetRowHeight(RectangleF bounds)
        {
            //   The code below is not optimal, but the XNA SpriteFont isn't very talkative
            //   when it comes to providing informations about itself ;)

            if (float.IsNaN(_rowHeight))
            {
                _rowHeight  = _graphics.MeasureString("list", bounds, "qyjpMAW!").Height;
                _rowHeight += 2.0f;
            }

            return(_rowHeight);
        }
        /// <summary>
        ///   Renders the specified control using the provided graphics interface
        /// </summary>
        /// <param name="control">Control that will be rendered</param>
        /// <param name="graphics">
        ///   Graphics interface that will be used to draw the control
        /// </param>
        public void Render(Controls.Desktop.InputControl control, IFlatGuiGraphics graphics)
        {
            RectangleF controlBounds = control.GetAbsoluteBounds();

            // Draw the control's frame and background
            if (control.CustomBackground == null)
            {
                graphics.DrawElement(Style, ref controlBounds);
            }
            else
            {
                graphics.DrawCustomTexture(control.CustomBackground, ref controlBounds);
            }

            if (control.CustomHintImage != null && string.IsNullOrEmpty(control.Text) && !control.HasFocus)
            {
                var bounds = controlBounds;

                bounds.Width  = control.CustomHintImage.Width;
                bounds.Height = control.CustomHintImage.Height;

                if (!control.CustomHintImageOffset.HasValue)
                {
                    var dx = (controlBounds.Height - bounds.Height) / 2;

                    bounds.X += dx;
                    bounds.Y += dx;
                }
                else
                {
                    bounds.X += control.CustomHintImageOffset.Value.X;
                    bounds.Y += control.CustomHintImageOffset.Value.Y;
                }

                graphics.DrawCustomTexture(control.CustomHintImage, ref bounds);
            }

            //Clip the region
            using (graphics.SetClipRegion(ref controlBounds))
            {
                string text = control.Text ?? string.Empty;

                // Amount by which the text will be moved within the input box in
                // order to keep the caret in view even when the text is wider than
                // the input box.
                float left = 0;

                // Only scroll the text within the input box when it has the input
                // focus and the caret is being shown.
                if (control.HasFocus)
                {
                    float offSetSize = 0;
                    // Find out where the cursor is from the left end of the text
                    RectangleF stringSize;
                    if (control.CustomFont == null)
                    {
                        stringSize = graphics.MeasureString(Style, ref controlBounds, text.Substring(0, control.CaretPosition));
                    }
                    else
                    {
                        stringSize = graphics.MeasureString(control.CustomFont, ref controlBounds, text.Substring(0, control.CaretPosition));
                        offSetSize = control.CustomFont.Size;
                    }

                    // exTODO: Renderer should query the size of the control's frame
                    //   Otherwise text will be visible over the frame, which might look bad
                    //   if a skin uses a frame wider than 2 pixels or in a different color
                    //   than the text.
                    while ((stringSize.Width + offSetSize) + left > controlBounds.Width)
                    {
                        left -= controlBounds.Width / 10.0f;
                    }
                }

                // Draw the text into the input box
                controlBounds.X += left;

                string textToDraw = control.Text;
                if (control.IsPassword)
                {
                    textToDraw = new string('*', textToDraw.Length);
                }

                int withCarret = -1;
                // If the input box is in focus, also draw the caret so the user knows
                // where characters will be inserted into the text.
                if (control.HasFocus)
                {
                    if (control.MillisecondsSinceLastCaretMovement % 500 < 250)
                    {
                        withCarret = control.CaretPosition;
                    }
                }

                if (control.ColorSet)
                {
                    ByteColor color = control.Color;

                    if (control.CustomFont != null)
                    {
                        if (!control.TextOffset.HasValue)
                        {
                            var dx = (controlBounds.Height - control.CustomFont.HeightInPoints) / 2;
                            var dy = (controlBounds.Height - control.CustomFont.CharHeight) / 2;

                            controlBounds.X += dx;
                            controlBounds.Y += dy;
                        }
                        else
                        {
                            controlBounds.X += control.TextOffset.Value.X;
                            controlBounds.Y += control.TextOffset.Value.Y;
                        }

                        graphics.DrawString(control.CustomFont, ref controlBounds, textToDraw, ref color, false, withCarret);
                    }
                    else
                    {
                        graphics.DrawString(Style, 0, ref controlBounds, textToDraw, ref color, false, withCarret);
                    }
                }
                else
                {
                    if (control.CustomFont != null)
                    {
                        ByteColor color = control.Color;
                        if (!control.TextOffset.HasValue)
                        {
                            var dx = (controlBounds.Height - control.CustomFont.HeightInPoints) / 2;
                            var dy = (controlBounds.Height - control.CustomFont.CharHeight) / 2;

                            controlBounds.X += dx;
                            controlBounds.Y += dy;
                        }
                        else
                        {
                            controlBounds.X += control.TextOffset.Value.X;
                            controlBounds.Y += control.TextOffset.Value.Y;
                        }

                        graphics.DrawString(control.CustomFont, ref controlBounds, textToDraw, ref color, false, withCarret);
                    }
                    else
                    {
                        graphics.DrawString(Style, 0, ref controlBounds, textToDraw, false, withCarret);
                    }
                }
            }

            // Let the control know that we can provide it with additional informations
            // about how its text is being rendered
            control.OpeningLocator = this;
            this.graphics          = graphics;
        }
        /// <summary>
        ///   Renders the specified control using the provided graphics interface
        /// </summary>
        /// <param name="control">Control that will be rendered</param>
        /// <param name="graphics">
        ///   Graphics interface that will be used to draw the control
        /// </param>
        public void Render(Controls.Desktop.InputKeyCatchControl control, IFlatGuiGraphics graphics)
        {
            RectangleF controlBounds = control.GetAbsoluteBounds();

            // Draw the control's frame and background
            if (control.CustomBackground == null)
            {
                if (control.HasFocus)
                {
                    graphics.DrawElement(Style, ref controlBounds, ref control.HasFocusBackColor);
                }
                else
                {
                    graphics.DrawElement(Style, ref controlBounds);
                }
            }
            else
            {
                graphics.DrawCustomTexture(control.CustomBackground, ref controlBounds);
            }

            if (control.CustomHintImage != null && string.IsNullOrEmpty(control.Text) && !control.HasFocus)
            {
                var bounds = controlBounds;

                bounds.Width  = control.CustomHintImage.Width;
                bounds.Height = control.CustomHintImage.Height;

                var dx = (controlBounds.Height - bounds.Height) / 2;

                bounds.X += dx;
                bounds.Y += dx;

                graphics.DrawCustomTexture(control.CustomHintImage, ref bounds);
            }

            using (graphics.SetClipRegion(ref controlBounds))
            {
                string text = control.Text ?? string.Empty;

                // Amount by which the text will be moved within the input box in
                // order to keep the caret in view even when the text is wider than
                // the input box.
                float left = 0;

                // Only scroll the text within the input box when it has the input
                // focus and the caret is being shown.
                if (control.HasFocus)
                {
                    // Find out where the cursor is from the left end of the text
                    RectangleF stringSize = graphics.MeasureString(Style, ref controlBounds, text);

                    // exTODO: Renderer should query the size of the control's frame
                    //   Otherwise text will be visible over the frame, which might look bad
                    //   if a skin uses a frame wider than 2 pixels or in a different color
                    //   than the text.
                    while (stringSize.Width + left > controlBounds.Width)
                    {
                        left -= controlBounds.Width / 10.0f;
                    }
                }

                // Draw the text into the input box
                controlBounds.X += left;

                string textToDraw = control.Text;

                // If the input box is in focus, also draw the caret so the user knows
                // where characters will be inserted into the text.
                if (control.ColorSet)
                {
                    ByteColor color = control.Color;

                    if (control.CustomFont != null)
                    {
                        var move = (controlBounds.Height - control.CustomFont.CharHeight) / 2;
                        controlBounds.X += move;
                        controlBounds.Y += move;
#if DEBUG
                        if (move <= 2)
                        {
                            logger.Warn("Input component height ({0}) too small for the font size ({1})!", controlBounds.Height, control.CustomFont.CharHeight);
                        }
#endif
                        graphics.DrawString(control.CustomFont, ref controlBounds, textToDraw, ref color, false);
                    }
                    else
                    {
                        graphics.DrawString(Style, 0, ref controlBounds, textToDraw, ref color, false);
                    }
                }
                else
                {
                    if (control.CustomFont != null)
                    {
                        ByteColor color = control.Color;
                        var       move  = (controlBounds.Height - control.CustomFont.CharHeight) / 2;
                        controlBounds.X += move;
                        controlBounds.Y += move;

                        graphics.DrawString(control.CustomFont, ref controlBounds, textToDraw, ref color, false);
                    }
                    else
                    {
                        graphics.DrawString(Style, 0, ref controlBounds, textToDraw, false);
                    }
                }
            }

            // Let the control know that we can provide it with additional informations
            // about how its text is being rendered
            this.graphics = graphics;
        }
Example #5
0
        public void Render(InventoryCell control, IFlatGuiGraphics graphics)
        {
            var controlBounds = control.GetAbsoluteBounds();

            #region Backgroung
            if (control.DrawCellBackground)
            {
                if (control.MouseHovering || control.IsCellSelected)
                {
                    if (control.CustomBackgroundHover != null)
                    {
                        if (control.CustomBackgroundAutosize)
                        {
                            graphics.DrawCustomTexture(control.CustomBackgroundHover, ref controlBounds, 0, control.DrawGroupId);
                        }
                        else
                        {
                            var dx = (controlBounds.Width - control.CustomBackgroundHover.Width) / 2;
                            var dy = (controlBounds.Height - control.CustomBackgroundHover.Height) / 2;

                            var bgBounds = controlBounds;

                            bgBounds.X     += dx;
                            bgBounds.Y     += dy;
                            bgBounds.Width  = control.CustomBackgroundHover.Width;
                            bgBounds.Height = control.CustomBackgroundHover.Height;

                            graphics.DrawCustomTexture(control.CustomBackgroundHover, ref bgBounds, 0, control.DrawGroupId);
                        }
                    }
                    else
                    {
                        graphics.DrawElement("button.highlighted", ref controlBounds);
                    }
                }
                else
                {
                    if (control.CustomBackground != null)
                    {
                        if (control.CustomBackgroundAutosize)
                        {
                            graphics.DrawCustomTexture(control.CustomBackground, ref controlBounds, 0, control.DrawGroupId);
                        }
                        else
                        {
                            var dx = (controlBounds.Width - control.CustomBackground.Width) / 2;
                            var dy = (controlBounds.Height - control.CustomBackground.Height) / 2;

                            var bgBounds = controlBounds;

                            bgBounds.X     += dx;
                            bgBounds.Y     += dy;
                            bgBounds.Width  = control.CustomBackground.Width;
                            bgBounds.Height = control.CustomBackground.Height;

                            graphics.DrawCustomTexture(control.CustomBackground, ref bgBounds, 0, control.DrawGroupId);
                        }
                    }
                    else
                    {
                        graphics.DrawElement("button.normal", ref controlBounds, control.DrawGroupId);
                    }
                }
            }
            #endregion

            #region Item icon
            if (control.Slot != null && !control.Slot.IsEmpty)
            {
                SpriteTexture tex;
                int           textureArrayIndex;
                control.IconFactory.Lookup(control.Slot.Item, out tex, out textureArrayIndex);
                if (tex != null)
                {
                    const int innerBorder = 3;
                    var       texBounds   = new RectangleF(
                        controlBounds.X + innerBorder,
                        controlBounds.Y + innerBorder,
                        controlBounds.Width - innerBorder * 2,
                        controlBounds.Height - innerBorder * 2
                        );

                    ByteColor?color = control.Color;

                    if (control.IsDisabledCell)
                    {
                        color = new ByteColor(255, 255, 255, 30);
                    }

                    graphics.DrawCustomTexture(tex, ref texBounds, textureArrayIndex, control.DrawIconsGroupId, color);
                }
                else
                {
                    var displayName = control.Slot.Item.Name;

                    graphics.DrawString("button.normal", 0, ref controlBounds, displayName, false, -1, control.DrawGroupId);
                }
            }
            #endregion

            #region Items count

            if ((control.Slot != null && control.Slot.ItemsCount > 1) || !string.IsNullOrEmpty(control.CountString))
            {
                var itemsCount = control.CountString ?? control.Slot.ItemsCount.ToString();

                var textSize = graphics.MeasureString("slot.items", ref controlBounds, itemsCount);

                var h = textSize.Height;
                var w = textSize.Width;

                var textPosition = new RectangleF(controlBounds.X + controlBounds.Width - textSize.Width - 6,
                                                  controlBounds.Y + controlBounds.Height - textSize.Height,
                                                  w,
                                                  h);
                graphics.DrawString("slot.items.shadow", 0, ref textPosition, itemsCount, false, -1, control.DrawGroupId);
                graphics.DrawString("slot.items", 0, ref textPosition, itemsCount, false, -1, control.DrawGroupId);
            }

            #endregion
        }