From cdb8328a5cd3da68eb645511aedeeb4fd187f3bd Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Sat, 14 Nov 2020 01:52:43 -0300 Subject: [PATCH] Added Dynamic Item (closes #8) --- LemonUI/Menus/NativeDynamicItem.cs | 161 +++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 LemonUI/Menus/NativeDynamicItem.cs diff --git a/LemonUI/Menus/NativeDynamicItem.cs b/LemonUI/Menus/NativeDynamicItem.cs new file mode 100644 index 00000000..716bedfe --- /dev/null +++ b/LemonUI/Menus/NativeDynamicItem.cs @@ -0,0 +1,161 @@ +using LemonUI.Elements; +using System.Drawing; + +namespace LemonUI.Menus +{ + /// + /// Dynamic Items allow you to dynamically change the item shown to the user. + /// + /// The type of item. + public class NativeDynamicItem : NativeSlidableItem + { + #region Fields + + private T item = default; + private ScaledText text = new ScaledText(PointF.Empty, "", 0.35f) + { + Color = NativeMenu.colorWhiteSmoke + }; + + #endregion + + #region Properties + + /// + /// The currently selected item. + /// + public T SelectedItem + { + get => item; + set + { + item = value; + } + } + + #endregion + + #region Events + + /// + /// Event triggered when the user has changed the item. + /// + public event ItemChangedEventHandler ItemChanged; + + #endregion + + #region Constructor + + /// + /// Creates a new Dynamic List Item. + /// + /// The Title of the item. + public NativeDynamicItem(string title) : this(title, "", default) + { + } + /// + /// Creates a new Dynamic List Item. + /// + /// The Title of the item. + /// The Item to set. + public NativeDynamicItem(string title, T item) : this(title, "", item) + { + } + /// + /// Creates a new Dynamic List Item. + /// + /// The Title of the item. + /// The Description of the item. + public NativeDynamicItem(string title, string description) : this(title, description, default) + { + } + /// + /// Creates a new Dynamic List Item. + /// + /// The Title of the item. + /// The Description of the item. + /// The Item to set. + public NativeDynamicItem(string title, string description, T item) : base(title, description) + { + this.item = item; + } + + #endregion + + #region Functions + + /// + /// Updates the currently selected item based on the index. + /// + private void UpdateItemName() + { + // This is the SAME as the normal NativeListItem + + text.Text = !SelectedItem.Equals(default) ? SelectedItem.ToString() : ""; + + text.Position = new PointF(RightArrow.Position.X - text.Width + 3, text.Position.Y); + LeftArrow.Position = new PointF(text.Position.X - LeftArrow.Size.Width, LeftArrow.Position.Y); + } + /// + /// Gets the previous item. + /// + public override void GoLeft() + { + ItemChangedEventArgs arguments = new ItemChangedEventArgs(item, -1, Direction.Left); + ItemChanged?.Invoke(this, arguments); + SelectedItem = arguments.Object; + UpdateItemName(); + } + /// + /// Gets the next item. + /// + public override void GoRight() + { + ItemChangedEventArgs arguments = new ItemChangedEventArgs(item, -1, Direction.Right); + ItemChanged?.Invoke(this, arguments); + SelectedItem = arguments.Object; + UpdateItemName(); + } + /// + /// Recalculates the position of the current List Item. + /// + /// The new position of the item. + /// The Size of the item. + /// If the item is selected or not. + public override void Recalculate(PointF pos, SizeF size, bool selected) + { + // This is the SAME as the normal NativeListItem + + base.Recalculate(pos, size, selected); + + if (!Enabled) + { + text.Color = NativeMenu.colorDisabled; + } + else if (selected) + { + text.Color = NativeMenu.colorBlack; + } + else + { + text.Color = NativeMenu.colorWhiteSmoke; + } + + float textWidth = RightArrow.Size.Width; + text.Position = new PointF(pos.X + size.Width - textWidth - 1 - text.Width, pos.Y + 3); + LeftArrow.Position = new PointF(text.Position.X - LeftArrow.Size.Width, pos.Y + 4); + + UpdateItemName(); + } + /// + /// Draws the List on the screen. + /// + public override void Draw() + { + base.Draw(); // Arrows, Title and Left Badge + text.Draw(); + } + + #endregion + } +}