using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc;
using MLEM.Textures;
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
///
/// A button element for use inside of a .
/// A button element can be pressed, hovered over and that can be disabled.
///
public class Button : Element {
///
/// The button's texture
///
public StyleProp Texture;
///
/// The color that the button draws its texture with
///
public StyleProp NormalColor = Color.White;
///
/// The texture that the button uses while being moused over.
/// If this is null, it uses its default .
///
public StyleProp HoveredTexture;
///
/// The color that the button uses for drawing while being moused over
///
public StyleProp HoveredColor;
///
/// The texture that the button uses when it .
/// If this is null, it uses its default .
///
public StyleProp DisabledTexture;
///
/// The color that the button uses for drawing when it
///
public StyleProp DisabledColor;
///
/// The of text that is displayed on the button.
/// Note that this is only nonnull by default if the constructor was passed a nonnull text.
///
public Paragraph Text;
///
/// The 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.
///
public Tooltip Tooltip;
///
/// Set this property to true to mark the button as disabled.
/// A disabled button cannot be moused over, selected or pressed.
///
public bool IsDisabled {
get => this.isDisabled;
set {
this.isDisabled = value;
this.CanBePressed = !value;
this.CanBeSelected = !value;
}
}
///
/// Whether this button's should be truncated if it exceeds this button's width.
/// Defaults to false.
///
public bool TruncateTextIfLong {
get => this.Text?.TruncateIfLong ?? false;
set {
if (this.Text != null)
this.Text.TruncateIfLong = value;
}
}
private bool isDisabled;
///
/// Creates a new button with the given settings
///
/// The button's anchor
/// The button's size
/// The text that should be displayed on the button
/// The text that should be displayed in a when hovering over this button
public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
if (text != null) {
this.Text = new Paragraph(Anchor.Center, 1, text, true);
this.Text.Padding.SetFromStyle(new Padding(1), 1);
this.AddChild(this.Text);
}
if (tooltipText != null)
this.Tooltip = this.AddTooltip(tooltipText);
}
///
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var tex = this.Texture;
var color = (Color) this.NormalColor * alpha;
if (this.IsDisabled) {
tex = this.DisabledTexture.OrDefault(tex);
color = (Color) this.DisabledColor * alpha;
} else if (this.IsMouseOver) {
tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha;
}
batch.Draw(tex, this.DisplayArea, color, this.Scale);
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
}
///
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.ButtonTexture);
this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture);
this.HoveredColor.SetFromStyle(style.ButtonHoveredColor);
this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture);
this.DisabledColor.SetFromStyle(style.ButtonDisabledColor);
}
}
}