From f28e23439207115aea61784bf867262c6c9683dc Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 5 Nov 2019 13:28:41 +0100 Subject: [PATCH] made style props a lot easier to use --- Demos/UiDemo.cs | 6 +++--- MLEM.Ui/Elements/Button.cs | 3 +-- MLEM.Ui/Elements/Checkbox.cs | 3 +-- MLEM.Ui/Elements/Dropdown.cs | 4 ++-- MLEM.Ui/Elements/Image.cs | 4 ++-- MLEM.Ui/Elements/Paragraph.cs | 2 +- MLEM.Ui/Elements/TextField.cs | 3 +-- MLEM.Ui/Style/StyleProp.cs | 15 +++++++++++++++ 8 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 2a66d2d..70910aa 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -81,11 +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.")); - var customButton = root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Change Style") { + 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) + PositionOffset = new Vector2(0, 1), + Texture = this.testPatch }); - 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 df8700a..02541d4 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -26,8 +26,7 @@ namespace MLEM.Ui.Elements { var tex = this.Texture; var color = Color.White * alpha; if (this.IsMouseOver) { - if (this.HoveredTexture.Value != null) - tex = this.HoveredTexture; + tex = this.HoveredTexture.OrDefault(tex); color = (Color) this.HoveredColor * alpha; } batch.Draw(tex, this.DisplayArea, color, this.Scale); diff --git a/MLEM.Ui/Elements/Checkbox.cs b/MLEM.Ui/Elements/Checkbox.cs index 2fe0055..783f812 100644 --- a/MLEM.Ui/Elements/Checkbox.cs +++ b/MLEM.Ui/Elements/Checkbox.cs @@ -51,8 +51,7 @@ namespace MLEM.Ui.Elements { var tex = this.Texture; var color = Color.White * alpha; if (this.IsMouseOver) { - if (this.HoveredTexture.Value != null) - tex = this.HoveredTexture; + tex = this.HoveredTexture.OrDefault(tex); color = (Color) this.HoveredColor * alpha; } diff --git a/MLEM.Ui/Elements/Dropdown.cs b/MLEM.Ui/Elements/Dropdown.cs index 043327e..e7c73e4 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.Set(Color.LightGray); - paragraph.OnMouseExit += e => paragraph.TextColor.Set(Color.White); + paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray; + paragraph.OnMouseExit += e => paragraph.TextColor = Color.White; this.AddElement(paragraph); } diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index 6bfba42..0e55ade 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -66,9 +66,9 @@ 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; + var color = this.Color.OrDefault(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 scale = Math.Min(this.DisplayArea.Width / this.texture.Width, this.DisplayArea.Height / 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 + center * scale + imageOffset, color, this.ImageRotation, center, scale * this.ImageScale, this.ImageEffects, 0); } else { diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 5ac2394..b3f97ea 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -81,7 +81,7 @@ namespace MLEM.Ui.Elements { var pos = this.DisplayArea.Location; var sc = this.TextScale * this.Scale; - var color = (this.TextColor.Value != default ? this.TextColor : Color.White) * alpha; + var color = this.TextColor.OrDefault(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.Value.DrawString(batch, this.splitText, pos, color, 0, Vector2.Zero, sc, SpriteEffects.None, 0); diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index b916e80..9e3a2c9 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -137,8 +137,7 @@ namespace MLEM.Ui.Elements { var tex = this.Texture; var color = Color.White * alpha; if (this.IsMouseOver) { - if (this.HoveredTexture.Value != null) - tex = this.HoveredTexture; + tex = this.HoveredTexture.OrDefault(tex); color = (Color) this.HoveredColor * alpha; } batch.Draw(tex, this.DisplayArea, color, this.Scale); diff --git a/MLEM.Ui/Style/StyleProp.cs b/MLEM.Ui/Style/StyleProp.cs index e1a7de6..eef277a 100644 --- a/MLEM.Ui/Style/StyleProp.cs +++ b/MLEM.Ui/Style/StyleProp.cs @@ -1,9 +1,16 @@ +using System.Collections.Generic; + namespace MLEM.Ui.Style { public struct StyleProp { public T Value { get; private set; } private bool isCustom; + public StyleProp(T value) { + this.isCustom = true; + this.Value = value; + } + public void SetFromStyle(T value) { if (!this.isCustom) { this.Value = value; @@ -15,9 +22,17 @@ namespace MLEM.Ui.Style { this.Value = value; } + public T OrDefault(T def) { + return EqualityComparer.Default.Equals(this.Value, default) ? def : this.Value; + } + public static implicit operator T(StyleProp prop) { return prop.Value; } + public static implicit operator StyleProp(T prop) { + return new StyleProp(prop); + } + } } \ No newline at end of file