diff --git a/Source/Demos/Demo.Gui/Content/adventure-gui-skin.json b/Source/Demos/Demo.Gui/Content/adventure-gui-skin.json index 705929c3..094acb04 100644 --- a/Source/Demos/Demo.Gui/Content/adventure-gui-skin.json +++ b/Source/Demos/Demo.Gui/Content/adventure-gui-skin.json @@ -83,6 +83,7 @@ { "Name": "text-box", "Type": "GuiTextBox", + "Padding": "5 0", "TextureRegion": "grey_textBox", "TextColor": "#444444" } diff --git a/Source/MonoGame.Extended.Gui/Controls/GuiCheckBox.cs b/Source/MonoGame.Extended.Gui/Controls/GuiCheckBox.cs index ba8f24dd..2f870861 100644 --- a/Source/MonoGame.Extended.Gui/Controls/GuiCheckBox.cs +++ b/Source/MonoGame.Extended.Gui/Controls/GuiCheckBox.cs @@ -58,17 +58,18 @@ namespace MonoGame.Extended.Gui.Controls public override void Draw(IGuiRenderer renderer, float deltaSeconds) { - var boundingRectangle = BoundingRectangle.ToRectangle(); + var boundingRectangle = BoundingRectangle; var checkRectangle = new Rectangle(boundingRectangle.Location, TextureRegion.Size); - renderer.DrawRegion(TextureRegion, checkRectangle, Color); + + renderer.DrawRegion(TextureRegion, checkRectangle, Color, ClippingRectangle); if (!string.IsNullOrWhiteSpace(Text)) { var font = Font ?? renderer.DefaultFont; - var textPosition = BoundingRectangle.Location + - new Vector2(TextureRegion.Width + 5, - TextureRegion.Height * 0.5f - font.LineHeight * 0.5f); - renderer.DrawText(Font, Text, textPosition, TextColor); + var textPosition = BoundingRectangle.Location.ToVector2() + + new Vector2(TextureRegion.Width + 5, TextureRegion.Height * 0.5f - font.LineHeight * 0.5f); + + renderer.DrawText(Font, Text, textPosition, TextColor, ClippingRectangle); } } } diff --git a/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs b/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs index 5575cfcd..cc808f21 100644 --- a/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs +++ b/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs @@ -2,14 +2,12 @@ using Microsoft.Xna.Framework; using MonoGame.Extended.BitmapFonts; using MonoGame.Extended.InputListeners; -using MonoGame.Extended.SceneGraphs; -using MonoGame.Extended.Shapes; using MonoGame.Extended.TextureAtlases; using Newtonsoft.Json; namespace MonoGame.Extended.Gui.Controls { - public abstract class GuiControl : ISceneEntity, IMovable, ISizable + public abstract class GuiControl : IMovable, ISizable { protected GuiControl() { @@ -33,18 +31,21 @@ namespace MonoGame.Extended.Gui.Controls [EditorBrowsable(EditorBrowsableState.Never)] [JsonIgnore] - public RectangleF BoundingRectangle + public Rectangle BoundingRectangle { get { var position = Parent != null ? Parent.Position - Parent.Size * Parent.Origin + Position : Position; - return new RectangleF(position - Size * Origin, Size); + return new Rectangle((position - Size * Origin).ToPoint(), (Point)Size); } } [EditorBrowsable(EditorBrowsableState.Never)] public Thickness Margin { get; set; } + [EditorBrowsable(EditorBrowsableState.Never)] + public Thickness Padding { get; set; } + [EditorBrowsable(EditorBrowsableState.Never)] public bool IsFocused { get; set; } @@ -55,8 +56,17 @@ namespace MonoGame.Extended.Gui.Controls public Size2 Size { get; set; } public Color Color { get; set; } - private TextureRegion2D _textureRegion; + public Rectangle ClippingRectangle + { + get + { + var r = BoundingRectangle; + return new Rectangle(r.Left + Padding.Left, r.Top + Padding.Top, + r.Width - Padding.Right - Padding.Left, r.Height - Padding.Bottom - Padding.Top); + } + } + private TextureRegion2D _textureRegion; public TextureRegion2D TextureRegion { get { return _textureRegion; } @@ -79,11 +89,10 @@ namespace MonoGame.Extended.Gui.Controls public string Text { get; set; } public Color TextColor { get; set; } public Vector2 TextOffset { get; set; } - + public GuiControlCollection Controls { get; } private bool _isEnabled; - public bool IsEnabled { get { return _isEnabled; } @@ -95,11 +104,9 @@ namespace MonoGame.Extended.Gui.Controls } public bool IsVisible { get; set; } - public GuiControlStyle HoverStyle { get; set; } private GuiControlStyle _disabledStyle; - public GuiControlStyle DisabledStyle { get { return _disabledStyle; } @@ -129,19 +136,17 @@ namespace MonoGame.Extended.Gui.Controls public virtual void Draw(IGuiRenderer renderer, float deltaSeconds) { - renderer.DrawRegion(TextureRegion, BoundingRectangle.ToRectangle(), Color); - - if (string.IsNullOrWhiteSpace(Text)) - return; + renderer.DrawRegion(TextureRegion, BoundingRectangle, Color, ClippingRectangle); - renderer.DrawText(Font, Text, GetTextPosition(renderer), TextColor); + if(!string.IsNullOrWhiteSpace(Text)) + renderer.DrawText(Font, Text, GetTextPosition(renderer), TextColor, ClippingRectangle); } protected Vector2 GetTextPosition(IGuiRenderer renderer) { var font = Font ?? renderer.DefaultFont; var textSize = font.GetStringRectangle(Text, Vector2.Zero).Size.ToVector2(); - var textPosition = BoundingRectangle.Center - textSize * 0.5f; + var textPosition = BoundingRectangle.Center.ToVector2() - textSize * 0.5f; return textPosition + TextOffset; } } diff --git a/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs b/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs index 291ba8d5..d56aa396 100644 --- a/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs +++ b/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs @@ -86,7 +86,7 @@ namespace MonoGame.Extended.Gui.Controls var textRectangle = font.GetStringRectangle(Text.Substring(0, SelectionStart), textPosition); textRectangle.X = textRectangle.Right; textRectangle.Width = 1; - renderer.DrawRegion(null, textRectangle, TextColor); + renderer.DrawRectangle(textRectangle, TextColor); } _nextCaretBlink -= deltaSeconds; diff --git a/Source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs b/Source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs index 18695951..7edc83cf 100644 --- a/Source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs +++ b/Source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs @@ -11,9 +11,10 @@ namespace MonoGame.Extended.Gui { BitmapFont DefaultFont { get; } void Begin(); - void DrawRegion(TextureRegion2D textureRegion, Rectangle rectangle, Color color); - void DrawRegion(TextureRegion2D textureRegion, Vector2 position, Color color); - void DrawText(BitmapFont font, string text, Vector2 position, Color color); + void DrawRegion(TextureRegion2D textureRegion, Rectangle rectangle, Color color, Rectangle? clippingRectangle); + void DrawRegion(TextureRegion2D textureRegion, Vector2 position, Color color, Rectangle? clippingRectangle); + void DrawText(BitmapFont font, string text, Vector2 position, Color color, Rectangle? clippingRectangle); + void DrawRectangle(Rectangle rectangle, Color color, float thickness = 1f); void End(); } @@ -47,22 +48,26 @@ namespace MonoGame.Extended.Gui _spriteBatch.End(); } - public void DrawRegion(TextureRegion2D textureRegion, Rectangle rectangle, Color color) + public void DrawRegion(TextureRegion2D textureRegion, Rectangle rectangle, Color color, Rectangle? clippingRectangle) + { + if(textureRegion != null) + _spriteBatch.Draw(textureRegion, rectangle, color, clippingRectangle); + } + + public void DrawRegion(TextureRegion2D textureRegion, Vector2 position, Color color, Rectangle? clippingRectangle) { if (textureRegion != null) - _spriteBatch.Draw(textureRegion, rectangle, color); - else - _spriteBatch.FillRectangle(rectangle, color); + _spriteBatch.Draw(textureRegion, position, color, clippingRectangle); } - public void DrawRegion(TextureRegion2D textureRegion, Vector2 position, Color color) + public void DrawText(BitmapFont font, string text, Vector2 position, Color color, Rectangle? clippingRectangle) { - _spriteBatch.Draw(textureRegion, position, color); + _spriteBatch.DrawString(font ?? DefaultFont, text, position, color, clippingRectangle); } - public void DrawText(BitmapFont font, string text, Vector2 position, Color color) + public void DrawRectangle(Rectangle rectangle, Color color, float thickness = 1f) { - _spriteBatch.DrawString(font ?? DefaultFont, text, position, color); + _spriteBatch.DrawRectangle(rectangle, color, thickness); } } } diff --git a/Source/MonoGame.Extended.Gui/GuiSystem.cs b/Source/MonoGame.Extended.Gui/GuiSystem.cs index f30628ce..94561ae0 100644 --- a/Source/MonoGame.Extended.Gui/GuiSystem.cs +++ b/Source/MonoGame.Extended.Gui/GuiSystem.cs @@ -53,7 +53,7 @@ namespace MonoGame.Extended.Gui var cursor = Screen.Skin?.Cursor; if (cursor != null) - _renderer.DrawRegion(cursor.TextureRegion, CursorPosition, cursor.Color); + _renderer.DrawRegion(cursor.TextureRegion, CursorPosition, cursor.Color, null); } _renderer.End(); @@ -62,10 +62,7 @@ namespace MonoGame.Extended.Gui private void DrawChildren(GuiControlCollection controls, float deltaSeconds) { foreach (var control in controls.Where(c => c.IsVisible)) - { - //_renderer.DrawRegion(null, control.BoundingRectangle.ToRectangle(), Color.Magenta); control.Draw(_renderer, deltaSeconds); - } foreach (var childControl in controls.Where(c => c.IsVisible)) DrawChildren(childControl.Controls, deltaSeconds); diff --git a/Source/MonoGame.Extended.Gui/Serialization/GuiJsonSerializer.cs b/Source/MonoGame.Extended.Gui/Serialization/GuiJsonSerializer.cs index cbe9fa99..c36872b8 100644 --- a/Source/MonoGame.Extended.Gui/Serialization/GuiJsonSerializer.cs +++ b/Source/MonoGame.Extended.Gui/Serialization/GuiJsonSerializer.cs @@ -13,6 +13,7 @@ namespace MonoGame.Extended.Gui.Serialization Converters.Add(new Vector2JsonConverter()); Converters.Add(new Size2JsonConverter()); Converters.Add(new ColorJsonConverter()); + Converters.Add(new ThicknessJsonConverter()); Converters.Add(new ContentManagerJsonConverter(contentManager, font => font.Name)); Converters.Add(new GuiControlStyleJsonConverter()); Converters.Add(new GuiTextureAtlasJsonConverter(contentManager, textureRegionService)); diff --git a/Source/MonoGame.Extended/Primitives/Size2.cs b/Source/MonoGame.Extended/Primitives/Size2.cs index 5943b2b1..8c97c683 100644 --- a/Source/MonoGame.Extended/Primitives/Size2.cs +++ b/Source/MonoGame.Extended/Primitives/Size2.cs @@ -270,6 +270,18 @@ namespace MonoGame.Extended return new Size2(size.Width, size.Height); } + /// + /// Performs an explicit conversion from a to a . + /// + /// The size. + /// + /// The resulting . + /// + public static explicit operator Point(Size2 size) + { + return new Point((int)size.Width, (int)size.Height); + } + /// /// Returns a that represents this . ///