mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
Made StyleProp immutable
This commit is contained in:
parent
c0ce5a07ad
commit
dae511e455
14 changed files with 80 additions and 77 deletions
|
@ -44,6 +44,7 @@ Improvements
|
||||||
- **Made Image ScaleToImage take ui scale into account**
|
- **Made Image ScaleToImage take ui scale into account**
|
||||||
- **Added style properties for a lot of hardcoded default element styles**
|
- **Added style properties for a lot of hardcoded default element styles**
|
||||||
- **Allow setting a custom effect and depth stencil state for ui drawing**
|
- **Allow setting a custom effect and depth stencil state for ui drawing**
|
||||||
|
- **Made StyleProp immutable**
|
||||||
- Exposed the epsilon value used by Element calculations
|
- 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 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
|
- Allow changing the entire ui style for a single element
|
||||||
|
|
|
@ -83,7 +83,7 @@ namespace MLEM.Ui.Elements {
|
||||||
public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
|
public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
|
||||||
if (text != null) {
|
if (text != null) {
|
||||||
this.Text = new Paragraph(Anchor.Center, 1, text, true);
|
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);
|
this.AddChild(this.Text);
|
||||||
}
|
}
|
||||||
if (tooltipText != null)
|
if (tooltipText != null)
|
||||||
|
@ -108,11 +108,11 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.Texture.SetFromStyle(style.ButtonTexture);
|
this.Texture = this.Texture.OrStyle(style.ButtonTexture);
|
||||||
this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture);
|
this.HoveredTexture = this.HoveredTexture.OrStyle(style.ButtonHoveredTexture);
|
||||||
this.HoveredColor.SetFromStyle(style.ButtonHoveredColor);
|
this.HoveredColor = this.HoveredColor.OrStyle(style.ButtonHoveredColor);
|
||||||
this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture);
|
this.DisabledTexture = this.DisabledTexture.OrStyle(style.ButtonDisabledTexture);
|
||||||
this.DisabledColor.SetFromStyle(style.ButtonDisabledColor);
|
this.DisabledColor = this.DisabledColor.OrStyle(style.ButtonDisabledColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,11 +102,11 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.Texture.SetFromStyle(style.CheckboxTexture);
|
this.Texture = this.Texture.OrStyle(style.CheckboxTexture);
|
||||||
this.HoveredTexture.SetFromStyle(style.CheckboxHoveredTexture);
|
this.HoveredTexture = this.HoveredTexture.OrStyle(style.CheckboxHoveredTexture);
|
||||||
this.HoveredColor.SetFromStyle(style.CheckboxHoveredColor);
|
this.HoveredColor = this.HoveredColor.OrStyle(style.CheckboxHoveredColor);
|
||||||
this.Checkmark.SetFromStyle(style.CheckboxCheckmark);
|
this.Checkmark = this.Checkmark.OrStyle(style.CheckboxCheckmark);
|
||||||
this.TextOffsetX.SetFromStyle(style.CheckboxTextOffsetX);
|
this.TextOffsetX = this.TextOffsetX.OrStyle(style.CheckboxTextOffsetX);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -1050,9 +1050,9 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="style">The new style</param>
|
/// <param name="style">The new style</param>
|
||||||
protected virtual void InitStyle(UiStyle style) {
|
protected virtual void InitStyle(UiStyle style) {
|
||||||
this.SelectionIndicator.SetFromStyle(style.SelectionIndicator);
|
this.SelectionIndicator = this.SelectionIndicator.OrStyle(style.SelectionIndicator);
|
||||||
this.ActionSound.SetFromStyle(style.ActionSound);
|
this.ActionSound = this.ActionSound.OrStyle(style.ActionSound);
|
||||||
this.SecondActionSound.SetFromStyle(style.ActionSound);
|
this.SecondActionSound = this.SecondActionSound.OrStyle(style.ActionSound);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -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) {
|
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 button = new Button(anchor, size, text, tooltipText);
|
||||||
var image = new Image(Anchor.CenterLeft, Vector2.One, texture);
|
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.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale;
|
||||||
button.AddChild(image, 0);
|
button.AddChild(image, 0);
|
||||||
return button;
|
return button;
|
||||||
|
|
|
@ -208,11 +208,11 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.Texture.SetFromStyle(style.PanelTexture);
|
this.Texture = this.Texture.OrStyle(style.PanelTexture);
|
||||||
this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll);
|
this.StepPerScroll = this.StepPerScroll.OrStyle(style.PanelStepPerScroll);
|
||||||
this.ScrollerSize.SetFromStyle(style.PanelScrollerSize);
|
this.ScrollerSize = this.ScrollerSize.OrStyle(style.PanelScrollerSize);
|
||||||
this.ScrollBarOffset.SetFromStyle(style.PanelScrollBarOffset);
|
this.ScrollBarOffset = this.ScrollBarOffset.OrStyle(style.PanelScrollBarOffset);
|
||||||
this.ChildPadding = this.ChildPadding.CopyFromStyle(style.PanelChildPadding);
|
this.ChildPadding = this.ChildPadding.OrStyle(style.PanelChildPadding);
|
||||||
this.SetScrollBarStyle();
|
this.SetScrollBarStyle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -157,9 +157,9 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(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.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.SetFromStyle(style.TextScale);
|
this.TextScale = this.TextScale.OrStyle(style.TextScale);
|
||||||
this.TextColor.SetFromStyle(style.TextColor);
|
this.TextColor = this.TextColor.OrStyle(style.TextColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -112,11 +112,11 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.Texture.SetFromStyle(style.ProgressBarTexture);
|
this.Texture = this.Texture.OrStyle(style.ProgressBarTexture);
|
||||||
this.Color.SetFromStyle(style.ProgressBarColor);
|
this.Color = this.Color.OrStyle(style.ProgressBarColor);
|
||||||
this.ProgressPadding.SetFromStyle(style.ProgressBarProgressPadding);
|
this.ProgressPadding = this.ProgressPadding.OrStyle(style.ProgressBarProgressPadding);
|
||||||
this.ProgressTexture.SetFromStyle(style.ProgressBarProgressTexture);
|
this.ProgressTexture = this.ProgressTexture.OrStyle(style.ProgressBarProgressTexture);
|
||||||
this.ProgressColor.SetFromStyle(style.ProgressBarProgressColor);
|
this.ProgressColor = this.ProgressColor.OrStyle(style.ProgressBarProgressColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,10 +39,10 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.Texture.SetFromStyle(style.RadioTexture);
|
this.Texture = this.Texture.OrStyle(style.RadioTexture);
|
||||||
this.HoveredTexture.SetFromStyle(style.RadioHoveredTexture);
|
this.HoveredTexture = this.HoveredTexture.OrStyle(style.RadioHoveredTexture);
|
||||||
this.HoveredColor.SetFromStyle(style.RadioHoveredColor);
|
this.HoveredColor = this.HoveredColor.OrStyle(style.RadioHoveredColor);
|
||||||
this.Checkmark.SetFromStyle(style.RadioCheckmark);
|
this.Checkmark = this.Checkmark.OrStyle(style.RadioCheckmark);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -217,10 +217,10 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.Background.SetFromStyle(style.ScrollBarBackground);
|
this.Background = this.Background.OrStyle(style.ScrollBarBackground);
|
||||||
this.ScrollerTexture.SetFromStyle(style.ScrollBarScrollerTexture);
|
this.ScrollerTexture = this.ScrollerTexture.OrStyle(style.ScrollBarScrollerTexture);
|
||||||
this.SmoothScrolling.SetFromStyle(style.ScrollBarSmoothScrolling);
|
this.SmoothScrolling = this.SmoothScrolling.OrStyle(style.ScrollBarSmoothScrolling);
|
||||||
this.SmoothScrollFactor.SetFromStyle(style.ScrollBarSmoothScrollFactor);
|
this.SmoothScrollFactor = this.SmoothScrollFactor.OrStyle(style.ScrollBarSmoothScrollFactor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -394,13 +394,13 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.TextScale.SetFromStyle(style.TextScale);
|
this.TextScale = this.TextScale.OrStyle(style.TextScale);
|
||||||
this.Font.SetFromStyle(style.Font);
|
this.Font = this.Font.OrStyle(style.Font);
|
||||||
this.Texture.SetFromStyle(style.TextFieldTexture);
|
this.Texture = this.Texture.OrStyle(style.TextFieldTexture);
|
||||||
this.HoveredTexture.SetFromStyle(style.TextFieldHoveredTexture);
|
this.HoveredTexture = this.HoveredTexture.OrStyle(style.TextFieldHoveredTexture);
|
||||||
this.HoveredColor.SetFromStyle(style.TextFieldHoveredColor);
|
this.HoveredColor = this.HoveredColor.OrStyle(style.TextFieldHoveredColor);
|
||||||
this.TextOffsetX.SetFromStyle(style.TextFieldTextOffsetX);
|
this.TextOffsetX = this.TextOffsetX.OrStyle(style.TextFieldTextOffsetX);
|
||||||
this.CaretWidth.SetFromStyle(style.TextFieldCaretWidth);
|
this.CaretWidth = this.CaretWidth.OrStyle(style.TextFieldCaretWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool FilterText(ref string text, bool removeMismatching) {
|
private bool FilterText(ref string text, bool removeMismatching) {
|
||||||
|
|
|
@ -76,12 +76,12 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void InitStyle(UiStyle style) {
|
protected override void InitStyle(UiStyle style) {
|
||||||
base.InitStyle(style);
|
base.InitStyle(style);
|
||||||
this.Texture.SetFromStyle(style.TooltipBackground);
|
this.Texture = this.Texture.OrStyle(style.TooltipBackground);
|
||||||
this.MouseOffset.SetFromStyle(style.TooltipOffset);
|
this.MouseOffset = this.MouseOffset.OrStyle(style.TooltipOffset);
|
||||||
this.Delay.SetFromStyle(style.TooltipDelay);
|
this.Delay = this.Delay.OrStyle(style.TooltipDelay);
|
||||||
this.ChildPadding = this.ChildPadding.CopyFromStyle(style.TooltipChildPadding);
|
this.ChildPadding = this.ChildPadding.OrStyle(style.TooltipChildPadding);
|
||||||
if (this.Paragraph != null) {
|
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);
|
this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,36 +9,31 @@ namespace MLEM.Ui.Style {
|
||||||
/// Note that <c>T</c> implicitly converts to <c>StyleProp{T}</c> and vice versa.
|
/// Note that <c>T</c> implicitly converts to <c>StyleProp{T}</c> and vice versa.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of style setting that this property stores</typeparam>
|
/// <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>
|
/// <summary>
|
||||||
/// The currently applied style
|
/// The currently applied style
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T Value { get; private set; }
|
public readonly T Value;
|
||||||
private byte priority;
|
private readonly byte priority;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new style property with the given custom style and a priority of <see cref="byte.MaxValue"/>.
|
/// 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>
|
/// </summary>
|
||||||
/// <param name="value">The custom style to apply</param>
|
/// <param name="value">The custom style to apply</param>
|
||||||
public StyleProp(T value) {
|
public StyleProp(T value) : this(value, byte.MaxValue) {
|
||||||
this.priority = byte.MaxValue;
|
|
||||||
this.Value = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private StyleProp(T value, byte priority) {
|
||||||
/// 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.Value = value;
|
||||||
this.priority = priority;
|
this.priority = priority;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a copy of this style property and sets its value and marks it as being set by a <see cref="UiStyle"/> if it doesn't have a custom value yet.
|
/// Creates a copy of this style property and sets its value and marks it as being set by a <see cref="UiStyle"/> if it doesn't have a custom value yet.
|
||||||
|
@ -46,12 +41,19 @@ namespace MLEM.Ui.Style {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The style to apply</param>
|
/// <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>
|
/// <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>
|
/// <returns>The style with the higher priority</returns>
|
||||||
/// <seealso cref="SetFromStyle"/>
|
public StyleProp<T> OrStyle(T value, byte priority = 0) {
|
||||||
public StyleProp<T> CopyFromStyle(T value, byte priority = 0) {
|
return this.OrStyle(new StyleProp<T>(value, priority));
|
||||||
var ret = this;
|
}
|
||||||
ret.SetFromStyle(value, priority);
|
|
||||||
return ret;
|
/// <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>
|
/// <summary>
|
||||||
|
@ -64,7 +66,7 @@ namespace MLEM.Ui.Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <returns>Whether this style property has a value</returns>
|
/// <returns>Whether this style property has a value</returns>
|
||||||
public bool HasValue() {
|
public bool HasValue() {
|
||||||
|
|
|
@ -77,15 +77,15 @@ namespace Tests {
|
||||||
public void TestStyle() {
|
public void TestStyle() {
|
||||||
var style = new StyleProp<string>();
|
var style = new StyleProp<string>();
|
||||||
Assert.AreEqual(null, style.Value);
|
Assert.AreEqual(null, style.Value);
|
||||||
style.SetFromStyle("from style");
|
style = style.OrStyle("from style");
|
||||||
Assert.AreEqual("from style", style.Value);
|
Assert.AreEqual("from style", style.Value);
|
||||||
style = "custom";
|
style = "custom";
|
||||||
Assert.AreEqual("custom", style.Value);
|
Assert.AreEqual("custom", style.Value);
|
||||||
style.SetFromStyle("from style again");
|
style = style.OrStyle("from style again");
|
||||||
Assert.AreEqual("custom", style.Value);
|
Assert.AreEqual("custom", style.Value);
|
||||||
|
|
||||||
var copy = style.CopyFromStyle("copy from style", byte.MaxValue);
|
var copy = style.OrStyle("copy from style", byte.MaxValue);
|
||||||
var weakCopy = style.CopyFromStyle("weak copy");
|
var weakCopy = style.OrStyle("weak copy");
|
||||||
Assert.AreEqual("copy from style", copy.Value);
|
Assert.AreEqual("copy from style", copy.Value);
|
||||||
Assert.AreEqual("custom", weakCopy.Value);
|
Assert.AreEqual("custom", weakCopy.Value);
|
||||||
Assert.AreEqual("custom", style.Value);
|
Assert.AreEqual("custom", style.Value);
|
||||||
|
|
Loading…
Reference in a new issue