-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormSystemOptimizer.cs
425 lines (385 loc) · 19.2 KB
/
FormSystemOptimizer.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HazeronProspector
{
public partial class FormSystemOptimizer : Form
{
HSystem _system;
Dictionary<ResourceType, Resource> _resourceBest;
public FormSystemOptimizer(HSystem system)
{
InitializeComponent();
Initialize(system);
}
public void Initialize(HSystem system)
{
_system = system;
comboBox1.SelectedIndex = 0;
this.Text += " - " + _system.Name;
tbxSystem.Text = _system.Name;
tbxSector.Text = _system.HostSector.Name;
tbxGalaxy.Text = _system.HostSector.HostGalaxy.Name;
tbxCoordinates.Text = _system.Coord.ToString();
CreateResourceTable(_system);
CreateLocationTable(_system);
}
/// <summary>
/// Resource filter option. Skip geosphere resources if "bioshpere resourcces" is selected, and so on.
/// </summary>
/// <param name="resourceType">The resource type.</param>
/// <returns>Returns true if the resource should be skipped.</returns>
private bool FilterResource(ResourceType resourceType)
{
if ((comboBox1.SelectedIndex == 2
&& (Resource.GetCategory(resourceType) == ResourceCategory.Geosphere
|| Resource.GetCategory(resourceType) == ResourceCategory.Hydrophere
|| Resource.GetCategory(resourceType) == ResourceCategory.Atmosphere))
|| (comboBox1.SelectedIndex == 1
&& Resource.GetCategory(resourceType) == ResourceCategory.Biosphere)
|| Resource.GetCategory(resourceType) == ResourceCategory.Photophere)
return true;
return false;
}
#region Buttons
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
UpdateTables();
}
#endregion
#region DataGridView
private void CreateResourceTable(HSystem system)
{
Dictionary<ResourceType, Resource> bestResources = _system.BestResources(true);
foreach (Resource resource in bestResources.Values)
{
DataGridViewRow row = dgvResources.Rows[dgvResources.Rows.Add()];
row.Cells["dgvResourcesColumnResource"].Value = resource.Name;
row.Cells["dgvResourcesColumnCurrent"].Value = null;
row.Cells["dgvResourcesColumnBest"].Value = resource;
if (resource.HostZone.HostCelestialBody.ResourceZones.Length == 1
|| (resource.HostZone.HostCelestialBody.ResourceZones.Length > 1
&& resource.AcrossZones))
row.Cells["dgvResourcesColumnBest"].ToolTipText = resource.HostZone.HostCelestialBody.Name;
else
row.Cells["dgvResourcesColumnBest"].ToolTipText = resource.HostZone.HostCelestialBody.Name + ", " + resource.HostZone.Name;
row.Cells["dgvResourcesColumnBest"].Style.ForeColor = resource.TechLevelColor;
}
dgvResources.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
private void CreateLocationTable(HSystem system)
{
foreach (CelestialBody planet in system.CelestialBodies.Values)
{
foreach (Zone zone in planet.ResourceZones)
{
DataGridViewRow row = dgvSuggentions.Rows[dgvSuggentions.Rows.Add()];
row.Cells["dgvSuggentionsColumnPlanet"].Value = planet;
row.Cells["dgvSuggentionsColumnZone"].Value = zone;
row.Cells["dgvSuggentionsColumnOrbit"].Value = planet.Orbit;
row.Cells["dgvSuggentionsColumnCity"].Value = false;
row.Cells["dgvSuggentionsColumnHarvest"].Value = string.Empty;
// Color row.
if (planet.Type == CelestialBodyType.Star
|| planet.Type == CelestialBodyType.Ring)
row.DefaultCellStyle.BackColor = Color.LightPink;
else if (planet.Type == CelestialBodyType.GasGiant)
row.DefaultCellStyle.BackColor = Color.LightBlue;
else if (planet.IsHabitable)
row.DefaultCellStyle.BackColor = Color.LightGreen;
}
}
dgvSuggentions.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
private void dgvSuggentions_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
// End of edition on each click on column of checkbox
if (e.ColumnIndex == dgvSuggentionsColumnCity.Index && e.RowIndex != -1)
{
dgvSuggentions.EndEdit();
}
}
private void dgvSuggentions_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dgvSuggentionsColumnCity.Index && e.RowIndex != -1)
UpdateTables();
}
private void UpdateTables()
{
Dictionary<ResourceType, List<Resource>> resourceOrders = new Dictionary<ResourceType, List<Resource>>();
_resourceBest = new Dictionary<ResourceType, Resource>();
Dictionary<Zone, string> cityHarvest = new Dictionary<Zone, string>();
foreach (DataGridViewRow row in dgvSuggentions.Rows)
{
bool city = (bool)row.Cells["dgvSuggentionsColumnCity"].Value;
if (city)
{
Zone zone = (Zone)row.Cells["dgvSuggentionsColumnZone"].Value;
cityHarvest.Add(zone, "");
foreach (KeyValuePair<ResourceType, Resource> resourceEntry in zone.Resources)
{
if (FilterResource(resourceEntry.Key))
continue;
if (!resourceOrders.ContainsKey(resourceEntry.Key))
resourceOrders.Add(resourceEntry.Key, new List<Resource>());
resourceOrders[resourceEntry.Key].Add(resourceEntry.Value);
}
}
else
row.Cells["dgvSuggentionsColumnHarvest"].Value = "";
}
if (cityHarvest.Count != 0)
{
foreach (KeyValuePair<ResourceType, List<Resource>> resourceEntry in resourceOrders)
{
_resourceBest.Add(resourceEntry.Key, resourceEntry.Value.OrderByDescending(x => x.Quality).ToList()[0]);
}
foreach (KeyValuePair<ResourceType, Resource> resourceEntry in _resourceBest)
{
Resource resource = resourceEntry.Value;
Zone zone = resource.HostZone;
// Ass the resouce to the
if (!String.IsNullOrEmpty(cityHarvest[zone]))
cityHarvest[zone] += ", ";
cityHarvest[zone] += resource.Name;
// If the resource is planet-wide, add it to all the zones on the planet.
if (resourceEntry.Value.AcrossZones && zone.HostCelestialBody.ResourceZones.Length > 1)
{
foreach (Zone zoneSibling in zone.HostCelestialBody.ResourceZones)
{
if (zoneSibling != zone && cityHarvest.ContainsKey(zoneSibling))
{
if (!String.IsNullOrEmpty(cityHarvest[zoneSibling]))
cityHarvest[zoneSibling] += ", ";
cityHarvest[zoneSibling] += resource.Name;
}
}
}
}
foreach (DataGridViewRow row in dgvSuggentions.Rows)
{
Zone zone = (Zone)row.Cells["dgvSuggentionsColumnZone"].Value;
if (cityHarvest.ContainsKey(zone))
{
DataGridViewCell cell = row.Cells["dgvSuggentionsColumnHarvest"];
cell.Value = cityHarvest[zone];
}
}
}
// Update the reources table.
foreach (DataGridViewRow row in dgvResources.Rows)
{
ResourceType resourceType = (row.Cells["dgvResourcesColumnBest"].Value as Resource).Type;
DataGridViewCell cell = row.Cells["dgvResourcesColumnCurrent"];
// Update quality of each resource on the list and add colors.
row.DefaultCellStyle.BackColor = Color.White;
if (_resourceBest.ContainsKey(resourceType))
{
cell.Value = _resourceBest[resourceType];
if (_resourceBest[resourceType].HostZone.HostCelestialBody.ResourceZones.Length == 1
|| (_resourceBest[resourceType].HostZone.HostCelestialBody.ResourceZones.Length > 1
&& Resource.IsAcrossZones(resourceType)))
cell.ToolTipText = _resourceBest[resourceType].HostZone.HostCelestialBody.Name;
else
cell.ToolTipText = _resourceBest[resourceType].HostZone.HostCelestialBody.Name + ", " + _resourceBest[resourceType].HostZone.Name;
cell.Style.ForeColor = _resourceBest[resourceType].TechLevelColor;
// Color the row green if the current is best.
if (((Resource)row.Cells["dgvResourcesColumnBest"].Value).Quality == _resourceBest[resourceType].Quality)
row.DefaultCellStyle.BackColor = Color.LightGreen;
}
else
cell.Value = null;
if (FilterResource(resourceType))
row.Visible = false;
else
row.Visible = true;
}
}
private void dgv_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{ // Override of the DataGridView's normal SortCompare. This version converts some of the fields to numbers before sorting them.
DataGridView dgv = (sender as DataGridView);
string columnName = e.Column.Name;
if (columnName == "dgvSuggentionsColumnCity")
{
bool value1 = (bool)e.CellValue1;
bool value2 = (bool)e.CellValue2;
e.SortResult = value1.CompareTo(value2);
}
else if (columnName == "dgvSuggentionsColumnHarvest")
{
int value1 = -1;
if ((string)e.CellValue1 != null)
value1 = e.CellValue1.ToString().Length;
int value2 = -1;
if ((string)e.CellValue2 != null)
value2 = e.CellValue2.ToString().Length;
e.SortResult = CompareNumbers(value1, value2);
if (e.SortResult == 0)
{
e.SortResult = String.Compare(
e.CellValue1.ToString(),
e.CellValue2.ToString());
}
}
else
{
// Try to sort based on the cells in the current column as srtings.
e.SortResult = String.Compare((e.CellValue1 ?? "").ToString(), (e.CellValue2 ?? "").ToString());
}
// If the cells are equal, sort based on the ID column.
if (e.SortResult == 0)
{
e.SortResult = String.Compare(
dgv.Rows[e.RowIndex1].Cells["dgvSuggentionsColumnPlanet"].Value.ToString(),
dgv.Rows[e.RowIndex2].Cells["dgvSuggentionsColumnPlanet"].Value.ToString());
}
if (e.SortResult == 0)
{
e.SortResult = String.Compare(
dgv.Rows[e.RowIndex1].Cells["dgvSuggentionsColumnZone"].Value.ToString(),
dgv.Rows[e.RowIndex2].Cells["dgvSuggentionsColumnZone"].Value.ToString());
}
e.Handled = true;
}
/// <summary>
/// Makes sure the string has one decimal separated with ".", and then pads the start of the string with spaces (" "s).
/// </summary>
private string Normalize(string s, int len)
{
s = s.Replace(',', '.');
if (!s.Contains('.'))
s += ".00";
return s.PadLeft(len + 3);
}
private int CompareNumberStrings(string value1, string value2)
{
int maxLen = Math.Max(value1.Length, value2.Length);
value1 = Normalize(value1, maxLen);
value2 = Normalize(value2, maxLen);
return String.Compare(value1, value2);
}
private int CompareNumbers(string value1, string value2)
{
double value1double = Double.Parse(value1, Hazeron.NumberFormat);
double value2double = Double.Parse(value2, Hazeron.NumberFormat);
return Math.Sign(value1double.CompareTo(value2double));
}
private int CompareNumbers(double value1, double value2)
{
return Math.Sign(value1.CompareTo(value2));
}
private int CompareNumbers(int value1, int value2)
{
return Math.Sign(value1.CompareTo(value2));
}
#endregion
#region DataGridView ContextMenu RightClick
private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{ // Based on: http://stackoverflow.com/questions/1718389/right-click-context-menu-for-datagrid.
if (e.ColumnIndex != -1 && e.RowIndex != -1 && e.Button == System.Windows.Forms.MouseButtons.Right)
{
DataGridView dgv = (sender as DataGridView);
dgv.ContextMenuStrip = cmsRightClick;
DataGridViewCell currentCell = dgv[e.ColumnIndex, e.RowIndex];
currentCell.DataGridView.ClearSelection();
currentCell.DataGridView.CurrentCell = currentCell;
currentCell.Selected = true;
//Rectangle r = currentCell.DataGridView.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, false);
//Point p = new Point(r.X + r.Width, r.Y + r.Height);
Point p = dgv.PointToClient(Control.MousePosition);
dgv.ContextMenuStrip.Show(currentCell.DataGridView, p);
dgv.ContextMenuStrip = null;
}
}
private void dgv_KeyDown(object sender, KeyEventArgs e)
{ // Based on: http://stackoverflow.com/questions/1718389/right-click-context-menu-for-datagrid.
DataGridView dgv = (sender as DataGridView);
DataGridViewCell currentCell = dgv.CurrentCell;
if (currentCell != null)
{
cmsRightClick_Opening(sender, null);
if ((e.KeyCode == Keys.F10 && !e.Control && e.Shift) || e.KeyCode == Keys.Apps)
{
dgv.ContextMenuStrip = cmsRightClick;
Rectangle r = currentCell.DataGridView.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, false);
Point p = new Point(r.X + r.Width, r.Y + r.Height);
dgv.ContextMenuStrip.Show(currentCell.DataGridView, p);
dgv.ContextMenuStrip = null;
}
else if (e.KeyCode == Keys.C && e.Control && !e.Shift)
cmsRightClickCopy_Click(sender, null);
}
}
private void cmsRightClick_Opening(object sender, CancelEventArgs e)
{
// Get table in question and currentCell.
DataGridView dgv = (sender as DataGridView);
if (dgv == null)
dgv = ((sender as ContextMenuStrip).SourceControl as DataGridView);
DataGridViewCell currentCell = dgv.CurrentCell;
// Freeze Column.
DataGridViewColumn nextCoumn = dgv.Columns.GetNextColumn(currentCell.OwningColumn, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
}
private void cmsRightClickCopy_Click(object sender, EventArgs e)
{ // http://stackoverflow.com/questions/4886327/determine-what-control-the-contextmenustrip-was-used-on
DataGridView dgv = (sender as DataGridView);
if (dgv == null)
dgv = (((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl as DataGridView);
DataGridViewCell currentCell = dgv.CurrentCell;
if (currentCell != null)
{
// Check if the cell is empty.
if (currentCell.Value != null && currentCell.Value.ToString() != String.Empty)
{ // If not empty, add to clipboard and inform the user.
Clipboard.SetText(currentCell.Value.ToString());
toolStripStatusLabel1.Text = "Cell content copied to clipboard (\"" + currentCell.Value.ToString() + "\")";
}
else
{ // Inform the user the cell was empty and therefor no reason to erase the clipboard.
toolStripStatusLabel1.Text = "Cell is empty";
#if DEBUG
// Debug code to see if the cell is null or "".
if (dgv.CurrentCell.Value == null)
toolStripStatusLabel1.Text += " (null)";
else
toolStripStatusLabel1.Text += " (\"\")";
#endif
}
}
}
private void cmsRightClickHideColumn_Click(object sender, EventArgs e)
{ // http://stackoverflow.com/questions/4886327/determine-what-control-the-contextmenustrip-was-used-on
DataGridView dgv = (sender as DataGridView);
if (dgv == null)
dgv = (((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl as DataGridView);
DataGridViewCell currentCell = dgv.CurrentCell;
if (currentCell != null)
{
// Hide the column.
currentCell.OwningColumn.Visible = false;
toolStripStatusLabel1.Text = "\"" + currentCell.OwningColumn.HeaderText + "\" column hidden";
}
}
private void cmsRightClickHideRow_Click(object sender, EventArgs e)
{ // http://stackoverflow.com/questions/4886327/determine-what-control-the-contextmenustrip-was-used-on
DataGridView dgv = (sender as DataGridView);
if (dgv == null)
dgv = (((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl as DataGridView);
DataGridViewCell currentCell = dgv.CurrentCell;
if (currentCell != null)
{
// Hide the selected row(s).
foreach (DataGridViewRow row in dgv.SelectedRows)
row.Visible = false;
toolStripStatusLabel1.Text = dgv.SelectedRows.Count + " row(s) hidden";
}
}
#endregion
}
}