/Scenes/UserInterfaces/Controls/Checkbox.cs
C# | 357 lines | 203 code | 41 blank | 113 comment | 12 complexity | dec78492c6746d048247100a1afe3ab8 MD5 | raw file
Possible License(s): Apache-2.0
- using System.IO;
- using Delta.Engine;
- 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 represents a simple checkBox control for check or uncheck things.
- /// </summary>
- public class Checkbox : Button
- {
- #region Constants
- /// <summary>
- /// The current version of the implementation of this control class.
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region Delegates
- /// <summary>
- /// The delegate declaration for the checked changed event.
- /// </summary>
- /// <param name="sender">Sender</param>
- public delegate void CheckedChangedEvent(Checkbox sender);
- #endregion
-
- #region IsChecked (Public)
- /// <summary>
- /// Is the checked property enabled ?
- /// </summary>
- public bool IsChecked
- {
- get;
- set;
- }
- #endregion
-
- #region Protected
-
- #region FallbackDesign (Protected)
- /// <summary>
- /// Defines the theme which will be used if no "Design" was set
- /// explicitely.
- /// </summary>
- protected override ControlDesign FallbackDesign
- {
- get
- {
- return Theme.Current.CheckboxDesign;
- } // get
- }
- #endregion
-
- #region CheckedSymbolArea (Protected)
- /// <summary>
- /// The element which describes the area where the checked or unchecked
- /// symbol will be shown.
- /// </summary>
- protected internal AlignableElement CheckedSymbolArea
- {
- get;
- private set;
- }
- #endregion
-
- #endregion
-
- #region Private
-
- #region lastIsChecked (Private)
- /// <summary>
- /// The last set 'IsChecked' value which is used to "detect" Checked
- /// changes of the control by the user.
- /// </summary>
- private bool lastIsChecked;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Creates a checkbox.
- /// </summary>
- public Checkbox()
- {
- // Define the area of the "checked" indicator
- CheckedSymbolArea = new AlignableElement
- {
- HorizontalAlignment = HorizontalAlignment.Left,
- VerticalAlignment = VerticalAlignment.Centered,
- };
- Add(CheckedSymbolArea);
-
- // Align the text area at the right side of the control
- TextContentElement.HorizontalAlignment = HorizontalAlignment.Right;
- TextContentElement.VerticalAlignment = VerticalAlignment.Centered;
-
- IsChecked = false;
- }
- #endregion
-
- #region CheckedChanged (Event)
- /// <summary>
- /// Occurs every time the 'IsChecked' value of the Checkbox will be
- /// changed.
- /// </summary>
- public event CheckedChangedEvent CheckedChanged;
- #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
- dataWriter.Write(IsChecked);
- }
- #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:
- IsChecked = dataReader.ReadBoolean();
- break;
-
- default:
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- break;
- } // switch
- }
- #endregion
-
- #region Methods (Private)
-
- #region OnSizeChanging
- /// <summary>
- /// On size changing
- /// </summary>
- /// <param name="oldSize">Old size</param>
- /// <returns>
- /// 'True' if the new value can be used or 'false' if the change should be
- /// aborted.
- /// </returns>
- protected override bool OnSizeChanging(Size oldSize)
- {
- if (base.OnSizeChanging(oldSize))
- {
- // Check if we need to auto-set the size of the "checked" symbol
- if (CheckedSymbolArea.Size == Size.Zero)
- {
- CheckedSymbolArea.Size = new Size(Size.Height);
- } // if
-
- if (IsTextElementAutoSizing)
- {
- // The text area is whole control area except the size of the
- // "Checked" symbol area
- TextContentElement.Size = new Size(
- Size.Width - CheckedSymbolArea.Size.Width, Size.Height);
- } // if
-
- return true;
- } // if
-
- return false;
- }
- #endregion
-
- #region OnClicked
- /// <summary>
- /// Contains the whole (Checkbox) logic which happens if the control was
- /// 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);
-
- // Accept a click only if it is occurring inside the control
- if (isInsideControl)
- {
- // We want to change the 'IsChecked' value on every click event
- IsChecked = !IsChecked;
- input.IsHandled = true;
- } // 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 virtual bool OnIsCheckedChanging(bool oldIsChecked)
- {
- return true;
- }
- #endregion
-
- #region DetectChanges
- /// <summary>
- /// This method implements the checks of the changes which are should be
- /// detected in this element. It also cares about triggering the events and
- /// the event handler methods.
- /// </summary>
- protected override void DetectChanges()
- {
- base.DetectChanges();
-
- // Detect 'IsChecked' changes
- if (IsChecked != lastIsChecked)
- {
- // Check now if the new value should be set
- if (OnIsCheckedChanging(lastIsChecked))
- {
- lastIsChecked = IsChecked;
-
- // After setting the new value also inform all external listeners
- // about the change
- if (CheckedChanged != null &&
- // but only if it isn't just the value initialization
- isRuntimeValueChange)
- {
- CheckedChanged.Invoke(this);
- } // if
- } // if
- else
- {
- IsChecked = lastIsChecked;
- } // else
- } // if
- }
- #endregion
-
- #region ShortElementInfo
- /// <summary>
- /// Short element info
- /// </summary>
- protected override string ShortElementInfo()
- {
- return base.ShortElementInfo() + ", IsChecked='" + IsChecked + "'";
- }
- #endregion
-
- #endregion
-
- /// <summary>
- /// Tests for Checkbox controls
- /// </summary>
- [Category("Visual")]
- internal class CheckboxTests
- {
- #region DisplayCheckbox (Static)
- /// <summary>
- /// Display checkbox
- /// </summary>
- [Test]
- public static void DisplayCheckbox()
- {
- Checkbox testCheckbox = new Checkbox
- {
- LocalArea = new Rectangle(0.2f, 0.3f, 0.4f, 0.05f),
- //Size = new Size(0.6f, 0.1f),
- IsChecked = true,
- Text = "DisplayCheckbox",
- };
-
- Screen testScene = new Screen();
- testScene.Add(testCheckbox);
-
- // Open now the scene to "activate" for the test
- Scene.Open(testScene);
-
- // 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 DisabledCheckbox (Static)
- /// <summary>
- /// Disabled checkbox
- /// </summary>
- [Test]
- public static void DisabledCheckbox()
- {
- Checkbox testCheckbox = new Checkbox
- {
- LocalArea = new Rectangle(0.2f, 0.3f, 0.4f, 0.05f),
- IsChecked = true,
- Text = "Enabled",
- };
-
- Screen testScene = new Screen();
- testScene.Add(testCheckbox);
-
- // Open now the scene to "activate" for the test
- testScene.Open();
-
- Application.Start(delegate
- {
- // Disable/enable the Checkbox by pressing the keyboard "Space" button
- if (Input.Keyboard.SpaceReleased)
- {
- testCheckbox.State = (testCheckbox.State >= ElementState.Enabled)
- ? ElementState.Disabled
- : ElementState.Enabled;
- testCheckbox.Text = testCheckbox.State.ToString();
- }
- });
- }
- #endregion
- }
- }
- }