Skip to content

File NativeItem.cs

File List > LemonUI > LemonUI > Menus > NativeItem.cs

Go to the documentation of this file


#if FIVEM
using Font = CitizenFX.Core.UI.Font;
#elif RAGEMP
using Font = RAGE.Game.Font;
#elif RPH
using Font = LemonUI.Elements.Font;
#elif SHVDN3 || SHVDNC
using Font = GTA.UI.Font;
#endif

using LemonUI.Elements;
using System;
using System.Drawing;
using LemonUI.Tools;

namespace LemonUI.Menus
{
    public class NativeItem : IDrawable
    {
        #region Fields

        protected internal ScaledText title;
        protected internal PointF lastPosition = PointF.Empty;
        protected internal SizeF lastSize = SizeF.Empty;
        protected internal bool lastSelected;
        protected internal I2Dimensional badgeLeft;
        protected internal I2Dimensional badgeRight;
        protected internal ScaledText altTitle;

        private bool enabled = true;
        private BadgeSet badgeSetLeft;
        private BadgeSet badgeSetRight;
        private ColorSet colors = new ColorSet();
        private ScaledRectangle background = new ScaledRectangle(PointF.Empty, SizeF.Empty);
        private string description = string.Empty;

        #endregion

        #region Properties

        public bool Enabled
        {
            get => enabled;
            set
            {
                if (enabled == value)
                {
                    return;
                }
                enabled = value;
                EnabledChanged?.Invoke(this, EventArgs.Empty);
                UpdateColors();
            }
        }
        public virtual object Tag { get; set; }
        public string Title
        {
            get => title.Text;
            set => title.Text = value ?? throw new ArgumentNullException(nameof(value));
        }
        public string AltTitle
        {
            get => altTitle.Text;
            set
            {
                altTitle.Text = value ?? throw new ArgumentNullException(nameof(value));
                Recalculate();
            }
        }
        public Font TitleFont
        {
            get => title.Font;
            set => title.Font = value;
        }
        public Font AltTitleFont
        {
            get => altTitle.Font;
            set => altTitle.Font = value;
        }
        public string Description
        {
            get => description;
            set => description = value ?? throw new ArgumentNullException(nameof(value));
        }
        public I2Dimensional LeftBadge
        {
            get => badgeLeft;
            set
            {
                if (badgeLeft == value)
                {
                    return;
                }

                badgeLeft = value;

                Recalculate();
                UpdateColors();
            }
        }
        public BadgeSet LeftBadgeSet
        {
            get => badgeSetLeft;
            set
            {
                if (badgeSetLeft == value)
                {
                    return;
                }

                badgeSetLeft = value;

                if (badgeSetLeft == null)
                {
                    badgeLeft = null;
                }

                Recalculate();
                UpdateColors();
            }
        }
        public I2Dimensional RightBadge
        {
            get => badgeRight;
            set
            {
                if (badgeRight == value)
                {
                    return;
                }

                badgeRight = value;

                Recalculate();
                UpdateColors();
            }
        }
        public BadgeSet RightBadgeSet
        {
            get => badgeSetRight;
            set
            {
                if (badgeSetRight == value)
                {
                    return;
                }

                badgeSetRight = value;

                if (badgeSetRight == null)
                {
                    badgeRight = null;
                }

                Recalculate();
                UpdateColors();
            }
        }
        public ColorSet Colors
        {
            get => colors;
            set
            {
                colors = value;
                UpdateColors();
            }
        }
        public NativePanel Panel { get; set; } = null;
        public bool UseCustomBackground { get; set; }
        public bool IsHovered => GameScreen.IsCursorInArea(background.Position, background.Size);

        #endregion

        #region Events

        public event SelectedEventHandler Selected;
        public event EventHandler Activated;
        public event EventHandler EnabledChanged;

        #endregion

        #region Constructors

        public NativeItem(string title) : this(title, string.Empty, string.Empty)
        {
        }
        public NativeItem(string title, string description) : this(title, description, string.Empty)
        {
        }
        public NativeItem(string title, string description, string altTitle)
        {
            this.title = new ScaledText(PointF.Empty, title, 0.345f);
            Description = description;
            this.altTitle = new ScaledText(PointF.Empty, altTitle, 0.345f);
        }

