1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-09 19:18:44 +02:00

Made StyleProp immutable

This commit is contained in:
Ell 2021-12-21 11:54:32 +01:00
parent c0ce5a07ad
commit dae511e455
14 changed files with 80 additions and 77 deletions

View file

@ -44,6 +44,7 @@ Improvements
- **Made Image ScaleToImage take ui scale into account**
- **Added style properties for a lot of hardcoded default element styles**
- **Allow setting a custom effect and depth stencil state for ui drawing**
- **Made StyleProp immutable**
- Exposed the epsilon value used by Element calculations
- Allow style properties to set style values with a higher priority, which allows elements to style their default children
- Allow changing the entire ui style for a single element

View file

@ -83,7 +83,7 @@ namespace MLEM.Ui.Elements {
public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
if (text != null) {
this.Text = new Paragraph(Anchor.Center, 1, text, true);
this.Text.Padding.SetFromStyle(new Padding(1), 1);
this.Text.Padding = this.Text.Padding.OrStyle(new Padding(1), 1);
this.AddChild(this.Text);
}
if (tooltipText != null)
@ -108,11 +108,11 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.ButtonTexture);
this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture);
this.HoveredColor.SetFromStyle(style.ButtonHoveredColor);
this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture);
this.DisabledColor.SetFromStyle(style.ButtonDisabledColor);
this.Texture = this.Texture.OrStyle(style.ButtonTexture);
this.HoveredTexture = this.HoveredTexture.OrStyle(style.ButtonHoveredTexture);
this.HoveredColor = this.HoveredColor.OrStyle(style.ButtonHoveredColor);
this.DisabledTexture = this.DisabledTexture.OrStyle(style.ButtonDisabledTexture);
this.DisabledColor = this.DisabledColor.OrStyle(style.ButtonDisabledColor);
}
}

View file

@ -102,11 +102,11 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.CheckboxTexture);
this.HoveredTexture.SetFromStyle(style.CheckboxHoveredTexture);
this.HoveredColor.SetFromStyle(style.CheckboxHoveredColor);
this.Checkmark.SetFromStyle(style.CheckboxCheckmark);
this.TextOffsetX.SetFromStyle(style.CheckboxTextOffsetX);
this.Texture = this.Texture.OrStyle(style.CheckboxTexture);
this.HoveredTexture = this.HoveredTexture.OrStyle(style.CheckboxHoveredTexture);
this.HoveredColor = this.HoveredColor.OrStyle(style.CheckboxHoveredColor);
this.Checkmark = this.Checkmark.OrStyle(style.CheckboxCheckmark);
this.TextOffsetX = this.TextOffsetX.OrStyle(style.CheckboxTextOffsetX);
}
/// <summary>

View file

@ -1050,9 +1050,9 @@ namespace MLEM.Ui.Elements {
/// </summary>
/// <param name="style">The new style</param>
protected virtual void InitStyle(UiStyle style) {
this.SelectionIndicator.SetFromStyle(style.SelectionIndicator);
this.ActionSound.SetFromStyle(style.ActionSound);
this.SecondActionSound.SetFromStyle(style.ActionSound);
this.SelectionIndicator = this.SelectionIndicator.OrStyle(style.SelectionIndicator);
this.ActionSound = this.ActionSound.OrStyle(style.ActionSound);
this.SecondActionSound = this.SecondActionSound.OrStyle(style.ActionSound);
}
/// <summary>

View file

@ -24,7 +24,7 @@ namespace MLEM.Ui.Elements {
public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float imagePadding = 2) {
var button = new Button(anchor, size, text, tooltipText);
var image = new Image(Anchor.CenterLeft, Vector2.One, texture);
image.Padding.SetFromStyle(new Padding(imagePadding), 1);
image.Padding = image.Padding.OrStyle(new Padding(imagePadding), 1);
button.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale;
button.AddChild(image, 0);
return button;

View file

@ -208,11 +208,11 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.PanelTexture);
this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll);
this.ScrollerSize.SetFromStyle(style.PanelScrollerSize);
this.ScrollBarOffset.SetFromStyle(style.PanelScrollBarOffset);
this.ChildPadding = this.ChildPadding.CopyFromStyle(style.PanelChildPadding);
this.Texture = this.Texture.OrStyle(style.PanelTexture);
this.StepPerScroll = this.StepPerScroll.OrStyle(style.PanelStepPerScroll);
this.ScrollerSize = this.ScrollerSize.OrStyle(style.PanelScrollerSize);
this.ScrollBarOffset = this.ScrollBarOffset.OrStyle(style.PanelScrollBarOffset);
this.ChildPadding = this.ChildPadding.OrStyle(style.PanelChildPadding);
this.SetScrollBarStyle();
}

