diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index be66ea9..47236f0 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -22,8 +22,6 @@ namespace MLEM.Ui.Elements { public StyleProp ItalicFont; public StyleProp FormatSettings; - public StyleProp Background; - public StyleProp BackgroundColor; public StyleProp TextColor; public StyleProp TextScale; public string Text { @@ -82,9 +80,6 @@ namespace MLEM.Ui.Elements { } public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { - if (this.Background.Value != null) - batch.Draw(this.Background, this.Area, (Color) this.BackgroundColor * alpha); - var pos = this.DisplayArea.Location; var sc = this.TextScale * this.Scale; diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index c027161..f07fe2e 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -5,19 +5,20 @@ using MLEM.Font; using MLEM.Ui.Style; namespace MLEM.Ui.Elements { - public class Tooltip : Group { + public class Tooltip : Panel { - public Vector2 MouseOffset = new Vector2(2, 3); + public StyleProp MouseOffset; public Paragraph Paragraph; public Tooltip(float width, string text = null, Element elementToHover = null) : - base(Anchor.TopLeft, Vector2.One) { + base(Anchor.TopLeft, Vector2.One, Vector2.Zero) { if (text != null) { this.Paragraph = this.AddChild(new Paragraph(Anchor.TopLeft, width, text)); this.Paragraph.AutoAdjustWidth = true; - this.Paragraph.Padding = new Vector2(2); - this.SetWidthBasedOnChildren = true; } + this.SetWidthBasedOnChildren = true; + this.SetHeightBasedOnChildren = true; + this.ChildPadding = new Vector2(2); this.CanBeMoused = false; if (elementToHover != null) { @@ -27,7 +28,7 @@ namespace MLEM.Ui.Elements { }; elementToHover.OnMouseExit += element => { if (this.System != null) - this.System.Remove(element.GetType().Name + "Tooltip"); + this.System.Remove(this.Root.Name); }; } } @@ -45,10 +46,8 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - if (this.Paragraph != null) { - this.Paragraph.Background.SetFromStyle(style.TooltipBackground); - this.Paragraph.BackgroundColor.SetFromStyle(style.TooltipBackgroundColor); - } + this.Texture.SetFromStyle(style.TooltipBackground); + this.MouseOffset.SetFromStyle(style.TooltipOffset); } public void SnapPositionToMouse() { diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 6271646..444f398 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -25,7 +25,7 @@ namespace MLEM.Ui.Style { public Color RadioHoveredColor; public TextureRegion RadioCheckmark; public NinePatch TooltipBackground; - public Color TooltipBackgroundColor; + public Vector2 TooltipOffset; public NinePatch ProgressBarTexture; public Color ProgressBarColor; public Vector2 ProgressBarProgressPadding; diff --git a/MLEM.Ui/Style/UntexturedStyle.cs b/MLEM.Ui/Style/UntexturedStyle.cs index 2a63ded..63415ce 100644 --- a/MLEM.Ui/Style/UntexturedStyle.cs +++ b/MLEM.Ui/Style/UntexturedStyle.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; using MLEM.Font; using MLEM.Textures; @@ -9,46 +10,29 @@ namespace MLEM.Ui.Style { public class UntexturedStyle : UiStyle { public UntexturedStyle(SpriteBatch batch) { - this.SelectionIndicator = GenerateTexture(batch, Color.Transparent, Color.Red); - this.ButtonTexture = GenerateTexture(batch, Color.CadetBlue); + this.SelectionIndicator = batch.GenerateTexture(Color.Transparent, Color.Red); + this.ButtonTexture = batch.GenerateTexture(Color.CadetBlue); this.ButtonHoveredColor = Color.LightGray; - this.PanelTexture = GenerateTexture(batch, Color.Gray); - this.TextFieldTexture = GenerateTexture(batch, Color.MediumBlue); + this.PanelTexture = batch.GenerateTexture(Color.Gray); + this.TextFieldTexture = batch.GenerateTexture(Color.MediumBlue); this.TextFieldHoveredColor = Color.LightGray; - this.ScrollBarBackground = GenerateTexture(batch, Color.LightBlue); - this.ScrollBarScrollerTexture = GenerateTexture(batch, Color.Blue); - this.CheckboxTexture = GenerateTexture(batch, Color.LightBlue); + this.ScrollBarBackground = batch.GenerateTexture(Color.LightBlue); + this.ScrollBarScrollerTexture = batch.GenerateTexture(Color.Blue); + this.CheckboxTexture = batch.GenerateTexture(Color.LightBlue); this.CheckboxHoveredColor = Color.LightGray; - this.CheckboxCheckmark = GenerateTexture(batch, Color.Blue).Region; - this.RadioTexture = GenerateTexture(batch, Color.AliceBlue); + this.CheckboxCheckmark = batch.GenerateTexture(Color.Blue).Region; + this.RadioTexture = batch.GenerateTexture(Color.AliceBlue); this.RadioHoveredColor = Color.LightGray; - this.RadioCheckmark = GenerateTexture(batch, Color.CornflowerBlue).Region; - this.TooltipBackground = GenerateTexture(batch, Color.DarkGray); - this.TooltipBackgroundColor = new Color(Color.Black, 0.65F); - this.ProgressBarTexture = GenerateTexture(batch, Color.RoyalBlue); + this.RadioCheckmark = batch.GenerateTexture(Color.CornflowerBlue).Region; + this.TooltipBackground = batch.GenerateTexture(Color.Black * 0.65F, Color.Black * 0.65F); + this.TooltipOffset = new Vector2(2, 3); + this.ProgressBarTexture = batch.GenerateTexture(Color.RoyalBlue); this.ProgressBarColor = Color.White; this.ProgressBarProgressPadding = new Vector2(1); this.ProgressBarProgressColor = Color.Red; this.Font = new EmptyFont(); } - private static NinePatch GenerateTexture(SpriteBatch batch, Color color, Color? outlineColor = null) { - var outli = outlineColor ?? Color.Black; - var tex = new Texture2D(batch.GraphicsDevice, 3, 3); - tex.SetData(new[] { - outli, outli, outli, - outli, color, outli, - outli, outli, outli - }); - batch.Disposing += (sender, args) => { - if (tex != null) { - tex.Dispose(); - tex = null; - } - }; - return new NinePatch(tex, 1); - } - private class EmptyFont : IGenericFont { public float LineHeight => 1; diff --git a/MLEM/Extensions/SpriteBatchExtensions.cs b/MLEM/Extensions/SpriteBatchExtensions.cs index 3cb34d4..6b45443 100644 --- a/MLEM/Extensions/SpriteBatchExtensions.cs +++ b/MLEM/Extensions/SpriteBatchExtensions.cs @@ -1,6 +1,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Misc; +using MLEM.Textures; namespace MLEM.Extensions { public static class SpriteBatchExtensions { @@ -21,6 +22,23 @@ namespace MLEM.Extensions { return blankTexture; } + public static NinePatch GenerateTexture(this SpriteBatch batch, Color color, Color? outlineColor = null) { + var outli = outlineColor ?? Color.Black; + var tex = new Texture2D(batch.GraphicsDevice, 3, 3); + tex.SetData(new[] { + outli, outli, outli, + outli, color, outli, + outli, outli, outli + }); + batch.Disposing += (sender, args) => { + if (tex != null) { + tex.Dispose(); + tex = null; + } + }; + return new NinePatch(tex, 1); + } + public static void DrawCenteredString(this SpriteBatch batch, SpriteFont font, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) { var size = font.MeasureString(text); var center = new Vector2(