        #endregion

        #region Tools

        protected internal void OnSelected(object sender, SelectedEventArgs e) => Selected?.Invoke(sender, e);
        protected internal void OnActivated(object sender) => Activated?.Invoke(sender, EventArgs.Empty);

        protected void Recalculate() => Recalculate(lastPosition, lastSize, lastSelected);

        #endregion

        #region Functions

        public virtual void Recalculate(PointF pos, SizeF size, bool selected)
        {
            lastPosition = pos;
            lastSize = size;
            lastSelected = selected;

            background.Position = pos;
            background.Size = size;

            if (badgeSetLeft != null)
            {
                if (!(badgeLeft is ScaledTexture))
                {
                    badgeLeft = new ScaledTexture(string.Empty, string.Empty);
                }
                ScaledTexture left = (ScaledTexture)badgeLeft;
                left.Dictionary = selected ? badgeSetLeft.HoveredDictionary : badgeSetLeft.NormalDictionary;
                left.Texture = selected ? badgeSetLeft.HoveredTexture : badgeSetLeft.NormalTexture;
            }
            if (badgeSetRight != null)
            {
                if (!(badgeRight is ScaledTexture))
                {
                    badgeRight = new ScaledTexture(string.Empty, string.Empty);
                }
                ScaledTexture right = (ScaledTexture)badgeRight;
                right.Dictionary = selected ? badgeSetRight.HoveredDictionary : badgeSetRight.NormalDictionary;
                right.Texture = selected ? badgeSetRight.HoveredTexture : badgeSetRight.NormalTexture;
            }

            if (badgeLeft != null)
            {
                badgeLeft.Position = new PointF(pos.X + 2, pos.Y - 3);
                badgeLeft.Size = new SizeF(45, 45);
            }
            if (badgeRight != null)
            {
                badgeRight.Position = new PointF(pos.X + size.Width - 47, pos.Y - 3);
                badgeRight.Size = new SizeF(45, 45);
            }

            title.Position = new PointF(pos.X + (badgeLeft == null ? 0 : 34) + 6, pos.Y + 3);
            altTitle.Position = new PointF(pos.X + size.Width - (badgeRight == null ? 0 : 34) - altTitle.Width - 6, pos.Y + 3);

            UpdateColors();
        }
        public virtual void Draw()
        {
            if (UseCustomBackground)
            {
                background.Draw();
            }

            title.Draw();
            altTitle.Draw();
            badgeLeft?.Draw();
            badgeRight?.Draw();
        }
        public virtual void UpdateColors()
        {
            if (!Enabled)
            {
                background.Color = Colors.BackgroundDisabled;
                title.Color = Colors.TitleDisabled;
                altTitle.Color = Colors.AltTitleDisabled;
                if (badgeLeft != null)
                {
                    badgeLeft.Color = Colors.BadgeLeftDisabled;
                }
                if (badgeRight != null)
                {
                    badgeRight.Color = Colors.BadgeRightDisabled;
                }
            }
            else if (lastSelected && !(this is NativeSeparatorItem))
            {
                background.Color = Colors.BackgroundHovered;
                title.Color = Colors.TitleHovered;
                altTitle.Color = Colors.AltTitleHovered;
                if (badgeLeft != null)
                {
                    badgeLeft.Color = Colors.BadgeLeftHovered;
                }
                if (badgeRight != null)
                {
                    badgeRight.Color = Colors.BadgeRightHovered;
                }
            }
            else
            {
                background.Color = Colors.BackgroundNormal;
                title.Color = Colors.TitleNormal;
                altTitle.Color = Colors.AltTitleNormal;
                if (badgeLeft != null)
                {
                    badgeLeft.Color = Colors.BadgeLeftNormal;
                }
                if (badgeRight != null)
                {
                    badgeRight.Color = Colors.BadgeRightNormal;
                }
            }
        }

        #endregion
    }
}