View file

@ -157,9 +157,9 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.RegularFont = this.RegularFont.CopyFromStyle(style.Font ?? throw new NotSupportedException("Paragraphs cannot use ui styles that don't have a font. Please supply a custom font by setting UiStyle.Font."));
this.TextScale.SetFromStyle(style.TextScale);
this.TextColor.SetFromStyle(style.TextColor);
this.RegularFont = this.RegularFont.OrStyle(style.Font ?? throw new NotSupportedException("Paragraphs cannot use ui styles that don't have a font. Please supply a custom font by setting UiStyle.Font."));
this.TextScale = this.TextScale.OrStyle(style.TextScale);
this.TextColor = this.TextColor.OrStyle(style.TextColor);
}
/// <summary>

View file

@ -112,11 +112,11 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
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);
this.Texture = this.Texture.OrStyle(style.ProgressBarTexture);
this.Color = this.Color.OrStyle(style.ProgressBarColor);
this.ProgressPadding = this.ProgressPadding.OrStyle(style.ProgressBarProgressPadding);
this.ProgressTexture = this.ProgressTexture.OrStyle(style.ProgressBarProgressTexture);
this.ProgressColor = this.ProgressColor.OrStyle(style.ProgressBarProgressColor);
}
}

View file

@ -39,10 +39,10 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.RadioTexture);
this.HoveredTexture.SetFromStyle(style.RadioHoveredTexture);
this.HoveredColor.SetFromStyle(style.RadioHoveredColor);
this.Checkmark.SetFromStyle(style.RadioCheckmark);
this.Texture = this.Texture.OrStyle(style.RadioTexture);
this.HoveredTexture = this.HoveredTexture.OrStyle(style.RadioHoveredTexture);
this.HoveredColor = this.HoveredColor.OrStyle(style.RadioHoveredColor);
this.Checkmark = this.Checkmark.OrStyle(style.RadioCheckmark);
}
}

View file

@ -217,10 +217,10 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Background.SetFromStyle(style.ScrollBarBackground);
this.ScrollerTexture.SetFromStyle(style.ScrollBarScrollerTexture);
this.SmoothScrolling.SetFromStyle(style.ScrollBarSmoothScrolling);
this.SmoothScrollFactor.SetFromStyle(style.ScrollBarSmoothScrollFactor);
this.Background = this.Background.OrStyle(style.ScrollBarBackground);
this.ScrollerTexture = this.ScrollerTexture.OrStyle(style.ScrollBarScrollerTexture);
this.SmoothScrolling = this.SmoothScrolling.OrStyle(style.ScrollBarSmoothScrolling);
this.SmoothScrollFactor = this.SmoothScrollFactor.OrStyle(style.ScrollBarSmoothScrollFactor);
}
/// <summary>

View file

@ -394,13 +394,13 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
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);
this.TextOffsetX.SetFromStyle(style.TextFieldTextOffsetX);
this.CaretWidth.SetFromStyle(style.TextFieldCaretWidth);
this.TextScale = this.TextScale.OrStyle(style.TextScale);
this.Font = this.Font.OrStyle(style.Font);
this.Texture = this.Texture.OrStyle(style.TextFieldTexture);
this.HoveredTexture = this.HoveredTexture.OrStyle(style.TextFieldHoveredTexture);
this.HoveredColor = this.HoveredColor.OrStyle(style.TextFieldHoveredColor);
this.TextOffsetX = this.TextOffsetX.OrStyle(style.TextFieldTextOffsetX);
this.CaretWidth = this.CaretWidth.OrStyle(style.TextFieldCaretWidth);
}
private bool FilterText(ref string text, bool removeMismatching) {

View file

@ -76,12 +76,12 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.TooltipBackground);
this.MouseOffset.SetFromStyle(style.TooltipOffset);
this.Delay.SetFromStyle(style.TooltipDelay);
this.ChildPadding = this.ChildPadding.CopyFromStyle(style.TooltipChildPadding);
this.Texture = this.Texture.OrStyle(style.TooltipBackground);
this.MouseOffset = this.MouseOffset.OrStyle(style.TooltipOffset);
this.Delay = this.Delay.OrStyle(style.TooltipDelay);
this.ChildPadding = this.ChildPadding.OrStyle(style.TooltipChildPadding);
if (this.Paragraph != null) {
this.Paragraph.TextColor.SetFromStyle(style.TooltipTextColor, 1);
this.Paragraph.TextColor = this.Paragraph.TextColor.OrStyle(style.TooltipTextColor, 1);
this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0);
}
}

