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 readonly ScaledText text = new ScaledText(PointF.Empty, string.Empty, 0.35f);
private T item = default;
#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, string.Empty, default)
{
}
///
/// Creates a new Dynamic List Item.
///
/// The Title of the item.
/// The Item to set.
public NativeDynamicItem(string title, T item) : this(title, string.Empty, 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() : string.Empty;
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);
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();
}
///
public override void UpdateColors()
{
base.UpdateColors();
if (!Enabled)
{
text.Color = Colors.TitleDisabled;
}
else if (lastSelected)
{
text.Color = Colors.TitleHovered;
}
else
{
text.Color = Colors.TitleNormal;
}
}
#endregion
}
}