-
Notifications
You must be signed in to change notification settings - Fork 5
/
TimingForm.Painting.cs
322 lines (280 loc) · 11.5 KB
/
TimingForm.Painting.cs
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NSFW.TimingEditor
{
/// <summary>
/// Painting code for the application's main window.
/// </summary>
public partial class TimingForm : Form
{
/// <summary>
/// Draw the side views that help visual=ize the table shape.
/// </summary>
private void DrawSideViews(int activeColumn, int activeRow)
{
Bitmap horizontalPanelBitmap = new Bitmap(this.horizontalPanel.Width, this.horizontalPanel.Height);
Graphics horizontalPanelBackBuffer = Graphics.FromImage(horizontalPanelBitmap);
//Graphics horizontalPanelBackBuffer = horizontalPanel.CreateGraphics();
Bitmap verticalPanelBitmap = new Bitmap(this.verticalPanel.Width, this.verticalPanel.Height);
Graphics verticalPanelBackBuffer = Graphics.FromImage(verticalPanelBitmap);
//Graphics verticalPanelBackBuffer = verticalPanel.CreateGraphics();
horizontalPanelBackBuffer.FillRectangle(Brushes.White, this.horizontalPanel.ClientRectangle);
verticalPanelBackBuffer.FillRectangle(Brushes.White, this.verticalPanel.ClientRectangle);
double min;
double max;
this.GetMinMax(out min, out max);
Pen pen = Pens.Gray;
for (int row = 0; row < this.dataGrid.Rows.Count; row++)
{
this.DrawRow(horizontalPanelBackBuffer, pen, row, min, max);
}
for (int column = 0; column < this.dataGrid.Columns.Count; column++)
{
this.DrawColumn(verticalPanelBackBuffer, pen, column, min, max);
}
if ((activeColumn >= 0) && (activeColumn < this.dataGrid.Columns.Count) &&
(activeRow >= 0) && (activeRow < this.dataGrid.Rows.Count))
{
using (Pen heavyPen = new Pen(Color.Black, 3))
{
this.DrawRow(horizontalPanelBackBuffer, heavyPen, activeRow, min, max);
this.DrawColumn(verticalPanelBackBuffer, heavyPen, activeColumn, min, max);
}
using (Pen lightPen = new Pen(Color.Gray, 2))
{
int x = this.GetRowX(activeColumn);
horizontalPanelBackBuffer.DrawLine(lightPen, x, 0, x, horizontalPanel.Height);
int y = this.GetColumnY(activeRow);
verticalPanelBackBuffer.DrawLine(lightPen, 0, y, verticalPanel.Width, y);
}
}
SmoothInfo si = this.GetSmoothInfo(min, max);
if (si != null)
{
if (si.A.RowIndex == si.B.RowIndex)
{
this.DrawRowSmooth(horizontalPanelBackBuffer, si);
}
else
{
this.DrawColumnSmooth(verticalPanelBackBuffer, si);
}
}
Graphics graphics = this.horizontalPanel.CreateGraphics();
graphics.DrawImage(horizontalPanelBitmap, 0, 0);
graphics = this.verticalPanel.CreateGraphics();
graphics.DrawImage(verticalPanelBitmap, 0, 0);
}
/// <summary>
/// Get the min and max values for the whole data-grid.
/// </summary>
private void GetMinMax(out double min, out double max)
{
min = double.MaxValue;
max = double.MinValue;
double value;
for (int row = 0; row < this.dataGrid.Rows.Count; row++)
{
for (int column = 0; column < this.dataGrid.Columns.Count; column++)
{
if (!this.TryGetValue(column, row, out value))
{
return;
}
min = Math.Min(min, value);
max = Math.Max(max, value);
}
}
}
/// <summary>
/// Min and Max data about a range of cells.
/// </summary>
private class SmoothInfo
{
public DataGridViewCell A;
public DataGridViewCell B;
public double MinValue;
public double MaxValue;
}
/// <summary>
/// Calculate the 'SmoothInfo' for a selected set of cells.
/// </summary>
private SmoothInfo GetSmoothInfo(double min, double max)
{
DataGridViewSelectedCellCollection selected = this.dataGrid.SelectedCells;
if (this.SelectedColumn(selected))
{
SmoothInfo result = new SmoothInfo();
result.MinValue = min;
result.MaxValue = max;
IEnumerable<DataGridViewCell> cells = selected.Cast<DataGridViewCell>();
int minY = cells.Min(cell => cell.RowIndex);
int maxY = cells.Max(cell => cell.RowIndex);
result.A = cells.Where(cell => cell.RowIndex == minY).First();
result.B = cells.Where(cell => cell.RowIndex == maxY).First();
return result;
}
if (this.SelectedRow(this.dataGrid.SelectedCells))
{
SmoothInfo result = new SmoothInfo();
result.MinValue = min;
result.MaxValue = max;
IEnumerable<DataGridViewCell> cells = selected.Cast<DataGridViewCell>();
int minX = cells.Min(cell => cell.ColumnIndex);
int maxX = cells.Max(cell => cell.ColumnIndex);
result.A = cells.Where(cell => cell.ColumnIndex == minX).First();
result.B = cells.Where(cell => cell.ColumnIndex == maxX).First();
return result;
}
return null;
}
/// <summary>
/// Draw a row of data.
/// </summary>
private void DrawRowSmooth(Graphics graphics, SmoothInfo si)
{
double valueA, valueB;
if (!this.TryGetValue(si.A.ColumnIndex, si.A.RowIndex, out valueA))
{
return;
}
if (!this.TryGetValue(si.B.ColumnIndex, si.B.RowIndex, out valueB))
{
return;
}
float x1 = this.GetRowX(si.A.ColumnIndex);
float y1 = this.GetRowY(si.MinValue, si.MaxValue, valueA);
float x2 = this.GetRowX(si.B.ColumnIndex);
float y2 = this.GetRowY(si.MinValue, si.MaxValue, valueB);
using (Pen pen = new Pen(Color.Blue, 3))
{
graphics.DrawLine(
pen,
x1,
y1,
x2,
y2);
}
}
/// <summary>
/// Draw a column of data.
/// </summary>
private void DrawColumnSmooth(Graphics graphics, SmoothInfo si)
{
double valueA, valueB;
if (!this.TryGetValue(si.A.ColumnIndex, si.A.RowIndex, out valueA))
{
return;
}
if (!this.TryGetValue(si.B.ColumnIndex, si.B.RowIndex, out valueB))
{
return;
}
using (Pen pen = new Pen(Color.Blue, 3))
{
graphics.DrawLine(
pen,
this.GetColumnX(si.MinValue, si.MaxValue, valueA),
this.GetColumnY(si.A.RowIndex),
this.GetColumnX(si.MinValue, si.MaxValue, valueB),
this.GetColumnY(si.B.RowIndex));
}
}
/// <summary>
/// Draw a row of data.
/// </summary>
private void DrawRow(Graphics graphics, Pen pen, int row, double min, double max)
{
double value;
if (!this.TryGetValue(0, row, out value))
{
return;
}
int lastX = this.dataGrid.RowHeadersWidth;
int lastY = this.GetRowY(min, max, value);
int nextX;
int nextY;
for (int i = 0; i < this.dataGrid.Columns.Count; i++)
{
if (!this.TryGetValue(i, row, out value))
{
return;
}
nextX = this.GetRowX(i);
nextY = this.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);
}
/// <summary>
/// Draw a column of data.
/// </summary>
private void DrawColumn(Graphics graphics, Pen pen, int column, double min, double max)
{
double value;
if (!this.TryGetValue(column, 0, out value))
{
return;
}
int lastX = this.GetColumnX(min, max, value);
int lastY = this.dataGrid.ColumnHeadersHeight;
int nextX;
int nextY;
for (int i = 0; i < this.dataGrid.Rows.Count; i++)
{
if (!this.TryGetValue(column, i, out value))
{
return;
}
nextX = this.GetColumnX(min, max, value);
nextY = this.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)
{
int width = this.horizontalPanel.Width - this.dataGrid.RowHeadersWidth;
int offset = (width / (this.dataGrid.Columns.Count * 2)) + this.dataGrid.RowHeadersWidth;
return ((width * i) / (this.dataGrid.Columns.Count)) + offset;
}
private int GetRowY(double min, double max, double value)
{
double difference = max - min;
double magnitude = difference == 0 ? 0.5 : (max - value) / difference;
double result = magnitude * (this.horizontalPanel.Height * 0.8);
return (int)result + (int)(this.horizontalPanel.Height * 0.1);
}
private int GetColumnX(double min, double max, double value)
{
double difference = max - min;
double magnitude = difference == 0 ? 0.5 : (max - value) / difference;
double result = magnitude * (this.verticalPanel.Width * 0.8);
return (int)result + (int)(this.verticalPanel.Width * 0.1);
}
private int GetColumnY(int i)
{
int height = this.verticalPanel.Height - this.dataGrid.ColumnHeadersHeight;
int offset = (height / (this.dataGrid.Columns.Count * 2)) + this.dataGrid.ColumnHeadersHeight;
return ((height * i) / (this.dataGrid.Rows.Count)) + offset;
}
}
}