using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Metis.AutoAnalysis { public class SampleStageControl : UserControl { List _sampleList = new List(); public int PaddingX; public int PaddingY; private float _diameter; public SampleStageControl() { SampleControl.Brushs.Add(new SolidBrush(Color.White)); SampleControl.Brushs.Add(new SolidBrush(Color.Red)); SampleControl.Brushs.Add(new SolidBrush(Color.Blue)); SampleControl.Brushs.Add(new SolidBrush(Color.Green)); } public void Update(SampleStageModel model) { _selectId = -1; Controls.Clear(); _sampleList.Clear(); List workPoints = model.WorkPoits; if (workPoints.Count < 1) return; float diameter = model.Diameter; PaddingX = model.PaddingX; PaddingY = model.PaddingY; var maxX = workPoints.Max((e) => e.X); var minX = workPoints.Min((e) => e.X); var maxY = workPoints.Max((e) => e.Y); var minY = workPoints.Min((e) => e.Y); var w = Parent.Width; var h = Parent.Height; _scale = Math.Min((w - 2 * PaddingX) / (maxX - minX + diameter), (h - 2 * PaddingY) / (maxY - minY + diameter)); diameter = diameter * _scale; _diameter = diameter; int i = 1; foreach (var item in workPoints) { var p = new SampleControl(); var x = (int)(item.X * _scale + PaddingX - minX * _scale); var y = (int)(item.Y * _scale + PaddingY - minY * _scale); p.Location = new Point(x, y); p.Diameter = diameter; p.Id = i++; p.Click += P_Click; Controls.Add(p); _sampleList.Add(p); } BackColor = Color.Gray; Width = (int)(2 * PaddingX + (maxX - minX) * _scale + _diameter); Height = (int)(2 * PaddingY + (maxY - minY) * _scale + _diameter); Location = new Point((Parent.Width - Width) / 2, (Parent.Height - Height) / 2); } private void P_Click(object sender, EventArgs e) { int id = _sampleList.IndexOf(sender as SampleControl); SelectIndex = id; } private int _selectId = -1; private float _scale; public int SelectIndex { get => _selectId; set { Select(value); OnSelectChanged?.Invoke(value); } } public void Select(int i) { if (_selectId != -1) { _sampleList[_selectId].Selected = false; _sampleList[_selectId].Invalidate(); } _sampleList[i].Selected = true; _sampleList[i].Invalidate(); _selectId = i; } public void Reset() { foreach (var item in _sampleList) { item.State = 0; item.Invalidate(); } } public void SetState(int i, int value) { _sampleList[i].State = value; _sampleList[i].Invalidate(); } public event Action OnSelectChanged; public event Action OnSampleClick; } public class SampleControl : UserControl { public int Id { get; set; } public float Diameter { get => Width; set { Width = (int)value; Height = (int)value; } } public int State { get; set; } public bool Selected; public static List Brushs { get; set; } = new List(); Font _font = new Font("Verdana", 12); public SampleControl() { } protected override void OnPaint(PaintEventArgs e) { var g = e.Graphics; float w = Diameter; float h = Diameter; g.FillEllipse(Brushs[State], new RectangleF(0, 0, w, h)); SizeF fSize = g.MeasureString(Id.ToString(), _font); g.DrawString(Id.ToString(), _font, new SolidBrush(Color.Black), new PointF((w - fSize.Width) / 2, (h - fSize.Height) / 2)); g.SmoothingMode = SmoothingMode.AntiAlias; if (Selected) { g.DrawEllipse(new Pen(Color.FromArgb(128, 0, 0), 2), new RectangleF(0, 0, w - 1, h - 1)); } } } }