Skip to content

File NativeCheckboxItem.cs

File List > LemonUI > LemonUI > Menus > NativeCheckboxItem.cs

Go to the documentation of this file


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

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

        protected internal ScaledTexture check = new ScaledTexture(PointF.Empty, SizeF.Empty, "commonmenu", string.Empty);
        private bool checked_ = false;

        #endregion

        #region Defaults

        public static readonly BadgeSet DefaultCheckedSet = new BadgeSet
        {
            NormalDictionary = "commonmenu",
            NormalTexture = "shop_box_tick",
            HoveredDictionary = "commonmenu",
            HoveredTexture = "shop_box_tickb"
        };
        public static readonly BadgeSet DefaultUncheckedSet = new BadgeSet
        {
            NormalDictionary = "commonmenu",
            NormalTexture = "shop_box_blank",
            HoveredDictionary = "commonmenu",
            HoveredTexture = "shop_box_blankb"
        };

        #endregion

        #region Properties

        public bool Checked
        {
            get => checked_;
            set
            {
                if (checked_ == value)
                {
                    return;
                }
                checked_ = value;
                UpdateTexture(lastSelected);
                CheckboxChanged?.Invoke(this, EventArgs.Empty);
            }
        }
        public BadgeSet CheckedSet { get; set; } = DefaultCheckedSet;
        public BadgeSet UncheckedSet { get; set; } = DefaultUncheckedSet;

        #endregion

        #region Events

        public event EventHandler CheckboxChanged;

        #endregion

        #region Constructor

        public NativeCheckboxItem(string title) : this(title, string.Empty, false)
        {
        }
        public NativeCheckboxItem(string title, bool check) : this(title, string.Empty, check)
        {
        }
        public NativeCheckboxItem(string title, string description) : this(title, description, false)
        {
        }
        public NativeCheckboxItem(string title, string description, bool check) : base(title, description)
        {
            Checked = check;
            Activated += Toggle;
            EnabledChanged += NativeCheckboxItem_EnabledChanged;
        }

        #endregion

        #region Event Functions

        private void NativeCheckboxItem_EnabledChanged(object sender, EventArgs e) => UpdateTexture(lastSelected);

        #endregion

        #region Tools

        private void Toggle(object sender, EventArgs e) => Checked = !Checked;
        protected internal void UpdateTexture(bool selected)
        {
            bool showLight = !selected || !Enabled;

            // If the item is not selected or is not enabled, use the white pictures
            if (Checked)
            {
                check.Texture = showLight ? CheckedSet.NormalTexture : CheckedSet.HoveredTexture;
            }
            // Otherwise, use the black ones
            else
            {
                check.Texture = showLight ? UncheckedSet.NormalTexture : UncheckedSet.HoveredTexture;
            }
        }

        #endregion

        #region Functions

        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);
        }
        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
    }
}