Skip to content

File NativeDynamicItem.cs

File List > LemonUI > LemonUI > Menus > NativeDynamicItem.cs

Go to the documentation of this file


using LemonUI.Elements;
using System.Drawing;

namespace LemonUI.Menus
{
    public class NativeDynamicItem<T> : NativeSlidableItem
    {
        #region Fields

        private readonly ScaledText text = new ScaledText(PointF.Empty, string.Empty, 0.35f);
        private T item = default;

        #endregion

        #region Properties

        public T SelectedItem
        {
            get => item;
            set
            {
                item = value;
                UpdateItemName();
            }
        }

        #endregion

        #region Events

        public event ItemChangedEventHandler<T> ItemChanged;

        #endregion

        #region Constructors

        public NativeDynamicItem(string title) : this(title, string.Empty, default)
        {
        }
        public NativeDynamicItem(string title, T item) : this(title, string.Empty, item)
        {
        }
        public NativeDynamicItem(string title, string description) : this(title, description, default)
        {
        }
        public NativeDynamicItem(string title, string description, T item) : base(title, description)
        {
            this.item = item;
        }

        #endregion

        #region Functions

        private void UpdateItemName()
        {
            // This is the SAME as the normal NativeListItem

            text.Text = SelectedItem == null ? string.Empty : 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);
        }
        public override void GoLeft()
        {
            ItemChangedEventArgs<T> arguments = new ItemChangedEventArgs<T>(item, -1, Direction.Left);
            ItemChanged?.Invoke(this, arguments);
            SelectedItem = arguments.Object;
            UpdateItemName();
        }
        public override void GoRight()
        {
            ItemChangedEventArgs<T> arguments = new ItemChangedEventArgs<T>(item, -1, Direction.Right);
            ItemChanged?.Invoke(this, arguments);
            SelectedItem = arguments.Object;
            UpdateItemName();
        }
        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();
        }
        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
    }
}