From ee35509f23d3f302031e32206f74918944ebf5cd Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 14 Oct 2019 21:28:12 +0200 Subject: [PATCH] made ui styling a lot more user friendly --- Demos.Android/Resources/Resource.Designer.cs | 20 +++++----- Demos/UiDemo.cs | 11 ++---- MLEM.Ui/Elements/Button.cs | 16 ++++---- MLEM.Ui/Elements/Checkbox.cs | 20 +++++----- MLEM.Ui/Elements/Dropdown.cs | 4 +- MLEM.Ui/Elements/Element.cs | 7 ++-- MLEM.Ui/Elements/Image.cs | 9 +++-- MLEM.Ui/Elements/Panel.cs | 4 +- MLEM.Ui/Elements/Paragraph.cs | 35 +++++++++--------- MLEM.Ui/Elements/ProgressBar.cs | 35 +++++++++--------- MLEM.Ui/Elements/RadioButton.cs | 8 ++-- MLEM.Ui/Elements/ScrollBar.cs | 8 ++-- MLEM.Ui/Elements/TextField.cs | 39 ++++++++++---------- MLEM.Ui/Elements/Tooltip.cs | 4 +- MLEM.Ui/Style/StyleProp.cs | 23 ++++++++++++ MLEM.Ui/UiSystem.cs | 2 +- Sandbox/GameImpl.cs | 12 ++---- 17 files changed, 137 insertions(+), 120 deletions(-) create mode 100644 MLEM.Ui/Style/StyleProp.cs diff --git a/Demos.Android/Resources/Resource.Designer.cs b/Demos.Android/Resources/Resource.Designer.cs index b79b7ac..9ce037a 100644 --- a/Demos.Android/Resources/Resource.Designer.cs +++ b/Demos.Android/Resources/Resource.Designer.cs @@ -44,11 +44,11 @@ namespace AndroidDemos public partial class Drawable { - // aapt resource value: 0x7f020000 - public const int Icon = 2130837504; + // aapt resource value: 0x7F010000 + public const int Icon = 2130771968; - // aapt resource value: 0x7f020001 - public const int Splash = 2130837505; + // aapt resource value: 0x7F010001 + public const int Splash = 2130771969; static Drawable() { @@ -63,11 +63,11 @@ namespace AndroidDemos public partial class String { - // aapt resource value: 0x7f030001 - public const int ApplicationName = 2130903041; + // aapt resource value: 0x7F020000 + public const int ApplicationName = 2130837504; - // aapt resource value: 0x7f030000 - public const int Hello = 2130903040; + // aapt resource value: 0x7F020001 + public const int Hello = 2130837505; static String() { @@ -82,8 +82,8 @@ namespace AndroidDemos public partial class Style { - // aapt resource value: 0x7f040000 - public const int Theme_Splash = 2130968576; + // aapt resource value: 0x7F030000 + public const int Theme_Splash = 2130903040; static Style() { diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index edcbe25..2a66d2d 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -81,16 +81,11 @@ namespace Demos { root.AddChild(new VerticalSpace(3)); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Note that the default style does not contain any textures or font files and, as such, is quite bland. However, the default style is quite easy to override.")); - root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Change Style") { + var customButton = root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Change Style") { OnPressed = element => this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle, - PositionOffset = new Vector2(0, 1), - // set HasCustomStyle to true before changing style information so that, when changing the style globally - // (like above), these custom values don't get undone - HasCustomStyle = true, - Texture = this.testPatch, - HoveredColor = Color.LightGray, - SelectionIndicator = style.SelectionIndicator + PositionOffset = new Vector2(0, 1) }); + customButton.Texture.Set(this.testPatch); root.AddChild(new VerticalSpace(3)); diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index 0589d9f..df8700a 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -7,9 +7,9 @@ using MLEM.Ui.Style; namespace MLEM.Ui.Elements { public class Button : Element { - public NinePatch Texture; - public NinePatch HoveredTexture; - public Color HoveredColor; + public StyleProp Texture; + public StyleProp HoveredTexture; + public StyleProp HoveredColor; public Paragraph Text; public Tooltip Tooltip; @@ -26,9 +26,9 @@ namespace MLEM.Ui.Elements { var tex = this.Texture; var color = Color.White * alpha; if (this.IsMouseOver) { - if (this.HoveredTexture != null) + if (this.HoveredTexture.Value != null) tex = this.HoveredTexture; - color = this.HoveredColor * alpha; + color = (Color) this.HoveredColor * alpha; } batch.Draw(tex, this.DisplayArea, color, this.Scale); base.Draw(time, batch, alpha, blendState, samplerState, matrix); @@ -36,9 +36,9 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture = style.ButtonTexture; - this.HoveredTexture = style.ButtonHoveredTexture; - this.HoveredColor = style.ButtonHoveredColor; + this.Texture.SetFromStyle(style.ButtonTexture); + this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture); + this.HoveredColor.SetFromStyle(style.ButtonHoveredColor); } } diff --git a/MLEM.Ui/Elements/Checkbox.cs b/MLEM.Ui/Elements/Checkbox.cs index 773afbf..7e4ddfa 100644 --- a/MLEM.Ui/Elements/Checkbox.cs +++ b/MLEM.Ui/Elements/Checkbox.cs @@ -8,10 +8,10 @@ using MLEM.Ui.Style; namespace MLEM.Ui.Elements { public class Checkbox : Element { - public NinePatch Texture; - public NinePatch HoveredTexture; - public Color HoveredColor; - public TextureRegion Checkmark; + public StyleProp Texture; + public StyleProp HoveredTexture; + public StyleProp HoveredColor; + public StyleProp Checkmark; public Paragraph Label; public float TextOffsetX = 2; @@ -50,9 +50,9 @@ namespace MLEM.Ui.Elements { var tex = this.Texture; var color = Color.White * alpha; if (this.IsMouseOver) { - if (this.HoveredTexture != null) + if (this.HoveredTexture.Value != null) tex = this.HoveredTexture; - color = this.HoveredColor * alpha; + color = (Color) this.HoveredColor * alpha; } var boxDisplayArea = new Rectangle(this.DisplayArea.Location, new Point(this.DisplayArea.Height)); @@ -64,10 +64,10 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture = style.CheckboxTexture; - this.HoveredTexture = style.CheckboxHoveredTexture; - this.HoveredColor = style.CheckboxHoveredColor; - this.Checkmark = style.CheckboxCheckmark; + this.Texture.SetFromStyle(style.CheckboxTexture); + this.HoveredTexture.SetFromStyle(style.CheckboxHoveredTexture); + this.HoveredColor.SetFromStyle(style.CheckboxHoveredColor); + this.Checkmark.SetFromStyle(style.CheckboxCheckmark); } public delegate void CheckStateChange(Checkbox box, bool checced); diff --git a/MLEM.Ui/Elements/Dropdown.cs b/MLEM.Ui/Elements/Dropdown.cs index e7c73e4..043327e 100644 --- a/MLEM.Ui/Elements/Dropdown.cs +++ b/MLEM.Ui/Elements/Dropdown.cs @@ -34,8 +34,8 @@ namespace MLEM.Ui.Elements { }; if (pressed != null) paragraph.OnPressed += pressed; - paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray; - paragraph.OnMouseExit += e => paragraph.TextColor = Color.White; + paragraph.OnMouseEnter += e => paragraph.TextColor.Set(Color.LightGray); + paragraph.OnMouseExit += e => paragraph.TextColor.Set(Color.White); this.AddElement(paragraph); } diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 129c31d..8cfcb32 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -89,7 +89,7 @@ namespace MLEM.Ui.Elements { get => this.system; internal set { this.system = value; - if (this.system != null && !this.HasCustomStyle) + if (this.system != null) this.InitStyle(this.system.Style); } } @@ -113,7 +113,6 @@ namespace MLEM.Ui.Elements { public bool CanBeSelected = true; public bool CanBeMoused = true; public float DrawAlpha = 1; - public bool HasCustomStyle; public bool SetHeightBasedOnChildren; public bool CanAutoAnchorsAttach = true; @@ -139,7 +138,7 @@ namespace MLEM.Ui.Elements { } private bool areaDirty; private bool sortedChildrenDirty; - public NinePatch SelectionIndicator; + public StyleProp SelectionIndicator; public Element(Anchor anchor, Vector2 size) { this.anchor = anchor; @@ -463,7 +462,7 @@ namespace MLEM.Ui.Elements { } protected virtual void InitStyle(UiStyle style) { - this.SelectionIndicator = style.SelectionIndicator; + this.SelectionIndicator.SetFromStyle(style.SelectionIndicator); } public delegate void TextInputCallback(Element element, Keys key, char character); diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index 00095bc..e3aed62 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -1,13 +1,13 @@ using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; using MLEM.Textures; +using MLEM.Ui.Style; namespace MLEM.Ui.Elements { public class Image : Element { - public Color Color = Color.White; + public StyleProp Color; private TextureRegion texture; public TextureCallback GetTextureCallback; public TextureRegion Texture { @@ -65,13 +65,14 @@ namespace MLEM.Ui.Elements { if (this.texture == null) return; var center = new Vector2(this.texture.Width / 2F, this.texture.Height / 2F); + var color = (this.Color.Value != default ? this.Color : Microsoft.Xna.Framework.Color.White) * alpha; if (this.MaintainImageAspect) { var scale = Math.Min(this.DisplayArea.Width / (float) this.texture.Width, this.DisplayArea.Height / (float) this.texture.Height); var imageOffset = new Vector2(this.DisplayArea.Width / 2F - this.texture.Width * scale / 2, this.DisplayArea.Height / 2F - this.texture.Height * scale / 2); - batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale + imageOffset, this.Color * alpha, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); + batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale + imageOffset, color, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); } else { var scale = new Vector2(1F / this.texture.Width, 1F / this.texture.Height) * this.DisplayArea.Size.ToVector2(); - batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale, this.Color * alpha, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); + batch.Draw(this.texture, this.DisplayArea.Location.ToVector2() + center * scale, color, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); } base.Draw(time, batch, alpha, blendState, samplerState, matrix); } diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 0d6ed28..14de445 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -11,7 +11,7 @@ using MLEM.Ui.Style; namespace MLEM.Ui.Elements { public class Panel : Element { - public NinePatch Texture; + public StyleProp Texture; public readonly ScrollBar ScrollBar; private readonly bool scrollOverflow; private RenderTarget2D renderTarget; @@ -169,7 +169,7 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture = style.PanelTexture; + this.Texture.SetFromStyle(style.PanelTexture); } } diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index c811ac9..5cd1f1e 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -16,14 +16,14 @@ namespace MLEM.Ui.Elements { private string text; private string splitText; private Dictionary codeLocations; - public IGenericFont RegularFont; - public IGenericFont BoldFont; - public IGenericFont ItalicFont; + public StyleProp RegularFont; + public StyleProp BoldFont; + public StyleProp ItalicFont; - public NinePatch Background; - public Color BackgroundColor; - public Color TextColor = Color.White; - public float TextScale; + public StyleProp Background; + public StyleProp BackgroundColor; + public StyleProp TextColor; + public StyleProp TextScale; public string Text { get => this.text; set { @@ -59,10 +59,10 @@ namespace MLEM.Ui.Elements { var size = base.CalcActualSize(parentArea); var sc = this.TextScale * this.Scale; - this.splitText = this.RegularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc); + this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc); this.codeLocations = this.text.GetFormattingCodes(); - var textDims = this.RegularFont.MeasureString(this.splitText) * sc; + var textDims = this.RegularFont.Value.MeasureString(this.splitText) * sc; return new Point(this.AutoAdjustWidth ? textDims.X.Ceil() + this.ScaledPadding.X * 2 : size.X, textDims.Y.Ceil() + this.ScaledPadding.Y * 2); } @@ -74,28 +74,29 @@ namespace MLEM.Ui.Elements { } public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { - if (this.Background != null) - batch.Draw(this.Background, this.Area, this.BackgroundColor * alpha); + if (this.Background.Value != null) + batch.Draw(this.Background, this.Area, (Color) this.BackgroundColor * alpha); var pos = this.DisplayArea.Location.ToVector2(); var sc = this.TextScale * this.Scale; + var color = (this.TextColor.Value != default ? this.TextColor : Color.White) * alpha; // if we don't have any formatting codes, then we don't need to do complex drawing if (this.codeLocations.Count <= 0) { - this.RegularFont.DrawString(batch, this.splitText, pos, this.TextColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0); + this.RegularFont.Value.DrawString(batch, this.splitText, pos, color, 0, Vector2.Zero, sc, SpriteEffects.None, 0); } else { // if we have formatting codes, we should do it - this.RegularFont.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, this.TextColor * alpha, sc, this.BoldFont, this.ItalicFont, 0, this.TimeIntoAnimation); + this.RegularFont.Value.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, color, sc, this.BoldFont.Value, this.ItalicFont.Value, 0, this.TimeIntoAnimation); } base.Draw(time, batch, alpha, blendState, samplerState, matrix); } protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.TextScale = style.TextScale; - this.RegularFont = style.Font; - this.BoldFont = style.BoldFont ?? style.Font; - this.ItalicFont = style.ItalicFont ?? style.Font; + this.TextScale.SetFromStyle(style.TextScale); + this.RegularFont.SetFromStyle(style.Font); + this.BoldFont.SetFromStyle(style.BoldFont ?? style.Font); + this.ItalicFont.SetFromStyle(style.ItalicFont ?? style.Font); } public delegate string TextCallback(Paragraph paragraph); diff --git a/MLEM.Ui/Elements/ProgressBar.cs b/MLEM.Ui/Elements/ProgressBar.cs index 9ca988f..67efd59 100644 --- a/MLEM.Ui/Elements/ProgressBar.cs +++ b/MLEM.Ui/Elements/ProgressBar.cs @@ -9,11 +9,11 @@ using MLEM.Ui.Style; namespace MLEM.Ui.Elements { public class ProgressBar : Element { - public NinePatch Texture; - public Color Color; - public Point ProgressPadding; - public NinePatch ProgressTexture; - public Color ProgressColor; + public StyleProp Texture; + public StyleProp Color; + public StyleProp ProgressPadding; + public StyleProp ProgressTexture; + public StyleProp ProgressColor; public Direction2 Direction; public float MaxValue; @@ -33,11 +33,12 @@ namespace MLEM.Ui.Elements { } public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { - batch.Draw(this.Texture, this.DisplayArea, this.Color * alpha, this.Scale); + batch.Draw(this.Texture, this.DisplayArea, (Color) this.Color * alpha, this.Scale); var percentage = this.CurrentValue / this.MaxValue; - var padHor = this.ProgressTexture != null ? (this.ProgressTexture.PaddingLeft + this.ProgressTexture.PaddingRight) * this.Scale : 0; - var padVer = this.ProgressTexture != null ? (this.ProgressTexture.PaddingTop + this.ProgressTexture.PaddingBottom) * this.Scale : 0; + var tex = this.ProgressTexture.Value; + var padHor = tex != null ? (tex.PaddingLeft + tex.PaddingRight) * this.Scale : 0; + var padVer = tex != null ? (tex.PaddingTop + tex.PaddingBottom) * this.Scale : 0; var width = (percentage * (this.DisplayArea.Width - padHor) + padHor).Floor(); var height = (percentage * (this.DisplayArea.Height - padVer) + padVer).Floor(); Rectangle progressArea; @@ -59,22 +60,22 @@ namespace MLEM.Ui.Elements { progressArea = new Rectangle(this.DisplayArea.Location, new Point(width, this.DisplayArea.Height)); break; } - var offsetArea = progressArea.Shrink(this.ProgressPadding.Multiply(this.Scale)); - if (this.ProgressTexture != null) { - batch.Draw(this.ProgressTexture, offsetArea, this.ProgressColor * alpha, this.Scale); + var offsetArea = progressArea.Shrink(this.ProgressPadding.Value.Multiply(this.Scale)); + if (this.ProgressTexture.Value != null) { + batch.Draw(this.ProgressTexture, offsetArea, (Color) this.ProgressColor * alpha, this.Scale); } else { - batch.Draw(batch.GetBlankTexture(), offsetArea, this.ProgressColor * alpha); + batch.Draw(batch.GetBlankTexture(), offsetArea, (Color) this.ProgressColor * alpha); } base.Draw(time, batch, alpha, blendState, samplerState, matrix); } protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture = style.ProgressBarTexture; - this.Color = style.ProgressBarColor; - this.ProgressPadding = style.ProgressBarProgressPadding; - this.ProgressTexture = style.ProgressBarProgressTexture; - this.ProgressColor = style.ProgressBarProgressColor; + this.Texture.SetFromStyle(style.ProgressBarTexture); + this.Color.SetFromStyle(style.ProgressBarColor); + this.ProgressPadding.SetFromStyle(style.ProgressBarProgressPadding); + this.ProgressTexture.SetFromStyle(style.ProgressBarProgressTexture); + this.ProgressColor.SetFromStyle(style.ProgressBarProgressColor); } } diff --git a/MLEM.Ui/Elements/RadioButton.cs b/MLEM.Ui/Elements/RadioButton.cs index 42229c6..61a4c9d 100644 --- a/MLEM.Ui/Elements/RadioButton.cs +++ b/MLEM.Ui/Elements/RadioButton.cs @@ -23,10 +23,10 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture = style.RadioTexture; - this.HoveredTexture = style.RadioHoveredTexture; - this.HoveredColor = style.RadioHoveredColor; - this.Checkmark = style.RadioCheckmark; + this.Texture.SetFromStyle(style.RadioTexture); + this.HoveredTexture.SetFromStyle(style.RadioHoveredTexture); + this.HoveredColor.SetFromStyle(style.RadioHoveredColor); + this.Checkmark.SetFromStyle(style.RadioCheckmark); } } diff --git a/MLEM.Ui/Elements/ScrollBar.cs b/MLEM.Ui/Elements/ScrollBar.cs index e00feb8..4769a4a 100644 --- a/MLEM.Ui/Elements/ScrollBar.cs +++ b/MLEM.Ui/Elements/ScrollBar.cs @@ -11,8 +11,8 @@ using MLEM.Ui.Style; namespace MLEM.Ui.Elements { public class ScrollBar : Element { - public NinePatch Background; - public NinePatch ScrollerTexture; + public StyleProp Background; + public StyleProp ScrollerTexture; public Point ScrollerOffset; public Point ScrollerSize; private float maxValue; @@ -132,8 +132,8 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Background = style.ScrollBarBackground; - this.ScrollerTexture = style.ScrollBarScrollerTexture; + this.Background.SetFromStyle(style.ScrollBarBackground); + this.ScrollerTexture.SetFromStyle(style.ScrollBarScrollerTexture); } public delegate void ValueChanged(Element element, float value); diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 3d154fd..20bf0bb 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -18,16 +18,16 @@ namespace MLEM.Ui.Elements { public static readonly Rule OnlyNumbers = (field, add) => add.All(char.IsNumber); public static readonly Rule LettersNumbers = (field, add) => add.All(c => char.IsLetter(c) || char.IsNumber(c)); - public NinePatch Texture; - public NinePatch HoveredTexture; - public Color HoveredColor; - public float TextScale; + public StyleProp Texture; + public StyleProp HoveredTexture; + public StyleProp HoveredColor; + public StyleProp TextScale; + public StyleProp Font; private readonly StringBuilder text = new StringBuilder(); public string Text => this.text.ToString(); public string PlaceholderText; public TextChanged OnTextChange; public float TextOffsetX = 4; - private IGenericFont font; private double caretBlinkTimer; private string displayedText; private int textOffset; @@ -50,7 +50,8 @@ namespace MLEM.Ui.Elements { public TextField(Anchor anchor, Vector2 size, Rule rule = null, IGenericFont font = null) : base(anchor, size) { this.InputRule = rule ?? DefaultRule; - this.font = font; + if (font != null) + this.Font.Set(font); if (WindowExtensions.SupportsTextInput()) { this.OnTextInput += (element, key, character) => { @@ -83,9 +84,9 @@ namespace MLEM.Ui.Elements { private void HandleTextChange(bool textChanged = true) { // not initialized yet - if (this.font == null) + if (this.Font.Value == null) return; - var length = this.font.MeasureString(this.text).X * this.TextScale; + var length = this.Font.Value.MeasureString(this.text).X * this.TextScale; var maxWidth = this.DisplayArea.Width / this.Scale - this.TextOffsetX * 2; if (length > maxWidth) { // if we're moving the caret to the left @@ -94,13 +95,13 @@ namespace MLEM.Ui.Elements { } else { // if we're moving the caret to the right var importantArea = this.text.ToString(this.textOffset, Math.Min(this.CaretPos, this.text.Length) - this.textOffset); - var bound = this.CaretPos - this.font.TruncateString(importantArea, maxWidth, this.TextScale, true).Length; + var bound = this.CaretPos - this.Font.Value.TruncateString(importantArea, maxWidth, this.TextScale, true).Length; if (this.textOffset < bound) { this.textOffset = bound; } } var visible = this.text.ToString(this.textOffset, this.text.Length - this.textOffset); - this.displayedText = this.font.TruncateString(visible, maxWidth, this.TextScale); + this.displayedText = this.Font.Value.TruncateString(visible, maxWidth, this.TextScale); } else { this.displayedText = this.Text; this.textOffset = 0; @@ -136,9 +137,9 @@ namespace MLEM.Ui.Elements { var tex = this.Texture; var color = Color.White * alpha; if (this.IsMouseOver) { - if (this.HoveredTexture != null) + if (this.HoveredTexture.Value != null) tex = this.HoveredTexture; - color = this.HoveredColor * alpha; + color = (Color) this.HoveredColor * alpha; } batch.Draw(tex, this.DisplayArea, color, this.Scale); @@ -146,9 +147,9 @@ namespace MLEM.Ui.Elements { if (this.text.Length > 0 || this.IsSelected) { var caret = this.IsSelected ? this.caretBlinkTimer >= 0.5F ? "|" : " " : ""; var display = this.displayedText.Insert(this.CaretPos - this.textOffset, caret); - this.font.DrawCenteredString(batch, display, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true); + this.Font.Value.DrawCenteredString(batch, display, textPos, this.TextScale * this.Scale, Color.White * alpha, false, true); } else if (this.PlaceholderText != null) { - this.font.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true); + this.Font.Value.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, Color.Gray * alpha, false, true); } base.Draw(time, batch, alpha, blendState, samplerState, matrix); } @@ -187,11 +188,11 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.TextScale = style.TextScale; - this.font = style.Font; - this.Texture = style.TextFieldTexture; - this.HoveredTexture = style.TextFieldHoveredTexture; - this.HoveredColor = style.TextFieldHoveredColor; + this.TextScale.SetFromStyle(style.TextScale); + this.Font.SetFromStyle(style.Font); + this.Texture.SetFromStyle(style.TextFieldTexture); + this.HoveredTexture.SetFromStyle(style.TextFieldHoveredTexture); + this.HoveredColor.SetFromStyle(style.TextFieldHoveredColor); } public delegate void TextChanged(TextField field, string text); diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index 785abc5..f47dcf7 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -48,8 +48,8 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Background = style.TooltipBackground; - this.BackgroundColor = style.TooltipBackgroundColor; + this.Background.SetFromStyle(style.TooltipBackground); + this.BackgroundColor.SetFromStyle(style.TooltipBackgroundColor); } } diff --git a/MLEM.Ui/Style/StyleProp.cs b/MLEM.Ui/Style/StyleProp.cs new file mode 100644 index 0000000..e1a7de6 --- /dev/null +++ b/MLEM.Ui/Style/StyleProp.cs @@ -0,0 +1,23 @@ +namespace MLEM.Ui.Style { + public struct StyleProp { + + public T Value { get; private set; } + private bool isCustom; + + public void SetFromStyle(T value) { + if (!this.isCustom) { + this.Value = value; + } + } + + public void Set(T value) { + this.isCustom = true; + this.Value = value; + } + + public static implicit operator T(StyleProp prop) { + return prop.Value; + } + + } +} \ No newline at end of file diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 217c559..dfcc70d 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -87,7 +87,7 @@ namespace MLEM.Ui { this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e)); this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e)); this.OnSelectedElementDrawn = (element, time, batch, alpha) => { - if (this.Controls.IsAutoNavMode && element.SelectionIndicator != null) { + if (this.Controls.IsAutoNavMode && element.SelectionIndicator.Value != null) { batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha, element.Scale / 2); } }; diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 21e17f7..9916e04 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -53,19 +53,15 @@ namespace Sandbox { var group = root.AddChild(new CustomDrawGroup(Anchor.AutoLeft, new Vector2(1, 10))); group.AddChild(new Button(Anchor.AutoLeft, Vector2.One, "Test text")); - this.progress = new ProgressBar(Anchor.Center, new Vector2(0.8F, 0.5F), Direction2.Down, 1) { - HasCustomStyle = true, - Texture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8), - Color = Color.White, - ProgressTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4), - ProgressColor = Color.White - }; + this.progress = new ProgressBar(Anchor.Center, new Vector2(0.8F, 0.5F), Direction2.Down, 1); + this.progress.Texture.Set(new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8)); + this.progress.ProgressTexture.Set(new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4)); this.UiSystem.Add("Progress", this.progress); } protected override void Update(GameTime gameTime) { base.Update(gameTime); - this.progress.CurrentValue = (float) (Math.Sin(gameTime.TotalGameTime.TotalSeconds/2) + 1) / 2; + this.progress.CurrentValue = (float) (Math.Sin(gameTime.TotalGameTime.TotalSeconds / 2) + 1) / 2; } protected override void DoDraw(GameTime gameTime) {