1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-10-31 21:00:51 +01:00
MLEM/MLEM.Ui/Elements/Button.cs

134 lines
6.1 KiB
C#
Raw Normal View History

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2022-04-25 15:25:58 +02:00
using MLEM.Graphics;
2021-10-30 15:33:38 +02:00
using MLEM.Misc;
using MLEM.Textures;
2019-08-10 21:37:10 +02:00
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
2020-05-22 17:02:24 +02:00
/// <summary>
/// A button element for use inside of a <see cref="UiSystem"/>.
/// A button element can be pressed, hovered over and that can be disabled.
/// </summary>
public class Button : Element {
2020-05-22 17:02:24 +02:00
/// <summary>
/// The button's texture
/// </summary>
public StyleProp<NinePatch> Texture;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The color that the button draws its texture with
/// </summary>
2019-12-06 21:18:43 +01:00
public StyleProp<Color> NormalColor = Color.White;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The texture that the button uses while being moused over.
/// If this is null, it uses its default <see cref="Texture"/>.
/// </summary>
public StyleProp<NinePatch> HoveredTexture;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The color that the button uses for drawing while being moused over
/// </summary>
public StyleProp<Color> HoveredColor;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The texture that the button uses when it <see cref="IsDisabled"/>.
/// If this is null, it uses its default <see cref="Texture"/>.
/// </summary>
2020-01-15 17:05:28 +01:00
public StyleProp<NinePatch> DisabledTexture;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The color that the button uses for drawing when it <see cref="IsDisabled"/>
/// </summary>
2020-01-15 17:05:28 +01:00
public StyleProp<Color> DisabledColor;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The <see cref="Paragraph"/> of text that is displayed on the button.
/// Note that this is only nonnull by default if the constructor was passed a nonnull text.
/// </summary>
public Paragraph Text;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The <see cref="Tooltip"/> that is displayed when hovering over the button.
/// Note that this is only nonnull by default if the constructor was passed a nonnull tooltip text.
/// </summary>
2019-08-13 23:54:29 +02:00
public Tooltip Tooltip;
2020-05-22 17:02:24 +02:00
/// <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"/>.
2020-05-22 17:02:24 +02:00
/// </summary>
public virtual bool IsDisabled {
get => this.isDisabled || this.AutoDisableCondition?.Invoke(this) == true;
set => this.isDisabled = value;
}
/// <summary>
/// Whether this button's <see cref="Text"/> should be truncated if it exceeds this button's width.
/// Defaults to false.
/// </summary>
public bool TruncateTextIfLong {
get => this.Text?.TruncateIfLong ?? false;
set {
if (this.Text != null)
this.Text.TruncateIfLong = value;
}
}
/// <summary>
/// Whether this button should be able to be selected even if it <see cref="IsDisabled"/>.
/// 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="IsDisabled"/>, this function's result will be ignored.
/// </summary>
public Func<Button, bool> AutoDisableCondition;
2020-01-15 17:05:28 +01:00
/// <inheritdoc />
public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled);
/// <inheritdoc />
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
private bool isDisabled;
2020-05-22 17:02:24 +02:00
/// <summary>
/// Creates a new button with the given settings
/// </summary>
/// <param name="anchor">The button's anchor</param>
/// <param name="size">The button's size</param>
/// <param name="text">The text that should be displayed on the button</param>
/// <param name="tooltipText">The text that should be displayed in a <see cref="Tooltip"/> when hovering over this button</param>
public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
if (text != null) {
2021-10-30 15:33:38 +02:00
this.Text = new Paragraph(Anchor.Center, 1, text, true);
2021-12-21 11:54:32 +01:00
this.Text.Padding = this.Text.Padding.OrStyle(new Padding(1), 1);
this.AddChild(this.Text);
}
if (tooltipText != null)
this.Tooltip = this.AddTooltip(tooltipText);
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
2022-04-25 15:25:58 +02:00
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
var tex = this.Texture;
2019-12-06 21:18:43 +01:00
var color = (Color) this.NormalColor * alpha;
2020-01-15 17:05:28 +01:00
if (this.IsDisabled) {
tex = this.DisabledTexture.OrDefault(tex);
color = (Color) this.DisabledColor * alpha;
} else if (this.IsMouseOver) {
2019-11-05 13:28:41 +01:00
tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha;
}
batch.Draw(tex, this.DisplayArea, color, this.Scale);
2022-04-25 15:25:58 +02:00
base.Draw(time, batch, alpha, context);
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
2019-08-10 21:37:10 +02:00
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
2021-12-21 11:54:32 +01:00
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);
2019-08-10 21:37:10 +02:00
}
}
2022-06-17 18:23:47 +02:00
}