forked from vimsh/TimingEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimingForm.Painting.cs
More file actions
291 lines (242 loc) · 10.3 KB
/
TimingForm.Painting.cs
File metadata and controls
291 lines (242 loc) · 10.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Linq.Expressions;
namespace NSFW.TimingEditor
{
public partial class TimingForm
{
private (int, int) GetGridSize()
{
if (dataGrid.Rows.Count == 0 || dataGrid.Rows[0].Cells.Count == 0)
{
return (0, 0);
}
var width = 0;
var height = 0;
width = dataGrid.PreferredSize.Width - dataGrid.Rows[0].Cells[1].Size.Width + 5;
height = dataGrid.PreferredSize.Height - dataGrid.Rows[0].Cells[1].Size.Width + 5;
return (width, height);
}
private void DrawSideViews(int activeColumn, int activeRow)
{
var (width, height) = GetGridSize();
horizontalPanel.Width = width;
verticalPanel.Height = height;
horizontalPanel.Left = dataGrid.RowHeadersWidth;
var horizontalPanelBitmap = new Bitmap(horizontalPanel.Width, horizontalPanel.Height);
var horizontalPanelBackBuffer = Graphics.FromImage(horizontalPanelBitmap);
//Graphics horizontalPanelBackBuffer = horizontalPanel.CreateGraphics();
var verticalPanelBitmap = new Bitmap(verticalPanel.Width, verticalPanel.Height);
var verticalPanelBackBuffer = Graphics.FromImage(verticalPanelBitmap);
//Graphics verticalPanelBackBuffer = verticalPanel.CreateGraphics();
horizontalPanelBackBuffer.FillRectangle(Brushes.White, horizontalPanel.ClientRectangle);
verticalPanelBackBuffer.FillRectangle(Brushes.White, verticalPanel.ClientRectangle);
GetMinMax(out var min, out var max);
var pen = Pens.Gray;
for (var row = 0; row < dataGrid.Rows.Count; row++)
{
DrawRow(horizontalPanelBackBuffer, pen, row, min, max);
}
for (var column = 0; column < dataGrid.Columns.Count; column++)
{
DrawColumn(verticalPanelBackBuffer, pen, column, min, max);
}
if ((activeColumn >= 0) && (activeColumn < dataGrid.Columns.Count) &&
(activeRow >= 0) && (activeRow < dataGrid.Rows.Count))
{
using (var heavyPen = new Pen(Color.Black, 3))
{
DrawRow(horizontalPanelBackBuffer, heavyPen, activeRow, min, max);
DrawColumn(verticalPanelBackBuffer, heavyPen, activeColumn, min, max);
}
using (var lightPen = new Pen(Color.Gray, 2))
{
var x = GetRowX(activeColumn);
horizontalPanelBackBuffer.DrawLine(lightPen, x, 0, x, horizontalPanel.Height);
var y = GetColumnY(activeRow);
verticalPanelBackBuffer.DrawLine(lightPen, 0, y, verticalPanel.Width, y);
}
}
var si = GetSmoothInfo(min, max);
if (si != null)
{
if (si.A.RowIndex == si.B.RowIndex)
{
DrawRowSmooth(horizontalPanelBackBuffer, si);
}
else
{
DrawColumnSmooth(verticalPanelBackBuffer, si);
}
}
var graphics = horizontalPanel.CreateGraphics();
graphics.DrawImage(horizontalPanelBitmap, 0, 0);
graphics = verticalPanel.CreateGraphics();
graphics.DrawImage(verticalPanelBitmap, 0, 0);
}
private void GetMinMax(out double min, out double max)
{
min = double.MaxValue;
max = double.MinValue;
double value;
for (var row = 0; row < dataGrid.Rows.Count; row++)
{
for (var column = 0; column < dataGrid.Columns.Count; column++)
{
if (!TryGetValue(column, row, out value))
{
return;
}
min = Math.Min(min, value);
max = Math.Max(max, value);
}
}
}
private class SmoothInfo
{
public DataGridViewCell A;
public DataGridViewCell B;
public double MinValue;
public double MaxValue;
}
private SmoothInfo GetSmoothInfo(double min, double max)
{
var selected = dataGrid.SelectedCells;
if (SelectedColumn(selected))
{
var result = new SmoothInfo();
result.MinValue = min;
result.MaxValue = max;
var cells = selected.Cast<DataGridViewCell>().ToList();
var minY = cells.Min(cell => cell.RowIndex);
var maxY = cells.Max(cell => cell.RowIndex);
result.A = cells.First(cell => cell.RowIndex == minY);
result.B = cells.First(cell => cell.RowIndex == maxY);
return result;
}
if (SelectedRow(dataGrid.SelectedCells))
{
var result = new SmoothInfo();
result.MinValue = min;
result.MaxValue = max;
var cells = selected.Cast<DataGridViewCell>().ToList();
var minX = cells.Min(cell => cell.ColumnIndex);
var maxX = cells.Max(cell => cell.ColumnIndex);
result.A = cells.First(cell => cell.ColumnIndex == minX);
result.B = cells.First(cell => cell.ColumnIndex == maxX);
return result;
}
return null;
}
private void DrawRowSmooth(Graphics graphics, SmoothInfo si)
{
if (!TryGetValue(si.A.ColumnIndex, si.A.RowIndex, out var valueA))
return;
if (!TryGetValue(si.B.ColumnIndex, si.B.RowIndex, out var valueB))
return;
float x1 = GetRowX(si.A.ColumnIndex);
float y1 = GetRowY(si.MinValue, si.MaxValue, valueA);
float x2 = GetRowX(si.B.ColumnIndex);
float y2 = GetRowY(si.MinValue, si.MaxValue, valueB);
using (var pen = new Pen(Color.Blue, 3))
{
graphics.DrawLine(
pen,
x1,
y1,
x2,
y2);
}
}
private void DrawColumnSmooth(Graphics graphics, SmoothInfo si)
{
if (!TryGetValue(si.A.ColumnIndex, si.A.RowIndex, out var valueA))
return;
if (!TryGetValue(si.B.ColumnIndex, si.B.RowIndex, out var valueB))
return;
using (var pen = new Pen(Color.Blue, 3))
{
graphics.DrawLine(
pen,
GetColumnX(si.MinValue, si.MaxValue, valueA),
GetColumnY(si.A.RowIndex),
GetColumnX(si.MinValue, si.MaxValue, valueB),
GetColumnY(si.B.RowIndex));
}
}
private void DrawRow(Graphics graphics, Pen pen, int row, double min, double max)
{
if (!TryGetValue(0, row, out var value))
return;
var lastX = dataGrid.RowHeadersWidth;
var lastY = GetRowY(min, max, value);
for (var i = 0; i < dataGrid.Columns.Count; i++)
{
if (!TryGetValue(i, row, out value))
return;
var nextX = GetRowX(i);
var nextY = GetRowY(min, max, value);
if (i != 0)
{
graphics.DrawLine(pen, lastX, lastY, nextX, nextY);
}
lastX = nextX;
lastY = nextY;
}
//nextX = this.horizontalPanel.Width;
//nextY = lastY;
//graphics.DrawLine(pen, lastX, lastY, nextX, nextY);
}
private void DrawColumn(Graphics graphics, Pen pen, int column, double min, double max)
{
if (!TryGetValue(column, 0, out var value))
return;
var lastX = GetColumnX(min, max, value);
var lastY = dataGrid.ColumnHeadersHeight;
for (var i = 0; i < dataGrid.Rows.Count; i++)
{
if (!TryGetValue(column, i, out value))
return;
var nextX = GetColumnX(min, max, value);
var nextY = GetColumnY(i);
if (i != 0)
{
graphics.DrawLine(pen, lastX, lastY, nextX, nextY);
}
lastX = nextX;
lastY = nextY;
}
//nextX = lastX;
//nextY = this.verticalPanel.Height;
//graphics.DrawLine(pen, lastX, lastY, nextX, nextY);
}
private int GetRowX(int i)
{
var width = horizontalPanel.Width - dataGrid.RowHeadersWidth;
var offset = (width / (dataGrid.Columns.Count * 2)) + dataGrid.RowHeadersWidth;
return ((width * i) / (dataGrid.Columns.Count)) + offset;
}
private int GetRowY(double min, double max, double value)
{
var difference = max - min;
var magnitude = difference == 0 ? 0.5 : (max - value) / difference;
var result = magnitude * (horizontalPanel.Height * 0.8);
return (int)result + (int)(horizontalPanel.Height * 0.1);
}
private int GetColumnX(double min, double max, double value)
{
var difference = max - min;
var magnitude = difference == 0 ? 0.5 : (max - value) / difference;
var result = magnitude * (verticalPanel.Width * 0.8);
return (int)result + (int)(verticalPanel.Width * 0.1);
}
private int GetColumnY(int i)
{
var height = verticalPanel.Height - dataGrid.ColumnHeadersHeight;
var offset = (height / (dataGrid.Columns.Count * 2)) + dataGrid.ColumnHeadersHeight;
return ((height * i) / (dataGrid.Rows.Count)) + offset;
}
}
}