1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00
MLEM/MLEM.Ui/Elements/Checkbox.cs

157 lines
6.8 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2022-04-25 15:25:58 +02:00
using MLEM.Graphics;
using MLEM.Maths;
using MLEM.Textures;
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
2020-05-22 17:02:24 +02:00
/// <summary>
/// A checkbox element to use inside of a <see cref="UiSystem"/>.
/// 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 <see cref="RadioButton"/>.
/// </summary>
public class Checkbox : Element {
2020-05-22 17:02:24 +02:00
/// <summary>
/// The texture that this checkbox uses for drawing.
2020-05-22 17:02:24 +02:00
/// </summary>
public StyleProp<NinePatch> Texture;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The texture that this checkbox uses when it is hovered.
/// If this is null, the default <see cref="Texture"/> is used.
/// </summary>
public StyleProp<NinePatch> HoveredTexture;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The color that this checkbox uses for drawing when it is hovered.
/// </summary>
public StyleProp<Color> HoveredColor;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The texture that the checkbox uses when it <see cref="IsDisabled"/>.
/// If this is null, it uses its default <see cref="Texture"/>.
/// </summary>
public StyleProp<NinePatch> DisabledTexture;
/// <summary>
/// The color that the checkbox uses for drawing when it <see cref="IsDisabled"/>.
/// </summary>
public StyleProp<Color> DisabledColor;
/// <summary>
2020-05-22 17:02:24 +02:00
/// The texture that is rendered on top of this checkbox when it is <see cref="Checked"/>.
/// </summary>
public StyleProp<TextureRegion> Checkmark;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The label <see cref="Paragraph"/> that displays next to this checkbox
/// </summary>
public Paragraph Label;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The width of the space between this checkbox and its <see cref="Label"/>
/// </summary>
public StyleProp<float> TextOffsetX {
get => this.textOffsetX;
set {
this.textOffsetX = value;
this.SetAreaDirty();
}
}
2020-05-22 17:02:24 +02:00
/// <summary>
/// Whether or not this checkbox is currently checked.
/// </summary>
public bool Checked {
get => this.isChecked;
set {
if (this.isChecked != value) {
this.isChecked = value;
this.OnCheckStateChange?.Invoke(this, this.isChecked);
}
}
}
2020-05-22 17:02:24 +02:00
/// <summary>
/// Set this property to true to mark the checkbox as disabled.
/// A disabled checkbox cannot be moused over, selected or toggled.
/// </summary>
public virtual bool IsDisabled { get; set; }
/// <summary>
2020-05-22 17:02:24 +02:00
/// An event that is invoked when this checkbox's <see cref="Checked"/> property changes
/// </summary>
public CheckStateChange OnCheckStateChange;
/// <summary>
/// Whether this checkbox 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;
/// <inheritdoc />
public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled);
/// <inheritdoc />
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
private bool isChecked;
private StyleProp<float> textOffsetX;
2020-05-22 17:02:24 +02:00
/// <summary>
/// Creates a new checkbox with the given settings
/// </summary>
/// <param name="anchor">The checkbox's anchor</param>
/// <param name="size">The checkbox's size</param>
/// <param name="label">The checkbox's label text</param>
/// <param name="defaultChecked">The default value of <see cref="Checked"/></param>
public Checkbox(Anchor anchor, Vector2 size, string label, bool defaultChecked = false) : base(anchor, size) {
this.isChecked = defaultChecked;
this.OnPressed += element => this.Checked = !this.Checked;
if (label != null) {
2019-08-24 00:07:54 +02:00
this.Label = new Paragraph(Anchor.CenterLeft, 0, label);
this.AddChild(this.Label);
}
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
public override void SetAreaAndUpdateChildren(RectangleF area) {
base.SetAreaAndUpdateChildren(area);
2019-08-24 00:07:54 +02:00
if (this.Label != null) {
this.Label.Size = new Vector2((area.Width - area.Height) / this.Scale - this.TextOffsetX, 1);
this.Label.PositionOffset = new Vector2(area.Height / this.Scale + this.TextOffsetX, 0);
2019-08-24 00:07:54 +02:00
}
}
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;
var color = Color.White * alpha;
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;
}
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);
2022-04-25 15:25:58 +02:00
base.Draw(time, batch, alpha, context);
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
2021-12-21 11:54:32 +01:00
this.Texture = this.Texture.OrStyle(style.CheckboxTexture);
this.HoveredTexture = this.HoveredTexture.OrStyle(style.CheckboxHoveredTexture);
this.HoveredColor = this.HoveredColor.OrStyle(style.CheckboxHoveredColor);
this.DisabledTexture = this.DisabledTexture.OrStyle(style.CheckboxDisabledTexture);
this.DisabledColor = this.DisabledColor.OrStyle(style.CheckboxDisabledColor);
2021-12-21 11:54:32 +01:00
this.Checkmark = this.Checkmark.OrStyle(style.CheckboxCheckmark);
this.TextOffsetX = this.TextOffsetX.OrStyle(style.CheckboxTextOffsetX);
}
2020-05-22 17:02:24 +02:00
/// <summary>
/// A delegate used for <see cref="Checkbox.OnCheckStateChange"/>
/// </summary>
/// <param name="box">The checkbox whose checked state changed</param>
/// <param name="checced">The new value of <see cref="Checkbox.Checked"/></param>
public delegate void CheckStateChange(Checkbox box, bool checced);
}
2022-06-17 18:23:47 +02:00
}