using System;
namespace MonoGame.Extended.NuclexGui.Controls.Desktop
{
/// Control displaying an exclusive choice the user can select
///
/// The choice control is equivalent to a radio button - if more than one
/// choice control is on a dialog, only one can be selected at a time.
/// To have several choice groups on a dialog, use panels to group them.
///
public class GuiChoiceControl : GuiPressableControl
{
/// Whether the choice is currently selected
public bool Selected;
/// Text that will be shown on the button
public string Text;
/// Determines where text or image will be shown relative to control
public GuiPressableDescriptionPosition DescriptionPosition;
/// Will be triggered when the choice is changed
public event EventHandler Changed;
/// Called when the button is pressed
protected override void OnPressed()
{
if (!Selected)
{
Selected = true;
// Unselect all sibling choice controls in the same container
UnselectSiblings();
OnChanged();
}
}
/// Triggers the changed event
protected virtual void OnChanged()
{
if (Changed != null)
Changed(this, EventArgs.Empty);
}
/// Disables all sibling choices on the same level
private void UnselectSiblings()
{
// Disable any other choices in the same frame
if (Parent != null)
{
var siblings = Parent.Children;
for (var index = 0; index < siblings.Count; ++index)
{
var control = siblings[index] as GuiChoiceControl;
if ((control != null) && (control != this) && control.Selected)
{
control.Selected = false;
control.OnChanged();
}
}
}
}
}
}