PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Scenes/UserInterfaces/Designs/CheckboxDesign.cs

#
C# | 370 lines | 263 code | 32 blank | 75 comment | 37 complexity | ff75d05d92fab8b002ccc7c9a4d1d18d MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.IO;
  3. using Delta.Rendering.Basics.Materials;
  4. using Delta.Scenes.Enums;
  5. using Delta.Scenes.UserInterfaces.Controls;
  6. using Delta.Utilities;
  7. using Delta.Utilities.Datatypes;
  8. namespace Delta.Scenes.UserInterfaces.Designs
  9. {
  10. /// <summary>
  11. /// The design for a Checkbox or RadioButton control.
  12. /// </summary>
  13. public class CheckboxDesign : TextControlDesign
  14. {
  15. #region Constants
  16. /// <summary>
  17. /// The current version of the implementation of this class.
  18. /// </summary>
  19. private const int VersionNumber = 1;
  20. #endregion
  21. #region CheckedSymbol (Public)
  22. /// <summary>
  23. /// Represents the design if the "Checked" property is enabled
  24. /// (control enabled state).
  25. /// </summary>
  26. public Material2DColored CheckedSymbol
  27. {
  28. get;
  29. set;
  30. }
  31. #endregion
  32. #region CheckedSymbolHover (Public)
  33. /// <summary>
  34. /// Represents the design if the "Checked" property is enabled and the
  35. /// control is hovered.
  36. /// </summary>
  37. public Material2DColored CheckedSymbolHover
  38. {
  39. get;
  40. set;
  41. }
  42. #endregion
  43. #region DisabledCheckedSymbol (Public)
  44. /// <summary>
  45. /// Represents the design if the "Checked" property is enabled
  46. /// (control disabled state).
  47. /// </summary>
  48. public Material2DColored DisabledCheckedSymbol
  49. {
  50. get;
  51. set;
  52. }
  53. #endregion
  54. #region UncheckedSymbol (Public)
  55. /// <summary>
  56. /// Represents the design if the "Checked" property is not enabled
  57. /// (control enabled state).
  58. /// </summary>
  59. public Material2DColored UncheckedSymbol
  60. {
  61. get;
  62. set;
  63. }
  64. #endregion
  65. #region UncheckedSymbolHover (Public)
  66. /// <summary>
  67. /// Represents the design if the "Checked" property is not enabled and the
  68. /// control is hovered.
  69. /// </summary>
  70. public Material2DColored UncheckedSymbolHover
  71. {
  72. get;
  73. set;
  74. }
  75. #endregion
  76. #region DisabledUncheckedSymbol (Public)
  77. /// <summary>
  78. /// Represents the design if the "Checked" property is not enabled
  79. /// (control disabled state).
  80. /// </summary>
  81. public Material2DColored DisabledUncheckedSymbol
  82. {
  83. get;
  84. set;
  85. }
  86. #endregion
  87. #region DrawControl (Public)
  88. /// <summary>
  89. /// Draw control
  90. /// </summary>
  91. /// <param name="control">Control</param>
  92. /// <param name="drawArea">Draw area</param>
  93. public override void DrawControl(BaseControl control, Rectangle drawArea)
  94. {
  95. if (control is Checkbox)
  96. {
  97. Checkbox checkbox = (Checkbox)control;
  98. switch (checkbox.State)
  99. {
  100. case ElementState.Disabled:
  101. // At first we have to draw the text background (if is set)
  102. if (DisabledTextBackground != null)
  103. {
  104. DrawStyle(checkbox, DisabledTextBackground,
  105. checkbox.TextDrawArea);
  106. } // if
  107. Material2DColored disabledCheckboxSymbol = (checkbox.IsChecked)
  108. ? DisabledCheckedSymbol
  109. : DisabledUncheckedSymbol;
  110. DrawStyle(checkbox, disabledCheckboxSymbol,
  111. checkbox.CheckedSymbolArea.DrawArea);
  112. break;
  113. case ElementState.Enabled:
  114. case ElementState.Hovered:
  115. case ElementState.Pressed:
  116. // At first we have to draw the text background (if is set)
  117. if (TextBackground != null)
  118. {
  119. DrawStyle(checkbox, TextBackground, checkbox.TextDrawArea);
  120. }
  121. // Special drawing code for the hovered state
  122. if (checkbox.State == ElementState.Hovered)
  123. {
  124. // Figure out if we have to show the checked or the unchecked
  125. // symbol
  126. // Note: We only can draw a hover symbol if there is one,
  127. // otherwise we just draw the normal one
  128. Material2DColored hoveredCheckboxSymbol;
  129. if (checkbox.IsChecked)
  130. {
  131. hoveredCheckboxSymbol = (CheckedSymbolHover != null)
  132. ? CheckedSymbolHover
  133. : CheckedSymbol;
  134. } // if
  135. else
  136. {
  137. hoveredCheckboxSymbol = (UncheckedSymbolHover != null)
  138. ? UncheckedSymbolHover
  139. : UncheckedSymbol;
  140. } // else
  141. DrawStyle(checkbox, hoveredCheckboxSymbol,
  142. checkbox.CheckedSymbolArea.DrawArea);
  143. } // if
  144. else // Enabled or Pressed state
  145. {
  146. Material2DColored checkboxSymbol = (checkbox.IsChecked)
  147. ? CheckedSymbol
  148. : UncheckedSymbol;
  149. DrawStyle(checkbox, checkboxSymbol,
  150. checkbox.CheckedSymbolArea.DrawArea);
  151. } // else
  152. break;
  153. } // switch
  154. } // if
  155. else
  156. {
  157. throw new InvalidOperationException("The given control '" + control +
  158. "' is no 'Checkbox'.");
  159. } // else
  160. }
  161. #endregion
  162. #region Save (Public)
  163. /// <summary>
  164. /// Saves all data which are necessary to restore the object again.
  165. /// </summary>
  166. /// <param name="dataWriter">The writer which contains the stream where the data should be saved
  167. /// into now.</param>
  168. public override void Save(BinaryWriter dataWriter)
  169. {
  170. // At first we write the data of the base class
  171. base.Save(dataWriter);
  172. // and then save the version of the current data format
  173. dataWriter.Write(VersionNumber);
  174. // before we can finally save the properties
  175. // UncheckedSymbol style
  176. bool hasUncheckedSymbol = UncheckedSymbol != null;
  177. dataWriter.Write(hasUncheckedSymbol);
  178. if (hasUncheckedSymbol)
  179. {
  180. SaveMaterial(dataWriter, UncheckedSymbol);
  181. } // if
  182. // UncheckedSymbolHover style
  183. bool hasUncheckedSymbolHover = UncheckedSymbolHover != null;
  184. dataWriter.Write(hasUncheckedSymbolHover);
  185. if (hasUncheckedSymbolHover)
  186. {
  187. SaveMaterial(dataWriter, UncheckedSymbolHover);
  188. } // if
  189. // DisabledUncheckedSymbol style
  190. bool hasDisabledUncheckedSymbol = DisabledUncheckedSymbol != null;
  191. dataWriter.Write(hasDisabledUncheckedSymbol);
  192. if (hasDisabledUncheckedSymbol)
  193. {
  194. SaveMaterial(dataWriter, DisabledUncheckedSymbol);
  195. } // if
  196. // Checked style
  197. bool hasCheckedDesign = CheckedSymbol != null;
  198. dataWriter.Write(hasCheckedDesign);
  199. if (hasCheckedDesign)
  200. {
  201. SaveMaterial(dataWriter, CheckedSymbol);
  202. } // if
  203. // CheckedHover style
  204. bool hasCheckedHoverDesign = CheckedSymbolHover != null;
  205. dataWriter.Write(hasCheckedHoverDesign);
  206. if (hasCheckedHoverDesign)
  207. {
  208. SaveMaterial(dataWriter, CheckedSymbolHover);
  209. } // if
  210. // DisabledChecked style
  211. bool hasDisabledCheckedDesign = DisabledCheckedSymbol != null;
  212. dataWriter.Write(hasDisabledCheckedDesign);
  213. if (hasDisabledCheckedDesign)
  214. {
  215. SaveMaterial(dataWriter, DisabledCheckedSymbol);
  216. } // if
  217. // TextBackground style
  218. bool hasTextBackgroundDesign = TextBackground != null;
  219. dataWriter.Write(hasTextBackgroundDesign);
  220. if (hasTextBackgroundDesign)
  221. {
  222. SaveMaterial(dataWriter, TextBackground);
  223. } // if
  224. // DisabledTextBackground style
  225. bool hasDisabledTextBackgroundDesign = DisabledTextBackground != null;
  226. dataWriter.Write(hasDisabledTextBackgroundDesign);
  227. if (hasDisabledTextBackgroundDesign)
  228. {
  229. SaveMaterial(dataWriter, DisabledTextBackground);
  230. } // if
  231. }
  232. #endregion
  233. #region Load (Public)
  234. /// <summary>
  235. /// Loads and restores all previously saved values that belongs to this
  236. /// class only from the given data reader.
  237. /// </summary>
  238. /// <param name="dataReader">The reader which contains the stream with the saved data which needs to
  239. /// be loaded now.</param>
  240. public override void Load(BinaryReader dataReader)
  241. {
  242. // At first we need to load all data of the base class
  243. base.Load(dataReader);
  244. // and then check which version of the data need to load now
  245. int version = dataReader.ReadInt32();
  246. switch (version)
  247. {
  248. // Version 1
  249. case VersionNumber:
  250. // UncheckedSymbol style
  251. bool hasUncheckedSymbol = dataReader.ReadBoolean();
  252. if (hasUncheckedSymbol)
  253. {
  254. UncheckedSymbol = LoadMaterial(dataReader);
  255. } // if
  256. else
  257. {
  258. UncheckedSymbol = null;
  259. } // else
  260. // UncheckedSymbolHover style
  261. bool hasUncheckedSymbolHover = dataReader.ReadBoolean();
  262. if (hasUncheckedSymbolHover)
  263. {
  264. UncheckedSymbolHover = LoadMaterial(dataReader);
  265. } // if
  266. else
  267. {
  268. UncheckedSymbolHover = null;
  269. } // else
  270. // DisabledUncheckedSymbol style
  271. bool hasDisabledUncheckedSymbol = dataReader.ReadBoolean();
  272. if (hasDisabledUncheckedSymbol)
  273. {
  274. DisabledUncheckedSymbol = LoadMaterial(dataReader);
  275. } // if
  276. else
  277. {
  278. DisabledUncheckedSymbol = null;
  279. } // else
  280. // Checked style
  281. bool hasCheckedDesign = dataReader.ReadBoolean();
  282. if (hasCheckedDesign)
  283. {
  284. CheckedSymbol = LoadMaterial(dataReader);
  285. } // if
  286. else
  287. {
  288. CheckedSymbol = null;
  289. } // else
  290. // CheckedHover style
  291. bool hasCheckedHoverDesign = dataReader.ReadBoolean();
  292. if (hasCheckedHoverDesign)
  293. {
  294. CheckedSymbolHover = LoadMaterial(dataReader);
  295. } // if
  296. else
  297. {
  298. CheckedSymbolHover = null;
  299. } // else
  300. // DisabledChecked style
  301. bool hasDisabledCheckedDesign = dataReader.ReadBoolean();
  302. if (hasDisabledCheckedDesign)
  303. {
  304. DisabledCheckedSymbol = LoadMaterial(dataReader);
  305. } // if
  306. else
  307. {
  308. DisabledCheckedSymbol = null;
  309. } // else
  310. // TextBackground style
  311. bool hasTextBackgroundDesign = dataReader.ReadBoolean();
  312. if (hasTextBackgroundDesign)
  313. {
  314. TextBackground = LoadMaterial(dataReader);
  315. } // if
  316. else
  317. {
  318. TextBackground = null;
  319. } // else
  320. // DisabledTextBackground style
  321. bool hasDisabledTextBackgroundDesign = dataReader.ReadBoolean();
  322. if (hasDisabledTextBackgroundDesign)
  323. {
  324. DisabledTextBackground = LoadMaterial(dataReader);
  325. } // if
  326. else
  327. {
  328. DisabledTextBackground = null;
  329. } // else
  330. break;
  331. default:
  332. Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
  333. break;
  334. } // switch
  335. }
  336. #endregion
  337. }
  338. }