View file

@ -9,35 +9,30 @@ namespace MLEM.Ui.Style {
/// Note that <c>T</c> implicitly converts to <c>StyleProp{T}</c> and vice versa.
/// </summary>
/// <typeparam name="T">The type of style setting that this property stores</typeparam>
public struct StyleProp<T> : IEquatable<StyleProp<T>> {
public readonly struct StyleProp<T> : IEquatable<StyleProp<T>> {
/// <summary>
/// The empty style property, with no <see cref="Value"/> and a priority of 0.
/// </summary>
public static StyleProp<T> None => default;
/// <summary>
/// The currently applied style
/// </summary>
public T Value { get; private set; }
private byte priority;
public readonly T Value;
private readonly byte priority;
/// <summary>
/// Creates a new style property with the given custom style and a priority of <see cref="byte.MaxValue"/>.
/// To create a style property with a lower priority, use <see cref="OrStyle(T,byte)"/> on an existing priority, or use <see cref="None"/>.
/// </summary>
/// <param name="value">The custom style to apply</param>
public StyleProp(T value) {
this.priority = byte.MaxValue;
this.Value = value;
public StyleProp(T value) : this(value, byte.MaxValue) {
}
/// <summary>
/// Sets this style property's value and marks it as being set by a <see cref="UiStyle"/> if it doesn't have a custom value yet.
/// This allows this property to be overridden by custom style settings using <see cref="StyleProp{T}(T)"/> or a higher <paramref name="priority"/>.
/// </summary>
/// <param name="value">The style to apply</param>
/// <param name="priority">The priority that this style value has. Higher priority style values will override lower priority style values.</param>
///<seealso cref="CopyFromStyle"/>
public void SetFromStyle(T value, byte priority = 0) {
if (priority >= this.priority) {
this.Value = value;
this.priority = priority;
}
private StyleProp(T value, byte priority) {
this.Value = value;
this.priority = priority;
}
/// <summary>
@ -46,12 +41,19 @@ namespace MLEM.Ui.Style {
/// </summary>
/// <param name="value">The style to apply</param>
/// <param name="priority">The priority that the style value has. Higher priority style values will override lower priority style values.</param>
/// <returns>The new style</returns>
/// <seealso cref="SetFromStyle"/>
public StyleProp<T> CopyFromStyle(T value, byte priority = 0) {
var ret = this;
ret.SetFromStyle(value, priority);
return ret;
/// <returns>The style with the higher priority</returns>
public StyleProp<T> OrStyle(T value, byte priority = 0) {
return this.OrStyle(new StyleProp<T>(value, priority));
}
/// <summary>
/// Chooses and returns the style property with the higher priority, out of this value and <paramref name="other"/>.
/// This allows this property to be overridden by custom style settings using <see cref="StyleProp{T}(T)"/> or a higher priority.
/// </summary>
/// <param name="other">The style property to compare with</param>
/// <returns>The style property with the higher priority</returns>
public StyleProp<T> OrStyle(StyleProp<T> other) {
return other.priority >= this.priority ? other : this;
}
/// <summary>
@ -64,7 +66,7 @@ namespace MLEM.Ui.Style {
}
/// <summary>
/// Returns whether this style property has a value assigned to it using <see cref="SetFromStyle"/> or <see cref="StyleProp{T}(T)"/>.
/// Returns whether this style property has a value assigned to it using <see cref="OrStyle(StyleProp{T})"/> or <see cref="StyleProp{T}(T)"/>.
/// </summary>
/// <returns>Whether this style property has a value</returns>
public bool HasValue() {

View file

@ -77,15 +77,15 @@ namespace Tests {
public void TestStyle() {
var style = new StyleProp<string>();
Assert.AreEqual(null, style.Value);
style.SetFromStyle("from style");
style = style.OrStyle("from style");
Assert.AreEqual("from style", style.Value);
style = "custom";
Assert.AreEqual("custom", style.Value);
style.SetFromStyle("from style again");
style = style.OrStyle("from style again");
Assert.AreEqual("custom", style.Value);
var copy = style.CopyFromStyle("copy from style", byte.MaxValue);
var weakCopy = style.CopyFromStyle("weak copy");
var copy = style.OrStyle("copy from style", byte.MaxValue);
var weakCopy = style.OrStyle("weak copy");
Assert.AreEqual("copy from style", copy.Value);
Assert.AreEqual("custom", weakCopy.Value);
Assert.AreEqual("custom", style.Value);