mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
avoid setting underlying values for ui element callbacks
This commit is contained in:
parent
45c668c992
commit
df2b9cc10e
3 changed files with 37 additions and 35 deletions
|
@ -52,14 +52,10 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set this property to true to mark the button as disabled.
|
/// Set this property to true to mark the button as disabled.
|
||||||
/// A disabled button cannot be moused over, selected or pressed.
|
/// A disabled button cannot be moused over, selected or pressed.
|
||||||
/// If this value changes often, consider using <see cref="AutoDisableCondition"/> to set it automatically.
|
/// If this value changes often, consider using <see cref="AutoDisableCondition"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool IsDisabled {
|
public virtual bool IsDisabled {
|
||||||
get {
|
get => this.isDisabled || this.AutoDisableCondition?.Invoke(this) == true;
|
||||||
if (this.AutoDisableCondition != null)
|
|
||||||
this.IsDisabled = this.AutoDisableCondition(this);
|
|
||||||
return this.isDisabled;
|
|
||||||
}
|
|
||||||
set => this.isDisabled = value;
|
set => this.isDisabled = value;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -79,7 +75,8 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool CanSelectDisabled;
|
public bool CanSelectDisabled;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An optional function that can be used to set <see cref="IsDisabled"/> automatically based on a user-defined condition. This removes the need to disable a button based on a condition in <see cref="Element.OnUpdated"/> or manually.
|
/// An optional function that can be used to modify the result of <see cref="IsDisabled"/> automatically based on a user-defined condition. This removes the need to disable a button based on a condition in <see cref="Element.OnUpdated"/> or manually.
|
||||||
|
/// Note that, if <see cref="IsDisabled"/>'s underlying value is set to <see langword="true"/> using <see cref="set_IsDisabled"/>, this function's result will be ignored.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<Button, bool> AutoDisableCondition;
|
public Func<Button, bool> AutoDisableCondition;
|
||||||
|
|
||||||
|
|
|
@ -30,17 +30,13 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TextureRegion Texture {
|
public TextureRegion Texture {
|
||||||
get {
|
get {
|
||||||
if (this.GetTextureCallback != null)
|
var ret = this.GetTextureCallback?.Invoke(this) ?? this.texture;
|
||||||
this.Texture = this.GetTextureCallback(this);
|
this.CheckTextureChange(ret);
|
||||||
return this.texture;
|
return ret;
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
if (this.texture != value) {
|
this.texture = value;
|
||||||
this.texture = value;
|
this.CheckTextureChange(value);
|
||||||
this.IsHidden = this.texture == null;
|
|
||||||
if (this.scaleToImage)
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -75,8 +71,12 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float ImageRotation;
|
public float ImageRotation;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override bool IsHidden => base.IsHidden || this.Texture == null;
|
||||||
|
|
||||||
private bool scaleToImage;
|
private bool scaleToImage;
|
||||||
private TextureRegion texture;
|
private TextureRegion texture;
|
||||||
|
private TextureRegion lastTexture;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new image with the given settings
|
/// Creates a new image with the given settings
|
||||||
|
@ -123,6 +123,15 @@ namespace MLEM.Ui.Elements {
|
||||||
base.Draw(time, batch, alpha, context);
|
base.Draw(time, batch, alpha, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void CheckTextureChange(TextureRegion newTexture) {
|
||||||
|
if (this.lastTexture == newTexture)
|
||||||
|
return;
|
||||||
|
var nullChanged = this.lastTexture == null != (newTexture == null);
|
||||||
|
this.lastTexture = newTexture;
|
||||||
|
if (nullChanged || this.scaleToImage)
|
||||||
|
this.SetAreaDirty();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate method used for <see cref="Image.GetTextureCallback"/>
|
/// A delegate method used for <see cref="Image.GetTextureCallback"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -53,20 +53,13 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Text {
|
public string Text {
|
||||||
get {
|
get {
|
||||||
this.QueryTextCallback();
|
var ret = this.GetTextCallback?.Invoke(this) ?? this.text;
|
||||||
return this.text;
|
this.CheckTextChange(ret);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
if (this.text != value) {
|
this.text = value;
|
||||||
this.text = value;
|
this.CheckTextChange(value);
|
||||||
this.SetTextDirty();
|
|
||||||
|
|
||||||
var force = string.IsNullOrWhiteSpace(this.text);
|
|
||||||
if (this.forceHide != force) {
|
|
||||||
this.forceHide = force;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -105,12 +98,12 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool IsHidden => base.IsHidden || this.forceHide;
|
public override bool IsHidden => base.IsHidden || string.IsNullOrWhiteSpace(this.Text);
|
||||||
|
|
||||||
private string text;
|
private string text;
|
||||||
|
private string lastText;
|
||||||
private StyleProp<TextAlignment> alignment;
|
private StyleProp<TextAlignment> alignment;
|
||||||
private StyleProp<GenericFont> regularFont;
|
private StyleProp<GenericFont> regularFont;
|
||||||
private bool forceHide = true;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new paragraph with the given settings.
|
/// Creates a new paragraph with the given settings.
|
||||||
|
@ -141,7 +134,6 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Update(GameTime time) {
|
public override void Update(GameTime time) {
|
||||||
this.QueryTextCallback();
|
|
||||||
base.Update(time);
|
base.Update(time);
|
||||||
if (this.TokenizedText != null)
|
if (this.TokenizedText != null)
|
||||||
this.TokenizedText.Update(time);
|
this.TokenizedText.Update(time);
|
||||||
|
@ -149,7 +141,6 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
|
||||||
this.QueryTextCallback();
|
|
||||||
var pos = this.DisplayArea.Location + new Vector2(this.GetAlignmentOffset(), 0);
|
var pos = this.DisplayArea.Location + new Vector2(this.GetAlignmentOffset(), 0);
|
||||||
var sc = this.TextScale * this.TextScaleMultiplier * this.Scale;
|
var sc = this.TextScale * this.TextScaleMultiplier * this.Scale;
|
||||||
var color = this.TextColor.OrDefault(Color.White) * alpha;
|
var color = this.TextColor.OrDefault(Color.White) * alpha;
|
||||||
|
@ -202,9 +193,14 @@ namespace MLEM.Ui.Elements {
|
||||||
this.SetAreaDirty();
|
this.SetAreaDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void QueryTextCallback() {
|
private void CheckTextChange(string newText) {
|
||||||
if (this.GetTextCallback != null)
|
if (this.lastText == newText)
|
||||||
this.Text = this.GetTextCallback(this);
|
return;
|
||||||
|
var emptyChanged = string.IsNullOrWhiteSpace(this.lastText) != string.IsNullOrWhiteSpace(newText);
|
||||||
|
this.lastText = newText;
|
||||||
|
if (emptyChanged)
|
||||||
|
this.SetAreaDirty();
|
||||||
|
this.SetTextDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private float GetAlignmentOffset() {
|
private float GetAlignmentOffset() {
|
||||||
|
|
Loading…
Reference in a new issue