PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Scenes/UserInterfaces/Controls/TODODropdownBox.cs

#
C# | 389 lines | 208 code | 56 blank | 125 comment | 9 complexity | c495549d85afc46d4302a3d5c45de6ce MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.Engine;
  3. using Delta.Scenes.Enums;
  4. using Delta.Utilities.Datatypes;
  5. using NUnit.Framework;
  6. namespace Delta.Scenes.UserInterfaces.Controls
  7. {
  8. /// <summary>
  9. /// Dropdownbox
  10. /// </summary>
  11. public class TODODropdownBox : TextBox
  12. {
  13. #region Constants
  14. /// <summary>
  15. /// The name of the class which is saved in the content data to
  16. /// describe and "detect" the data in the loading code again.
  17. /// </summary>
  18. private const string SavingClassName = "UIDropdownBox";
  19. /// <summary>
  20. /// The current version of the implementation of this class.
  21. /// </summary>
  22. private const int WorkingVersion = 1;
  23. #endregion
  24. #region Delegates
  25. /// <summary>
  26. /// The delegate declaration for the "DropdownBox.IndexChanged" event.
  27. /// </summary>
  28. /// <param name="newIndex"></param>
  29. public delegate void SelectedIndexChangedDelegate(int newIndex);
  30. #endregion
  31. #region Items (Public)
  32. /// <summary>
  33. /// Items
  34. /// </summary>
  35. public object[] Items
  36. {
  37. get
  38. {
  39. return items;
  40. }
  41. set
  42. {
  43. items = value;
  44. //for (int i = 0; i < itemsLabel.Count; i++)
  45. //{
  46. // itemsLabel[i].Dispose();
  47. //}
  48. //itemsLabel.Clear();
  49. //isDropDownListVisible = false;
  50. //if (items == null ||
  51. // items.Length == 0)
  52. //{
  53. // SelectedIndex = -1;
  54. // return;
  55. //} // if
  56. //for (int i = 0; i < Items.Length; i++)
  57. //{
  58. // Label item = new Label
  59. // {
  60. // IsAutoSizing = false,
  61. // IsTextClipped = true,
  62. // Text = Items[i].ToString()
  63. // };
  64. // item.Clicked += OnItemClicked;
  65. // itemsLabel.Add(item);
  66. // Add(item);
  67. //} // for
  68. SelectedIndex = 0;
  69. } // set
  70. }
  71. #endregion
  72. #region SelectedItem (Public)
  73. /// <summary>
  74. /// Selected item
  75. /// </summary>
  76. public object SelectedItem
  77. {
  78. get;
  79. private set;
  80. }
  81. #endregion
  82. #region SelectedIndex (Public)
  83. /// <summary>
  84. /// Selected index
  85. /// </summary>
  86. public int SelectedIndex
  87. {
  88. get
  89. {
  90. return selectedIndex;
  91. }
  92. set
  93. {
  94. selectedIndex = value;
  95. if (selectedIndex < 0)
  96. {
  97. SelectedItem = "'null'";
  98. }
  99. else
  100. {
  101. SelectedItem = Items[selectedIndex];
  102. }
  103. // Set the new text
  104. Text = SelectedItem.ToString();
  105. // Call the event
  106. if (SelectionChanged != null)
  107. {
  108. SelectionChanged.Invoke(selectedIndex);
  109. }
  110. } // set
  111. }
  112. #endregion
  113. #region Protected
  114. #region ExpanderArea (Protected)
  115. /// <summary>
  116. /// Expander area
  117. /// </summary>
  118. protected internal Rectangle ExpanderArea
  119. {
  120. get
  121. {
  122. // Calc rotation
  123. Size size = new Size(Size.Height);
  124. Point pos = DrawArea.TopRight - DrawArea.Center;
  125. pos += size * 0.5f;
  126. pos.Rotate(Rotation);
  127. pos -= size * 0.5f;
  128. pos += DrawArea.Center;
  129. return new Rectangle(pos, size);
  130. } // get
  131. }
  132. #endregion
  133. #endregion
  134. #region Private
  135. #region items (Private)
  136. /// <summary>
  137. /// Items
  138. /// </summary>
  139. private object[] items;
  140. #endregion
  141. #region selectedIndex (Private)
  142. /// <summary>
  143. /// Selected index
  144. /// </summary>
  145. private int selectedIndex;
  146. #endregion
  147. #region itemsLabel (Private)
  148. /// <summary>
  149. /// Items label
  150. /// </summary>
  151. private readonly List<Label> itemsLabel;
  152. #endregion
  153. #endregion
  154. #region Constructors
  155. /// <summary>
  156. /// Create dropdownbox
  157. /// </summary>
  158. public TODODropdownBox()
  159. {
  160. itemsLabel = new List<Label>();
  161. IsReadOnly = true;
  162. Items = new object[0];
  163. }
  164. #endregion
  165. #region SelectionChanged (Event)
  166. /// <summary>
  167. /// Occurs every time the selected object resp index is changed.
  168. /// </summary>
  169. public event SelectedIndexChangedDelegate SelectionChanged;
  170. #endregion
  171. #region Methods (Private)
  172. #region ShowDropDownList
  173. /// <summary>
  174. /// Draw drop down list
  175. /// </summary>
  176. private void ShowDropDownList()
  177. {
  178. if (Items == null)
  179. {
  180. return;
  181. }
  182. float rotation = Rotation;
  183. Rotation = 0;
  184. // Add the items as labels in this ui control
  185. //for (int i = 0; i < Items.Length; i++)
  186. //{
  187. // Label item = new Label();
  188. // AddChild(item);
  189. // item.IsAutoSizing = false;
  190. // item.IsTextClipped = true;
  191. // item.Text = Items[i].ToString();
  192. // item.Size = new Size(Size.Width, TextFont.LineHeight);
  193. // Point pos = new Point(0, Size.Height + item.Size.Height * i) -
  194. // DrawingArea.Center;
  195. // pos += item.Size * 0.5f;
  196. // pos.Rotate(Rotation);
  197. // pos -= item.Size * 0.5f;
  198. // pos += DrawingArea.Center;
  199. // item.LocalPosition = pos;
  200. // item.Clicked += new ControlEvent(OnItemClicked);
  201. // itemsLabel.Add(item);
  202. //} // for
  203. for (int i = 0; i < itemsLabel.Count; i++)
  204. {
  205. itemsLabel[i].Size = new Size(Size.Width, TextFont.LineHeight);
  206. Point pos = new Point(0, Size.Height + itemsLabel[i].Size.Height * i) -
  207. DrawArea.Center;
  208. pos += itemsLabel[i].Size * 0.5f;
  209. pos.Rotate(Rotation);
  210. pos -= itemsLabel[i].Size * 0.5f;
  211. pos += DrawArea.Center;
  212. itemsLabel[i].LocalPosition = pos;
  213. //AddChild(itemsLabel[i]);
  214. //itemsLabel[i].IsEnabled = true;
  215. //itemsLabel[i].IsVisible = true;
  216. itemsLabel[i].State = ElementState.Enabled;
  217. } // for
  218. Rotation = rotation;
  219. // ToEval:
  220. //isDropDownListVisible = true;
  221. }
  222. #endregion
  223. // ShowDropDownList()
  224. #region HideDropDownList
  225. /// <summary>
  226. /// Hide drop down list
  227. /// </summary>
  228. private void HideDropDownList()
  229. {
  230. if (itemsLabel == null)
  231. {
  232. return;
  233. }
  234. // Destroy all item labels
  235. for (int i = 0; i < itemsLabel.Count; i++)
  236. {
  237. //itemsLabel[i].Destroy();
  238. //itemsLabel[i].IsEnabled = false;
  239. //itemsLabel[i].IsVisible = false;
  240. itemsLabel[i].State = ElementState.Invisible;
  241. } // for
  242. //itemsLabel.Clear();
  243. // ToEval:
  244. //isDropDownListVisible = false;
  245. }
  246. #endregion
  247. #endregion
  248. /// <summary>
  249. /// Tests for DropdownBox controls
  250. /// </summary>
  251. [Category("Visual")]
  252. internal class DropdownBoxTests
  253. {
  254. #region DisplayDropdownbox (Static)
  255. /// <summary>
  256. /// Dropdownbox label
  257. /// </summary>
  258. [Test]
  259. public static void DisplayDropdownbox()
  260. {
  261. // Init the Dropdownbox
  262. TODODropdownBox testDropdownBox = new TODODropdownBox
  263. {
  264. Items = new[]
  265. {
  266. "Hallo",
  267. "Delta",
  268. "blaaaaaaaaaaaaaaaa!!!"
  269. },
  270. LocalArea = new Rectangle(0.25f, 0.25f, 0.4f, 0.075f),
  271. };
  272. Screen testScene = new Screen();
  273. testScene.Add(testDropdownBox);
  274. // Open now the scene to "activate" for the test
  275. testScene.Open();
  276. // Just start the game screen that has been setup so far.
  277. Application.Start();
  278. }
  279. #endregion
  280. #region DisplayNonAutoSizeDropdownbox (Static)
  281. /// <summary>
  282. /// Display non auto size dropdownbox
  283. /// </summary>
  284. [Test]
  285. public static void DisplayNonAutoSizeDropdownbox()
  286. {
  287. // Init the Dropdownbox
  288. TODODropdownBox testDropdownBox = new TODODropdownBox
  289. {
  290. Items = new[]
  291. {
  292. "Hallo",
  293. "Delta",
  294. "blaaaaaaaaaaaaaaaa!!!"
  295. },
  296. LocalArea = new Rectangle(0.25f, 0.25f, 0.2f, 0.05f),
  297. };
  298. Screen testScene = new Screen();
  299. testScene.Add(testDropdownBox);
  300. // Open now the scene to "activate" for the test
  301. testScene.Open();
  302. Application.Start();
  303. }
  304. #endregion
  305. #region DisplayRotatedDropdownbox (Static)
  306. /// <summary>
  307. /// Display rotated dropdownbox
  308. /// </summary>
  309. [Test]
  310. public static void DisplayRotatedDropdownbox()
  311. {
  312. // Init the Dropdownbox
  313. TODODropdownBox testDropdownBox = new TODODropdownBox
  314. {
  315. Items = new[]
  316. {
  317. "Hallo",
  318. "Delta",
  319. "blaaaaaaaaaaaaaaaa!!!"
  320. },
  321. LocalArea = new Rectangle(0.25f, 0.25f, 0.4f, 0.075f),
  322. Rotation = 45,
  323. };
  324. Screen testScene = new Screen();
  325. testScene.Add(testDropdownBox);
  326. // Open now the scene to "activate" for the test
  327. testScene.Open();
  328. Application.Start();
  329. }
  330. #endregion
  331. }
  332. }
  333. }