领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using Bro.Common.Interface;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using static Bro.Common.Helper.EnumHelper;
using Bro.Common.Helper;
 
namespace Bro.Common.ImageCanvas.Shapes
{
    public abstract class ShapeElementBase : DrawingVisual, IShapeElement
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        public int Index { get; set; }
 
        public string ID { get; set; } = Guid.NewGuid().ToString();
        public string Name { get; set; }
 
        #region Draw
        public bool IsShowing { get; set; } = false;
 
        public void Draw()
        {
            if (!IsShowing)
                return;
 
            if (Pen == null)
            {
                SetNormalPen();
            }
 
            try
            {
                using (DrawingContext dc = RenderOpen())
                {
                    DrawShape(dc);
                }
            }
            catch (Exception ex)
            {
            }
        }
 
        protected abstract void DrawShape(DrawingContext dc);
 
        public abstract void Draw(System.Drawing.Graphics g);
        #endregion
 
        #region 画笔相关
        #region 绘图画笔
        private System.Windows.Media.Pen pen = null;
        public System.Windows.Media.Pen Pen
        {
            get => pen;
            set => SetAndDraw(ref pen, value);
        }
 
        public virtual void SetNormalPen()
        {
            Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Black), 3);
        }
 
        public virtual void SetHoverPen()
        {
            Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.BlueViolet), 10);
        }
 
        public virtual void SetInSidePen()
        {
            Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.YellowGreen), 6);
        }
 
        public virtual void SetSelectedPen()
        {
            Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Pink), 10);
        }
 
        protected virtual void SetMeasureDoneOKPen()
        {
            Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Green), 6);
        }
 
        protected virtual void SetMeasureDoneNGPen()
        {
            Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Red), 6);
        }
 
        protected virtual void SetMeasuringPen()
        {
            Pen = new System.Windows.Media.Pen(new SolidColorBrush(Colors.Yellow), 3);
        }
        #endregion
 
        #region 文字画笔
        /// <summary>
        /// 字体大小
        /// </summary>
        public virtual float FontSize { get; set; } = 30;
        /// <summary>
        /// 字体和基元的距离
        /// </summary>
        public virtual int FontDistance { get; set; } = 5;
 
        public virtual bool IsShowRemark { get; set; } = true;
 
        public string Text { get; set; }
 
        public virtual Typeface Typeface { get; set; } = new Typeface(new System.Windows.Media.FontFamily("宋体"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
        #endregion
        #endregion
 
        #region 鼠标位置判断
        protected abstract bool IsMouseInSide(System.Windows.Point point);
        protected abstract bool IsMouseHover(System.Windows.Point point);
        #endregion
 
        #region 状态
        private ElementState state = ElementState.New;
        public ElementState State
        {
            get
            {
                return state;
            }
            set
            {
                if (state != value)
                {
                    state = value;
 
                    switch (state)
                    {
                        case ElementState.MouseHover:
                            SetHoverPen();
                            break;
                        case ElementState.MouseInSide:
                            SetInSidePen();
                            break;
                        case ElementState.Selected:
                            SetSelectedPen();
                            break;
                        case ElementState.Normal:
                            SetNormalPen();
                            break;
                        case ElementState.Measuring:
                            SetMeasuringPen();
                            break;
                        case ElementState.MeasureDoneOK:
                            SetMeasureDoneOKPen();
                            break;
                        case ElementState.MeasureDoneNG:
                            SetMeasureDoneNGPen();
                            break;
                    }
                }
            }
        }
 
        /// <summary>
        /// 是否是运行模式
        /// </summary>
        [Browsable(false)]
        public RunMode RunMode { get; set; } = RunMode.SetMode;
        public bool IsEnabled { get; set; } = true;
 
        public virtual void CheckMouseState(System.Windows.Point point)
        {
            if (State != ElementState.Selected)
            {
                if (IsMouseInSide(point))
                {
                    State = ElementState.MouseInSide;
                }
                else if (IsMouseHover(point))
                {
                    State = ElementState.MouseHover;
                }
                else
                {
                    State = ElementState.Normal;
                }
            }
        }
        #endregion
 
        #region 变化导致重绘事件
        protected virtual void SetAndDraw<T>(ref T field, T value)
        {
            if (value != null)
            {
                if (!value.Equals(field))
                {
                    field = value;
                    Draw();
                }
            }
        }
 
        public virtual void OnMouseDown(System.Drawing.Point point)
        {
        }
 
        public virtual void OnMouseUp(System.Drawing.Point point)
        {
        }
 
        public virtual void OnMouseMove(System.Drawing.Point point)
        {
        }
 
        public virtual void OnMouseDoubleClick(System.Drawing.Point point)
        {
        }
 
        public virtual bool IsIntersect(Rectangle rect)
        {
            return false;
        }
        #endregion
 
        #region ICloneable
        public virtual object Clone()
        {
            return this.DeepSerializeClone();
        }
        #endregion
    }
}