PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. using System.IO;
  2. using Delta.Engine;
  3. using Delta.InputSystem;
  4. using Delta.Scenes.Enums;
  5. using Delta.Utilities;
  6. using Delta.Utilities.Datatypes;
  7. using Delta.Utilities.Datatypes.Advanced;
  8. using NUnit.Framework;
  9. namespace Delta.Scenes.UserInterfaces.Controls
  10. {
  11. /// <summary>
  12. /// This represents a simple checkBox control for check or uncheck things.
  13. /// </summary>
  14. public class Checkbox : Button
  15. {
  16. #region Constants
  17. /// <summary>
  18. /// The current version of the implementation of this control class.
  19. /// </summary>
  20. private const int VersionNumber = 1;
  21. #endregion
  22. #region Delegates
  23. /// <summary>
  24. /// The delegate declaration for the checked changed event.
  25. /// </summary>
  26. /// <param name="sender">Sender</param>
  27. public delegate void CheckedChangedEvent(Checkbox sender);
  28. #endregion
  29. #region IsChecked (Public)
  30. /// <summary>
  31. /// Is the checked property enabled ?
  32. /// </summary>
  33. public bool IsChecked
  34. {
  35. get;
  36. set;
  37. }
  38. #endregion
  39. #region Protected
  40. #region FallbackDesign (Protected)
  41. /// <summary>
  42. /// Defines the theme which will be used if no "Design" was set
  43. /// explicitely.
  44. /// </summary>
  45. protected override ControlDesign FallbackDesign
  46. {
  47. get
  48. {
  49. return Theme.Current.CheckboxDesign;
  50. } // get
  51. }
  52. #endregion
  53. #region CheckedSymbolArea (Protected)
  54. /// <summary>
  55. /// The element which describes the area where the checked or unchecked
  56. /// symbol will be shown.
  57. /// </summary>
  58. protected internal AlignableElement CheckedSymbolArea
  59. {
  60. get;
  61. private set;
  62. }
  63. #endregion
  64. #endregion
  65. #region Private
  66. #region lastIsChecked (Private)
  67. /// <summary>
  68. /// The last set 'IsChecked' value which is used to "detect" Checked
  69. /// changes of the control by the user.
  70. /// </summary>
  71. private bool lastIsChecked;
  72. #endregion
  73. #endregion
  74. #region Constructors
  75. /// <summary>
  76. /// Creates a checkbox.
  77. /// </summary>
  78. public Checkbox()
  79. {
  80. // Define the area of the "checked" indicator
  81. CheckedSymbolArea = new AlignableElement
  82. {
  83. HorizontalAlignment = HorizontalAlignment.Left,
  84. VerticalAlignment = VerticalAlignment.Centered,
  85. };
  86. Add(CheckedSymbolArea);
  87. // Align the text area at the right side of the control
  88. TextContentElement.HorizontalAlignment = HorizontalAlignment.Right;
  89. TextContentElement.VerticalAlignment = VerticalAlignment.Centered;
  90. IsChecked = false;
  91. }
  92. #endregion
  93. #region CheckedChanged (Event)
  94. /// <summary>
  95. /// Occurs every time the 'IsChecked' value of the Checkbox will be
  96. /// changed.
  97. /// </summary>
  98. public event CheckedChangedEvent CheckedChanged;
  99. #endregion
  100. #region Save (Public)
  101. /// <summary>
  102. /// Saves all data which are necessary to restore the object again.
  103. /// </summary>
  104. /// <param name="dataWriter">
  105. /// The writer which contains the stream where the data should be saved
  106. /// into now.
  107. /// </param>
  108. public override void Save(BinaryWriter dataWriter)
  109. {
  110. // At first we write the data of the base class
  111. base.Save(dataWriter);
  112. // and then save the version of the current data format
  113. dataWriter.Write(VersionNumber);
  114. // before we can finally save the properties
  115. dataWriter.Write(IsChecked);
  116. }
  117. #endregion
  118. #region Load (Public)
  119. /// <summary>
  120. /// Loads and restores all previously saved values that belongs to this
  121. /// class only from the given data reader.
  122. /// </summary>
  123. /// <param name="dataReader">
  124. /// The reader which contains the stream with the saved data which needs to
  125. /// be loaded now.
  126. /// </param>
  127. public override void Load(BinaryReader dataReader)
  128. {
  129. // At first we need to load all data of the base class
  130. base.Load(dataReader);
  131. // and then check which version of the data need to load now
  132. int version = dataReader.ReadInt32();
  133. switch (version)
  134. {
  135. // Version 1
  136. case VersionNumber:
  137. IsChecked = dataReader.ReadBoolean();
  138. break;
  139. default:
  140. Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
  141. break;
  142. } // switch
  143. }
  144. #endregion
  145. #region Methods (Private)
  146. #region OnSizeChanging
  147. /// <summary>
  148. /// On size changing
  149. /// </summary>
  150. /// <param name="oldSize">Old size</param>
  151. /// <returns>
  152. /// 'True' if the new value can be used or 'false' if the change should be
  153. /// aborted.
  154. /// </returns>
  155. protected override bool OnSizeChanging(Size oldSize)
  156. {
  157. if (base.OnSizeChanging(oldSize))
  158. {
  159. // Check if we need to auto-set the size of the "checked" symbol
  160. if (CheckedSymbolArea.Size == Size.Zero)
  161. {
  162. CheckedSymbolArea.Size = new Size(Size.Height);
  163. } // if
  164. if (IsTextElementAutoSizing)
  165. {
  166. // The text area is whole control area except the size of the
  167. // "Checked" symbol area
  168. TextContentElement.Size = new Size(
  169. Size.Width - CheckedSymbolArea.Size.Width, Size.Height);
  170. } // if
  171. return true;
  172. } // if
  173. return false;
  174. }
  175. #endregion
  176. #region OnClicked
  177. /// <summary>
  178. /// Contains the whole (Checkbox) logic which happens if the control was
  179. /// clicked.
  180. /// </summary>
  181. /// <param name="input">Input data like the click position.</param>
  182. /// <param name="isInsideControl">
  183. /// 'True' if the event is occurring inside the control, otherwise 'false'.
  184. /// </param>
  185. protected override void OnClicked(CommandTrigger input,
  186. bool isInsideControl)
  187. {
  188. base.OnClicked(input, isInsideControl);
  189. // Accept a click only if it is occurring inside the control
  190. if (isInsideControl)
  191. {
  192. // We want to change the 'IsChecked' value on every click event
  193. IsChecked = !IsChecked;
  194. input.IsHandled = true;
  195. } // if
  196. }
  197. #endregion
  198. #region OnIsCheckedChanging
  199. /// <summary>
  200. /// On is checked changing
  201. /// </summary>
  202. /// <param name="oldIsChecked">Old value</param>
  203. /// <returns>
  204. /// 'True' if the new value can be used or 'false' if the change should be
  205. /// aborted.
  206. /// </returns>
  207. protected virtual bool OnIsCheckedChanging(bool oldIsChecked)
  208. {
  209. return true;
  210. }
  211. #endregion
  212. #region DetectChanges
  213. /// <summary>
  214. /// This method implements the checks of the changes which are should be
  215. /// detected in this element. It also cares about triggering the events and
  216. /// the event handler methods.
  217. /// </summary>
  218. protected override void DetectChanges()
  219. {
  220. base.DetectChanges();
  221. // Detect 'IsChecked' changes
  222. if (IsChecked != lastIsChecked)
  223. {
  224. // Check now if the new value should be set
  225. if (OnIsCheckedChanging(lastIsChecked))
  226. {
  227. lastIsChecked = IsChecked;
  228. // After setting the new value also inform all external listeners
  229. // about the change
  230. if (CheckedChanged != null &&
  231. // but only if it isn't just the value initialization
  232. isRuntimeValueChange)
  233. {
  234. CheckedChanged.Invoke(this);
  235. } // if
  236. } // if
  237. else
  238. {
  239. IsChecked = lastIsChecked;
  240. } // else
  241. } // if
  242. }
  243. #endregion
  244. #region ShortElementInfo
  245. /// <summary>
  246. /// Short element info
  247. /// </summary>
  248. protected override string ShortElementInfo()
  249. {
  250. return base.ShortElementInfo() + ", IsChecked='" + IsChecked + "'";
  251. }
  252. #endregion
  253. #endregion
  254. /// <summary>
  255. /// Tests for Checkbox controls
  256. /// </summary>
  257. [Category("Visual")]
  258. internal class CheckboxTests
  259. {
  260. #region DisplayCheckbox (Static)
  261. /// <summary>
  262. /// Display checkbox
  263. /// </summary>
  264. [Test]
  265. public static void DisplayCheckbox()
  266. {
  267. Checkbox testCheckbox = new Checkbox
  268. {
  269. LocalArea = new Rectangle(0.2f, 0.3f, 0.4f, 0.05f),
  270. //Size = new Size(0.6f, 0.1f),
  271. IsChecked = true,
  272. Text = "DisplayCheckbox",
  273. };
  274. Screen testScene = new Screen();
  275. testScene.Add(testCheckbox);
  276. // Open now the scene to "activate" for the test
  277. Scene.Open(testScene);
  278. // We just call here Application.Start() to display the image, but we
  279. // don't need to call the "Image.Draw()" explicitely, because this
  280. // already handled automatically by the UI manager.
  281. Application.Start();
  282. }
  283. #endregion
  284. #region DisabledCheckbox (Static)
  285. /// <summary>
  286. /// Disabled checkbox
  287. /// </summary>
  288. [Test]
  289. public static void DisabledCheckbox()
  290. {
  291. Checkbox testCheckbox = new Checkbox
  292. {
  293. LocalArea = new Rectangle(0.2f, 0.3f, 0.4f, 0.05f),
  294. IsChecked = true,
  295. Text = "Enabled",
  296. };
  297. Screen testScene = new Screen();
  298. testScene.Add(testCheckbox);
  299. // Open now the scene to "activate" for the test
  300. testScene.Open();
  301. Application.Start(delegate
  302. {
  303. // Disable/enable the Checkbox by pressing the keyboard "Space" button
  304. if (Input.Keyboard.SpaceReleased)
  305. {
  306. testCheckbox.State = (testCheckbox.State >= ElementState.Enabled)
  307. ? ElementState.Disabled
  308. : ElementState.Enabled;
  309. testCheckbox.Text = testCheckbox.State.ToString();
  310. }
  311. });
  312. }
  313. #endregion
  314. }
  315. }
  316. }