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