2019-08-09 22:04:26 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2021-10-30 15:33:38 +02:00
|
|
|
using MLEM.Misc;
|
2019-08-09 22:04:26 +02:00
|
|
|
using MLEM.Textures;
|
2019-08-10 21:37:10 +02:00
|
|
|
using MLEM.Ui.Style;
|
2019-08-09 22:04:26 +02:00
|
|
|
|
|
|
|
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>
|
2019-08-09 22:04:26 +02:00
|
|
|
public class Button : Element {
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The button's texture
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
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>
|
2019-10-14 21:28:12 +02:00
|
|
|
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>
|
2019-10-14 21:28:12 +02:00
|
|
|
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>
|
2019-08-13 21:23:20 +02:00
|
|
|
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.
|
|
|
|
/// </summary>
|
2020-01-15 17:05:28 +01:00
|
|
|
public bool IsDisabled {
|
|
|
|
get => this.isDisabled;
|
|
|
|
set {
|
|
|
|
this.isDisabled = value;
|
|
|
|
this.CanBePressed = !value;
|
|
|
|
this.CanBeSelected = !value;
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 16:47:38 +02:00
|
|
|
/// <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;
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 17:05:28 +01:00
|
|
|
|
2021-10-30 15:01:04 +02:00
|
|
|
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>
|
2021-10-29 23:33:15 +02:00
|
|
|
public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
|
2019-08-09 22:04:26 +02:00
|
|
|
if (text != null) {
|
2021-10-30 15:33:38 +02:00
|
|
|
this.Text = new Paragraph(Anchor.Center, 1, text, true);
|
|
|
|
this.Text.Padding.SetFromStyle(new Padding(1), 1);
|
2019-08-09 22:04:26 +02:00
|
|
|
this.AddChild(this.Text);
|
|
|
|
}
|
2019-08-24 20:45:40 +02:00
|
|
|
if (tooltipText != null)
|
2021-10-29 23:33:15 +02:00
|
|
|
this.Tooltip = this.AddTooltip(tooltipText);
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <inheritdoc />
|
2019-09-20 13:22:05 +02:00
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
|
2019-08-09 22:04:26 +02:00
|
|
|
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);
|
2019-10-14 21:28:12 +02:00
|
|
|
color = (Color) this.HoveredColor * alpha;
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
2019-09-04 17:19:31 +02:00
|
|
|
batch.Draw(tex, this.DisplayArea, color, this.Scale);
|
2019-09-20 13:22:05 +02:00
|
|
|
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2019-10-14 21:28:12 +02:00
|
|
|
this.Texture.SetFromStyle(style.ButtonTexture);
|
|
|
|
this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture);
|
|
|
|
this.HoveredColor.SetFromStyle(style.ButtonHoveredColor);
|
2020-01-15 17:05:28 +01:00
|
|
|
this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture);
|
|
|
|
this.DisabledColor.SetFromStyle(style.ButtonDisabledColor);
|
2019-08-10 21:37:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
}
|