PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Scenes/UserInterfaces/Designs/TextControlDesign.cs

#
C# | 411 lines | 272 code | 44 blank | 95 comment | 26 complexity | 19aa59fcc19a9a3cccbc682f903cbc59 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.IO;
  2. using Delta.Engine.Dynamic;
  3. using Delta.Rendering.Basics.Fonts;
  4. using Delta.Rendering.Basics.Materials;
  5. using Delta.Scenes.Enums;
  6. using Delta.Scenes.UserInterfaces.Controls;
  7. using Delta.Utilities;
  8. using Delta.Utilities.Datatypes;
  9. using NUnit.Framework;
  10. namespace Delta.Scenes.UserInterfaces.Designs
  11. {
  12. /// <summary>
  13. /// Label design.
  14. /// </summary>
  15. public class TextControlDesign : ControlDesign, ITextDesign
  16. {
  17. #region Constants
  18. /// <summary>
  19. /// The current version of the implementation of this class.
  20. /// </summary>
  21. private const int VersionNumber = 1;
  22. #endregion
  23. #region TextFont (Public)
  24. /// <summary>
  25. /// Gets or sets the font which is used to displaying the text.
  26. /// </summary>
  27. public Font TextFont
  28. {
  29. get
  30. {
  31. if (textFont == null)
  32. {
  33. Log.Warning(
  34. "The text font is not valid for '" + this + "' will now use " +
  35. "the default font of the engine instead.");
  36. textFont = Font.Default;
  37. }
  38. return textFont;
  39. }
  40. set
  41. {
  42. textFont = value;
  43. }
  44. }
  45. #endregion
  46. #region DisabledTextFont (Public)
  47. /// <summary>
  48. /// Gets or sets the font which is used to displaying the text if the
  49. /// control is in the "Disabled" state.
  50. /// </summary>
  51. public Font DisabledTextFont
  52. {
  53. get
  54. {
  55. if (disabledTextFont == null)
  56. {
  57. Log.Warning(
  58. "The disabled text font is not valid for '" + this + "' will " +
  59. "now use the default font of the engine instead.");
  60. disabledTextFont = Font.Default;
  61. }
  62. return disabledTextFont;
  63. }
  64. set
  65. {
  66. disabledTextFont = value;
  67. }
  68. }
  69. #endregion
  70. #region TextColor (Public)
  71. /// <summary>
  72. /// Get the color of the displayed text. To change the text color a new
  73. /// font must be initialized (like with all font properties).
  74. /// </summary>
  75. public Color TextColor
  76. {
  77. get
  78. {
  79. return TextFont.Color;
  80. } // get
  81. }
  82. #endregion
  83. #region TextBackground (Public)
  84. /// <summary>
  85. /// The own background design for the text area instead of the "global"
  86. /// background design (enabled state).
  87. /// </summary>
  88. public Material2DColored TextBackground
  89. {
  90. get;
  91. set;
  92. }
  93. #endregion
  94. #region TextHoverBackground (Public)
  95. /// <summary>
  96. /// The own background design for the text area instead of the "global"
  97. /// background design (hovered state).
  98. /// </summary>
  99. public Material2DColored TextHoverBackground
  100. {
  101. get;
  102. set;
  103. }
  104. #endregion
  105. #region DisabledTextBackground (Public)
  106. /// <summary>
  107. /// The own background design for the text area instead of the "global"
  108. /// background design (disabled state).
  109. /// </summary>
  110. public Material2DColored DisabledTextBackground
  111. {
  112. get;
  113. set;
  114. }
  115. #endregion
  116. #region Pressed (Public)
  117. /// <summary>
  118. /// Gets or sets the design to show when the button is pressed. Note: This
  119. /// design is optional, which means if it isn't set, the "basic" rendering
  120. /// (with BackgroundDesign and HoverDesign) behavior will be used instead.
  121. /// </summary>
  122. public Material2DColored Pressed
  123. {
  124. get;
  125. set;
  126. }
  127. #endregion
  128. #region Private
  129. #region textFont (Private)
  130. /// <summary>
  131. /// Text font used, usually set because this is a text control design
  132. /// after all. If nothing is set will be set to the fallback Font.Default.
  133. /// </summary>
  134. private Font textFont;
  135. #endregion
  136. #region disabledTextFont (Private)
  137. /// <summary>
  138. /// Disabled text font used, can be null when it is never accessed.
  139. /// </summary>
  140. private Font disabledTextFont;
  141. #endregion
  142. #endregion
  143. #region DrawControl (Public)
  144. /// <summary>
  145. /// Draw control. Note: This method will only be called if the control
  146. /// state is at least visible.
  147. /// </summary>
  148. /// <param name="control">Control to draw in this design.</param>
  149. /// <param name="drawArea">The draw area.</param>
  150. public override void DrawControl(BaseControl control, Rectangle drawArea)
  151. {
  152. // First draw the general design of the control
  153. base.DrawControl(control, drawArea);
  154. Label textControl = control as Label;
  155. if (textControl == null)
  156. {
  157. Log.Warning(
  158. "The design '" + this + "' will be applied on the non-text " +
  159. "control '" + control + "' which is not really useful because " +
  160. "there is no text to draw, so please use a non-text design " +
  161. "instead for this control.");
  162. return;
  163. } // if
  164. // and next draw the text area related style
  165. switch (textControl.State)
  166. {
  167. case ElementState.Disabled:
  168. if (DisabledTextBackground != null)
  169. {
  170. DrawStyle(textControl, DisabledTextBackground,
  171. textControl.TextDrawArea);
  172. } // if
  173. break;
  174. case ElementState.Hovered:
  175. case ElementState.Active:
  176. Material2DColored textHoverStyle =
  177. TextHoverBackground != null
  178. ? TextHoverBackground
  179. : TextBackground;
  180. if (textHoverStyle != null)
  181. {
  182. DrawStyle(textControl, textHoverStyle, textControl.TextDrawArea);
  183. } // if
  184. break;
  185. default:
  186. Material2DColored textBackground = TextBackground;
  187. if (textBackground != null)
  188. {
  189. DrawStyle(textControl, textBackground, textControl.TextDrawArea);
  190. } // if
  191. break;
  192. } // switch
  193. }
  194. #endregion
  195. #region Save (Public)
  196. /// <summary>
  197. /// Saves all data which are necessary to restore the object again.
  198. /// </summary>
  199. /// <param name="dataWriter">The writer which contains the stream where the data should be saved
  200. /// into now.</param>
  201. public override void Save(BinaryWriter dataWriter)
  202. {
  203. // At first we write the data of the base class
  204. base.Save(dataWriter);
  205. // and then save the version of the current data format
  206. dataWriter.Write(VersionNumber);
  207. // before we can finally save the properties
  208. bool isTextFontAvailable = textFont != null;
  209. dataWriter.Write(isTextFontAvailable);
  210. if (isTextFontAvailable)
  211. {
  212. Factory.Save(dataWriter, textFont);
  213. }
  214. bool isDisabledTextFontAvailable = disabledTextFont != null;
  215. dataWriter.Write(isDisabledTextFontAvailable);
  216. if (isDisabledTextFontAvailable)
  217. {
  218. Factory.Save(dataWriter, disabledTextFont);
  219. }
  220. // Pressed style
  221. bool hasPressedDesign = Pressed != null;
  222. dataWriter.Write(hasPressedDesign);
  223. if (hasPressedDesign)
  224. {
  225. SaveMaterial(dataWriter, Pressed);
  226. }
  227. }
  228. #endregion
  229. #region Load (Public)
  230. /// <summary>
  231. /// Loads and restores all previously saved values that belongs to this
  232. /// class only from the given data reader.
  233. /// </summary>
  234. /// <param name="dataReader">The reader which contains the stream with the saved data which needs to
  235. /// be loaded now.</param>
  236. public override void Load(BinaryReader dataReader)
  237. {
  238. // At first we need to load all data of the base class
  239. base.Load(dataReader);
  240. // and then check which version of the data need to load now
  241. int version = dataReader.ReadInt32();
  242. switch (version)
  243. {
  244. // Version 1
  245. case VersionNumber:
  246. // Is a text font available ?
  247. if (dataReader.ReadBoolean())
  248. {
  249. // then load the saved font
  250. textFont = Factory.Load<Font>(dataReader);
  251. }
  252. if (dataReader.ReadBoolean())
  253. {
  254. // then load the saved font
  255. disabledTextFont = Factory.Load<Font>(dataReader);
  256. }
  257. // Pressed style
  258. bool hasPressedDesign = dataReader.ReadBoolean();
  259. if (hasPressedDesign)
  260. {
  261. Pressed = LoadMaterial(dataReader);
  262. }
  263. else
  264. {
  265. Pressed = null;
  266. }
  267. break;
  268. default:
  269. Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
  270. break;
  271. }
  272. }
  273. #endregion
  274. #region Methods (Private)
  275. #region GetControlStyle
  276. /// <summary>
  277. /// Returns the set style of the controls based on its current state.
  278. /// </summary>
  279. /// <param name="control">Control</param>
  280. protected override Material2DColored GetControlStyle(BaseControl control)
  281. {
  282. switch (control.State)
  283. {
  284. case ElementState.Pressed:
  285. return (Pressed != null)
  286. ? Pressed
  287. : base.GetControlStyle(control);
  288. default:
  289. return base.GetControlStyle(control);
  290. }
  291. }
  292. #endregion
  293. #endregion
  294. /// <summary>
  295. /// Tests for TextControlDesign
  296. /// </summary>
  297. [Category("LongRunning")]
  298. internal class TextControlDesignTests
  299. {
  300. #region SaveAndLoad
  301. /// <summary>
  302. /// Save and load
  303. /// </summary>
  304. [Test]
  305. public void SaveAndLoad()
  306. {
  307. // Create a design
  308. TextControlDesign textDesign = new TextControlDesign
  309. {
  310. Background = BaseTheme.GetUIMaterial("ButtonBackground"),
  311. DisabledBackground = BaseTheme.GetUIMaterial("ButtonBackground",
  312. Color.Grey),
  313. Hover = BaseTheme.GetUIMaterial("ButtonHover", Color.Yellow),
  314. Pressed = BaseTheme.GetUIMaterial("ButtonPressed", Color.Yellow),
  315. TextFont = new Font(Font.Default, Color.Blue),
  316. DisabledTextFont = new Font(Font.Default, Color.DarkBlue),
  317. };
  318. // Next create a second instance
  319. TextControlDesign loadedDesign = new TextControlDesign();
  320. //// and save it out
  321. //byte[] savedData = textDesign.Save();
  322. //// and make a sanity check that it hasn't already the same data
  323. //// Now load the saved data of the first instance
  324. //loadedDesign.Load(savedData);
  325. // and check that all values are the same
  326. // Background design
  327. Assert.Equal(loadedDesign.Background.Name, textDesign.Background.Name);
  328. Assert.Equal(loadedDesign.Background.DrawLayer,
  329. textDesign.Background.DrawLayer);
  330. Assert.Equal(loadedDesign.Background.BlendColor,
  331. textDesign.Background.BlendColor);
  332. // Disabled background design
  333. Assert.Equal(loadedDesign.DisabledBackground.Name,
  334. textDesign.DisabledBackground.Name);
  335. Assert.Equal(loadedDesign.DisabledBackground.DrawLayer,
  336. textDesign.DisabledBackground.DrawLayer);
  337. Assert.Equal(loadedDesign.DisabledBackground.BlendColor,
  338. textDesign.DisabledBackground.BlendColor);
  339. // Hover design
  340. Assert.Equal(loadedDesign.Hover.Name, textDesign.Hover.Name);
  341. Assert.Equal(loadedDesign.Hover.DrawLayer,
  342. textDesign.Hover.DrawLayer);
  343. Assert.Equal(loadedDesign.Hover.BlendColor,
  344. textDesign.Hover.BlendColor);
  345. // Pressed design
  346. Assert.Equal(loadedDesign.Pressed.Name, textDesign.Pressed.Name);
  347. Assert.Equal(loadedDesign.Pressed.DrawLayer,
  348. textDesign.Pressed.DrawLayer);
  349. Assert.Equal(loadedDesign.Pressed.BlendColor,
  350. textDesign.Hover.BlendColor);
  351. // Text font
  352. Assert.Equal(loadedDesign.TextFont.FamilyName,
  353. textDesign.TextFont.FamilyName);
  354. Assert.Equal(loadedDesign.DisabledTextFont.FamilyName,
  355. textDesign.DisabledTextFont.FamilyName);
  356. Assert.Equal(loadedDesign.TextFont.LineHeight,
  357. textDesign.TextFont.LineHeight);
  358. // Text colors
  359. Assert.Equal(loadedDesign.TextFont.Color,
  360. textDesign.TextFont.Color);
  361. Assert.Equal(loadedDesign.DisabledTextFont.Color,
  362. textDesign.DisabledTextFont.Color);
  363. }
  364. #endregion
  365. }
  366. }
  367. }