From 7e49eaef10cd806a61a9a159b4ea59c657cf7439 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 24 Dec 2021 12:10:04 +0100 Subject: [PATCH] Allow for checkboxes and radio buttons to be disabled --- CHANGELOG.md | 4 ++++ Demos/UiDemo.cs | 1 + MLEM.Ui/Elements/Checkbox.cs | 43 +++++++++++++++++++++++++++++------- MLEM.Ui/Style/UiStyle.cs | 8 +++++++ MLEM/Font/GenericFont.cs | 2 +- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98db3bb..6f7852a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ Additions Improvements - Generify GenericFont's string drawing +### MLEM.Ui +Improvements +- Allow for checkboxes and radio buttons to be disabled + ## 5.2.0 ### MLEM Additions diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 989c0d9..ce19001 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -207,6 +207,7 @@ namespace Demos { this.root.AddChild(new VerticalSpace(3)); this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled button", "This button can't be clicked or moved to using automatic navigation") {IsDisabled = true}).PositionOffset = new Vector2(0, 1); + this.root.AddChild(new Checkbox(Anchor.AutoLeft, new Vector2(1, 10), "Disabled checkbox") {IsDisabled = true}).PositionOffset = new Vector2(0, 1); const string alignText = "Paragraphs can have left aligned text, right aligned text and center aligned text."; this.root.AddChild(new VerticalSpace(3)); diff --git a/MLEM.Ui/Elements/Checkbox.cs b/MLEM.Ui/Elements/Checkbox.cs index 2fdff3a..b1de9e7 100644 --- a/MLEM.Ui/Elements/Checkbox.cs +++ b/MLEM.Ui/Elements/Checkbox.cs @@ -13,7 +13,7 @@ namespace MLEM.Ui.Elements { public class Checkbox : Element { /// - /// The texture that this checkbox uses for drawing + /// The texture that this checkbox uses for drawing. /// public StyleProp Texture; /// @@ -26,6 +26,15 @@ namespace MLEM.Ui.Elements { /// public StyleProp HoveredColor; /// + /// The texture that the checkbox uses when it . + /// If this is null, it uses its default . + /// + public StyleProp DisabledTexture; + /// + /// The color that the checkbox uses for drawing when it . + /// + public StyleProp DisabledColor; + /// /// The texture that is rendered on top of this checkbox when it is . /// public StyleProp Checkmark; @@ -41,20 +50,33 @@ namespace MLEM.Ui.Elements { /// Whether or not this checkbox is currently checked. /// public bool Checked { - get => this.checced; + get => this.isChecked; set { - if (this.checced != value) { - this.checced = value; - this.OnCheckStateChange?.Invoke(this, this.checced); + if (this.isChecked != value) { + this.isChecked = value; + this.OnCheckStateChange?.Invoke(this, this.isChecked); } } } /// + /// Set this property to true to mark the checkbox as disabled. + /// A disabled checkbox cannot be moused over, selected or toggled. + /// + public bool IsDisabled { + get => this.isDisabled; + set { + this.isDisabled = value; + this.CanBePressed = !value; + this.CanBeSelected = !value; + } + } + /// /// An event that is invoked when this checkbox's property changes /// public CheckStateChange OnCheckStateChange; - private bool checced; + private bool isChecked; + private bool isDisabled; /// /// Creates a new checkbox with the given settings @@ -64,7 +86,7 @@ namespace MLEM.Ui.Elements { /// 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.isChecked = defaultChecked; this.OnPressed += element => this.Checked = !this.Checked; if (label != null) { @@ -87,7 +109,10 @@ namespace MLEM.Ui.Elements { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, Effect effect, Matrix matrix) { var tex = this.Texture; var color = Color.White * alpha; - if (this.IsMouseOver) { + 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; } @@ -105,6 +130,8 @@ namespace MLEM.Ui.Elements { 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); this.Checkmark = this.Checkmark.OrStyle(style.CheckboxCheckmark); this.TextOffsetX = this.TextOffsetX.OrStyle(style.CheckboxTextOffsetX); } diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index b9727c9..8aa7451 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -110,6 +110,14 @@ namespace MLEM.Ui.Style { /// public Color CheckboxHoveredColor = Color.LightGray; /// + /// The texture that the element uses when it . + /// + public NinePatch CheckboxDisabledTexture; + /// + /// The color that the element uses when it . + /// + public Color CheckboxDisabledColor = Color.Gray; + /// /// The texture that the element renders on top of its regular texture when it is /// public TextureRegion CheckboxCheckmark; diff --git a/MLEM/Font/GenericFont.cs b/MLEM/Font/GenericFont.cs index 6961637..fa55078 100644 --- a/MLEM/Font/GenericFont.cs +++ b/MLEM/Font/GenericFont.cs @@ -26,7 +26,7 @@ namespace MLEM.Font { public const char Nbsp = '\u00A0'; /// /// This field holds the unicode representation of a zero-width space. - /// Whereas a regular would have to explicitly support this character for width calculations and string splitting, generic fonts implicitly support it in and . + /// Whereas a regular would have to explicitly support this character for width calculations and string splitting, generic fonts implicitly support it in and . /// public const char Zwsp = '\u200B';