领胜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
using Bro.Common.ImageCanvas.Shapes;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace Bro.Common.ImageCanvas
{
    public class ObserableCollectionCanvas : Canvas
    {
        #region Visual
        //public static readonly DependencyProperty VisualListProperty =
        //DependencyProperty.Register("VisualList", typeof(ObservableCollection<ShapeElementBase>), typeof(ObserableCollectionCanvas), new PropertyMetadata(new ObservableCollection<ShapeElementBase>(), ObservableCollectionChangedCallback));
 
        //private static void ObservableCollectionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    var rectangleObserableCollectionCanvas = (ObserableCollectionCanvas)d;
        //    rectangleObserableCollectionCanvas?.Redraw();
        //}
 
        private volatile ObservableCollection<ShapeElementBase> visualList = new ObservableCollection<ShapeElementBase>();
        public ObservableCollection<ShapeElementBase> VisualList
        {
            //get => GetValue(VisualListProperty) as ObservableCollection<ShapeElementBase>;
            //set => SetValue(VisualListProperty, value);
 
            get => visualList;
            set
            {
                if (visualList != value)
                {
                    visualList = value;
                    Redraw();
 
                    visualList.CollectionChanged += VisualList_CollectionChanged;
                }
            }
        }
 
        private void VisualList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
            //{
            //    int i = VisualChildrenCount;
            //}
            //else
            //Dispatcher.Invoke(() =>
            //{
            if (e.OldItems != null)
            {
                foreach (ShapeElementBase shape in e.OldItems)
                {
                    DeleteVisual(shape);
                }
            }
 
            if (e.NewItems != null)
            {
                foreach (ShapeElementBase shape in e.NewItems)
                {
                    shape.Draw();
                    AddVisual(shape);
                }
            }
            //});
        }
 
        protected override int VisualChildrenCount => visualList.Count;
 
        protected override Visual GetVisualChild(int index)
        {
            return VisualList.ElementAt(index);
        }
 
        public void AddVisual(ShapeElementBase visual)
        {
            AddVisualChild(visual);
            AddLogicalChild(visual);
        }
 
        public void DeleteVisual(ShapeElementBase visual)
        {
            RemoveVisualChild(visual);
            RemoveLogicalChild(visual);
        }
        #endregion
 
        static ObserableCollectionCanvas()
        {
            IsHitTestVisibleProperty.OverrideMetadata(typeof(ObserableCollectionCanvas), new FrameworkPropertyMetadata(false));
            BackgroundProperty.OverrideMetadata(typeof(ObserableCollectionCanvas), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        }
 
        public static readonly DependencyProperty ShowProperty =
                DependencyProperty.Register("Show", typeof(bool), typeof(ObserableCollectionCanvas), new PropertyMetadata(true, PropertyChangedCallback));
        public bool Show
        {
            get { return (bool)GetValue(ShowProperty); }
            set { SetValue(ShowProperty, value); }
        }
 
        public static readonly DependencyProperty ScaleProperty =
                DependencyProperty.Register("Scale", typeof(double), typeof(ObserableCollectionCanvas), new PropertyMetadata(.95, PropertyChangedCallback));
        public double Scale { get { return (double)GetValue(ScaleProperty); } set { SetValue(ScaleProperty, value); } }
 
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            Redraw();
        }
 
        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            (dependencyObject as ObserableCollectionCanvas)?.Redraw();
        }
 
        protected void Redraw()
        {
            if (!Show) return;
            try
            {
                if (Math.Abs(ActualHeight) < 1 || Math.Abs(ActualWidth) < 1) return;
                foreach (ShapeElementBase v in VisualList)
                {
                    DeleteVisual(v);
                    v.Draw();
                    AddVisual(v);
                }
            }
            catch (Exception ex) { }
        }
 
        public void OnMousePointChanged(Point point)
        {
            Dispatcher.Invoke(() =>
            {
                //Parallel.ForEach(VisualList, (v) =>
                //{
                //    v.CheckMouseState(point);
                //});
 
                foreach (ShapeElementBase shape in VisualList)
                {
                    shape.CheckMouseState(point);
                }
            });
        }
    }
}