-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMapForm.cs
2978 lines (2786 loc) · 120 KB
/
MapForm.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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* YOGEME.exe, All-in-one Mission Editor for the X-wing series, XW through XWA
* Copyright (C) 2007-2024 Michael Gaisser ([email protected])
* Licensed under the MPL v2.0 or later
*
* VERSION: 1.16
*
* CHANGELOG
* v1.16, 241013
* [UPD] cleanup
* v1.15.5, 231222
* [FIX #94] WP1 on hyper orders being enabled even if disabled
* [FIX] bad offsets if hyper WP1 disabled
* v1.15.4, 231125
* [FIX #93] "From any" buoys detected
* v1.14, 230804
* [FIX] pct.MouseLeave will now clear shiftState
* [FIX] buoys also check Des2 [#83]
* [FIX] virtual exit points now only selectable when visible [#83]
* [FIX] virtual aim points now only selectable when visible [#83]
* v1.13.12, 230116
* [FIX] Can select disabled XWA SP1, since they're shown
* [FIX] Selection corners X location
* [FIX] Removed a leftover debug bypass from the 1.13.10 testing
* [FIX] XWA Enabled Order Waypoints are no longer displayed if the craft never visits that Region
* v1.13.11, 221030
* [FIX] XWA SP1 now always treated as Enabled
* v1.13.10, 221018
* [NEW] Correct display of hyper exit points
* [FIX] Wireframe roll
* v1.13.5, 220608
* [FIX] Removed a chunk that could've executed relating to not-yet-implemented changes
* v1.13.4, 220606
* [UPD] isVisibleInRegion now returns an enum to denote actually present versus other regions
* [UPD] moved chkWP[12+] checks in MapPaint to non-XWA block
* v1.13.3, 220402
* [FIX] pctMap_Enter stealing focus when window wasn't active
* v1.13.2, 220319
* [NEW] checkboxes to toggle wireframes and icon limit
* [UPD] increased minimum form width
* [UPD] fixed selection buttons to left side
* v1.13.1, 220208
* [UPD] XWA SP3 now labeled "RDV"
* v1.12, 220103
* [FIX] Cleared sortedMapDataList on reload to fix multi-delete [JB]
* [FIX] Listbox scrolling
* v1.11, 210801
* [UPD] FGs now also take into account Hyper orders for region visiblity [JB]
* v1.10, 210520
* [FIX #58] XWA wireframes when using rotations instead of Waypoints [JB]
* v1.8, 201004
* Lots of stuff here, may not list it all...
* [NEW] Selection expansion [JB]
* [NEW] New M-click actions [JB]
* [NEW] Traces can show ETA, speed, throttle. Also, options to control what's shown [JB]
* [NEW] Wireframes can be faded and hidden [JB]
* [UPD] Purple IFF now consistently MediumOrchid [JB]
* [NEW] Snap ability when moving [JB]
* [NEW] Keyboard commands for WP movement, selection [JB]
* [NEW] Ability to change WP's region for XWA [JB]
* [NEW] Ability to show/hide WPs based on Difficulty or IFF [JB]
* [NEW] Ability to zoom map to fit selected or fit all [JB]
* [UPD] Map Options save, don't need the Options dialog to change defaults [JB]
* v1.7, 200816
* [UPD] MapPaint now always persistent
* [NEW #12] Wireframe implementation [JB]
* [UPD] Max zoom and zoom speed adjusted [JB]
* [UPD] FgIndex added to MapData [JB]
* [FIX] Unregister Tick handler to prevent misfires after closing [JB]
* [UPD] form handlers renamed
* v1.6.5, 200704
* [NEW] if pulling from imgCraft trips an OutOfRange, default to img[0]
* v1.5, 180910
* [NEW] lots of variables for UI tracking [JB]
* [NEW] Xwing capability [JB]
* [NEW] Callbacks in ctors [JB]
* [UPD] Import moved to after Init [JB]
* [NEW] chkDistance, hide buttons, listboxes, help button [JB]
* [UPD] form closing events updated [JB]
* [UPD] paint bypass if !visible [JB]
* [NEW] GetIFFColor, GetDrawColor, SwapSelectedItems, UpdateFlightGroup, lots of new map functions [JB]
* [FIX] waypoint stuff [JB]
* [NEW] selection box [JB]
* [NEW] redraw timer [JB]
* [NEW] MapData.FullName, .Visible, .Selected, .Difficulty [JB]
* [UPD] keyboard/mouse controls reworked [JB]
* [NEW] added Deselect() to several controls [JB]
* [NEW] SelectionData [JB]
* v1.4, 171016
* [ADD #11] form is now resizable, can be maximized
* v1.2.3, 141214
* [UPD] change to MPL
* v1.2, 121006
* - Settings passed in
* v1.1.1, 120814
* [FIX] MapData.Waypoints now based on BaseFlightGroup.BaseWaypoint, now updates back and forth with parent Form
* - MapPaint() Orientation switch{} removed and condensed
* - class renamed
* v1.0, 110921
* - Release
*/
using Idmr.Common;
using Idmr.Platform;
using Idmr.Yogeme.MapWireframe;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace Idmr.Yogeme
{
// TODO: Change Shift+Drag to move all WPs for selection
/// <summary>graphical interface for craft waypoints</summary>
public partial class MapForm : Form
{
#region vars and stuff
int _zoom = 40; //Current zoom scale, in pixels per kilometer
#pragma warning disable IDE1006 // Naming Styles
int w, h, _mapX, _mapY, _mapZ; //The map vars store offset of the center of the game world (0,0,0) relative to the top left corner of the viewport, taking zoom into account.
#pragma warning restore IDE1006 // Naming Styles
public enum Orientation { XY, XZ, YZ };
Orientation _displayMode = Orientation.XY;
Bitmap _map;
MapData[] _mapData;
readonly List<SelectionData> _selectionList = new List<SelectionData>(); // List of all craft and waypoints that are currently selected and itemized in the list control.
readonly List<int> _sortedMapDataList = new List<int>(); // List of _mapData indices sorted for drawing order.
readonly int[] _dragIcon = new int[2]; // [0] = fg, [1] = wp
bool _isLoading = false;
#pragma warning disable IDE1006 // Naming Styles
readonly CheckBox[] chkWP = new CheckBox[22];
#pragma warning restore IDE1006 // Naming Styles
readonly Settings.Platform _platform;
bool _isDragged;
bool _leftButtonDown = false; // Left mouse button currently held down.
bool _rightButtonDown = false;
bool _dragSelectActive = false; // Indicates that a drag-select is in progress, and to draw the selection box.
bool _dragMoveActive = false; // A drag-move is in progress.
bool _dragMoveSnapReady = false; // The starting position for drag-move operation has been assigned, so that snapping can work correctly.
Point _clickPixelDown = new Point(0, 0); //Form pixel coordinates of mouse click.
Point _clickPixelUp = new Point(0, 0);
Point _clickMapDown = new Point(0, 0); //Virtual map coordinates of mouse click
Point _clickMapUp = new Point(0, 0);
Point _dragMapPrevious = new Point(0, 0); //If dragging continuously, holds the last position so we know how much to update.
bool _mapFocus = false;
bool _shiftState = false;
bool _ignoreSelectionEvents = false; // Ignore control events if something is modifying the selection lists.
int _repeatSelectCount = 0; // Notifies the user if they keep trying to select a filtered item.
int[] _previousCraftSelection = null; // Maintains which craft items were previously selected, to detect which items were selected or unselected if selection was changed.
readonly Color _fadeColor = Color.FromArgb(52, 52, 52); // Icon and wireframe draw color for faded items.
int _lastSnapUnit = 0; // Used to detect if the user has changed the unit of measurement for movement snapping.
Keys _lastWaypointKeyCode = Keys.None; // These lastWaypoint values are used to detect keyboard double-taps for the purposes of selecting or creating waypoints via keypress.
Keys _lastWaypointKeyModifiers = Keys.None;
int _lastWaypointKeyTime = 0;
bool _mapPaintScheduled = false; //True if a paint is scheduled, that is a paint request is called while a paint is already in progress.
static WireframeManager _wireframeManager = null;
enum ExpandSelection { Craft, Iff, Size, Invert };
public enum MiddleClickAction { DoNothing, ResetToCenter, FitToWorld, FitToSelection, CenterOnSelection };
#pragma warning disable IDE1006 // Naming Styles
EventHandler onDataModified = null;
#pragma warning restore IDE1006 // Naming Styles
bool _isClosing = false; //Need a flag during form close to check whether external MapPaint() calls should be ignored.
readonly Settings _settings = Settings.GetInstance();
bool _hasFocus;
enum WaypointVisibility { Absent, Present, OtherRegion };
#endregion vars
#region ctors
/// <param name="fg">XwingFlights array</param>
public MapForm(Platform.Xwing.FlightGroupCollection fg, EventHandler dataModifiedCallback)
{
_platform = Settings.Platform.XWING;
InitializeComponent();
if (_settings.XwingOverrideExternal)
{
//Since the XW craft list is mapped to use TIE's list for the sake of the map, replace TIE's strings. Must be done prior to import.
Platform.Tie.Strings.OverrideShipList(null, null);
string[] xwType = Platform.Xwing.Strings.CraftType;
string[] xwAbbrev = Platform.Xwing.Strings.CraftAbbrv;
string[] newType = Platform.Tie.Strings.CraftType;
string[] newAbbrev = Platform.Tie.Strings.CraftAbbrv;
Platform.Xwing.FlightGroup temp = new Platform.Xwing.FlightGroup();
for (int i = 0; i < xwType.Length; i++)
{
temp.CraftType = (byte)i;
int remap = (i == xwType.Length - 1) ? 4 : temp.GetTIECraftType(); //B-wing is special case, last item in XW list. Replace directly.
if (remap >= 0 && remap < newType.Length)
{
newType[remap] = xwType[i];
newAbbrev[remap] = xwAbbrev[i];
}
}
Platform.Tie.Strings.OverrideShipList(newType, newAbbrev);
}
Import(fg);
try { imgCraft.Images.AddStrip(Image.FromFile(Application.StartupPath + "\\images\\craft_TIE.bmp")); }
catch (Exception x)
{
MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
onDataModified = dataModifiedCallback;
startup();
}
/// <param name="fg">TFlights array</param>
public MapForm(Platform.Tie.FlightGroupCollection fg, EventHandler dataModifiedCallback)
{
_platform = Settings.Platform.TIE;
InitializeComponent();
Import(fg);
try { imgCraft.Images.AddStrip(Image.FromFile(Application.StartupPath + "\\images\\craft_TIE.bmp")); }
catch (Exception x)
{
MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
onDataModified = dataModifiedCallback;
startup();
}
/// <param name="fg">XFlights array</param>
public MapForm(bool isBoP, Platform.Xvt.FlightGroupCollection fg, EventHandler dataModifiedCallback)
{
_platform = isBoP ? Settings.Platform.BoP : Settings.Platform.XvT;
InitializeComponent();
Import(fg);
try { imgCraft.Images.AddStrip(Image.FromFile(Application.StartupPath + "\\images\\craft_XvT.bmp")); }
catch (Exception x)
{
MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
onDataModified = dataModifiedCallback;
startup();
}
/// <param name="fg">WFlights array</param>
public MapForm(Platform.Xwa.FlightGroupCollection fg, EventHandler dataModifiedCallback)
{
_platform = Settings.Platform.XWA;
InitializeComponent();
Import(fg);
try { imgCraft.Images.AddStrip(Image.FromFile(Application.StartupPath + "\\images\\craft_XWA.bmp")); }
catch (Exception x)
{
MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
onDataModified = dataModifiedCallback;
startup();
}
#endregion ctors
#region private methods
/// <summary>Converts the location on the physical map to a raw craft waypoint (160 raw units = 1 km).</summary>
void convertMousepointToWaypoint(int mouseX, int mouseY, ref Point pt)
{
switch (_displayMode)
{
case Orientation.XY:
pt.X = Convert.ToInt32((mouseX - _mapX) / Convert.ToDouble(_zoom) * 160);
pt.Y = Convert.ToInt32((_mapY - mouseY) / Convert.ToDouble(_zoom) * 160);
break;
case Orientation.XZ:
pt.X = Convert.ToInt32((mouseX - _mapX) / Convert.ToDouble(_zoom) * 160);
pt.Y = Convert.ToInt32((_mapZ - mouseY) / Convert.ToDouble(_zoom) * 160);
break;
case Orientation.YZ:
pt.X = Convert.ToInt32((mouseX - _mapY) / Convert.ToDouble(_zoom) * 160);
pt.Y = Convert.ToInt32((_mapZ - mouseY) / Convert.ToDouble(_zoom) * 160);
break;
}
}
void drawCraft(Graphics g, Bitmap bmp, MapData dat, int x, int y)
{
WireframeInstance model = _wireframeManager.GetOrCreateWireframeInstance(dat.Craft, dat.FgIndex);
if (!chkWireframe.Checked || model == null || model.ModelDef == null || (chkLimit.Checked && model.ModelDef.LongestSpanMeters < _settings.WireframeIconThresholdSize))
{
g.DrawImageUnscaled(bmp, x - 8, y - 8);
return;
}
//Simple bounds check to determine if it's definitely off screen.
double calcSpan = (double)model.ModelDef.LongestSpanRaw / 40960 * _zoom;
int viewSpan = (int)calcSpan;
if (x + viewSpan < 0 || x - viewSpan > w || y + viewSpan < 0 || y - viewSpan > h) return;
BaseFlightGroup.Waypoint dst;
int region = -1;
if (_platform == Settings.Platform.XWA)
{
region = (int)numRegion.Value - 1;
int ord = (int)(region * 4 + numOrder.Value);
dst = dat.WPs[ord][0];
if (!dat.WPs[ord][0].Enabled) // Default to first order if no waypoint defined, this keeps the orientation consistent if checking different orders and they're empty.
dst = dat.WPs[1][0];
}
else dst = dat.WPs[0][4];
int meshZoom = _zoom;
if (_settings.WireframeMeshIconEnabled && _settings.WireframeMeshIconSize > 0 && calcSpan < _settings.WireframeMeshIconSize)
{
if (calcSpan < 0.00001) calcSpan = 0.00001;
double scale = _settings.WireframeMeshIconSize / calcSpan;
meshZoom = (int)(_zoom * scale);
}
if (_platform == Settings.Platform.XWA && !dst.Enabled) model.UpdateSimple(meshZoom, _settings.WireframeMeshTypeVisibility, dat.Yaw, dat.Pitch, dat.Roll);
else if (_platform == Settings.Platform.XWA && dat.WPs[17][region].Enabled && ((Platform.Xwa.FlightGroup.XwaWaypoint)dat.WPs[0][0]).Region != region)
model.UpdateParams(dat.WPs[17][region], dat.WPs[18][region], meshZoom, _displayMode, _settings.WireframeMeshTypeVisibility, dat.Roll);
else model.UpdateParams(dat.WPs[0][0], dst, meshZoom, _displayMode, _settings.WireframeMeshTypeVisibility, dat.Roll);
Pen body = new Pen(dat.View == Visibility.Fade ? _fadeColor : getIFFColor(dat.IFF));
Pen hangar = new Pen(dat.View == Visibility.Fade ? _fadeColor : Color.White);
Pen dock = new Pen(dat.View == Visibility.Fade ? _fadeColor : Color.Yellow);
Pen p;
int x1, x2, y1, y2;
int lineDrawCount = 0;
foreach (MeshLayerInstance layer in model.LayerInstances)
{
if (layer.MatchMeshFilter(_settings.WireframeMeshTypeVisibility))
{
p = body;
MeshType mt = layer.MeshLayerDefinition.MeshType;
if (mt == MeshType.Hangar) p = hangar;
else if (mt == MeshType.DockingPlatform || mt == MeshType.LandingPlatform) p = dock;
lineDrawCount += layer.MeshLayerDefinition.Lines.Count;
if (_displayMode == Orientation.XY)
{
foreach (Line line in layer.MeshLayerDefinition.Lines)
{
x1 = x + (int)layer.Vertices[line.V1].X;
x2 = x + (int)layer.Vertices[line.V2].X;
y1 = y + (int)layer.Vertices[line.V1].Y;
y2 = y + (int)layer.Vertices[line.V2].Y;
g.DrawLine(p, x1, y1, x2, y2);
}
}
else if (_displayMode == Orientation.XZ)
{
foreach (Line line in layer.MeshLayerDefinition.Lines)
{
x1 = x + (int)layer.Vertices[line.V1].X;
x2 = x + (int)layer.Vertices[line.V2].X;
y1 = y + (int)layer.Vertices[line.V1].Z;
y2 = y + (int)layer.Vertices[line.V2].Z;
g.DrawLine(p, x1, y1, x2, y2);
}
}
else if (_displayMode == Orientation.YZ)
{
foreach (Line line in layer.MeshLayerDefinition.Lines)
{
x1 = x + (int)-layer.Vertices[line.V1].Y;
x2 = x + (int)-layer.Vertices[line.V2].Y;
y1 = y + (int)layer.Vertices[line.V1].Z;
y2 = y + (int)layer.Vertices[line.V2].Z;
g.DrawLine(p, x1, y1, x2, y2);
}
}
}
}
//If we didn't draw anything at all (empty model, or none matched the mesh filter) then our failsafe is a normal icon just so it's not invisible.
//For larger models that appear large enough on screen, draw a pip at the origin so the user knows where the selection point is.
if (lineDrawCount == 0) g.DrawImageUnscaled(bmp, x - 8, y - 8);
else if (model.ModelDef.LongestSpanMeters > 30 && viewSpan > 32) g.DrawEllipse(hangar, x - 1, y - 1, 2, 2);
}
/// <summary>Get the center pixel of pctMap and the coordinates it pertains to</summary>
/// <returns>A point with the map coordinates in klicks</returns>
PointF getCenterCoord()
{
PointF coord = new PointF();
switch (_displayMode)
{
case Orientation.XY:
coord.X = (w / 2 - _mapX) / Convert.ToSingle(_zoom);
coord.Y = (_mapY - h / 2) / Convert.ToSingle(_zoom);
break;
case Orientation.XZ:
coord.X = (w / 2 - _mapX) / Convert.ToSingle(_zoom);
coord.Y = (_mapZ - h / 2) / Convert.ToSingle(_zoom);
break;
case Orientation.YZ:
coord.X = (w / 2 - _mapY) / Convert.ToSingle(_zoom);
coord.Y = (_mapZ - h / 2) / Convert.ToSingle(_zoom);
break;
}
return coord;
}
string getDistanceString(BaseFlightGroup.Waypoint wp1, BaseFlightGroup.Waypoint wp2)
{
double xlen = wp1.X - wp2.X;
double ylen = wp1.Y - wp2.Y;
double zlen = wp1.Z - wp2.Z;
double dist = Math.Sqrt((xlen * xlen) + (ylen * ylen) + (zlen * zlen));
return Math.Round(dist, 2).ToString() + " km";
}
string getTimeString(object fg, int orderIndex, BaseFlightGroup.Waypoint wp1, BaseFlightGroup.Waypoint wp2)
{
double xlen = wp1.X - wp2.X;
double ylen = wp1.Y - wp2.Y;
double zlen = wp1.Z - wp2.Z;
double dist = Math.Sqrt((xlen * xlen) + (ylen * ylen) + (zlen * zlen));
int command = 0;
int throttle = 0;
int speed = 0;
switch (_platform)
{
case Settings.Platform.XWING:
command = ((Platform.Xwing.FlightGroup)fg).Order;
throttle = ((Platform.Xwing.FlightGroup)fg).DockTimeThrottle;
if (throttle == 0) throttle = 100;
else if (throttle <= 9) throttle = (throttle + 1) * 10;
else throttle = 0;
if (command == 0 || command == 0xC || (command >= 0x1D && command <= 0x20)) // Hold Steady, Disabled, SS Await (Return, Launch, Boarding, Arrival)
throttle = 0;
break;
case Settings.Platform.TIE:
if (orderIndex > 2)
orderIndex = 2;
command = ((Platform.Tie.FlightGroup)fg).Orders[orderIndex].Command;
throttle = ((Platform.Tie.FlightGroup)fg).Orders[orderIndex].Throttle * 10;
break;
case Settings.Platform.XvT:
case Settings.Platform.BoP:
command = ((Platform.Xvt.FlightGroup)fg).Orders[orderIndex].Command;
throttle = ((Platform.Xvt.FlightGroup)fg).Orders[orderIndex].Throttle * 10;
speed = ((Platform.Xvt.FlightGroup)fg).Orders[orderIndex].Speed;
break;
case Settings.Platform.XWA:
command = ((Platform.Xwa.FlightGroup)fg).Orders[(int)numRegion.Value - 1, orderIndex].Command;
throttle = ((Platform.Xwa.FlightGroup)fg).Orders[(int)numRegion.Value - 1, orderIndex].Throttle * 10;
speed = ((Platform.Xwa.FlightGroup)fg).Orders[(int)numRegion.Value - 1, orderIndex].Speed;
break;
}
if (_platform != Settings.Platform.XWING)
{
var cmd = (Platform.Xwa.FlightGroup.Order.CommandList)command;
switch (cmd)
{
case Platform.Xwa.FlightGroup.Order.CommandList.HoldSteady:
case Platform.Xwa.FlightGroup.Order.CommandList.Disabled:
case Platform.Xwa.FlightGroup.Order.CommandList.AwaitBoarding:
case Platform.Xwa.FlightGroup.Order.CommandList.Wait:
case Platform.Xwa.FlightGroup.Order.CommandList.SSWait:
case Platform.Xwa.FlightGroup.Order.CommandList.SSAwaitReturn:
case Platform.Xwa.FlightGroup.Order.CommandList.SSLaunch:
case Platform.Xwa.FlightGroup.Order.CommandList.Hold1C:
case Platform.Xwa.FlightGroup.Order.CommandList.Hold1E:
case Platform.Xwa.FlightGroup.Order.CommandList.Hold21:
case Platform.Xwa.FlightGroup.Order.CommandList.Hold22:
case Platform.Xwa.FlightGroup.Order.CommandList.Hold23:
case Platform.Xwa.FlightGroup.Order.CommandList.SelfDestruct:
speed = 0;
throttle = 0;
break;
}
if (command >= 0x25 && _platform != Settings.Platform.XWA)
{
speed = 0;
throttle = 0;
}
}
if (speed > 0) // Using an explicit speed value instead of throttle.
{
speed = (int)((speed * 5) / 2.2235);
throttle = 0;
}
else
{
speed = CraftDataManager.GetInstance().GetCraftSpeed(((BaseFlightGroup)fg).CraftType);
speed = (int)(((double)throttle / 100) * speed);
}
if (speed == 0) return "stationary";
int seconds = (int)((dist * 1000) / speed);
return (seconds / 60).ToString() + ":" + ((seconds % 60 < 10) ? "0" : "") + (seconds % 60).ToString() + " @ " + (throttle != 0 ? throttle + "%" : speed + " mglt");
}
Brush getDrawColor(MapData dat)
{
if (!passFilter(dat)) return Brushes.DarkSlateGray;
Brush brText = SystemBrushes.ControlText;
switch (dat.IFF)
{
case 0:
brText = Brushes.Lime; //LimeGreen;
break;
case 1:
brText = Brushes.Red; //Crimson;
break;
case 2:
brText = Brushes.DodgerBlue; //RoyalBlue;
break;
case 3:
if (_platform == Settings.Platform.XWING) brText = Brushes.RoyalBlue;
else if (_platform == Settings.Platform.TIE) brText = Brushes.MediumOrchid; // FF9932CC
else brText = Brushes.Yellow;
break;
case 4:
brText = Brushes.OrangeRed; //Red;
break;
case 5:
brText = Brushes.MediumOrchid; //DarkOrchid;
break;
}
return brText;
}
Color getIFFColor(int IFF)
{
switch (IFF)
{
case 0: return Color.Lime; // FF00FF00 //[JB] Changed colors (was LimeGreen)
case 1: return Color.Red; // FFFF0000 (was Crimson)
case 2: return Color.DodgerBlue; // FF1E90FF (was Royal Blue)
case 3:
if (_platform == Settings.Platform.XWING) return Color.RoyalBlue; // FF4169E1
else if (_platform == Settings.Platform.TIE) return Color.MediumOrchid; // FFBA55D3 (was DarkOrchid)
else return Color.Yellow; // FFFFFF00
case 4: return Color.OrangeRed; // FFFF4500 (was Red)
case 5:
return Color.MediumOrchid; // FFBA55D3
}
return Color.White; //Nothing should return this color.
}
/// <summary>Take the original image from the craft image strip and adds the RGB values from the craft IFF.</summary>
/// <param name="craftImage">The greyscale craft image.</param>
/// <param name="iff">The craft's IFF colors.</param>
/// <returns>Colorized craft image according to IFF.</returns>
static Bitmap applyIffMask(Bitmap craftImage, Color iff)
{
// craftImage comes in as 32bppRGB, but we force the image into 24bppRGB with LockBits
BitmapData bmData = GraphicsFunctions.GetBitmapData(craftImage, PixelFormat.Format24bppRgb);
byte[] pix = new byte[bmData.Stride * bmData.Height];
GraphicsFunctions.CopyImageToBytes(bmData, pix);
for (int y = 0; y < craftImage.Height; y++)
{
for (int x = 0, pos = bmData.Stride * y; x < craftImage.Width; x++)
{
// stupid thing returns BGR instead of RGB
// get intensity, apply to IFF mask
pix[pos + x * 3] = (byte)(pix[pos + x * 3] * iff.B / 255);
pix[pos + x * 3 + 1] = (byte)(pix[pos + x * 3 + 1] * iff.G / 255);
pix[pos + x * 3 + 2] = (byte)(pix[pos + x * 3 + 2] * iff.R / 255);
}
}
GraphicsFunctions.CopyBytesToImage(pix, bmData);
craftImage.UnlockBits(bmData);
craftImage.MakeTransparent(Color.Black);
return craftImage;
}
void moveMapToCursor()
{
int offx = (_clickPixelUp.X - _dragMapPrevious.X);
int offy = (_clickPixelUp.Y - _dragMapPrevious.Y);
switch (_displayMode)
{
case Orientation.XY:
_mapX += offx;
_mapY += offy;
break;
case Orientation.XZ:
_mapX += offx;
_mapZ += offy;
break;
case Orientation.YZ:
_mapY += offx;
_mapZ += offy;
break;
}
_dragMapPrevious = _clickPixelUp;
}
/// <summary>Retrieves the snap distance amount, in raw map units, as specified in the form control.</summary>
int getRawSnapAmount()
{
int snapAmount;
snapAmount = (cboSnapUnit.SelectedIndex == 0 ? (int)(numSnapAmount.Value * 160) : (int)numSnapAmount.Value); //km to Raw or already Raw
if (snapAmount < 1) snapAmount = 1;
return snapAmount;
}
/// <summary>Attempts to move a selected object or waypoint relative to its location.</summary>
void moveObject(SelectionData dat, int offsetX, int offsetY)
{
// If part of a larger move operation, it must be flagged as ready after the first frame of movement has been processed.
// This initializes the starting point so that snapping can work properly.
if (_dragMoveActive && !_dragMoveSnapReady)
{
dat.WpDragStartX = dat.WPRef.RawX;
dat.WpDragStartY = dat.WPRef.RawY;
dat.WpDragStartZ = dat.WPRef.RawZ;
}
else if (!_dragMoveActive) _dragMoveSnapReady = false;
int currentX = 0, currentY = 0;
switch (_displayMode)
{
case Orientation.XY:
currentX = (_dragMoveActive ? dat.WpDragStartX : dat.WPRef.RawX);
currentY = (_dragMoveActive ? dat.WpDragStartY : dat.WPRef.RawY);
break;
case Orientation.XZ:
currentX = (_dragMoveActive ? dat.WpDragStartX : dat.WPRef.RawX);
currentY = (_dragMoveActive ? dat.WpDragStartZ : dat.WPRef.RawZ);
break;
case Orientation.YZ:
currentX = (_dragMoveActive ? dat.WpDragStartY : dat.WPRef.RawY);
currentY = (_dragMoveActive ? dat.WpDragStartZ : dat.WPRef.RawZ);
break;
}
// Calculate new position based on snap settings.
// Integer division by the amount prevents any fractional values.
int newX = 0, newY = 0;
int snapAmount = getRawSnapAmount();
int snapType = (_dragMoveActive ? cboSnapTo.SelectedIndex : 0);
switch (snapType)
{
case 0: // No snap.
newX = currentX + offsetX;
newY = currentY + offsetY;
break;
case 1: // Snap to self.
newX = currentX + ((offsetX / snapAmount) * snapAmount);
newY = currentY + ((offsetY / snapAmount) * snapAmount);
break;
case 2: // Snap to grid.
newX = ((currentX + offsetX) / snapAmount) * snapAmount;
newY = ((currentY + offsetY) / snapAmount) * snapAmount;
break;
}
switch (_displayMode)
{
case Orientation.XY:
dat.WPRef.RawX = (short)newX;
dat.WPRef.RawY = (short)newY;
break;
case Orientation.XZ:
dat.WPRef.RawX = (short)newX;
dat.WPRef.RawZ = (short)newY;
break;
case Orientation.YZ:
dat.WPRef.RawY = (short)newX;
dat.WPRef.RawZ = (short)newY;
break;
}
// also apply the offset to the intial order WP
if (dat.WpOrder == 18) moveObject(new SelectionData(0, dat.MapDataRef, dat.WpIndex * 4 + 1, 0, dat.Region), offsetX, offsetY);
if (_platform == Settings.Platform.XWA && ((Platform.Xwa.FlightGroup)dat.MapDataRef.FlightGroup).CraftType == 85) processHyperPoints();
}
void moveSelectionToCursor()
{
int offsetX = _clickMapUp.X - _clickMapDown.X;
int offsetY = _clickMapUp.Y - _clickMapDown.Y;
foreach (SelectionData dat in _selectionList)
if (dat.Active && dat.WpOrder != 17) // do not move Exit points. Will update on next load
moveObject(dat, offsetX, offsetY);
_dragMoveSnapReady = true;
onDataModified?.Invoke(0, new EventArgs());
}
/// <summary>Processes a keyboard event to move all selected objects in a particular direction.</summary>
/// <remarks>Amount is derived from the snap amount if snapping is enabled (otherwise uses a default value), but snapping is always relative to self.</remarks>
void moveSelectionByKey(KeyEventArgs e, int directionX, int directionY)
{
if (_dragMoveActive || _selectionList.Count == 0) return;
int amount;
if (cboSnapTo.SelectedIndex > 0) amount = getRawSnapAmount();
else
{
amount = (int)(4 / Convert.ToDouble(_zoom) * 160);
if (_zoom < 1000 && amount < 2) amount = 2;
else if (amount < 1) amount = 1;
}
if (e.Shift)
{
amount /= 4;
if (amount < 1) amount = 1;
}
if (e.Control)
{
amount *= 4;
if (amount > 40) amount = 40;
}
int offsetX = amount * directionX;
int offsetY = amount * directionY;
foreach (SelectionData dat in _selectionList) if (dat.Active) moveObject(dat, offsetX, offsetY);
if (_selectionList.Count == 1)
{
SelectionData dat = _selectionList[0];
switch (_displayMode)
{
case Orientation.XY:
lblCoor1.Text = "New X: " + Math.Round(dat.WPRef.X, 2).ToString();
lblCoor2.Text = "New Y: " + Math.Round(dat.WPRef.Y, 2).ToString();
break;
case Orientation.XZ:
lblCoor1.Text = "New X: " + Math.Round(dat.WPRef.X, 2).ToString();
lblCoor2.Text = "New Z: " + Math.Round(dat.WPRef.Z, 2).ToString();
break;
case Orientation.YZ:
lblCoor1.Text = "New Y: " + Math.Round(dat.WPRef.Y, 2).ToString();
lblCoor2.Text = "New Z: " + Math.Round(dat.WPRef.Z, 2).ToString();
break;
}
}
onDataModified?.Invoke(0, new EventArgs());
MapPaint();
}
/// <summary>Moves all selected craft or waypoints into a different region. XWA only.</summary>
void moveSelectionToRegion(int region)
{
bool regionChanged = false, waypointCreated = false;
foreach (SelectionData dat in _selectionList)
{
if (dat.Active)
{
Platform.Xwa.FlightGroup.XwaWaypoint curWp = (Platform.Xwa.FlightGroup.XwaWaypoint)dat.WPRef;
if (dat.WpOrder == 0)
{
curWp.Region = Convert.ToByte(region);
if (dat.WpIndex == 0)
{
dat.MapDataRef.Region = region;
regionChanged = true;
}
}
else
{
int targetOrd = (int)(region * 4 + numOrder.Value);
Platform.Xwa.FlightGroup.Waypoint targWp = (Platform.Xwa.FlightGroup.Waypoint)_mapData[dat.MapDataIndex].WPs[targetOrd][dat.WpIndex];
targWp.RawX = curWp.RawX;
targWp.RawY = curWp.RawY;
targWp.RawZ = curWp.RawZ;
targWp.Enabled = curWp.Enabled;
curWp.Enabled = false;
waypointCreated = true;
}
}
}
if (regionChanged || waypointCreated)
{
deselect();
onDataModified?.Invoke(0, new EventArgs());
if (regionChanged) lstCraft.Refresh();
scheduleMapPaint();
}
}
/// <summary>Disables all selected waypoints and removes them from the selection list.</summary>
void disableSelectionWaypoints()
{
bool modified = false;
if (_selectionList.Count == 0) return;
// Work backwards because we have to remove from selection, and this disrupts the selection order.
for (int i = _selectionList.Count - 1; i >= 0; i--)
{
SelectionData dat = _selectionList[i];
if (dat.Active && (dat.WpIndex != 0 || dat.WpOrder != 0) && dat.WPRef.Enabled)
{
dat.WPRef.Enabled = false;
dat.Active = false;
setSelectionState(dat.MapDataIndex, false, dat.WpOrder, dat.WpIndex);
modified = true;
}
}
if (!modified) return;
updateSelectionListSize();
updatePreviousCraftSelection();
onDataModified?.Invoke(0, new EventArgs());
scheduleMapPaint();
}
/// <summary>Converts points to become top-left (<paramref name="a"/>) and bottom-right (<paramref name="b"/>)</summary>
void normalizePoint(ref Point a, ref Point b)
{
if (b.X < a.X)
{
int t = a.X;
a.X = b.X;
b.X = t;
}
if (b.Y < a.Y)
{
int t = a.Y;
a.Y = b.Y;
b.Y = t;
}
}
/// <summary>Determines if the craft meets the filter settings for Difficulty and IFF.</summary>
/// <remarks>Although this is similar to hiding, and many contexts check both, there are some distinctions in the usage.</remarks>
bool passFilter(MapData dat)
{
bool passDiff = (cboViewDifficulty.SelectedIndex == 0 || _platform == Settings.Platform.XWING || Convert.ToBoolean(dat.Difficulty & (1 << cboViewDifficulty.SelectedIndex - 1)));
bool passIff = (cboViewIff.SelectedIndex == 0 || dat.IFF == cboViewIff.SelectedIndex - 1);
return (passDiff && passIff);
}
/// <summary>Clears all selection states, lists, and resets their associated controls.</summary>
void deselect()
{
bool btemp = _ignoreSelectionEvents;
_ignoreSelectionEvents = true;
_previousCraftSelection = null;
lstCraft.ClearSelected();
_selectionList.Clear();
lstSelection.Items.Clear();
lstSelection.Visible = false;
lblSelection.Visible = false;
_ignoreSelectionEvents = btemp;
}
/// <summary>Returns true if the specified object parameters are in the current selection list, and active.</summary>
bool getSelectionState(int datIndex, int order, int wp)
{
foreach (SelectionData dat in _selectionList) if (dat.Active && dat.MapDataIndex == datIndex && dat.WpOrder == order && dat.WpIndex == wp) return true;
return false;
}
/// <summary>Adds or removes a selected object based on its parameters. Ensures the object is unique.</summary>
/// <remarks>Automatically maintains the list and its associated controls.</remarks>
void setSelectionState(int datIndex, bool state, int order, int wp)
{
if (datIndex < 0 || datIndex >= _mapData.Length) return;
bool btemp = _ignoreSelectionEvents;
_ignoreSelectionEvents = true;
if (!state)
{
int i = 0;
while (i < _selectionList.Count)
{
if (_selectionList[i].MapDataIndex == datIndex && _selectionList[i].WpOrder == order &&_selectionList[i].WpIndex == wp)
{
_selectionList.RemoveAt(i);
lstSelection.Items.RemoveAt(i);
if (order == 0 && wp == 0) lstCraft.SetSelected(datIndex, false);
}
else i++;
}
}
else
{
bool found = false;
foreach (SelectionData dat in _selectionList)
if (dat.MapDataIndex == datIndex && dat.WpOrder == order && dat.WpIndex == wp)
{
found = true;
break;
}
if (!found)
{
_selectionList.Add(new SelectionData(datIndex, _mapData[datIndex], order, wp, (byte)(numRegion.Value - 1)));
int chkIndex = (order > 0 ? 4 + wp : wp);
lstSelection.Items.Add(_mapData[datIndex].FullName + " " + chkWP[chkIndex].Text);
lstSelection.SetSelected(lstSelection.Items.Count - 1, true);
if (order == 0 && wp == 0) lstCraft.SetSelected(datIndex, true);
}
}
_ignoreSelectionEvents = btemp;
}
/// <summary>Selects objects via mouse click or bounding-box dragging.</summary>
void performSelection()
{
// Select anything inside the bounding box, or a single object if the user clicked on something.
// If the Control key is held down, add the bounding box to the current selection. Or toggle selection on a single object.
if (!ModifierKeys.HasFlag(Keys.Control)) deselect();
List<SelectionData> objects = new List<SelectionData>();
enumSelectionObjects(ref _clickPixelDown, ref _clickPixelUp, objects);
bool isClick = isPointClicked(ref _clickPixelDown, ref _clickPixelUp);
if (isClick && objects.Count > 1) objects.Sort(SelectionData.Compare);
foreach (SelectionData dat in objects)
{
setSelectionState(dat.MapDataIndex, (objects.Count != 1 || !ModifierKeys.HasFlag(Keys.Control) || !getSelectionState(dat.MapDataIndex, dat.WpOrder, dat.WpIndex)), dat.WpOrder, dat.WpIndex);
if (isClick) break;
}
updateSelectionListSize();
updatePreviousCraftSelection();
}
/// <summary>Populates a list of objects (craft or their waypoints) that can be selected inside a rectangle of two opposing points.</summary>
/// <remarks>Points are the physical pixel coordinates on the map. The points will be interpreted as either a click (similar or identical points) or a bounding-box.</remarks>
void enumSelectionObjects(ref Point p1, ref Point p2, List<SelectionData> output)
{
Point m1 = new Point();
Point m2 = new Point();
convertMousepointToWaypoint(p1.X, p1.Y, ref m1);
convertMousepointToWaypoint(p2.X, p2.Y, ref m2);
normalizePoint(ref m1, ref m2);
if (isPointClicked(ref p1, ref p2)) // Also handles normalizing.
{
double msX = 0.0, msY = 0.0;
switch (_displayMode)
{
case Orientation.XY:
msX = (p2.X - _mapX) / Convert.ToDouble(_zoom) * 160;
msY = (_mapY - p2.Y) / Convert.ToDouble(_zoom) * 160;
break;
case Orientation.XZ:
msX = (p2.X - _mapX) / Convert.ToDouble(_zoom) * 160;
msY = (_mapZ - p2.Y) / Convert.ToDouble(_zoom) * 160;
break;
case Orientation.YZ:
msX = (p2.X - _mapY) / Convert.ToDouble(_zoom) * 160;
msY = (_mapZ - p2.Y) / Convert.ToDouble(_zoom) * 160;
break;
}
int tolerance = (int)(10 / Convert.ToDouble(_zoom) * 160);
if (tolerance < 1) tolerance = 1; // For very high zoom levels.
m1.X = (int)msX - tolerance;
m1.Y = (int)msY - tolerance;
m2.X = (int)msX + tolerance;
m2.Y = (int)msY + tolerance;
}
int ord = 0;
int reg = 0;
if (_platform == Settings.Platform.XWA)
{
reg = (int)(numRegion.Value - 1);
ord = (int)(reg * 4 + numOrder.Value);
}
for (int i = 0; i < _mapData.Length; i++)
{
if (_mapData[i].View == Visibility.Hide || !passFilter(_mapData[i])) continue;
for (int wp = 0; wp < _mapData[i].WPs[0].Length; wp++)
{
if (!chkWP[wp].Checked || (!_mapData[i].WPs[0][wp].Enabled && _platform != Settings.Platform.XWA && wp != 0)) continue;
if (_platform == Settings.Platform.XWA)
{
Platform.Xwa.FlightGroup.XwaWaypoint cwp = (Platform.Xwa.FlightGroup.XwaWaypoint)_mapData[i].WPs[0][wp];
if (cwp.Region != reg) continue;
}
if (waypointInside(_mapData[i].WPs[0][wp], ref m1, ref m2)) output.Add(new SelectionData(i, _mapData[i], 0, wp, (byte)reg));
}
if (ord > 0)
{
for (int wp = 0; wp < _mapData[i].WPs[ord].Length; wp++)
{
if (!chkWP[4 + wp].Checked || !_mapData[i].WPs[ord][wp].Enabled) continue;
if (wp == 0 && _mapData[i].WPs[18][reg].Enabled && _mapData[i].WPs[0][0][4] != reg) continue;
if (waypointInside(_mapData[i].WPs[ord][wp], ref m1, ref m2)) output.Add(new SelectionData(i, _mapData[i], ord, wp, (byte)reg));
}
var hyp = _mapData[i].WPs[17][reg];
if (hyp.Enabled && waypointInside(hyp, ref m1, ref m2)) output.Add(new SelectionData(i, _mapData[i], 17, reg));
//TODO: need to add the order check here ^ since it's grabbing hidden WPs
// I think what would be better is to completely overhaul this; centralize the visibility, use that for selection and display
var exit = _mapData[i].WPs[18][reg];
if (exit.Enabled && waypointInside(exit, ref m1, ref m2))
{
if (_mapData[i].WPs[0][0][4] != reg && numOrder.Value == 1) output.Add(new SelectionData(i, _mapData[i], 18, reg, (byte)reg));
else
{
var fg = (Platform.Xwa.FlightGroup)_mapData[i].FlightGroup;
for (int o = 0; o < 4; o++)
if (fg.Orders[reg, o].Command == 50 && (o + 1) == numOrder.Value)
{
output.Add(new SelectionData(i, _mapData[i], 18, reg, (byte)reg));
break;
}
}
}
}
}
}
/// <summary>Returns true if two points are close enough to be considered a clicked point rather than a bounding box.</summary>