#if FIVEM using Font = CitizenFX.Core.UI.Font; #elif RAGEMP using Font = RAGE.Game.Font; #elif RPH using Font = LemonUI.Elements.Font; #elif SHVDN3 using Font = GTA.UI.Font; #endif using LemonUI.Elements; using System; using System.Drawing; namespace LemonUI.Menus { /// /// Basic Rockstar-like item. /// public class NativeItem : IDrawable { #region Protected Internal Fields /// /// The title of the object. /// protected internal ScaledText title = null; /// /// The last known Item Position. /// protected internal PointF lastPosition = PointF.Empty; /// /// The last known Item Size. /// protected internal SizeF lastSize = SizeF.Empty; /// /// The last known Item Selection. /// protected internal bool lastSelected = false; /// /// The left badge of the Item. /// protected internal I2Dimensional badgeLeft = null; /// /// The left badge of the Item. /// protected internal I2Dimensional badgeRight = null; /// /// The alternate title of the menu. /// protected internal ScaledText altTitle = null; #endregion #region Private Fields private bool enabled = true; private BadgeSet badgeSetLeft = null; private BadgeSet badgeSetRight = null; private ColorSet colors = new ColorSet(); private ScaledRectangle background = new ScaledRectangle(PointF.Empty, SizeF.Empty); #endregion #region Public Properties /// /// If this item can be used or not. /// public bool Enabled { get => enabled; set { if (enabled == value) { return; } enabled = value; EnabledChanged?.Invoke(this, EventArgs.Empty); UpdateColors(); } } /// /// Object that contains data about this Item. /// public virtual object Tag { get; set; } /// /// The title of the item. /// public string Title { get => title.Text; set => title.Text = value; } /// /// The alternative title of the item shown on the right. /// public string AltTitle { get => altTitle.Text; set { altTitle.Text = value; Recalculate(); } } /// /// The font of title item. /// public Font TitleFont { get => title.Font; set => title.Font = value; } /// /// The font of alternative title item shown on the right. /// public Font AltTitleFont { get => altTitle.Font; set => altTitle.Font = value; } /// /// The description of the item. /// public string Description { get; set; } /// /// The Left badge of the Item. /// public I2Dimensional LeftBadge { get => badgeLeft; set { badgeLeft = value; Recalculate(); UpdateColors(); } } /// /// The Left badge set of the Item. /// public BadgeSet LeftBadgeSet { get => badgeSetLeft; set { badgeSetLeft = value; Recalculate(); UpdateColors(); } } /// /// The Right badge of the Item. /// public I2Dimensional RightBadge { get => badgeRight; set { badgeRight = value; Recalculate(); UpdateColors(); } } /// /// The Right badge set of the Item. /// public BadgeSet RightBadgeSet { get => badgeSetRight; set { badgeSetRight = value; Recalculate(); UpdateColors(); } } /// /// The different colors that change dynamically when the item is used. /// public ColorSet Colors { get => colors; set { colors = value; UpdateColors(); } } /// /// The Panel asociated to this . /// public NativePanel Panel { get; set; } = null; /// /// If a custom colored background should be used. /// public bool UseCustomBackground { get; set; } /// /// If this item is being hovered. /// public bool IsHovered => Screen.IsCursorInArea(background.Position, background.Size); #endregion #region Events /// /// Event triggered when the item is selected. /// public event SelectedEventHandler Selected; /// /// Event triggered when the item is activated. /// public event EventHandler Activated; /// /// Event triggered when the property is changed. /// public event EventHandler EnabledChanged; #endregion #region Constructors /// /// Creates a new . /// /// The title of the item. public NativeItem(string title) : this(title, string.Empty, string.Empty) { } /// /// Creates a new . /// /// The title of the item. /// The description of the item. public NativeItem(string title, string description) : this(title, description, string.Empty) { } /// /// Creates a new . /// /// The title of the item. /// The description of the item. /// The alternative title of the item, shown on the right. 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 Event Triggers /// /// Triggers the Selected event. /// protected internal void OnSelected(object sender, SelectedEventArgs e) => Selected?.Invoke(sender, e); /// /// Triggers the Activated event. /// protected internal void OnActivated(object sender) => Activated?.Invoke(sender, EventArgs.Empty); #endregion #region Private Functions /// /// Recalculates the item with the last known values. /// protected void Recalculate() => Recalculate(lastPosition, lastSize, lastSelected); #endregion #region Public Functions /// /// Recalculates the item positions and sizes with the specified values. /// /// The position of the item. /// The size of the item. /// If this item has been selected. 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(); } /// /// Draws the item. /// public virtual void Draw() { if (UseCustomBackground) { background.Draw(); } title.Draw(); altTitle.Draw(); badgeLeft?.Draw(); badgeRight?.Draw(); } /// /// Updates the colors of the from the . /// 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) { 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 } }