agrobot_base/AgroBase/AgroBase/Forms/IHM/Controls/HmiModuleBadge.cs

174 lines
4.6 KiB
C#

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace AgroBase.Forms.IHM.Controls
{
public enum HmiModuleState
{
Operante = 0,
Alerta = 1,
Falha = 2,
Desconectado = 3,
Selecionado = 4
}
[DefaultEvent("Click")]
public class HmiModuleBadge : Control
{
private HmiModuleState _state = HmiModuleState.Operante;
private string _moduleCode = "MOD";
public HmiModuleBadge()
{
SetStyle(
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw,
true);
DoubleBuffered = true;
BackColor = Color.Transparent;
ForeColor = HmiTheme.Text;
Cursor = Cursors.Hand;
Font = new Font("Segoe UI", 8F, FontStyle.Bold);
Size = new Size(45, 27);
MinimumSize = new Size(42, 26);
UpdateStyles();
}
[Category("Conteúdo")]
public string ModuleCode
{
get { return _moduleCode; }
set
{
_moduleCode = value ?? string.Empty;
Invalidate();
}
}
[Category("Estado")]
public HmiModuleState State
{
get { return _state; }
set
{
_state = value;
Invalidate();
}
}
private Color Accent
{
get
{
switch (_state)
{
case HmiModuleState.Alerta:
return HmiTheme.Warning;
case HmiModuleState.Falha:
return HmiTheme.Danger;
case HmiModuleState.Desconectado:
return HmiTheme.TextMuted;
case HmiModuleState.Selecionado:
return HmiTheme.Info;
case HmiModuleState.Operante:
default:
return HmiTheme.Success;
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
using (GraphicsPath path = CriarCaminhoArredondado(rect, 7))
{
Color accent = Accent;
using (SolidBrush fill = new SolidBrush(
Color.FromArgb(34, accent.R, accent.G, accent.B)))
{
e.Graphics.FillPath(fill, path);
}
using (Pen border = new Pen(
accent,
_state == HmiModuleState.Selecionado ? 2F : 1.2F))
{
e.Graphics.DrawPath(border, path);
}
}
TextRenderer.DrawText(
e.Graphics,
_moduleCode,
Font,
rect,
_state == HmiModuleState.Desconectado
? HmiTheme.TextMuted
: HmiTheme.Text,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter |
TextFormatFlags.EndEllipsis);
}
private static GraphicsPath CriarCaminhoArredondado(
Rectangle bounds,
int radius)
{
GraphicsPath path = new GraphicsPath();
int diameter = radius * 2;
path.AddArc(
bounds.Left,
bounds.Top,
diameter,
diameter,
180,
90);
path.AddArc(
bounds.Right - diameter,
bounds.Top,
diameter,
diameter,
270,
90);
path.AddArc(
bounds.Right - diameter,
bounds.Bottom - diameter,
diameter,
diameter,
0,
90);
path.AddArc(
bounds.Left,
bounds.Bottom - diameter,
diameter,
diameter,
90,
90);
path.CloseFigure();
return path;
}
}
}