/Scenes/UserInterfaces/Controls/RadioButton.cs
C# | 326 lines | 193 code | 35 blank | 98 comment | 15 complexity | 7fda125cfdd2e8aa8c73f877475421c7 MD5 | raw file
Possible License(s): Apache-2.0
- using System.IO;
- using Delta.Engine;
- using Delta.Engine.Dynamic;
- using Delta.InputSystem;
- using Delta.Scenes.Enums;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Datatypes.Advanced;
- using NUnit.Framework;
-
- namespace Delta.Scenes.UserInterfaces.Controls
- {
- /// <summary>
- /// This is a radio button control which provides to select always only one
- /// option of a choice of several options. The list of options will be
- /// grouped by setting the 'GroupObject' property.
- /// </summary>
- public class RadioButton : Checkbox
- {
- #region Constants
- /// <summary>
- /// The current version of the implementation of this class.
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region GroupObject (Public)
- /// <summary>
- /// The "group object" where this RadioButton belongs to.
- /// </summary>
- /// <value>
- /// The group object.
- /// </value>
- public ISaveLoadBinary GroupObject
- {
- get;
- set;
- }
- #endregion
-
- #region Protected
-
- #region FallbackDesign (Protected)
- /// <summary>
- /// Defines the theme which will be used if no "Theme" was set explicitely.
- /// </summary>
- protected override ControlDesign FallbackDesign
- {
- get
- {
- return Theme.Current.RadioButtonDesign;
- }
- }
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create a radio button.
- /// </summary>
- public RadioButton()
- {
- GroupObject = null;
- }
- #endregion
-
- #region Save (Public)
- /// <summary>
- /// Saves all data which are necessary to restore the object again.
- /// </summary>
- /// <param name="dataWriter">
- /// The writer which contains the stream where the data should be saved
- /// into now.
- /// </param>
- public override void Save(BinaryWriter dataWriter)
- {
- // At first we write the data of the base class
- base.Save(dataWriter);
-
- // and then save the version of the current data format
- dataWriter.Write(VersionNumber);
-
- // before we can finally save the properties
- // Here we only have to save the "grouping object"
- bool isGroupObjectAvaiable = GroupObject != null;
- dataWriter.Write(isGroupObjectAvaiable);
- if (isGroupObjectAvaiable)
- {
- Factory.Save(dataWriter, GroupObject);
- } // if
- }
- #endregion
-
- #region Load (Public)
- /// <summary>
- /// Loads and restores all previously saved values that belongs to this
- /// class only from the given data reader.
- /// </summary>
- /// <param name="dataReader">The reader which contains the stream with the saved data which needs to
- /// be loaded now.</param>
- public override void Load(BinaryReader dataReader)
- {
- // At first we need to load all data of the base class
- base.Load(dataReader);
-
- // and then check which version of the data need to load now
- int version = dataReader.ReadInt32();
- switch (version)
- {
- // Version 1
- case VersionNumber:
- // Load our group object if there was one
- if (dataReader.ReadBoolean())
- {
- GroupObject = Factory.Load<ISaveLoadBinary>(dataReader);
- } // if
- break;
-
- default:
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- break;
- } // switch
- }
- #endregion
-
- #region Methods (Private)
-
- #region OnClicked
- /// <summary>
- /// On clicked
- /// </summary>
- /// <param name="input">Input data like the click position.</param>
- /// <param name="isInsideControl">
- /// 'True' if the event is occurring inside the control, otherwise 'false'.
- /// </param>
- protected override void OnClicked(CommandTrigger input,
- bool isInsideControl)
- {
- base.OnClicked(input, isInsideControl);
-
- if (isInsideControl)
- {
- // Make sure the user can't uncheck the RadioButton by clicking
- // on it again because we no Checkbox anymore ;)
- // So in the case the user has currently unchecked the button (by the
- // base code call) then just make it checked again.
- // Note:
- // This will also prevent a value change detection
- if (IsChecked == false)
- {
- IsChecked = true;
- } // if
- } // if
- }
- #endregion
-
- #region OnIsCheckedChanging
- /// <summary>
- /// On is checked changing
- /// </summary>
- /// <param name="oldIsChecked">Old value</param>
- /// <returns>
- /// 'True' if the new value can be used or 'false' if the change should be
- /// aborted.
- /// </returns>
- protected override bool OnIsCheckedChanging(bool oldIsChecked)
- {
- // Next check if the base code will not abort the change already
- if (base.OnIsCheckedChanging(oldIsChecked) == false)
- {
- return false;
- } // if
-
- // If we will be checked now, then we have uncheck all other RadioButton
- // members of our group
- if (IsChecked &&
- oldIsChecked == false)
- {
- // If our RadioButton has a group set, then receive all other
- // "group members", else if we don't are in a group then we are only
- // the member.
- RadioButton[] buttonsInGroup =
- (GroupObject != null)
- ? (Screen as Screen).GetRadioButtons(GroupObject)
- : new[]
- {
- this
- };
-
- // Iterate through all members
- foreach (RadioButton radioButton in buttonsInGroup)
- {
- // and "deactivate" all the other members
- if (radioButton != this)
- {
- radioButton.IsChecked = false;
- } // if
- } // foreach
- } // if
-
- // In the case that we are only deactivated or already checked we don't
- // need to anything here because everything was done already in the
- //"base code"
- return true;
- }
- #endregion
-
- #endregion
-
- /// <summary>
- /// Tests for RadioButton controls
- /// </summary>
- [Category("Visual")]
- internal class RadioButtonTests
- {
- #region DisplayRadioButton (Static)
- /// <summary>
- /// Checkbox
- /// </summary>
- [Test]
- public static void DisplayRadioButton()
- {
- // The used UI scene for the unit test
- Screen testScene = new Screen();
-
- // Create a grouping condition to which both radio buttons belong to
- ISaveLoadBinary groupTag = new DataContainer
- {
- StringData = "Group 1",
- };
-
- RadioButton radioButtonOne = new RadioButton
- {
- LocalArea = new Rectangle(0.3f, 0.3f, 0.4f, 0.05f),
- //Size = new Size(0.6f, 0.1f),
- GroupObject = groupTag,
- Text = "Option A",
- };
- testScene.Add(radioButtonOne);
-
- RadioButton radioButtonTwo = new RadioButton
- {
- LocalArea = new Rectangle(0.3f, 0.4f, 0.4f, 0.05f),
- //Size = new Size(0.6f, 0.1f),
- GroupObject = groupTag,
- Text = "Option B",
- };
- testScene.Add(radioButtonTwo);
-
- RadioButton radioButtonThree = new RadioButton
- {
- LocalArea = new Rectangle(0.3f, 0.5f, 0.4f, 0.05f),
- //Size = new Size(0.6f, 0.1f),
- GroupObject = groupTag,
- Text = "Option C",
- };
- testScene.Add(radioButtonThree);
-
- // Open now the scene to "activate" for the test
- testScene.Open();
-
- // We just call here Application.Start() to display the image, but we
- // don't need to call the "Image.Draw()" explicitely, because this
- // already handled automatically by the UI manager
- Application.Start();
- }
- #endregion
-
- #region DisabledRadioButton (Static)
- /// <summary>
- /// Disabled radio button
- /// </summary>
- [Test]
- public static void DisabledRadioButton()
- {
- // The used UI scene for the unit test
- Screen testScene = new Screen();
-
- // Create a grouping condition to which both radio buttons belong to
- BaseControl groupTag = new Image();
-
- RadioButton radioButton1 = new RadioButton
- {
- LocalArea = new Rectangle(0.3f, 0.3f, 0.4f, 0.05f),
- //Size = new Size(0.6f, 0.1f),
- GroupObject = groupTag,
- Text = "Enabled",
- };
- testScene.Add(radioButton1);
-
- RadioButton radioButton2 = new RadioButton
- {
- LocalArea = new Rectangle(0.3f, 0.4f, 0.4f, 0.05f),
- //Size = new Size(0.6f, 0.1f),
- GroupObject = groupTag,
- Text = "Enabled",
- };
- testScene.Add(radioButton2);
-
- // Open now the scene to "activate" for the test
- testScene.Open();
-
- Application.Start(delegate
- {
- // Start the visual test to see the RadioButton's
-
- // Disable/enable the RadioButton's by pressing the "Space" key
- if (Input.Keyboard.SpaceReleased)
- {
- radioButton1.State = (radioButton1.State >= ElementState.Enabled)
- ? ElementState.Disabled
- : ElementState.Enabled;
- radioButton1.Text = radioButton1.State.ToString();
-
- radioButton2.State = (radioButton2.State >= ElementState.Enabled)
- ? ElementState.Disabled
- : ElementState.Enabled;
- radioButton2.Text = radioButton2.State.ToString();
- } // if
- });
- }
- #endregion
- }
- }
- }