SampleStageControl.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace Metis.AutoAnalysis
  10. {
  11. public class SampleStageControl : UserControl
  12. {
  13. List<SampleControl> _sampleList = new List<SampleControl>();
  14. public int PaddingX;
  15. public int PaddingY;
  16. private float _diameter;
  17. public SampleStageControl()
  18. {
  19. SampleControl.Brushs.Add(new SolidBrush(Color.White));
  20. SampleControl.Brushs.Add(new SolidBrush(Color.Red));
  21. SampleControl.Brushs.Add(new SolidBrush(Color.Blue));
  22. SampleControl.Brushs.Add(new SolidBrush(Color.Green));
  23. }
  24. public void Update(SampleStageModel model)
  25. {
  26. _selectId = -1;
  27. Controls.Clear();
  28. _sampleList.Clear();
  29. List<WorkPoint> workPoints = model.WorkPoits;
  30. if (workPoints.Count < 1)
  31. return;
  32. float diameter = model.Diameter;
  33. PaddingX = model.PaddingX;
  34. PaddingY = model.PaddingY;
  35. var maxX = workPoints.Max((e) => e.X);
  36. var minX = workPoints.Min((e) => e.X);
  37. var maxY = workPoints.Max((e) => e.Y);
  38. var minY = workPoints.Min((e) => e.Y);
  39. var w = Parent.Width;
  40. var h = Parent.Height;
  41. _scale = Math.Min((w - 2 * PaddingX) / (maxX - minX + diameter), (h - 2 * PaddingY) / (maxY - minY + diameter));
  42. diameter = diameter * _scale;
  43. _diameter = diameter;
  44. int i = 1;
  45. foreach (var item in workPoints)
  46. {
  47. var p = new SampleControl();
  48. var x = (int)(item.X * _scale + PaddingX - minX * _scale);
  49. var y = (int)(item.Y * _scale + PaddingY - minY * _scale);
  50. p.Location = new Point(x, y);
  51. p.Diameter = diameter;
  52. p.Id = i++;
  53. p.Click += P_Click;
  54. Controls.Add(p);
  55. _sampleList.Add(p);
  56. }
  57. BackColor = Color.Gray;
  58. Width = (int)(2 * PaddingX + (maxX - minX) * _scale + _diameter);
  59. Height = (int)(2 * PaddingY + (maxY - minY) * _scale + _diameter);
  60. Location = new Point((Parent.Width - Width) / 2, (Parent.Height - Height) / 2);
  61. }
  62. private void P_Click(object sender, EventArgs e)
  63. {
  64. int id = _sampleList.IndexOf(sender as SampleControl);
  65. SelectIndex = id;
  66. }
  67. private int _selectId = -1;
  68. private float _scale;
  69. public int SelectIndex
  70. {
  71. get => _selectId;
  72. set
  73. {
  74. Select(value);
  75. OnSelectChanged?.Invoke(value);
  76. }
  77. }
  78. public void Select(int i)
  79. {
  80. if (_selectId != -1)
  81. {
  82. _sampleList[_selectId].Selected = false;
  83. _sampleList[_selectId].Invalidate();
  84. }
  85. _sampleList[i].Selected = true;
  86. _sampleList[i].Invalidate();
  87. _selectId = i;
  88. }
  89. public void Reset()
  90. {
  91. foreach (var item in _sampleList)
  92. {
  93. item.State = 0;
  94. item.Invalidate();
  95. }
  96. }
  97. public void SetState(int i, int value)
  98. {
  99. _sampleList[i].State = value;
  100. _sampleList[i].Invalidate();
  101. }
  102. public event Action<int> OnSelectChanged;
  103. public event Action<int> OnSampleClick;
  104. }
  105. public class SampleControl : UserControl
  106. {
  107. public int Id { get; set; }
  108. public float Diameter { get => Width; set { Width = (int)value; Height = (int)value; } }
  109. public int State { get; set; }
  110. public bool Selected;
  111. public static List<Brush> Brushs { get; set; } = new List<Brush>();
  112. Font _font = new Font("Verdana", 12);
  113. public SampleControl()
  114. { }
  115. protected override void OnPaint(PaintEventArgs e)
  116. {
  117. var g = e.Graphics;
  118. float w = Diameter;
  119. float h = Diameter;
  120. g.FillEllipse(Brushs[State], new RectangleF(0, 0, w, h));
  121. SizeF fSize = g.MeasureString(Id.ToString(), _font);
  122. g.DrawString(Id.ToString(), _font, new SolidBrush(Color.Black), new PointF((w - fSize.Width) / 2, (h - fSize.Height) / 2));
  123. g.SmoothingMode = SmoothingMode.AntiAlias;
  124. if (Selected)
  125. {
  126. g.DrawEllipse(new Pen(Color.FromArgb(128, 0, 0), 2), new RectangleF(0, 0, w - 1, h - 1));
  127. }
  128. }
  129. }
  130. }