-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormMain.cs
833 lines (764 loc) · 37.3 KB
/
FormMain.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
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;
using System.Threading;
using System.Threading.Tasks;
namespace HazeronProspector
{
public partial class FormMain : Form
{
HazeronStarMapReader _hStarMap;
Coordinate coord;
Dictionary<string, bool> _columnVisability = new Dictionary<string, bool>();
bool _errorCoordinate = true;
bool _optionSystemWide = false;
public FormMain()
{
InitializeComponent();
#if DEBUG
this.Text += " (DEBUG MODE)";
#endif
foreach (DataGridViewColumn column in dgvSurvey.Columns)
{
_columnVisability.Add(column.Name, true);
}
ToggleButtons(false);
toolStripProgressBar1.Visible = false;
openFileDialog1.FileName = "<empire> Star Map";
openFileDialog1.Filter = "Hazeron XML Star Map|*.xml|All files|*.*";
//openFileDialog1.InitialDirectory = Application.StartupPath;
toolStripStatusLabel1.Text = "No star map";
#if DEBUG
menuStrip1Debug.Visible = true;
menuStrip1DebugSystemOptimizer.Click += menuStrip1DebugSystemOptimizer_Click;
#endif
}
/// <summary>
/// Toggles between ready and non-ready state of all buttons and options.
/// </summary>
/// <param name="enable">Setting to true change buttons to ready state. Setting to false change buttons to non-ready state.</param>
private void ToggleButtons(bool enable)
{
btnImportMerger.Enabled = enable;
menuStrip1FileImportMerger.Enabled = enable;
cobSelectionGalaxy.Enabled = enable;
rabSelectionDropdown.Enabled = enable;
rabSelectionCoordinate.Enabled = enable;
if (enable)
rabSelection_Click(rabSelectionDropdown, null);
else
{
cobSelectionSector.Enabled = enable;
cobSelectionSystem.Enabled = enable;
tbxSelectionCoordinate.Enabled = enable;
}
rabFilterNone.Enabled = enable;
rabFilterRange.Enabled = enable;
rabFilterWormhole.Enabled = enable;
if (enable)
rabFilter_Click(rabFilterNone, null);
else
{
nudFilterRange.Enabled = enable;
nudFilterWormhole.Enabled = enable;
}
cbxOptionsSystemWide.Enabled = enable;
btnSearch.Enabled = enable;
}
private void StarMapImport()
{
ToggleButtons(false);
if (openFileDialog1.ShowDialog(this) != System.Windows.Forms.DialogResult.OK)
return;
_hStarMap = new HazeronStarMapReader();
_hStarMap.ReadXmlFile(openFileDialog1.FileName);
if (_hStarMap.GalaxyList.Count == 0)
{
MessageBox.Show(this,
"The starmap XML export file does not contain any surveyed solar systems.",
"Starmap XML Export Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
ToggleButtons(true);
Galaxy[] galaxies = _hStarMap.GalaxyList.Values.OrderBy(x => x.Name).ToArray();
cobSelectionGalaxy.Items.Clear();
cobSelectionGalaxy.Items.AddRange(galaxies);
cobSelectionGalaxy.SelectedIndex = 0;
toolStripStatusLabel1.Text = "Star map loaded";
}
private void StarMapImportMerger()
{
if (openFileDialog1.ShowDialog(this) != System.Windows.Forms.DialogResult.OK)
return;
_hStarMap.AppendXmlFile(openFileDialog1.FileName);
Galaxy[] galaxies = _hStarMap.GalaxyList.Values.OrderBy(x => x.Name).ToArray();
cobSelectionGalaxy.Items.Clear();
cobSelectionGalaxy.Items.AddRange(galaxies);
cobSelectionGalaxy.SelectedIndex = 0;
toolStripStatusLabel1.Text = "Additional star map loaded";
}
private void StarMapSearch()
{
toolStripStatusLabel1.Text = "Searching star map...";
List<HSystem> selectedSystems = new List<HSystem>();
// Selection of center
if (rabSelectionDropdown.Checked) // Dropdown search
{
if (cobSelectionSector.SelectedIndex > 0) // Selected sector
{
if (cobSelectionSystem.SelectedIndex > 0) // Selected system
{
HSystem system = (cobSelectionSystem.SelectedItem as HSystem);
selectedSystems.Add(system);
coord = system.Coord;
}
else // All systems in sector
{
Sector sector = (cobSelectionSector.SelectedItem as Sector);
selectedSystems.AddRange(sector.Systems.Values);
coord = sector.Coord;
}
}
else // All sectors in galaxy
{
Galaxy galaxy = (cobSelectionGalaxy.SelectedItem as Galaxy);
foreach (Sector sector in galaxy.Sectors.Values)
selectedSystems.AddRange(sector.Systems.Values);
coord = new Coordinate();
}
}
else if (rabSelectionCoordinate.Checked) // Coordinate search
{
if (!_errorCoordinate)
{
string[] coordinateArray = tbxSelectionCoordinate.Text.Split(new string[] { ", ", " ", "," }, StringSplitOptions.RemoveEmptyEntries);
coord = new Coordinate(
Double.Parse(coordinateArray[0], System.Globalization.NumberStyles.Number, Hazeron.NumberFormat),
Double.Parse(coordinateArray[1], System.Globalization.NumberStyles.Number, Hazeron.NumberFormat),
Double.Parse(coordinateArray[2], System.Globalization.NumberStyles.Number, Hazeron.NumberFormat));
HSystem system = _hStarMap.SystemList.Values.SingleOrDefault(x => x.Coord.Equals(coord));
if (system == null)
{
IEnumerable<Sector> sectors = _hStarMap.SectorList.Values.Where(x => x.Coord.Distance(coord) < 10);
foreach (Sector sector in sectors)
selectedSystems.AddRange(sector.Systems.Values.Where(x => x.Coord.Distance(coord) < 0.5));
}
}
else
{
MessageBox.Show(this, "The entered coordinate is not valid. Pleaese check it for errors or use another selection type.", "Invalid Coordinate", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
else // Error?
throw new Exception("No selection type selected!?");
// Filters for surounding systmes
if (rabFilterRange.Checked && nudFilterRange.Value > 0) // Range filter
{
foreach (Sector sector in _hStarMap.SectorList.Values)
{
double dist = coord.Distance(sector.Coord);
if (dist <= (double)nudFilterRange.Value + 15)
{
foreach (HSystem system in sector.Systems.Values)
{
if (!selectedSystems.Contains(system))
{
dist = coord.Distance(system.Coord);
if (dist <= (double)nudFilterRange.Value)
{
selectedSystems.Add(system);
}
}
}
}
}
}
else if (rabFilterWormhole.Checked && nudFilterWormhole.Value > 0) // Wormhole filter
{
if (selectedSystems.Count == 0)
{
MessageBox.Show(this, "The entered coordinate did not yield a system." + Environment.NewLine + "If no system is at or very close to the coordinate, then it is impossible to filter with wormhole jumps.", "No System at Coordinate", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else if (rabSelectionDropdown.Checked && cobSelectionSector.SelectedIndex == 0)
{
// If all sectors are selected, don't bother with this.
}
else
{
List<HSystem> nextWormholeReach = selectedSystems.ToList();
for (int i = 0; i < nudFilterWormhole.Value; i++)
{
List<HSystem> currentWormholeReach = nextWormholeReach.ToList();
nextWormholeReach.Clear();
foreach (HSystem system in currentWormholeReach)
{
foreach (HSystem destinationSystem in system.WormholeLinks)
{
if (!selectedSystems.Contains(destinationSystem))
{
nextWormholeReach.Add(destinationSystem);
selectedSystems.Add(destinationSystem);
}
}
}
}
}
}
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Step = 1;
toolStripProgressBar1.Maximum = selectedSystems.Count;
toolStripProgressBar1.Visible = true;
// Clear and fill the table.
TableClear();
foreach (HSystem system in selectedSystems)
{
TableAddSystem(system);
toolStripProgressBar1.PerformStep();
}
toolStripProgressBar1.Visible = false;
dgvSurvey.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
toolStripStatusLabel1.Text = selectedSystems.Count + " systems found resulting in " + dgvSurvey.Rows.Count + " entries";
}
private void ResetTableSettings()
{
// Show all columns.
foreach (DataGridViewColumn column in dgvSurvey.Columns)
{
_columnVisability[column.Name] = true;
column.Visible = true;
}
// Unfreeze all columns.
foreach (DataGridViewColumn column in dgvSurvey.Columns)
{
column.Frozen = false;
column.DividerWidth = 0;
}
// Freeze specific column.
if (!_optionSystemWide)
{
dgvSurvey.Columns["dgvSurveyColumnZone"].Frozen = true;
dgvSurvey.Columns["dgvSurveyColumnZone"].DividerWidth = 2;
}
else
{
dgvSurvey.Columns["dgvSurveyColumnSystem"].Frozen = true;
dgvSurvey.Columns["dgvSurveyColumnSystem"].DividerWidth = 2;
}
}
#region Buttons
private void btnImport_Click(object sender, EventArgs e)
{
StarMapImport();
}
private void btnImportMerger_Click(object sender, EventArgs e)
{
StarMapImportMerger();
}
private void rabSelection_Click(object sender, EventArgs e)
{
RadioButton reb = (sender as RadioButton);
rabSelectionDropdown.Checked = (rabSelectionDropdown == reb);
cobSelectionSector.Enabled = (rabSelectionDropdown == reb);
cobSelectionSystem.Enabled = (rabSelectionDropdown == reb && cobSelectionSector.SelectedIndex > 0);
rabSelectionCoordinate.Checked = (rabSelectionCoordinate == reb);
tbxSelectionCoordinate.Enabled = (rabSelectionCoordinate == reb);
}
private void rabFilter_Click(object sender, EventArgs e)
{
RadioButton reb = (sender as RadioButton);
rabFilterNone.Checked = (rabFilterNone == reb);
rabFilterRange.Checked = (rabFilterRange == reb);
nudFilterRange.Enabled = (rabFilterRange == reb);
rabFilterWormhole.Checked = (rabFilterWormhole == reb);
nudFilterWormhole.Enabled = (rabFilterWormhole == reb);
}
private void cobSelectionGalaxy_SelectedIndexChanged(object sender, EventArgs e)
{
cobSelectionSector.Items.Clear();
cobSelectionSector.Items.Add("<All sectors>");
cobSelectionSector.SelectedIndex = 0;
Sector[] sectors = (cobSelectionGalaxy.SelectedItem as Galaxy).Sectors.Values.OrderBy(x => x.Name).ToArray();
cobSelectionSector.Items.AddRange(sectors);
}
private void cobSelectionSector_SelectedIndexChanged(object sender, EventArgs e)
{
cobSelectionSystem.Items.Clear();
if (cobSelectionSector.SelectedIndex > 0)
{
cobSelectionSystem.Items.Add("<All systems>");
cobSelectionSystem.SelectedIndex = 0;
HSystem[] systems = (cobSelectionSector.SelectedItem as Sector).Systems.Values.OrderBy(x => x.Name).ToArray();
cobSelectionSystem.Items.AddRange(systems);
cobSelectionSystem.Enabled = true;
}
else
cobSelectionSystem.Enabled = false;
}
private void tbxSelectionCoordinate_TextChanged(object sender, EventArgs e)
{
string[] coordinateArray = tbxSelectionCoordinate.Text.Split(new string[] { ", ", " ", "," }, StringSplitOptions.RemoveEmptyEntries);
if (coordinateArray.Length != 3)
{
tbxSelectionCoordinate.ForeColor = Color.Red;
_errorCoordinate = true;
return;
}
foreach (string coordinate in coordinateArray)
{
double none;
if (!Double.TryParse(coordinate, System.Globalization.NumberStyles.Number, Hazeron.NumberFormat, out none))
{
tbxSelectionCoordinate.ForeColor = Color.Red;
_errorCoordinate = true;
return;
}
}
tbxSelectionCoordinate.ForeColor = Color.Black;
_errorCoordinate = false;
}
private void cbxOptionsSystemWide_Click(object sender, EventArgs e)
{
_optionSystemWide = !_optionSystemWide;
cbxOptionsSystemWide.Checked = _optionSystemWide;
}
private void btnSearch_Click(object sender, EventArgs e)
{
StarMapSearch();
}
#endregion
#region DataGridView
private void TableAddSystem(HSystem system)
{
dgvSurvey.Columns["dgvSurveyColumnZone"].Visible = _columnVisability["dgvSurveyColumnZone"] && !_optionSystemWide;
dgvSurvey.Columns["dgvSurveyColumnDiameter"].Visible = _columnVisability["dgvSurveyColumnDiameter"] && !_optionSystemWide;
dgvSurvey.Columns["dgvSurveyColumnBodyType"].Visible = _columnVisability["dgvSurveyColumnBodyType"] && !_optionSystemWide;
if (_optionSystemWide)
{ // SystemWide mode
dgvSurvey.Rows.Add();
DataGridViewRow row = dgvSurvey.Rows[dgvSurvey.RowCount - 1];
row.Cells["dgvSurveyColumnGalaxy"].Value = system.HostSector.HostGalaxy;
row.Cells["dgvSurveyColumnSector"].Value = system.HostSector;
row.Cells["dgvSurveyColumnSystem"].Value = system;
row.Cells["dgvSurveyColumnSystemCatalog"].Value = system.CatalogName;
row.Cells["dgvSurveyColumnWorld"].Value = system.CelestialBodies.Values.Count(x => x.Type != CelestialBodyType.Star && x.Type != CelestialBodyType.Ring) + " worlds (" + system.HabitbleCount() + " habitable)";
row.Cells["dgvSurveyColumnOrbit"].Value = system.CelestialBodies.Values.Count(x => x.Type == CelestialBodyType.Star) + " Stars";
row.Cells["dgvSurveyColumnCoordinates"].Value = system.Coord;
foreach (Resource resource in system.BestResources().Values)
{
TableAddResourceRow(row, resource);
}
}
else
{ // Normal HazeronScouter mode
foreach (CelestialBody planet in system.CelestialBodies.Values)
{
foreach (Zone zone in planet.ResourceZones)
{
dgvSurvey.Rows.Add();
DataGridViewRow row = dgvSurvey.Rows[dgvSurvey.RowCount - 1];
row.Cells["dgvSurveyColumnGalaxy"].Value = system.HostSector.HostGalaxy;
row.Cells["dgvSurveyColumnSector"].Value = system.HostSector;
row.Cells["dgvSurveyColumnSystem"].Value = system;
row.Cells["dgvSurveyColumnSystemCatalog"].Value = system.CatalogName;
row.Cells["dgvSurveyColumnWorld"].Value = planet;
row.Cells["dgvSurveyColumnZone"].Value = zone;
switch (planet.Orbit)
{
case CelestialBodyOrbit.Inferno:
row.Cells["dgvSurveyColumnOrbit"].Value = "Inferno";
break;
case CelestialBodyOrbit.Inner:
row.Cells["dgvSurveyColumnOrbit"].Value = "Inner";
break;
case CelestialBodyOrbit.Habitable:
row.Cells["dgvSurveyColumnOrbit"].Value = "Habitable";
break;
case CelestialBodyOrbit.Outer:
row.Cells["dgvSurveyColumnOrbit"].Value = "Outer";
break;
case CelestialBodyOrbit.Frigid:
row.Cells["dgvSurveyColumnOrbit"].Value = "Frigid";
break;
}
row.Cells["dgvSurveyColumnCoordinates"].Value = system.Coord;
switch (planet.Type)
{
case CelestialBodyType.Star:
row.Cells["dgvSurveyColumnBodyType"].Value = "Star";
break;
case CelestialBodyType.NeutronStar:
row.Cells["dgvSurveyColumnBodyType"].Value = "Neutron Star";
break;
case CelestialBodyType.BlackHole:
row.Cells["dgvSurveyColumnBodyType"].Value = "Black Holes";
break;
case CelestialBodyType.Planet:
row.Cells["dgvSurveyColumnBodyType"].Value = "Planet";
break;
case CelestialBodyType.Moon:
row.Cells["dgvSurveyColumnBodyType"].Value = "Moon";
break;
case CelestialBodyType.GasGiant:
row.Cells["dgvSurveyColumnBodyType"].Value = "Gas Giant";
break;
case CelestialBodyType.Titan:
row.Cells["dgvSurveyColumnBodyType"].Value = "Titan";
break;
case CelestialBodyType.Ring:
row.Cells["dgvSurveyColumnBodyType"].Value = "Ring";
break;
case CelestialBodyType.Planetoid:
row.Cells["dgvSurveyColumnBodyType"].Value = "Planetoid";
break;
case CelestialBodyType.RingworldArc:
row.Cells["dgvSurveyColumnBodyType"].Value = "Ringworld Arc";
break;
}
if (planet.Diameter > 0 && planet.Type != CelestialBodyType.Ring)
row.Cells["dgvSurveyColumnDiameter"].Value = planet.Diameter;
foreach (Resource resource in zone.Resources.Values)
{
TableAddResourceRow(row, resource);
}
}
}
}
}
private void TableAddResourceRow(DataGridViewRow row, Resource resource)
{
DataGridViewCell cell = row.Cells["dgvSurveyColumnResource" + Enum.GetName(typeof(ResourceType), (int)resource.Type)];
cell.Value = resource;
cell.Style.ForeColor = resource.TechLevelColor;
cell.Style.SelectionForeColor = resource.TechLevelColor;
if (resource.HostZone.HostCelestialBody.ResourceZones.Length == 1
|| (resource.HostZone.HostCelestialBody.ResourceZones.Length > 1 && resource.AcrossZones))
cell.ToolTipText = resource.HostZone.HostCelestialBody.Name;
else
cell.ToolTipText = resource.HostZone.HostCelestialBody.Name + ", " + resource.HostZone.Name;
}
private void TableClear()
{
dgvSurvey.Rows.Clear();
dgvSurvey.Refresh();
}
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;
const int ColumnResourceNameLenght = 23; // "dgvSurveyColumnResource".Length
if (columnName.Length > ColumnResourceNameLenght && columnName.Remove(ColumnResourceNameLenght) == "dgvSurveyColumnResource")
{
int value1 = -1;
if ((e.CellValue1 as Resource) != null)
value1 = (e.CellValue1 as Resource).Quality;
int value2 = -1;
if ((e.CellValue2 as Resource) != null)
value2 = (e.CellValue2 as Resource).Quality;
e.SortResult = CompareNumbers(value1, value2);
if (e.SortResult == 0)
{
value1 = -1;
if ((e.CellValue1 as Resource) != null)
value1 = (e.CellValue1 as Resource).Abundance;
value2 = -1;
if ((e.CellValue2 as Resource) != null)
value2 = (e.CellValue2 as Resource).Abundance;
e.SortResult = CompareNumbers(value1, value2);
}
}
else
{
// Try to sort based on the cells in the current column as padded srtings.
string value1 = (e.CellValue1 ?? "").ToString(), value2 = (e.CellValue2 ?? "").ToString();
e.SortResult = CompareNumberStrings(value1, value2);
}
// If the cells are equal, sort based on the ID column.
if (e.SortResult == 0)
{
e.SortResult = String.Compare(
dgv.Rows[e.RowIndex1].Cells["dgvSurveyColumnSector"].Value.ToString(),
dgv.Rows[e.RowIndex2].Cells["dgvSurveyColumnSector"].Value.ToString());
}
if (e.SortResult == 0)
{
e.SortResult = String.Compare(
dgv.Rows[e.RowIndex1].Cells["dgvSurveyColumnSystem"].Value.ToString(),
dgv.Rows[e.RowIndex2].Cells["dgvSurveyColumnSystem"].Value.ToString());
}
if (e.SortResult == 0)
{
e.SortResult = String.Compare(
dgv.Rows[e.RowIndex1].Cells["dgvSurveyColumnWorld"].Value.ToString(),
dgv.Rows[e.RowIndex2].Cells["dgvSurveyColumnWorld"].Value.ToString());
}
if (e.SortResult == 0
&& dgv.Rows[e.RowIndex1].Cells["dgvSurveyColumnZone"].Value != null
&& dgv.Rows[e.RowIndex2].Cells["dgvSurveyColumnZone"].Value != null)
{
e.SortResult = String.Compare(
dgv.Rows[e.RowIndex1].Cells["dgvSurveyColumnZone"].Value.ToString(),
dgv.Rows[e.RowIndex2].Cells["dgvSurveyColumnZone"].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));
}
private void dgvSurvey_SelectionChanged(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "Entries " + dgvSurvey.Rows.Count;
if (dgvSurvey.SelectedRows != null && dgvSurvey.SelectedRows.Count > 0)
toolStripStatusLabel1.Text += " (" + dgvSurvey.SelectedRows.Count + " selected)";
}
#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);
if (currentCell.OwningColumn.Frozen && (nextCoumn == null || !nextCoumn.Frozen))
cmsRightClickFreezeColumn.Text = "Unfreeze Column";
else
cmsRightClickFreezeColumn.Text = "Freeze Column";
}
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 cmsRightClickFreezeColumn_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)
{
foreach (DataGridViewColumn column in dgv.Columns)
{
column.DividerWidth = 0;
}
DataGridViewColumn nextCoumn = dgv.Columns.GetNextColumn(currentCell.OwningColumn, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
// Is it frozen already?
if (currentCell.OwningColumn.Frozen && (nextCoumn == null || !nextCoumn.Frozen))
{
// Unfreeze all columns.
foreach (DataGridViewColumn column in dgv.Columns)
{
column.Frozen = false;
}
toolStripStatusLabel1.Text = "\"" + currentCell.OwningColumn.HeaderText + "\" column unfrozen";
}
else
{
// Freeze the column.
currentCell.OwningColumn.Frozen = true;
if (nextCoumn != null)
nextCoumn.Frozen = false;
currentCell.OwningColumn.DividerWidth = 2;
toolStripStatusLabel1.Text = "\"" + currentCell.OwningColumn.HeaderText + "\" column frozen";
}
}
}
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
#region menuStrip1
private void menuStrip1FileImport_Click(object sender, EventArgs e)
{
StarMapImport();
}
private void menuStrip1FileImportMerger_Click(object sender, EventArgs e)
{
StarMapImportMerger();
}
private void menuStrip1FileExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void menuStrip1OptionsShowHidenRows_Click(object sender, EventArgs e)
{
// Show all rows.
foreach (DataGridViewRow row in dgvSurvey.Rows)
{
row.Visible = true;
}
}
private void menuStrip1OptionsResetTableSettings_Click(object sender, EventArgs e)
{
ResetTableSettings();
}
private void menuStrip1HelpGithub_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(@"https://github.com/Deantwo/HazeronProspector");
}
private void menuStrip1HelpThread_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(@"http://hazeron.com/mybb/showthread.php?tid=31");
}
private void menuStrip1HelpAbout_Click(object sender, EventArgs e)
{
MessageBox.Show(
"Program:" + Environment.NewLine +
" HazeronProspector" + Environment.NewLine +
"" + Environment.NewLine +
"Version:" + Environment.NewLine +
" " + Application.ProductVersion + Environment.NewLine +
"" + Environment.NewLine +
"Creator:" + Environment.NewLine +
" Deantwo" + Environment.NewLine +
"" + Environment.NewLine +
"Feedback, suggestions, and bug reports should be posted in the forum thread or PMed to Deantwo please."
, "About HazeronProspector", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
private void menuStrip1HelpHowToUse_Click(object sender, EventArgs e)
{
MessageBox.Show(
"Export StarMap XML:" + Environment.NewLine +
"1. Log into Hazeron with your character" + Environment.NewLine +
"2. Open the Locator window (F7)" + Environment.NewLine +
"4. Go to the Star Map tab" + Environment.NewLine +
"5. Click the \"Explored/Scanned/Detected Space\" button" + Environment.NewLine +
"6. Click the \"Save Starmap\" button" + Environment.NewLine +
"7. Choose a location for the .xml file that you will be able to find later"
, "How to use HazeronProspector", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
#endregion
#region DEGUG
#if DEBUG
private void menuStrip1DebugSystemOptimizer_Click(object sender, EventArgs e)
{
if (dgvSurvey.SelectedRows.Count != 0)
{
HSystem system = (HSystem)dgvSurvey.SelectedRows[0].Cells["dgvSurveyColumnSystem"].Value;
FormSystemOptimizer window = new FormSystemOptimizer(system);
window.Show(this);
}
}
#endif
#endregion
}
}