/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
- using System.Collections.Generic;
- using Delta.Engine;
- using Delta.Scenes.Enums;
- using Delta.Utilities.Datatypes;
- using NUnit.Framework;
-
- namespace Delta.Scenes.UserInterfaces.Controls
- {
- /// <summary>
- /// Dropdownbox
- /// </summary>
- public class TODODropdownBox : TextBox
- {
- #region Constants
- /// <summary>
- /// The name of the class which is saved in the content data to
- /// describe and "detect" the data in the loading code again.
- /// </summary>
- private const string SavingClassName = "UIDropdownBox";
-
- /// <summary>
- /// The current version of the implementation of this class.
- /// </summary>
- private const int WorkingVersion = 1;
- #endregion
-
- #region Delegates
- /// <summary>
- /// The delegate declaration for the "DropdownBox.IndexChanged" event.
- /// </summary>
- /// <param name="newIndex"></param>
- public delegate void SelectedIndexChangedDelegate(int newIndex);
- #endregion
-
- #region Items (Public)
- /// <summary>
- /// Items
- /// </summary>
- public object[] Items
- {
- get
- {
- return items;
- }
- set
- {
- items = value;
-
- //for (int i = 0; i < itemsLabel.Count; i++)
- //{
- // itemsLabel[i].Dispose();
- //}
- //itemsLabel.Clear();
-
- //isDropDownListVisible = false;
-
- //if (items == null ||
- // items.Length == 0)
- //{
- // SelectedIndex = -1;
- // return;
- //} // if
-
- //for (int i = 0; i < Items.Length; i++)
- //{
- // Label item = new Label
- // {
- // IsAutoSizing = false,
- // IsTextClipped = true,
- // Text = Items[i].ToString()
- // };
-
- // item.Clicked += OnItemClicked;
-
- // itemsLabel.Add(item);
- // Add(item);
- //} // for
-
- SelectedIndex = 0;
- } // set
- }
- #endregion
-
- #region SelectedItem (Public)
- /// <summary>
- /// Selected item
- /// </summary>
- public object SelectedItem
- {
- get;
- private set;
- }
- #endregion
-
- #region SelectedIndex (Public)
- /// <summary>
- /// Selected index
- /// </summary>
- public int SelectedIndex
- {
- get
- {
- return selectedIndex;
- }
- set
- {
- selectedIndex = value;
-
- if (selectedIndex < 0)
- {
- SelectedItem = "'null'";
- }
- else
- {
- SelectedItem = Items[selectedIndex];
- }
-
- // Set the new text
- Text = SelectedItem.ToString();
-
- // Call the event
- if (SelectionChanged != null)
- {
- SelectionChanged.Invoke(selectedIndex);
- }
- } // set
- }
- #endregion
-
- #region Protected
-
- #region ExpanderArea (Protected)
- /// <summary>
- /// Expander area
- /// </summary>
- protected internal Rectangle ExpanderArea
- {
- get
- {
- // Calc rotation
- Size size = new Size(Size.Height);
- Point pos = DrawArea.TopRight - DrawArea.Center;
- pos += size * 0.5f;
- pos.Rotate(Rotation);
- pos -= size * 0.5f;
- pos += DrawArea.Center;
-
- return new Rectangle(pos, size);
- } // get
- }
- #endregion
-
- #endregion
-
- #region Private
-
- #region items (Private)
- /// <summary>
- /// Items
- /// </summary>
- private object[] items;
- #endregion
-
- #region selectedIndex (Private)
- /// <summary>
- /// Selected index
- /// </summary>
- private int selectedIndex;
- #endregion
-
- #region itemsLabel (Private)
- /// <summary>
- /// Items label
- /// </summary>
- private readonly List<Label> itemsLabel;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create dropdownbox
- /// </summary>
- public TODODropdownBox()
- {
- itemsLabel = new List<Label>();
- IsReadOnly = true;
- Items = new object[0];
- }
- #endregion
-
- #region SelectionChanged (Event)
- /// <summary>
- /// Occurs every time the selected object resp index is changed.
- /// </summary>
- public event SelectedIndexChangedDelegate SelectionChanged;
- #endregion
-
- #region Methods (Private)
-
- #region ShowDropDownList
- /// <summary>
- /// Draw drop down list
- /// </summary>
- private void ShowDropDownList()
- {
- if (Items == null)
- {
- return;
- }
-
- float rotation = Rotation;
- Rotation = 0;
-
- // Add the items as labels in this ui control
- //for (int i = 0; i < Items.Length; i++)
- //{
- // Label item = new Label();
- // AddChild(item);
- // item.IsAutoSizing = false;
- // item.IsTextClipped = true;
- // item.Text = Items[i].ToString();
- // item.Size = new Size(Size.Width, TextFont.LineHeight);
-
- // Point pos = new Point(0, Size.Height + item.Size.Height * i) -
- // DrawingArea.Center;
- // pos += item.Size * 0.5f;
- // pos.Rotate(Rotation);
- // pos -= item.Size * 0.5f;
- // pos += DrawingArea.Center;
-
- // item.LocalPosition = pos;
-
- // item.Clicked += new ControlEvent(OnItemClicked);
-
- // itemsLabel.Add(item);
- //} // for
-
- for (int i = 0; i < itemsLabel.Count; i++)
- {
- itemsLabel[i].Size = new Size(Size.Width, TextFont.LineHeight);
-
- Point pos = new Point(0, Size.Height + itemsLabel[i].Size.Height * i) -
- DrawArea.Center;
- pos += itemsLabel[i].Size * 0.5f;
- pos.Rotate(Rotation);
- pos -= itemsLabel[i].Size * 0.5f;
- pos += DrawArea.Center;
-
- itemsLabel[i].LocalPosition = pos;
-
- //AddChild(itemsLabel[i]);
- //itemsLabel[i].IsEnabled = true;
- //itemsLabel[i].IsVisible = true;
-
- itemsLabel[i].State = ElementState.Enabled;
- } // for
-
- Rotation = rotation;
- // ToEval:
- //isDropDownListVisible = true;
- }
- #endregion
-
- // ShowDropDownList()
-
- #region HideDropDownList
- /// <summary>
- /// Hide drop down list
- /// </summary>
- private void HideDropDownList()
- {
- if (itemsLabel == null)
- {
- return;
- }
-
- // Destroy all item labels
- for (int i = 0; i < itemsLabel.Count; i++)
- {
- //itemsLabel[i].Destroy();
- //itemsLabel[i].IsEnabled = false;
- //itemsLabel[i].IsVisible = false;
- itemsLabel[i].State = ElementState.Invisible;
- } // for
- //itemsLabel.Clear();
-
- // ToEval:
- //isDropDownListVisible = false;
- }
- #endregion
-
- #endregion
-
- /// <summary>
- /// Tests for DropdownBox controls
- /// </summary>
- [Category("Visual")]
- internal class DropdownBoxTests
- {
- #region DisplayDropdownbox (Static)
- /// <summary>
- /// Dropdownbox label
- /// </summary>
- [Test]
- public static void DisplayDropdownbox()
- {
- // Init the Dropdownbox
- TODODropdownBox testDropdownBox = new TODODropdownBox
- {
- Items = new[]
- {
- "Hallo",
- "Delta",
- "blaaaaaaaaaaaaaaaa!!!"
- },
- LocalArea = new Rectangle(0.25f, 0.25f, 0.4f, 0.075f),
- };
-
- Screen testScene = new Screen();
- testScene.Add(testDropdownBox);
- // Open now the scene to "activate" for the test
- testScene.Open();
-
- // Just start the game screen that has been setup so far.
- Application.Start();
- }
- #endregion
-
- #region DisplayNonAutoSizeDropdownbox (Static)
- /// <summary>
- /// Display non auto size dropdownbox
- /// </summary>
- [Test]
- public static void DisplayNonAutoSizeDropdownbox()
- {
- // Init the Dropdownbox
- TODODropdownBox testDropdownBox = new TODODropdownBox
- {
- Items = new[]
- {
- "Hallo",
- "Delta",
- "blaaaaaaaaaaaaaaaa!!!"
- },
- LocalArea = new Rectangle(0.25f, 0.25f, 0.2f, 0.05f),
- };
-
- Screen testScene = new Screen();
- testScene.Add(testDropdownBox);
- // Open now the scene to "activate" for the test
- testScene.Open();
-
- Application.Start();
- }
- #endregion
-
- #region DisplayRotatedDropdownbox (Static)
- /// <summary>
- /// Display rotated dropdownbox
- /// </summary>
- [Test]
- public static void DisplayRotatedDropdownbox()
- {
- // Init the Dropdownbox
- TODODropdownBox testDropdownBox = new TODODropdownBox
- {
- Items = new[]
- {
- "Hallo",
- "Delta",
- "blaaaaaaaaaaaaaaaa!!!"
- },
- LocalArea = new Rectangle(0.25f, 0.25f, 0.4f, 0.075f),
- Rotation = 45,
- };
-
- Screen testScene = new Screen();
- testScene.Add(testDropdownBox);
- // Open now the scene to "activate" for the test
- testScene.Open();
-
- Application.Start();
- }
- #endregion
- }
- }
- }
-