Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaClient/Source/GUI/GUIRadioButtonGroup.cs
Joonas Rikkonen 044fd3344b 2f107db...5202af9
2019-03-18 21:42:26 +02:00

75 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
namespace Barotrauma
{
public class GUIRadioButtonGroup : GUIComponent
{
private Dictionary<Enum, GUITickBox> radioButtons; //TODO: use children list instead?
public GUIRadioButtonGroup() : base("GUIFrame")
{
radioButtons = new Dictionary<Enum, GUITickBox>();
}
public void AddRadioButton(Enum key, GUITickBox radioButton)
{
if (selected == key) radioButton.Selected = true;
else if (radioButton.Selected) selected = key;
radioButton.SetRadioButtonGroup(this);
radioButtons.Add(key, radioButton);
}
public delegate void RadioButtonGroupDelegate(GUIRadioButtonGroup rbg, Enum val);
public RadioButtonGroupDelegate OnSelect = null;
public void SelectRadioButton(GUITickBox radioButton)
{
foreach (KeyValuePair<Enum, GUITickBox> rbPair in radioButtons)
{
if (radioButton == rbPair.Value)
{
Selected = rbPair.Key;
return;
}
}
}
private Enum selected;
public Enum Selected
{
get
{
return selected;
}
set
{
OnSelect?.Invoke(this, value);
if (selected != null && selected.Equals((Enum)value)) return;
selected = value;
foreach (KeyValuePair<Enum, GUITickBox> radioButton in radioButtons)
{
if (radioButton.Key.Equals((Enum)value))
{
radioButton.Value.Selected = true;
}
else if (radioButton.Value.Selected) radioButton.Value.Selected = false;
}
}
}
public GUITickBox SelectedRadioButton
{
get
{
return radioButtons[selected];
}
}
}
}