-
Notifications
You must be signed in to change notification settings - Fork 5
/
TimingForm.Smoothing.cs
238 lines (224 loc) · 7.23 KB
/
TimingForm.Smoothing.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NSFW.TimingEditor
{
/// <summary>
/// Table-smoothing code for the application main window.
/// </summary>
public partial class TimingForm : Form
{
/// <summary>
/// Invoked when the 'Smooth' button is clicked.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void smoothButton_Click(object sender, EventArgs e)
{
this.Smooth(this.dataGrid.SelectedCells, true);
}
/// <summary>
/// At one point I intended to add code to smooth the whole table. Never did.
/// </summary>
private void smoothTableButton_Click(object sender, EventArgs e)
{
// create 2d array same size as table
// smooth across rows
// smooth across columns
// apply deltas
}
/// <summary>
/// Indicate wehether smoothing is possible for the selected cell.
/// </summary>
private bool CanSmooth
{
get
{
return this.Smooth(this.dataGrid.SelectedCells, false);
}
}
/// <summary>
/// Depending on the value of the 'forReal' parmeter, either smooth
/// the selected cells, or indicate whether smoothing is possible.
/// </summary>
private bool Smooth(DataGridViewSelectedCellCollection selectedCells, bool forReal)
{
if (this.SelectedColumn(selectedCells))
{
if (forReal)
{
IList<DataGridViewCell> cells = this.SortColumn(selectedCells);
this.Smooth(cells);
}
return true;
}
else if (this.SelectedRow(selectedCells))
{
if (forReal)
{
IList<DataGridViewCell> cells = this.SortRow(selectedCells);
this.Smooth(cells);
}
return true;
}
return false;
}
/// <summary>
/// Determine whether the selected cells are all in a column.
/// </summary>
private bool SelectedColumn(System.Collections.ICollection selectedCells)
{
int column = -1;
int total = 0;
foreach (DataGridViewCell cell in selectedCells)
{
total++;
if (column == -1)
{
column = cell.ColumnIndex;
}
else
{
if (column != cell.ColumnIndex)
{
return false;
}
}
}
if ((column == -1) || (total <= 2))
{
return false;
}
return true;
}
/// <summary>
/// Indicate whether the selected cells are all in a row.
/// </summary>
/// <param name="selectedCells"></param>
/// <returns></returns>
private bool SelectedRow(System.Collections.ICollection selectedCells)
{
int row = -1;
int total = 0;
foreach (DataGridViewCell cell in selectedCells)
{
total++;
if (row == -1)
{
row = cell.RowIndex;
}
else
{
if (row != cell.RowIndex)
{
return false;
}
}
}
if ((row == -1) || (total <= 2))
{
return false;
}
return true;
}
/// <summary>
/// Helper function for smoothing a list of cells.
/// </summary>
private void Smooth(IList<DataGridViewCell> cells)
{
try
{
double cellMinValue = cells[0].ValueAsDouble();
double cellMaxValue = cells[cells.Count - 1].ValueAsDouble();
double step = (cellMaxValue - cellMinValue) / (cells.Count - 1);
double min, max;
if (cellMinValue < cellMaxValue)
{
min = cellMinValue;
max = cellMaxValue;
for (int i = 0; i < cells.Count; i++)
{
double value = min + (step * i);
cells[i].Value = value.ToString(Util.DoubleFormat);
}
}
else
{
min = cellMaxValue;
max = cellMinValue;
for (int i = 0; i < cells.Count; i++)
{
double value = max + (step * i);
cells[i].Value = value.ToString(Util.DoubleFormat);
}
}
}
catch (FormatException e)
{
statusStrip1.Items[0].Text = e.Message;
}
catch (ArgumentNullException)
{
}
}
/// <summary>
/// Sort the values in a collection of cells.
/// </summary>
private List<DataGridViewCell> SortColumn(DataGridViewSelectedCellCollection input)
{
List<DataGridViewCell> result = new List<DataGridViewCell>();
foreach (DataGridViewCell cell in input)
{
if (cell == null)
{
continue;
}
result.Add(cell);
}
result.Sort(delegate(DataGridViewCell a, DataGridViewCell b)
{
if (a.RowIndex < b.RowIndex)
{
return -1;
}
if (a.RowIndex > b.RowIndex)
{
return 1;
}
return 0;
});
return result;
}
/// <summary>
/// Sort the values in a selected row.
/// </summary>
private List<DataGridViewCell> SortRow(DataGridViewSelectedCellCollection input)
{
List<DataGridViewCell> result = new List<DataGridViewCell>();
foreach (DataGridViewCell cell in input)
{
if (cell == null)
{
continue;
}
result.Add(cell);
}
result.Sort(delegate(DataGridViewCell a, DataGridViewCell b)
{
if (a.ColumnIndex < b.ColumnIndex)
{
return -1;
}
if (a.ColumnIndex > b.ColumnIndex)
{
return 1;
}
return 0;
});
return result;
}
}
}