From d8230649792c1fb601363d34f8db35b9906f271e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 6 Feb 2020 17:36:51 +0100 Subject: [PATCH] added HasValue() to styleprop --- MLEM.Ui/Elements/Panel.cs | 2 +- MLEM.Ui/Elements/ProgressBar.cs | 7 +++---- MLEM.Ui/Elements/TextField.cs | 2 +- MLEM.Ui/Style/StyleProp.cs | 6 +++++- MLEM.Ui/UiSystem.cs | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 7e3d926..b3806b3 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -132,7 +132,7 @@ namespace MLEM.Ui.Elements { } public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { - if (this.Texture.Value != null) + if (this.Texture.HasValue()) batch.Draw(this.Texture, this.DisplayArea, Color.White * alpha, this.Scale); // if we handle overflow, draw using the render target in DrawUnbound if (!this.scrollOverflow || this.renderTarget == null) { diff --git a/MLEM.Ui/Elements/ProgressBar.cs b/MLEM.Ui/Elements/ProgressBar.cs index 3b236d0..2466363 100644 --- a/MLEM.Ui/Elements/ProgressBar.cs +++ b/MLEM.Ui/Elements/ProgressBar.cs @@ -36,9 +36,8 @@ namespace MLEM.Ui.Elements { batch.Draw(this.Texture, this.DisplayArea, (Color) this.Color * alpha, this.Scale); var percentage = this.CurrentValue / this.MaxValue; - var tex = this.ProgressTexture.Value; - var padHor = tex != null ? tex.Padding.Width * this.Scale : 0; - var padVer = tex != null ? tex.Padding.Height * this.Scale : 0; + var padHor = this.ProgressTexture.HasValue() ? this.ProgressTexture.Value.Padding.Width * this.Scale : 0; + var padVer = this.ProgressTexture.HasValue() ? this.ProgressTexture.Value.Padding.Height * this.Scale : 0; var width = percentage * (this.DisplayArea.Width - padHor) + padHor; var height = percentage * (this.DisplayArea.Height - padVer) + padVer; RectangleF progressArea; @@ -61,7 +60,7 @@ namespace MLEM.Ui.Elements { break; } var offsetArea = progressArea.Shrink(this.ProgressPadding.Value * this.Scale); - if (this.ProgressTexture.Value != null) { + if (this.ProgressTexture.HasValue()) { batch.Draw(this.ProgressTexture, offsetArea, (Color) this.ProgressColor * alpha, this.Scale); } else { batch.Draw(batch.GetBlankTexture(), offsetArea, (Color) this.ProgressColor * alpha); diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 0cd8601..8364612 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -84,7 +84,7 @@ namespace MLEM.Ui.Elements { private void HandleTextChange(bool textChanged = true) { // not initialized yet - if (this.Font.Value == null) + if (!this.Font.HasValue()) return; var length = this.Font.Value.MeasureString(this.text).X * this.TextScale; var maxWidth = this.DisplayArea.Width / this.Scale - this.TextOffsetX * 2; diff --git a/MLEM.Ui/Style/StyleProp.cs b/MLEM.Ui/Style/StyleProp.cs index eef277a..6d50092 100644 --- a/MLEM.Ui/Style/StyleProp.cs +++ b/MLEM.Ui/Style/StyleProp.cs @@ -23,7 +23,11 @@ namespace MLEM.Ui.Style { } public T OrDefault(T def) { - return EqualityComparer.Default.Equals(this.Value, default) ? def : this.Value; + return this.HasValue() ? this.Value : def; + } + + public bool HasValue() { + return !EqualityComparer.Default.Equals(this.Value, default); } public static implicit operator T(StyleProp prop) { diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index b01059c..ed91cd1 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -85,7 +85,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.Value != null) { + if (this.Controls.IsAutoNavMode && element.SelectionIndicator.HasValue()) { batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha, element.Scale / 2); } };