using Microsoft.Xna.Framework; using MLEM.Ui.Style; namespace MLEM.Ui.Elements { /// /// A radio button element to use inside of a . /// A radio button is a variation of a that causes all other radio buttons in the same to be deselected upon selection. /// public class RadioButton : Checkbox { /// /// The group that this radio button has. /// All other radio buttons in the same that have the same group will be deselected when this radio button is selected. /// public string Group; /// /// Creates a new radio button with the given settings /// /// The radio button's anchor /// The radio button's size /// The label to display next to the radio button /// If the radio button should be checked by default /// The group that the radio button has public RadioButton(Anchor anchor, Vector2 size, string label, bool defaultChecked = false, string group = "") : base(anchor, size, label, defaultChecked) { this.Group = group; // don't += because we want to override the checking/unchecking behavior of Checkbox this.OnPressed = element => { this.Checked = true; this.Root.Element.AndChildren(e => { if (e != this && e is RadioButton r && r.Group == this.Group) r.Checked = false; }); }; } /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); this.Texture = this.Texture.OrStyle(style.RadioTexture); this.HoveredTexture = this.HoveredTexture.OrStyle(style.RadioHoveredTexture); this.HoveredColor = this.HoveredColor.OrStyle(style.RadioHoveredColor); this.Checkmark = this.Checkmark.OrStyle(style.RadioCheckmark); } } }