using LemonUI.Elements; using System; using System.Drawing; namespace LemonUI.Menus { /// /// Rockstar-like checkbox item. /// public class NativeCheckboxItem : NativeItem { #region Fields /// /// The image shown on the checkbox. /// internal protected ScaledTexture check = new ScaledTexture(PointF.Empty, SizeF.Empty, "commonmenu", string.Empty); /// /// If this item is checked or not. /// private bool checked_ = false; #endregion #region Properties /// /// If this item is checked or not. /// public bool Checked { get => checked_; set { if (checked_ == value) { return; } checked_ = value; UpdateTexture(lastSelected); CheckboxChanged?.Invoke(this, EventArgs.Empty); } } #endregion #region Events /// /// Event triggered when the checkbox changes. /// public event EventHandler CheckboxChanged; #endregion #region Constructor /// /// Creates a new . /// /// The title used for the Item. public NativeCheckboxItem(string title) : this(title, string.Empty, false) { } /// /// Creates a new . /// /// The title used for the Item. /// If the checkbox should be enabled or not. public NativeCheckboxItem(string title, bool check) : this(title, string.Empty, check) { } /// /// Creates a new . /// /// The title used for the Item. /// The description of the Item. public NativeCheckboxItem(string title, string description) : this(title, description, false) { } /// /// Creates a new . /// /// The title used for the Item. /// The description of the Item. /// If the checkbox should be enabled or not. public NativeCheckboxItem(string title, string description, bool check) : base(title, description) { Checked = check; Activated += Toggle; EnabledChanged += NativeCheckboxItem_EnabledChanged; } #endregion #region Local Events private void NativeCheckboxItem_EnabledChanged(object sender, EventArgs e) => UpdateTexture(lastSelected); #endregion #region Internal Functions /// /// Inverts the checkbox activation. /// private void Toggle(object sender, EventArgs e) => Checked = !Checked; /// /// Updates the texture of the sprite. /// internal protected void UpdateTexture(bool selected) { // If the item is not selected or is not enabled, use the white pictures if (!selected || !Enabled) { check.Texture = Checked ? "shop_box_tick" : "shop_box_blank"; } // Otherwise, use the black ones else { check.Texture = Checked ? "shop_box_tickb" : "shop_box_blankb"; } } #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 override void Recalculate(PointF pos, SizeF size, bool selected) { base.Recalculate(pos, size, selected); // Set the correct texture UpdateTexture(selected); // And set the checkbox positions check.Position = new PointF(pos.X + size.Width - 50, pos.Y - 6); check.Size = new SizeF(50, 50); } /// /// Draws the Checkbox on the screen. /// public override void Draw() { title.Draw(); badgeLeft?.Draw(); check.Draw(); } /// public override void UpdateColors() { base.UpdateColors(); if (!Enabled) { check.Color = Colors.BadgeRightDisabled; } else if (lastSelected) { check.Color = Colors.BadgeRightHovered; } else { check.Color = Colors.BadgeRightNormal; } } #endregion } }