mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-14 02:09:08 +01:00
77 lines
No EOL
2.7 KiB
C#
77 lines
No EOL
2.7 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using MLEM.Extensions;
|
|
using MLEM.Input;
|
|
using MLEM.Textures;
|
|
using MLEM.Ui.Style;
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
public class Checkbox : Element {
|
|
|
|
public NinePatch Texture;
|
|
public NinePatch HoveredTexture;
|
|
public Color HoveredColor;
|
|
public TextureRegion Checkmark;
|
|
public Paragraph Label;
|
|
|
|
private bool checced;
|
|
public bool Checked {
|
|
get => this.checced;
|
|
set {
|
|
if (this.checced != value) {
|
|
this.checced = value;
|
|
this.OnCheckStateChange?.Invoke(this, this.checced);
|
|
}
|
|
}
|
|
}
|
|
public CheckStateChange OnCheckStateChange;
|
|
|
|
public Checkbox(Anchor anchor, Vector2 size, string label, bool defaultChecked = false) : base(anchor, size) {
|
|
this.checced = defaultChecked;
|
|
|
|
this.OnClicked += (element, button) => {
|
|
if (button == MouseButton.Left)
|
|
this.Checked = !this.Checked;
|
|
};
|
|
|
|
if (label != null) {
|
|
this.Label = new Paragraph(Anchor.CenterRight, 0, label, true);
|
|
this.AddChild(this.Label);
|
|
}
|
|
}
|
|
|
|
protected override Point CalcActualSize(Rectangle parentArea) {
|
|
var size = base.CalcActualSize(parentArea);
|
|
if (this.Label != null)
|
|
this.Label.Size = new Vector2((size.X - size.Y) / this.Scale, 1);
|
|
return size;
|
|
}
|
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
|
|
var tex = this.Texture;
|
|
var color = Color.White * alpha;
|
|
if (this.IsMouseOver) {
|
|
if (this.HoveredTexture != null)
|
|
tex = this.HoveredTexture;
|
|
color = this.HoveredColor * alpha;
|
|
}
|
|
|
|
var boxDisplayArea = new Rectangle(this.DisplayArea.Location, new Point(this.DisplayArea.Height)).OffsetCopy(offset);
|
|
batch.Draw(tex, boxDisplayArea, color, this.Scale);
|
|
if (this.Checked)
|
|
batch.Draw(this.Checkmark, boxDisplayArea, Color.White * alpha);
|
|
base.Draw(time, batch, alpha, offset);
|
|
}
|
|
|
|
protected override void InitStyle(UiStyle style) {
|
|
base.InitStyle(style);
|
|
this.Texture = style.CheckboxTexture;
|
|
this.HoveredTexture = style.CheckboxHoveredTexture;
|
|
this.HoveredColor = style.CheckboxHoveredColor;
|
|
this.Checkmark = style.CheckboxCheckmark;
|
|
}
|
|
|
|
public delegate void CheckStateChange(Checkbox box, bool checced);
|
|
|
|
}
|
|
} |