mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
Compare commits
No commits in common. "df2b9cc10e8120f24a3a86573bfca65e35968918" and "b41341d01e3852b32ef68eb4a2c2cd82bd531a73" have entirely different histories.
df2b9cc10e
...
b41341d01e
4 changed files with 29 additions and 47 deletions
|
@ -57,7 +57,6 @@ Additions
|
|||
- Added ScrollBar.MouseDragScrolling
|
||||
- Added Panel.ScrollToElement
|
||||
- Added ElementHelper.MakeGrid
|
||||
- Added Button.AutoDisableCondition
|
||||
|
||||
Improvements
|
||||
- Allow elements to auto-adjust their size even when their children are aligned oddly
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MLEM.Graphics;
|
||||
|
@ -52,12 +51,8 @@ namespace MLEM.Ui.Elements {
|
|||
/// <summary>
|
||||
/// Set this property to true to mark the button as disabled.
|
||||
/// A disabled button cannot be moused over, selected or pressed.
|
||||
/// If this value changes often, consider using <see cref="AutoDisableCondition"/>.
|
||||
/// </summary>
|
||||
public virtual bool IsDisabled {
|
||||
get => this.isDisabled || this.AutoDisableCondition?.Invoke(this) == true;
|
||||
set => this.isDisabled = value;
|
||||
}
|
||||
public virtual bool IsDisabled { get; set; }
|
||||
/// <summary>
|
||||
/// Whether this button's <see cref="Text"/> should be truncated if it exceeds this button's width.
|
||||
/// Defaults to false.
|
||||
|
@ -74,19 +69,12 @@ namespace MLEM.Ui.Elements {
|
|||
/// If this is true, <see cref="CanBeSelected"/> will be able to return true even if <see cref="IsDisabled"/> is true.
|
||||
/// </summary>
|
||||
public bool CanSelectDisabled;
|
||||
/// <summary>
|
||||
/// 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>
|
||||
public Func<Button, bool> AutoDisableCondition;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled);
|
||||
/// <inheritdoc />
|
||||
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
|
||||
|
||||
private bool isDisabled;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new button with the given settings
|
||||
/// </summary>
|
||||
|
|
|
@ -30,13 +30,17 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public TextureRegion Texture {
|
||||
get {
|
||||
var ret = this.GetTextureCallback?.Invoke(this) ?? this.texture;
|
||||
this.CheckTextureChange(ret);
|
||||
return ret;
|
||||
if (this.GetTextureCallback != null)
|
||||
this.Texture = this.GetTextureCallback(this);
|
||||
return this.texture;
|
||||
}
|
||||
set {
|
||||
this.texture = value;
|
||||
this.CheckTextureChange(value);
|
||||
if (this.texture != value) {
|
||||
this.texture = value;
|
||||
this.IsHidden = this.texture == null;
|
||||
if (this.scaleToImage)
|
||||
this.SetAreaDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
|
@ -71,12 +75,8 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public float ImageRotation;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsHidden => base.IsHidden || this.Texture == null;
|
||||
|
||||
private bool scaleToImage;
|
||||
private TextureRegion texture;
|
||||
private TextureRegion lastTexture;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new image with the given settings
|
||||
|
@ -123,15 +123,6 @@ namespace MLEM.Ui.Elements {
|
|||
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>
|
||||
/// A delegate method used for <see cref="Image.GetTextureCallback"/>
|
||||
/// </summary>
|
||||
|
|
|
@ -53,13 +53,20 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public string Text {
|
||||
get {
|
||||
var ret = this.GetTextCallback?.Invoke(this) ?? this.text;
|
||||
this.CheckTextChange(ret);
|
||||
return ret;
|
||||
this.QueryTextCallback();
|
||||
return this.text;
|
||||
}
|
||||
set {
|
||||
this.text = value;
|
||||
this.CheckTextChange(value);
|
||||
if (this.text != value) {
|
||||
this.text = value;
|
||||
this.SetTextDirty();
|
||||
|
||||
var force = string.IsNullOrWhiteSpace(this.text);
|
||||
if (this.forceHide != force) {
|
||||
this.forceHide = force;
|
||||
this.SetAreaDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
|
@ -98,12 +105,12 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsHidden => base.IsHidden || string.IsNullOrWhiteSpace(this.Text);
|
||||
public override bool IsHidden => base.IsHidden || this.forceHide;
|
||||
|
||||
private string text;
|
||||
private string lastText;
|
||||
private StyleProp<TextAlignment> alignment;
|
||||
private StyleProp<GenericFont> regularFont;
|
||||
private bool forceHide = true;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new paragraph with the given settings.
|
||||
|
@ -134,6 +141,7 @@ namespace MLEM.Ui.Elements {
|
|||
|
||||
/// <inheritdoc />
|
||||
public override void Update(GameTime time) {
|
||||
this.QueryTextCallback();
|
||||
base.Update(time);
|
||||
if (this.TokenizedText != null)
|
||||
this.TokenizedText.Update(time);
|
||||
|
@ -141,6 +149,7 @@ namespace MLEM.Ui.Elements {
|
|||
|
||||
/// <inheritdoc />
|
||||
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 sc = this.TextScale * this.TextScaleMultiplier * this.Scale;
|
||||
var color = this.TextColor.OrDefault(Color.White) * alpha;
|
||||
|
@ -193,14 +202,9 @@ namespace MLEM.Ui.Elements {
|
|||
this.SetAreaDirty();
|
||||
}
|
||||
|
||||
private void CheckTextChange(string newText) {
|
||||
if (this.lastText == newText)
|
||||
return;
|
||||
var emptyChanged = string.IsNullOrWhiteSpace(this.lastText) != string.IsNullOrWhiteSpace(newText);
|
||||
this.lastText = newText;
|
||||
if (emptyChanged)
|
||||
this.SetAreaDirty();
|
||||
this.SetTextDirty();
|
||||
private void QueryTextCallback() {
|
||||
if (this.GetTextCallback != null)
|
||||
this.Text = this.GetTextCallback(this);
|
||||
}
|
||||
|
||||
private float GetAlignmentOffset() {
|
||||
|
|
Loading…
Reference in a new issue