1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Automatically set area dirty when changing child padding or paragraph fonts

This commit is contained in:
Ell 2021-12-21 00:01:57 +01:00
parent 79354c444b
commit 3541b8d3e1
8 changed files with 43 additions and 18 deletions

View file

@ -50,6 +50,7 @@ Improvements
- Skip unnecessary area updates for elements with dirty parents
- Calculate panel scroll bar height based on content height
- Remember the location that a scroll bar scroller was grabbed in when scrolling
- Automatically set area dirty when changing child padding or paragraph fonts
Fixes
- Fixed VerticalSpace height parameter being an integer

View file

@ -281,9 +281,14 @@ namespace MLEM.Ui.Elements {
/// <summary>
/// The child padding that this element has.
/// The child padding moves any <see cref="Children"/> added to this element inwards by the given amount in each direction.
/// When setting this style after this element has already been added to a ui, <see cref="SetAreaDirty"/> should be called.
/// </summary>
public StyleProp<Padding> ChildPadding;
public StyleProp<Padding> ChildPadding {
get => this.childPadding;
set {
this.childPadding = value;
this.SetAreaDirty();
}
}
/// <summary>
/// Event that is called after this element is drawn, but before its children are drawn
@ -405,6 +410,7 @@ namespace MLEM.Ui.Elements {
private bool isHidden;
private int priority;
private UiStyle style;
private StyleProp<Padding> childPadding;
/// <summary>
/// Creates a new element with the given anchor and size and sets up some default event reactions.

View file

@ -211,8 +211,8 @@ namespace MLEM.Ui.Elements {
this.Texture.SetFromStyle(style.PanelTexture);
this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll);
this.ScrollerSize.SetFromStyle(style.PanelScrollerSize);
this.ChildPadding.SetFromStyle(style.PanelChildPadding);
this.ScrollBarOffset.SetFromStyle(style.PanelScrollBarOffset);
this.ChildPadding = this.ChildPadding.CopyFromStyle(style.PanelChildPadding);
this.SetScrollBarStyle();
}

View file

@ -20,7 +20,14 @@ namespace MLEM.Ui.Elements {
/// The font that this paragraph draws text with.
/// To set its bold and italic font, use <see cref="GenericFont.Bold"/> and <see cref="GenericFont.Italic"/>.
/// </summary>
public StyleProp<GenericFont> RegularFont;
public StyleProp<GenericFont> RegularFont {
get => this.regularFont;
set {
this.regularFont = value;
this.SetAreaDirty();
this.TokenizedText = null;
}
}
/// <summary>
/// The tokenized version of the <see cref="Text"/>
/// </summary>
@ -53,7 +60,6 @@ namespace MLEM.Ui.Elements {
this.text = value;
this.IsHidden = string.IsNullOrWhiteSpace(this.text);
this.SetAreaDirty();
// force text to be re-tokenized
this.TokenizedText = null;
}
}
@ -96,6 +102,7 @@ namespace MLEM.Ui.Elements {
private string text;
private TextAlignment alignment;
private StyleProp<GenericFont> regularFont;
/// <summary>
/// Creates a new paragraph with the given settings.
@ -150,7 +157,7 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.RegularFont.SetFromStyle(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.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);
}

View file

@ -211,7 +211,7 @@ namespace MLEM.Ui.Elements {
this.InputRule = rule ?? DefaultRule;
this.Multiline = multiline;
if (font != null)
this.Font.Set(font);
this.Font = font;
if (text != null)
this.SetText(text, true);

View file

@ -79,7 +79,7 @@ namespace MLEM.Ui.Elements {
this.Texture.SetFromStyle(style.TooltipBackground);
this.MouseOffset.SetFromStyle(style.TooltipOffset);
this.Delay.SetFromStyle(style.TooltipDelay);
this.ChildPadding.SetFromStyle(style.TooltipChildPadding);
this.ChildPadding = this.ChildPadding.CopyFromStyle(style.TooltipChildPadding);
if (this.Paragraph != null) {
this.Paragraph.TextColor.SetFromStyle(style.TooltipTextColor, 1);
this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0);

View file

@ -26,11 +26,12 @@ namespace MLEM.Ui.Style {
}
/// <summary>
/// Sets this style property's value and marks it as being set by a <see cref="UiStyle"/>.
/// This allows this property to be overridden by custom style settings using <see cref="Set"/>.
/// 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.lastSetPriority) {
this.Value = value;
@ -39,13 +40,17 @@ namespace MLEM.Ui.Style {
}
/// <summary>
/// Sets this style property's value and marks it as being custom.
/// This causes <see cref="SetFromStyle"/> not to override the style value through a <see cref="UiStyle"/>.
/// 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.
/// 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"></param>
public void Set(T value) {
this.lastSetPriority = byte.MaxValue;
this.Value = value;
/// <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;
}
/// <summary>
@ -58,7 +63,7 @@ namespace MLEM.Ui.Style {
}
/// <summary>
/// Returns whether this style property has a value assigned to it using <see cref="SetFromStyle"/> or <see cref="Set"/>.
/// Returns whether this style property has a value assigned to it using <see cref="SetFromStyle"/> or <see cref="StyleProp{T}(T)"/>.
/// </summary>
/// <returns>Whether this style property has a value</returns>
public bool HasValue() {

View file

@ -79,10 +79,16 @@ namespace Tests {
Assert.AreEqual(null, style.Value);
style.SetFromStyle("from style");
Assert.AreEqual("from style", style.Value);
style.Set("custom");
style = "custom";
Assert.AreEqual("custom", style.Value);
style.SetFromStyle("from style again");
Assert.AreEqual("custom", style.Value);
var copy = style.CopyFromStyle("copy from style", byte.MaxValue);
var weakCopy = style.CopyFromStyle("weak copy");
Assert.AreEqual("copy from style", copy.Value);
Assert.AreEqual("custom", weakCopy.Value);
Assert.AreEqual("custom", style.Value);
}
[Test]