aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Diagnostics/Controls/DiagnosticsSelectionGrid.cs
blob: 48e2cbad63b5635c963148caad257b854a65b407 (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Tango.FSE.Diagnostics.Controls
{
    public class DiagnosticsSelectionGrid : DiagnosticsGrid
    {
        private List<Rectangle> _rects;
        private Point _mouseDownLocation;
        private bool _isMouseDown;
        private List<Rectangle> _selectionRects;

        public Brush RectanglesBackgroundBrush
        {
            get { return (Brush)GetValue(RectanglesBackgroundBrushProperty); }
            set { SetValue(RectanglesBackgroundBrushProperty, value); }
        }
        public static readonly DependencyProperty RectanglesBackgroundBrushProperty =
            DependencyProperty.Register("RectanglesBackgroundBrush", typeof(Brush), typeof(DiagnosticsSelectionGrid), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(25, 25, 25))));

        public Brush RectanglesHighlightBrush
        {
            get { return (Brush)GetValue(RectanglesHighlightBrushProperty); }
            set { SetValue(RectanglesHighlightBrushProperty, value); }
        }
        public static readonly DependencyProperty RectanglesHighlightBrushProperty =
            DependencyProperty.Register("RectanglesHighlightBrush", typeof(Brush), typeof(DiagnosticsSelectionGrid), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(30, 30, 30))));

        public Rect SelectionRect
        {
            get { return (Rect)GetValue(SelectionRectProperty); }
            set { SetValue(SelectionRectProperty, value); }
        }
        public static readonly DependencyProperty SelectionRectProperty =
            DependencyProperty.Register("SelectionRect", typeof(Rect), typeof(DiagnosticsSelectionGrid), new PropertyMetadata(default(Rect)));

        public ICommand SelectionCommand
        {
            get { return (ICommand)GetValue(SelectionCommandProperty); }
            set { SetValue(SelectionCommandProperty, value); }
        }
        public static readonly DependencyProperty SelectionCommandProperty =
            DependencyProperty.Register("SelectionCommand", typeof(ICommand), typeof(DiagnosticsSelectionGrid), new PropertyMetadata(null));

        public ICommand RightClickCommand
        {
            get { return (ICommand)GetValue(RightClickCommandProperty); }
            set { SetValue(RightClickCommandProperty, value); }
        }
        public static readonly DependencyProperty RightClickCommandProperty =
            DependencyProperty.Register("RightClickCommand", typeof(ICommand), typeof(DiagnosticsSelectionGrid), new PropertyMetadata(null));

        public SelectionMode SelectionMode
        {
            get { return (SelectionMode)GetValue(SelectionModeProperty); }
            set { SetValue(SelectionModeProperty, value); }
        }
        public static readonly DependencyProperty SelectionModeProperty =
            DependencyProperty.Register("SelectionMode", typeof(SelectionMode), typeof(DiagnosticsSelectionGrid), new PropertyMetadata(SelectionMode.Multiple));
        
        static DiagnosticsSelectionGrid()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DiagnosticsSelectionGrid), new FrameworkPropertyMetadata(typeof(DiagnosticsSelectionGrid)));
        }

        public DiagnosticsSelectionGrid()
        {
            _rects = new List<Rectangle>();
            _selectionRects = new List<Rectangle>();
            Cursor = Cursors.Cross;
            Background = Brushes.Transparent;
        }

        public void ClearSelection()
        {
            _selectionRects.Clear();
            foreach (var rect in _rects)
            {
                rect.Fill = RectanglesBackgroundBrush;
            }
        }

        protected override void LoadDefinitions()
        {
            base.LoadDefinitions();

            if (Rows != null && Columns != null)
            {
                _rects.Clear();
                _selectionRects.Clear();
                Children.Clear();

                for (int columnIndex = 0; columnIndex < Columns.Count; columnIndex++)
                {
                    for (int rowIndex = 0; rowIndex < Rows.Count; rowIndex++)
                    {
                        var rect = new Rectangle();
                        rect.Margin = new Thickness(1);
                        rect.Fill = RectanglesBackgroundBrush;
                        rect.IsHitTestVisible = false;
                        Grid.SetColumn(rect, columnIndex);
                        Grid.SetRow(rect, rowIndex);
                        Children.Add(rect);
                        _rects.Add(rect);
                    }
                }
            }
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            if (SelectionMode == SelectionMode.Single) return;

            if (e.ChangedButton == MouseButton.Left)
            {
                _mouseDownLocation = e.GetPosition(this);
                _isMouseDown = true;
            }
        }

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);
            _isMouseDown = false;

            if (e.ChangedButton == MouseButton.Right)
            {
                RightClickCommand?.Execute(null);
                return;
            }

            if (_selectionRects.Count > 0)
            {
                var column = _selectionRects.Min(x => Grid.GetColumn(x));
                var row = _selectionRects.Min(x => Grid.GetRow(x));
                var columnSpan = Math.Max(_selectionRects.Max(x => Grid.GetColumn(x)) - column + 1, 1);
                var rowSpan = Math.Max(_selectionRects.Max(x => Grid.GetRow(x)) - row + 1, 1);

                SelectionRect = new Rect(column, row, columnSpan, rowSpan);
                SelectionCommand?.Execute(SelectionRect);

                ClearSelection();
            }
        }

        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            base.OnPreviewMouseMove(e);

            if (SelectionMode == SelectionMode.Single)
            {
                var position = e.GetPosition(this);
                var selection = new Rect(position.X, position.Y, 1, 1);
                HighlightIntersections(selection);
            }
            else if (_isMouseDown)
            {
                var position = e.GetPosition(this);

                var x = Math.Min(position.X, _mouseDownLocation.X);
                var y = Math.Min(position.Y, _mouseDownLocation.Y);
                var w = Math.Max(position.X, _mouseDownLocation.X) - Math.Min(_mouseDownLocation.X, position.X);
                var h = Math.Max(position.Y, _mouseDownLocation.Y) - Math.Min(_mouseDownLocation.Y, position.Y);

                var selection = new Rect(x, y, w, h);
                HighlightIntersections(selection);
            }
        }

        private void HighlightIntersections(Rect selection)
        {
            _selectionRects.Clear();

            foreach (var rect in _rects)
            {
                GeneralTransform gt = rect.TransformToAncestor(this);
                var rectBounds = gt.TransformBounds(new Rect(0, 0, rect.ActualWidth, rect.ActualHeight));

                if (selection.IntersectsWith(rectBounds))
                {
                    rect.Fill = RectanglesHighlightBrush;
                    _selectionRects.Add(rect);
                }
                else
                {
                    rect.Fill = RectanglesBackgroundBrush;
                }
            }
        }
    }
}