SafeDispatch/MotoRepeaterCore/MenuButton.cs
2024-02-22 18:43:59 +02:00

167 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MotoRepeater
{
public partial class MenuButton : UserControl
{
public enum ThemeStyle { Teal, Read, Safenet };
private Color foreColorDisabled;
private Color foreColorSelected;
[Description("Fore Color Disabled"), Category("Data"), Browsable(true)]
public Color ForeColorDisabled
{
get { return foreColorDisabled; }
set { foreColorDisabled = value; UpdateForeColor(); }
}
[Description("Fore Color Selected"), Category("Data"), Browsable(true)]
public Color ForeColorSelected
{
get { return foreColorSelected; }
set { foreColorSelected = value; UpdateForeColor(); }
}
[Description("Title"), Category("Data"), Browsable(true)]
public string Title
{
get { return labelName.Text; }
set { labelName.Text = value; }
}
[Description("Image"), Category("Data")]
public Image Image
{
get { return pictureBox.Image; }
set { pictureBox.Image = value; }
}
[Description("Theme"), Category("Data")]
private ThemeStyle _theme;
public ThemeStyle Theme
{
get { return _theme; }
set { _theme = value; }
}
[Description("Selected"), Category("Data"), DefaultValue(false), Browsable(true)]
private bool _selected = false;
public bool Selected
{
get { return _selected; }
set
{
if (_selectable)
{
_selected = value;
if (!_selected)
UnSelected();
else
SelectedChanged();
}
}
}
[Description("Selectable"), Category("Data"), DefaultValue(true), Browsable(true)]
private bool _selectable = true;
public bool Selectable
{
get { return _selectable; }
set { _selectable = value;
_selected = false;
UnSelected();
}
}
public MenuButton()
{
InitializeComponent();
this.foreColorDisabled = Color.Gray;
this.foreColorSelected = Color.Turquoise;
this.ForeColor = Color.White;
//UnSelected();
}
public void SelectedChanged()
{
switch (this._theme)
{
case ThemeStyle.Safenet:
{
panelBack.BackColor = Color.FromArgb(14, 93, 100);
this.BackColor = Color.FromArgb(14, 93, 100);
labelName.ForeColor = Color.FromArgb(185, 95, 68);
if (OnButtonClick != null)
OnButtonClick();
break;
}
default:
{
panelBack.BackColor = Color.FromArgb(31, 180, 174);
this.BackColor = Color.FromArgb(31, 180, 174);
labelName.ForeColor = Color.Black;
if (OnButtonClick != null)
OnButtonClick();
break;
}
}
}
public void UnSelected()
{
panelBack.BackColor = Color.Transparent;
this.BackColor = Color.Transparent;
labelName.ForeColor = Color.FromArgb(190, 192, 189);
}
/// <summary>
/// Update the ForeColor of the label based on the state of the button
/// </summary>
public void UpdateForeColor()
{
if(this.Enabled)
labelName.ForeColor = this.ForeColor;
if(!this.Enabled)
labelName.ForeColor = foreColorDisabled;
if(this.Selected)
labelName.ForeColor = foreColorSelected;
}
public delegate void OnClickEvent();
public event OnClickEvent OnButtonClick;
private void OnObjClick(object sender, EventArgs e)
{
if (!_selected && _selectable)
{
//if (!_selectable)
{
Selected = true;
SelectedChanged();
}
this.OnClick(EventArgs.Empty);
}
if (!_selected)
this.OnClick(EventArgs.Empty);
}
}
}