2019-08-13 21:23:20 +02:00
using Microsoft.Xna.Framework ;
using MLEM.Ui.Style ;
namespace MLEM.Ui.Elements {
2020-05-22 17:02:24 +02:00
/// <summary>
/// A radio button element to use inside of a <see cref="UiSystem"/>.
/// A radio button is a variation of a <see cref="Checkbox"/> that causes all other radio buttons in the same <see cref="Group"/> to be deselected upon selection.
/// </summary>
2019-08-13 21:23:20 +02:00
public class RadioButton : Checkbox {
2020-05-22 17:02:24 +02:00
/// <summary>
/// The group that this radio button has.
/// All other radio buttons in the same <see cref="RootElement"/> that have the same group will be deselected when this radio button is selected.
/// </summary>
2019-08-13 21:23:20 +02:00
public string Group ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// Creates a new radio button with the given settings
/// </summary>
/// <param name="anchor">The radio button's anchor</param>
/// <param name="size">The radio button's size</param>
/// <param name="label">The label to display next to the radio button</param>
/// <param name="defaultChecked">If the radio button should be checked by default</param>
/// <param name="group">The group that the radio button has</param>
2019-08-13 21:23:20 +02:00
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
2019-08-25 21:49:27 +02:00
this . OnPressed = element = > {
this . Checked = true ;
2019-09-12 12:39:18 +02:00
foreach ( var sib in this . GetSiblings ( ) ) {
2019-08-25 21:49:27 +02:00
if ( sib is RadioButton radio & & radio . Group = = this . Group )
radio . Checked = false ;
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 . RadioTexture ) ;
this . HoveredTexture = this . HoveredTexture . OrStyle ( style . RadioHoveredTexture ) ;
this . HoveredColor = this . HoveredColor . OrStyle ( style . RadioHoveredColor ) ;
this . Checkmark = this . Checkmark . OrStyle ( style . RadioCheckmark ) ;
2019-08-13 21:23:20 +02:00
}
}
}