2019-08-13 21:23:20 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2019-11-02 14:53:59 +01:00
|
|
|
using MLEM.Misc;
|
2019-08-13 21:23:20 +02:00
|
|
|
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>
|
2019-08-13 21:23:20 +02:00
|
|
|
public class Checkbox : Element {
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The texture that this checkbox uses for drawing
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
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>
|
2019-10-14 21:28:12 +02:00
|
|
|
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>
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<Color> HoveredColor;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The texture that is rendered on top of this checkbox when it is <see cref="Checked"/>.
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
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>
|
2019-08-13 21:23:20 +02:00
|
|
|
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>
|
2021-10-29 23:33:15 +02:00
|
|
|
public StyleProp<float> TextOffsetX;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Whether or not this checkbox is currently checked.
|
|
|
|
/// </summary>
|
2019-08-13 21:23:20 +02:00
|
|
|
public bool Checked {
|
|
|
|
get => this.checced;
|
|
|
|
set {
|
|
|
|
if (this.checced != value) {
|
|
|
|
this.checced = value;
|
|
|
|
this.OnCheckStateChange?.Invoke(this, this.checced);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// An event that is invoked when this checkbox's <see cref="Checked"/> property changes
|
|
|
|
/// </summary>
|
2019-08-13 21:23:20 +02:00
|
|
|
public CheckStateChange OnCheckStateChange;
|
|
|
|
|
2021-10-30 15:01:04 +02:00
|
|
|
private bool checced;
|
|
|
|
|
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>
|
2019-08-13 21:23:20 +02:00
|
|
|
public Checkbox(Anchor anchor, Vector2 size, string label, bool defaultChecked = false) : base(anchor, size) {
|
|
|
|
this.checced = defaultChecked;
|
2019-08-25 21:49:27 +02:00
|
|
|
this.OnPressed += element => this.Checked = !this.Checked;
|
2019-08-13 21:23:20 +02:00
|
|
|
|
|
|
|
if (label != null) {
|
2019-08-24 00:07:54 +02:00
|
|
|
this.Label = new Paragraph(Anchor.CenterLeft, 0, label);
|
2019-08-13 21:23:20 +02:00
|
|
|
this.AddChild(this.Label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <inheritdoc />
|
2019-11-02 14:53:59 +01:00
|
|
|
protected override Vector2 CalcActualSize(RectangleF parentArea) {
|
2019-08-13 21:23:20 +02:00
|
|
|
var size = base.CalcActualSize(parentArea);
|
2019-08-24 00:07:54 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-08-13 21:23:20 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <inheritdoc />
|
2021-11-22 17:42:08 +01:00
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, Effect effect, Matrix matrix) {
|
2019-08-13 21:23:20 +02:00
|
|
|
var tex = this.Texture;
|
|
|
|
var color = Color.White * alpha;
|
|
|
|
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-13 21:23:20 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:53:59 +01:00
|
|
|
var boxDisplayArea = new RectangleF(this.DisplayArea.Location, new Vector2(this.DisplayArea.Height));
|
2019-08-13 21:23:20 +02:00
|
|
|
batch.Draw(tex, boxDisplayArea, color, this.Scale);
|
|
|
|
if (this.Checked)
|
|
|
|
batch.Draw(this.Checkmark, boxDisplayArea, Color.White * alpha);
|
2021-11-22 17:42:08 +01:00
|
|
|
base.Draw(time, batch, alpha, blendState, samplerState, depthStencilState, effect, matrix);
|
2019-08-13 21:23:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <inheritdoc />
|
2019-08-13 21:23:20 +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.CheckboxTexture);
|
|
|
|
this.HoveredTexture = this.HoveredTexture.OrStyle(style.CheckboxHoveredTexture);
|
|
|
|
this.HoveredColor = this.HoveredColor.OrStyle(style.CheckboxHoveredColor);
|
|
|
|
this.Checkmark = this.Checkmark.OrStyle(style.CheckboxCheckmark);
|
|
|
|
this.TextOffsetX = this.TextOffsetX.OrStyle(style.CheckboxTextOffsetX);
|
2019-08-13 21:23:20 +02:00
|
|
|
}
|
|
|
|
|
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>
|
2019-08-13 21:23:20 +02:00
|
|
|
public delegate void CheckStateChange(Checkbox box, bool checced);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|