using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc;
using MLEM.Textures;
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
///
/// A checkbox element to use inside of a .
/// A checkbox can be checked by pressing it and will stay checked until it is pressed again.
/// For a checkbox that causes neighboring checkboxes to be deselected automatically, use .
///
public class Checkbox : Element {
///
/// The texture that this checkbox uses for drawing
///
public StyleProp Texture;
///
/// The texture that this checkbox uses when it is hovered.
/// If this is null, the default is used.
///
public StyleProp HoveredTexture;
///
/// The color that this checkbox uses for drawing when it is hovered.
///
public StyleProp HoveredColor;
///
/// The texture that is rendered on top of this checkbox when it is .
///
public StyleProp Checkmark;
///
/// The label that displays next to this checkbox
///
public Paragraph Label;
///
/// The width of the space between this checkbox and its
///
public StyleProp TextOffsetX;
///
/// Whether or not this checkbox is currently checked.
///
public bool Checked {
get => this.checced;
set {
if (this.checced != value) {
this.checced = value;
this.OnCheckStateChange?.Invoke(this, this.checced);
}
}
}
///
/// An event that is invoked when this checkbox's property changes
///
public CheckStateChange OnCheckStateChange;
private bool checced;
///
/// Creates a new checkbox with the given settings
///
/// The checkbox's anchor
/// The checkbox's size
/// The checkbox's label text
/// The default value of
public Checkbox(Anchor anchor, Vector2 size, string label, bool defaultChecked = false) : base(anchor, size) {
this.checced = defaultChecked;
this.OnPressed += element => this.Checked = !this.Checked;
if (label != null) {
this.Label = new Paragraph(Anchor.CenterLeft, 0, label);
this.AddChild(this.Label);
}
}
///
protected override Vector2 CalcActualSize(RectangleF parentArea) {
var size = base.CalcActualSize(parentArea);
if (this.Label != null) {
this.Label.Size = new Vector2((size.X - size.Y) / this.Scale - this.TextOffsetX, 1);
this.Label.PositionOffset = new Vector2(size.Y / this.Scale + this.TextOffsetX, 0);
}
return size;
}
///
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var tex = this.Texture;
var color = Color.White * alpha;
if (this.IsMouseOver) {
tex = this.HoveredTexture.OrDefault(tex);
color = (Color) this.HoveredColor * alpha;
}
var boxDisplayArea = new RectangleF(this.DisplayArea.Location, new Vector2(this.DisplayArea.Height));
batch.Draw(tex, boxDisplayArea, color, this.Scale);
if (this.Checked)
batch.Draw(this.Checkmark, boxDisplayArea, Color.White * alpha);
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
}
///
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.Texture.SetFromStyle(style.CheckboxTexture);
this.HoveredTexture.SetFromStyle(style.CheckboxHoveredTexture);
this.HoveredColor.SetFromStyle(style.CheckboxHoveredColor);
this.Checkmark.SetFromStyle(style.CheckboxCheckmark);
this.TextOffsetX.SetFromStyle(style.CheckboxTextOffsetX);
}
///
/// A delegate used for
///
/// The checkbox whose checked state changed
/// The new value of
public delegate void CheckStateChange(Checkbox box, bool checced);
}
}