-
Notifications
You must be signed in to change notification settings - Fork 24
/
PreciseNode.cs
936 lines (850 loc) · 36.2 KB
/
PreciseNode.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
using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.IO;
/******************************************************************************
* Copyright (c) 2013-2014, Justin Bengtson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
namespace RegexKSP {
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class PreciseNode : MonoBehaviour {
public PluginConfiguration config;
private PNOptions options = new PNOptions();
private NodeManager curState = new NodeManager();
private List<Action> scheduledForLayout = new List<Action>();
private bool configLoaded = false;
private bool conicsLoaded = false;
private bool shown = true;
private bool showTimeNext = false;
private bool waitForKey = false;
private bool showOptions = false;
private bool showKeymapper = false;
private bool showEncounter = false;
private byte currentWaitKey = 255;
private double keyWaitTime = 0.0;
/// <summary>
/// Overridden function from MonoBehavior
/// </summary>
public void Awake() {
CancelInvoke();
loadConfig();
}
/// <summary>
/// Overridden function from MonoBehavior
/// </summary>
public void OnDisable() {
saveConfig();
}
/// <summary>
/// Overridden function from MonoBehavior
/// </summary>
public void Update() {
if(!FlightDriver.Pause && canShowNodeEditor) {
PatchedConicSolver solver = NodeTools.getSolver();
if(solver.maneuverNodes.Count > 0) {
if(!curState.hasNode() || !solver.maneuverNodes.Contains(curState.node)) {
// get the first one if we can't find the current or it's null
curState = new NodeManager(solver.maneuverNodes[0]);
} else if(curState.hasNode()) {
curState.updateNode();
curState = curState.nextState();
}
} else {
if(curState.hasNode()) {
curState = new NodeManager();
curState.resizeClockWindow = true;
}
}
processKeyInput();
}
}
public void FixedUpdate() {
if(!FlightDriver.Pause) {
PatchedConicSolver p = NodeTools.getSolver();
if(options.removeUsedNodes && p.maneuverNodes.Count > 0) {
ManeuverNode node = p.maneuverNodes[0];
if(node.GetBurnVector(FlightGlobals.ActiveVessel.Orbit).magnitude < options.usedNodeThreshold) {
solver.RemoveManeuverNode(node);
//TODO: Clean up states after removing the node.
}
}
}
}
/// <summary>
/// Overridden function from MonoBehavior
/// </summary>
public void OnGUI() {
// Porcess any scheduled functions
if(Event.current.type == EventType.Layout && !FlightDriver.Pause && scheduledForLayout.Count > 0) {
foreach(Action a in scheduledForLayout) {
a();
}
scheduledForLayout.Clear();
}
if(canShowNodeEditor) {
if(Event.current.type == EventType.Layout && !FlightDriver.Pause) {
// On layout we should see if we have nodes to act on.
if(curState.resizeMainWindow) {
options.mainWindowPos.height = 250;
}
if(curState.resizeClockWindow) {
options.clockWindowPos.height = 65;
}
showEncounter = curState.encounter;
// this prevents the clock window from showing the time to
// next node when the next state is created during repaint.
showTimeNext = curState.hasNode();
}
if(!conicsLoaded) {
NodeTools.changeConicsMode(options.conicsMode);
conicsLoaded = true;
}
if(shown) {
drawGUI();
} else if(canShowConicsWindow) {
drawConicsGUI();
}
} else if(canShowConicsWindow) {
drawConicsGUI();
}
if(canShowClock) {
drawClockGUI();
}
}
/// <summary>
/// Draw Node Editor and Options GUI
/// </summary>
public void drawGUI() {
GUI.skin = null;
options.mainWindowPos = GUILayout.Window(21349, options.mainWindowPos, drawMainWindow, "Precise Node", GUILayout.ExpandHeight(true));
if(showOptions) {
options.optionsWindowPos = GUILayout.Window(21350, options.optionsWindowPos, drawOptionsWindow, "Precise Node Options", GUILayout.ExpandHeight(true));
}
if(showKeymapper) {
options.keymapperWindowPos = GUILayout.Window(21351, options.keymapperWindowPos, drawKeymapperWindow, "Precise Node Keys", GUILayout.ExpandHeight(true));
}
if(options.showTrip) {
options.tripWindowPos = GUILayout.Window(21352, options.tripWindowPos, drawTripWindow, "Trip Info", GUILayout.ExpandHeight(true));
}
}
/// <summary>
/// Draw Clock GUI
/// </summary>
public void drawClockGUI() {
GUI.skin = null;
options.clockWindowPos = GUILayout.Window(21353, options.clockWindowPos, drawClockWindow, "Clock", GUILayout.ExpandHeight(true));
}
/// <summary>
/// Draw Conics GUI
/// </summary>
public void drawConicsGUI() {
GUI.skin = null;
options.conicsWindowPos = GUILayout.Window(21354, options.conicsWindowPos, drawConicsWindow, "Conics Controls", GUILayout.ExpandHeight(true));
}
/// <summary>
/// Draws the Node Editor window.
/// </summary>
/// <param name="id">Identifier.</param>
public void drawMainWindow(int id) {
Color defaultColor = GUI.backgroundColor;
Color contentColor = GUI.contentColor;
Color curColor = defaultColor;
PatchedConicSolver solver = NodeTools.getSolver();
// Options button
if(showOptions) { GUI.backgroundColor = Color.green; }
if(GUI.Button(new Rect(options.mainWindowPos.width - 48, 2, 22, 18), "O")) {
showOptions = !showOptions;
}
GUI.backgroundColor = defaultColor;
// Keymapping button
if(showKeymapper) { GUI.backgroundColor = Color.green; }
if(GUI.Button(new Rect(options.mainWindowPos.width - 24, 2, 22, 18), "K")) {
showKeymapper = !showKeymapper;
}
GUI.backgroundColor = defaultColor;
GUILayout.BeginVertical();
if(options.showManeuverPager) {
GUIParts.drawManeuverPager(curState);
}
// Human-readable time
GUIParts.drawDoubleLabel("Time:", 100, NodeTools.convertUTtoHumanTime(curState.currentUT()), 130);
// Increment buttons
GUILayout.BeginHorizontal();
GUILayout.Label("Increment:", GUILayout.Width(100));
GUIParts.drawButton("0.01", (options.increment == 0.01?Color.yellow:defaultColor), delegate() { options.increment = 0.01; });
GUIParts.drawButton("0.1", (options.increment == 0.1?Color.yellow:defaultColor), delegate() { options.increment = 0.1; });
GUIParts.drawButton("1", (options.increment == 1?Color.yellow:defaultColor), delegate() { options.increment = 1; });
GUIParts.drawButton("10", (options.increment == 10?Color.yellow:defaultColor), delegate() { options.increment = 10; });
GUIParts.drawButton("100", (options.increment == 100?Color.yellow:defaultColor), delegate() { options.increment = 100; });
GUILayout.EndHorizontal();
drawTimeControls(contentColor);
drawProgradeControls(contentColor);
drawNormalControls(contentColor);
drawRadialControls(contentColor);
// total delta-V display
GUIParts.drawDoubleLabel("Total delta-V:", 100, curState.currentMagnitude().ToString("0.##") + "m/s", 130);
drawEAngle();
drawEncounter(defaultColor);
// Conics mode controls
GUILayout.BeginHorizontal();
GUILayout.Label("Conics mode: ", GUILayout.Width(100));
GUIParts.drawButton("0", (options.conicsMode == 0?Color.yellow:defaultColor), delegate() { options.setConicsMode(0); });
GUIParts.drawButton("1", (options.conicsMode == 1?Color.yellow:defaultColor), delegate() { options.setConicsMode(1); });
GUIParts.drawButton("2", (options.conicsMode == 2?Color.yellow:defaultColor), delegate() { options.setConicsMode(2); });
GUIParts.drawButton("3", (options.conicsMode == 3?Color.yellow:defaultColor), delegate() { options.setConicsMode(3); });
GUIParts.drawButton("4", (options.conicsMode == 4?Color.yellow:defaultColor), delegate() { options.setConicsMode(4); });
GUILayout.EndHorizontal();
// conics patch limit editor.
GUILayout.BeginHorizontal();
GUILayout.Label("Change Conics Samples", GUILayout.Width(200));
GUIParts.drawButton("-", Color.red, delegate() { solver.DecreasePatchLimit(); });
GUIParts.drawButton("+", Color.red, delegate() { solver.IncreasePatchLimit(); });
GUILayout.EndHorizontal();
// trip info button and vessel focus buttons
GUILayout.BeginHorizontal();
GUIParts.drawButton("Trip Info", (options.showTrip?Color.yellow:defaultColor), delegate() { options.showTrip = !options.showTrip; });
GUIParts.drawButton("Focus on Vessel", defaultColor, delegate() { MapView.MapCamera.SetTarget(FlightGlobals.ActiveVessel.vesselName); });
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUI.DragWindow();
}
// debugging function
private void drawEAngle() {
// Ejection angle
if(options.showEAngle) {
String eangle = "n/a";
if(FlightGlobals.ActiveVessel.orbit.referenceBody.name != "Sun") {
eangle = NodeTools.getEjectionAngle(FlightGlobals.ActiveVessel.orbit, curState.currentUT()).ToString("0.##") + "°";
}
GUIParts.drawDoubleLabel("Ejection Angle:", 100, eangle, 130);
}
}
// debugging function
private void drawEncounter(Color defaultColor) {
// Additional Information
if(options.showOrbitInfo) {
// Find the next encounter, if any, in our flight plan.
if(showEncounter) {
Orbit nextEnc = NodeTools.findNextEncounter(curState.node);
string name = "N/A";
string PeA = "N/A";
if(nextEnc != null) {
name = nextEnc.referenceBody.name;
PeA = NodeTools.formatMeters(nextEnc.PeA);
} else {
curState.encounter = false;
}
// Next encounter periapsis
GUIParts.drawDoubleLabel("(" + name + ") Pe:", 100, PeA, 130);
GUILayout.BeginHorizontal();
GUILayout.Label("", GUILayout.Width(100));
GUIParts.drawButton("Focus on " + name, defaultColor, delegate() { MapView.MapCamera.SetTarget(name); });
GUILayout.EndHorizontal();
} else {
if(curState.node.solver.flightPlan.Count > 1) {
// output the apoapsis and periapsis of our projected orbit.
GUIParts.drawDoubleLabel("Apoapsis:", 100, NodeTools.formatMeters(curState.node.nextPatch.ApA), 100);
GUIParts.drawDoubleLabel("Periapsis:", 100, NodeTools.formatMeters(curState.node.nextPatch.PeA), 130);
}
}
}
}
// debugging function
private void drawTimeControls(Color contentColor) {
// Universal time controls
GUILayout.BeginHorizontal();
GUILayout.Label((options.largeUTIncrement?"UT: (10x inc)":"UT:"), GUILayout.Width(100));
GUI.backgroundColor = Color.green;
if(!curState.timeParsed) {
GUI.contentColor = Color.red;
}
string check = GUILayout.TextField(curState.timeText, GUILayout.Width(100));
if(!curState.timeText.Equals(check, StringComparison.Ordinal)) {
curState.setUT(check);
}
GUI.contentColor = contentColor;
double ut_increment = options.increment * (options.largeUTIncrement ? 10.0 : 1.0);
GUIParts.drawButton("-", Color.red, delegate() { curState.addUT(ut_increment * -1.0); });
GUIParts.drawButton("+", Color.green, delegate() { curState.addUT(ut_increment); });
GUILayout.EndHorizontal();
// extended time controls
if(options.showUTControls) {
GUILayout.BeginHorizontal();
GUIParts.drawButton("Peri", Color.yellow, delegate() { curState.setPeriapsis(); });
GUIParts.drawButton("DN", Color.magenta, delegate() {
Orbit targ = NodeTools.getTargetOrbit();
if(targ != null) {
curState.setUT(NodeTools.getTargetDNUT(curState.node.patch, targ));
} else {
curState.setUT(NodeTools.getEquatorialDNUT(curState.node.patch));
}
});
if (options.largeUTIncrement) {
GUIParts.drawButton("-Orb", Color.red, delegate() { curState.addUT(-curState.node.patch.period); });
GUIParts.drawButton("+Orb", Color.green, delegate() { curState.addUT(curState.node.patch.period); });
} else {
GUIParts.drawButton("-1K", Color.red, delegate() { curState.addUT(-1000); });
GUIParts.drawButton("+1K", Color.green, delegate() { curState.addUT(1000); });
}
GUIParts.drawButton("AN", Color.cyan, delegate() {
Orbit targ = NodeTools.getTargetOrbit();
if(targ != null) {
curState.setUT(NodeTools.getTargetANUT(curState.node.patch, targ));
} else {
curState.setUT(NodeTools.getEquatorialANUT(curState.node.patch));
}
});
GUIParts.drawButton("Apo", Color.blue, delegate() { curState.setApoapsis(); });
GUILayout.EndHorizontal();
}
}
// debugging function
private void drawProgradeControls(Color contentColor) {
// Prograde controls
GUILayout.BeginHorizontal();
GUILayout.Label("Prograde:", GUILayout.Width(100));
if(!curState.progradeParsed) {
GUI.contentColor = Color.red;
}
string check = GUILayout.TextField(curState.progradeText, GUILayout.Width(100));
if(!curState.progradeText.Equals(check, StringComparison.Ordinal)) {
curState.setPrograde(check);
}
GUI.contentColor = contentColor;
GUIParts.drawButton("-", Color.red, delegate() { curState.addPrograde(options.increment * -1.0); });
GUIParts.drawButton("+", Color.green, delegate() { curState.addPrograde(options.increment); });
GUILayout.EndHorizontal();
}
// debugging function
private void drawNormalControls(Color contentColor) {
// Normal controls
GUILayout.BeginHorizontal();
GUILayout.Label("Normal:", GUILayout.Width(100));
if(!curState.normalParsed) {
GUI.contentColor = Color.red;
}
string check = GUILayout.TextField(curState.normalText, GUILayout.Width(100));
if(!curState.normalText.Equals(check, StringComparison.Ordinal)) {
curState.setNormal(check);
}
GUI.contentColor = contentColor;
GUIParts.drawButton("-", Color.red, delegate() { curState.addNormal(options.increment * -1.0); });
GUIParts.drawButton("+", Color.green, delegate() { curState.addNormal(options.increment); });
GUILayout.EndHorizontal();
}
// debugging function
private void drawRadialControls(Color contentColor) {
// radial controls
GUILayout.BeginHorizontal();
GUILayout.Label("Radial:", GUILayout.Width(100));
if(!curState.radialParsed) {
GUI.contentColor = Color.red;
}
string check = GUILayout.TextField(curState.radialText, GUILayout.Width(100));
if(!curState.radialText.Equals(check, StringComparison.Ordinal)) {
curState.setRadial(check);
}
GUI.contentColor = contentColor;
GUIParts.drawButton("-", Color.red, delegate() { curState.addRadial(options.increment * -1.0); });
GUIParts.drawButton("+", Color.green, delegate() { curState.addRadial(options.increment); });
GUILayout.EndHorizontal();
}
/// <summary>
/// Draws the Clock window.
/// </summary>
/// <param name="id">Identifier.</param>
public void drawClockWindow(int id) {
Color defaultColor = GUI.backgroundColor;
double timeNow = Planetarium.GetUniversalTime();
String timeUT = timeNow.ToString("F0");
String timeHuman = NodeTools.convertUTtoHumanTime(timeNow);
GUILayout.BeginVertical();
GUIParts.drawDoubleLabel("Time:", 35, timeHuman, 150);
GUIParts.drawDoubleLabel("UT:", 35, Math.Floor(timeNow).ToString("F0"), 150);
if(showTimeNext) {
double next = 0.0;
string labelText = "";
if(NodeTools.getSolver().maneuverNodes.Count > 0) {
// protection from index out of range errors.
// should probably handle this better.
next = timeNow - NodeTools.getSolver().maneuverNodes[0].UT;
}
if(next < 0) {
labelText = "T- " + NodeTools.convertUTtoHumanDuration(next);
} else {
labelText = "T+ " + NodeTools.convertUTtoHumanDuration(next);
}
GUIParts.drawDoubleLabel("Next:", 35, labelText, 150);
}
GUILayout.EndVertical();
GUI.DragWindow();
}
/// <summary>
/// Draws the Conics window.
/// </summary>
/// <param name="id">Identifier.</param>
public void drawConicsWindow(int id) {
PatchedConicSolver solver = NodeTools.getSolver();
Color defaultColor = GUI.backgroundColor;
GUILayout.BeginVertical();
// Conics mode controls
GUILayout.BeginHorizontal();
GUILayout.Label("Conics mode: ", GUILayout.Width(100));
GUIParts.drawButton("0", (options.conicsMode == 0?Color.yellow:defaultColor), delegate() { options.setConicsMode(0); });
GUIParts.drawButton("1", (options.conicsMode == 1?Color.yellow:defaultColor), delegate() { options.setConicsMode(1); });
GUIParts.drawButton("2", (options.conicsMode == 2?Color.yellow:defaultColor), delegate() { options.setConicsMode(2); });
GUIParts.drawButton("3", (options.conicsMode == 3?Color.yellow:defaultColor), delegate() { options.setConicsMode(3); });
GUIParts.drawButton("4", (options.conicsMode == 4?Color.yellow:defaultColor), delegate() { options.setConicsMode(4); });
GUILayout.EndHorizontal();
// conics patch limit editor.
GUILayout.BeginHorizontal();
GUILayout.Label("Change Conics Samples", GUILayout.Width(200));
GUIParts.drawButton("-", Color.red, delegate() { solver.DecreasePatchLimit(); });
GUIParts.drawButton("+", Color.red, delegate() { solver.IncreasePatchLimit(); });
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUI.DragWindow();
}
/// <summary>
/// Draws the Options window.
/// </summary>
/// <param name="id">Identifier.</param>
public void drawOptionsWindow(int id) {
Color defaultColor = GUI.backgroundColor;
// Close button
if(GUI.Button(new Rect(options.optionsWindowPos.width - 24, 2, 22, 18), "X")) {
showOptions = false;
}
GUILayout.BeginVertical();
options.showConicsAlways = GUILayout.Toggle(options.showConicsAlways, "Always Show Conics Controls");
options.showClock = GUILayout.Toggle(options.showClock, "Show Clock");
// use a temp variable so we can check whether the main window needs resizing.
bool temp = GUILayout.Toggle(options.showManeuverPager, "Show Maneuver Pager");
if(temp != options.showManeuverPager) {
options.showManeuverPager = temp;
curState.resizeMainWindow = true;
}
temp = GUILayout.Toggle(options.showUTControls, "Show Additional UT Controls");
if(temp != options.showUTControls) {
options.showUTControls = temp;
curState.resizeMainWindow = true;
}
options.largeUTIncrement = GUILayout.Toggle(options.largeUTIncrement, "Use 10x UT increment");
temp = GUILayout.Toggle(options.showEAngle, "Show Ejection Angle");
if(temp != options.showEAngle) {
options.showEAngle = temp;
curState.resizeMainWindow = true;
}
temp = GUILayout.Toggle(options.showOrbitInfo, "Show Orbit Information");
if(temp != options.showOrbitInfo) {
options.showOrbitInfo = temp;
curState.resizeMainWindow = true;
}
options.removeUsedNodes = GUILayout.Toggle(options.removeUsedNodes, "Remove Used Nodes");
//TODO: Add threshold controls for removing used nodes
GUILayout.EndVertical();
GUI.DragWindow();
}
/// <summary>
/// Draws the Keymapper window.
/// </summary>
/// <param name="id">Identifier.</param>
public void drawKeymapperWindow(int id) {
Color defaultColor = GUI.backgroundColor;
// Close button
if(GUI.Button(new Rect(options.keymapperWindowPos.width - 24, 2, 22, 18), "X")) {
showKeymapper = false;
}
GUILayout.BeginVertical();
// Set window control
GUILayout.BeginHorizontal();
GUILayout.Label("Hide/Show Window: " + options.hideWindow.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Hide/Show Window...", (byte)KEYS.HIDEWINDOW); });
GUILayout.EndHorizontal();
// Set add node widget button
GUILayout.BeginHorizontal();
GUILayout.Label("Open Node Gizmo: " + options.addWidget.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Open Node Gizmo...", (byte)KEYS.ADDWIDGET); });
GUILayout.EndHorizontal();
// Set prograde controls
GUILayout.BeginHorizontal();
GUILayout.Label("Increment Prograde: " + options.progInc.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Increment Prograde...", (byte)KEYS.PROGINC); });
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Decrement Prograde: " + options.progDec.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Decrement Prograde...", (byte)KEYS.PROGDEC); });
GUILayout.EndHorizontal();
// set normal controls
GUILayout.BeginHorizontal();
GUILayout.Label("Increment Normal: " + options.normInc.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Increment Normal...", (byte)KEYS.NORMINC); });
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Decrement Normal: " + options.normDec.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Decrement Normal...", (byte)KEYS.NORMDEC); });
GUILayout.EndHorizontal();
// set radial controls
GUILayout.BeginHorizontal();
GUILayout.Label("Increment Radial: " + options.radiInc.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Increment Radial...", (byte)KEYS.RADIINC); });
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Decrement Radial: " + options.radiDec.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Decrement Radial...", (byte)KEYS.RADIDEC); });
GUILayout.EndHorizontal();
// set time controls
GUILayout.BeginHorizontal();
GUILayout.Label("Increment Time: " + options.timeInc.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Increment Time...", (byte)KEYS.TIMEINC); });
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Decrement Time: " + options.timeDec.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Decrement Time...", (byte)KEYS.TIMEDEC); });
GUILayout.EndHorizontal();
// set paging controls
GUILayout.BeginHorizontal();
GUILayout.Label("Page Increment: " + options.pageIncrement.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Page Increment...", (byte)KEYS.PAGEINC); });
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Page Conics: " + options.pageConics.ToString(), GUILayout.Width(200));
GUIParts.drawButton("set", defaultColor, delegate() { doWaitForKey("Press a key to bind Page Conics Mode...", (byte)KEYS.PAGECON); });
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUI.DragWindow();
}
public void drawTripWindow(int id) {
PatchedConicSolver solver = NodeTools.getSolver();
GUILayout.BeginVertical();
if(solver.maneuverNodes.Count < 1) {
GUILayout.BeginHorizontal();
GUILayout.Label("No nodes to show.", GUILayout.Width(200));
GUILayout.EndHorizontal();
} else {
double total = 0.0;
double timeNow = Planetarium.GetUniversalTime();
GUILayout.BeginHorizontal();
GUILayout.Label("", GUILayout.Width(60));
GUILayout.Label("Δv", GUILayout.Width(90));
GUILayout.Label("Time Until", GUILayout.Width(200));
GUILayout.Label("", GUILayout.Width(120));
GUILayout.EndHorizontal();
foreach(ManeuverNode curNode in solver.maneuverNodes) {
int idx = solver.maneuverNodes.IndexOf(curNode);
double timeDiff = curNode.UT - timeNow;
GUILayout.BeginHorizontal();
GUILayout.Label("Node " + idx, GUILayout.Width(60));
GUILayout.Label(curNode.DeltaV.magnitude.ToString("F2") + "m/s", GUILayout.Width(90));
GUILayout.Label(NodeTools.convertUTtoHumanDuration(timeDiff), GUILayout.Width(200));
// these will be scheduled for during the next layout pass
if(idx > 0) {
GUIParts.drawButton("merge ▲", Color.white, delegate() {scheduledForLayout.Add(new Action(() => {NodeTools.mergeNodeDown(solver.maneuverNodes[idx]);}));});
}
GUILayout.EndHorizontal();
total += curNode.DeltaV.magnitude;
}
GUILayout.BeginHorizontal();
GUILayout.Label("Total", GUILayout.Width(60));
GUILayout.Label(total.ToString("F2") + "m/s", GUILayout.Width(90));
GUILayout.Label("", GUILayout.Width(200));
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
GUI.DragWindow();
}
/// <summary>
/// Returns whether the Node Editor can be shown based on a number of global factors.
/// </summary>
/// <value><c>true</c> if the Node Editor can be shown; otherwise, <c>false</c>.</value>
private bool canShowNodeEditor {
get {
return FlightGlobals.fetch != null && FlightGlobals.ActiveVessel != null && MapView.MapIsEnabled && NodeTools.getSolver().maneuverNodes.Count > 0;
}
}
/// <summary>
/// Returns whether the Conics Window can be shown based on a number of global factors.
/// </summary>
/// <value><c>true</c> if the Conics Window can be shown; otherwise, <c>false</c>.</value>
private bool canShowConicsWindow {
get {
return FlightGlobals.fetch != null && FlightGlobals.ActiveVessel != null && MapView.MapIsEnabled && options.showConicsAlways;
}
}
/// <summary>
/// Returns whether the Clock Window can be shown based on a number of global factors.
/// </summary>
/// <value><c>true</c> if the Clock Window can be shown; otherwise, <c>false</c>.</value>
private bool canShowClock {
get {
return FlightGlobals.fetch != null && FlightGlobals.ActiveVessel != null && RenderingManager.fetch.enabled && options.showClock;
}
}
private void doWaitForKey(String msg, byte key) {
ScreenMessages.PostScreenMessage(msg, 5.0f, ScreenMessageStyle.UPPER_CENTER);
waitForKey = true;
currentWaitKey = key;
keyWaitTime = Planetarium.GetUniversalTime();
}
/// <summary>
/// Processes keyboard input.
/// </summary>
private void processKeyInput() {
if(!Input.anyKeyDown) {
return;
}
// Fix for a bug in Linux where typing would still control game elements even if
// a textbox was focused.
if(GUIUtility.keyboardControl != 0) {
return;
}
// process any key input for settings
if(waitForKey) {
KeyCode key = NodeTools.fetchKey();
// if the time is up or we have no key to process, reset.
if(((keyWaitTime + 5.0) < Planetarium.GetUniversalTime()) || key == KeyCode.None) {
currentWaitKey = 255;
waitForKey = false;
return;
}
// which key are we waiting for?
switch(currentWaitKey) {
case (byte)KEYS.PROGINC:
options.progInc = key;
break;
case (byte)KEYS.PROGDEC:
options.progDec = key;
break;
case (byte)KEYS.NORMINC:
options.normInc = key;
break;
case (byte)KEYS.NORMDEC:
options.normDec = key;
break;
case (byte)KEYS.RADIINC:
options.radiInc = key;
break;
case (byte)KEYS.RADIDEC:
options.radiDec = key;
break;
case (byte)KEYS.TIMEINC:
options.timeInc = key;
break;
case (byte)KEYS.TIMEDEC:
options.timeDec = key;
break;
case (byte)KEYS.PAGEINC:
options.pageIncrement = key;
break;
case (byte)KEYS.PAGECON:
options.pageConics = key;
break;
case (byte)KEYS.HIDEWINDOW:
options.hideWindow = key;
break;
case (byte)KEYS.ADDWIDGET:
options.addWidget = key;
break;
}
currentWaitKey = 255;
waitForKey = false;
return;
}
// process normal keyboard input
// change increment
if(Input.GetKeyDown(options.pageIncrement)) {
if(Event.current.alt) {
options.downIncrement();
} else {
options.upIncrement();
}
}
// prograde increment
if(Input.GetKeyDown(options.progInc)) {
curState.addPrograde(options.increment);
}
// prograde decrement
if(Input.GetKeyDown(options.progDec)) {
curState.addPrograde(options.increment * -1.0);
}
// normal increment
if(Input.GetKeyDown(options.normInc)) {
curState.addNormal(options.increment);
}
// normal decrement
if(Input.GetKeyDown(options.normDec)) {
curState.addNormal(options.increment * -1.0);
}
// radial increment
if(Input.GetKeyDown(options.radiInc)) {
curState.addRadial(options.increment);
}
// radial decrement
if(Input.GetKeyDown(options.radiDec)) {
curState.addRadial(options.increment * -1.0);
}
// UT increment
if(Input.GetKeyDown(options.timeInc)) {
curState.addUT(options.increment * (options.largeUTIncrement ? 10.0 : 1.0));
}
// UT decrement
if(Input.GetKeyDown(options.timeDec)) {
curState.addUT(options.increment * (options.largeUTIncrement ? -10.0 : -1.0));
}
// Page Conics
if(Input.GetKeyDown(options.pageConics)) {
options.pageConicsMode();
}
// hide/show window
if(Input.GetKeyDown(options.hideWindow)) {
shown = !shown;
}
// open node gizmo
if(Input.GetKeyDown(options.addWidget)) {
NodeTools.CreateNodeGizmo(curState.node);
}
}
/// <summary>
/// Load any saved configuration from file.
/// </summary>
private void loadConfig() {
Debug.Log("Loading PreciseNode settings.");
if(!configLoaded) {
config = KSP.IO.PluginConfiguration.CreateForType<PreciseNode>(null);
config.load();
configLoaded = true;
try {
options.conicsMode = config.GetValue<int>("conicsMode", 3);
options.mainWindowPos.x = config.GetValue<int>("mainWindowX", Screen.width / 10);
options.mainWindowPos.y = config.GetValue<int>("mainWindowY", 20);
options.optionsWindowPos.x = config.GetValue<int>("optWindowX", Screen.width / 3);
options.optionsWindowPos.y = config.GetValue<int>("optWindowY", 20);
options.keymapperWindowPos.x = config.GetValue<int>("keyWindowX", Screen.width / 5);
options.keymapperWindowPos.y = config.GetValue<int>("keyWindowY", 20);
options.clockWindowPos.x = config.GetValue<int>("clockWindowX", Screen.width / 3);
options.clockWindowPos.y = config.GetValue<int>("clockWindowY", Screen.height / 2);
options.conicsWindowPos.x = config.GetValue<int>("conicsWindowX", Screen.width / 5);
options.conicsWindowPos.y = config.GetValue<int>("conicsWindowY", Screen.height / 2);
options.tripWindowPos.x = config.GetValue<int>("tripWindowX", Screen.width / 5);
options.tripWindowPos.y = config.GetValue<int>("tripWindowY", Screen.height / 5);
options.showClock = config.GetValue<bool>("showClock", false);
options.showEAngle = config.GetValue<bool>("showEAngle", true);
options.showConicsAlways = config.GetValue<bool>("showConicsAlways", false);
options.showOrbitInfo = config.GetValue<bool>("showOrbitInfo", false);
options.showUTControls = config.GetValue<bool>("showUTControls", false);
options.showManeuverPager = config.GetValue<bool>("showManeuverPager", true);
options.removeUsedNodes = config.GetValue<bool>("removeUsedNodes", false);
options.usedNodeThreshold = config.GetValue<double>("usedNodeThreshold", 0.5);
options.largeUTIncrement = config.GetValue<bool>("largeUTIncrement", false);
string temp = config.GetValue<String>("progInc", "Keypad8");
options.progInc = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("progDec", "Keypad5");
options.progDec = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("normInc", "Keypad9");
options.normInc = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("normDec", "Keypad7");
options.normDec = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("radiInc", "Keypad6");
options.radiInc = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("radiDec", "Keypad4");
options.radiDec = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("timeInc", "Keypad3");
options.timeInc = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("timeDec", "Keypad1");
options.timeDec = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("pageIncrement", "Keypad0");
options.pageIncrement = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("pageConics", "KeypadEnter");
options.pageConics = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("hideWindow", "P");
options.hideWindow = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
temp = config.GetValue<String>("addWidget", "O");
options.addWidget = (KeyCode)Enum.Parse(typeof(KeyCode), temp);
} catch(ArgumentException) {
// do nothing here, the defaults are already set
}
}
}
/// <summary>
/// Save our configuration to file.
/// </summary>
private void saveConfig() {
Debug.Log("Saving PreciseNode settings.");
if(!configLoaded) {
config = KSP.IO.PluginConfiguration.CreateForType<PreciseNode>(null);
}
config["conicsMode"] = options.conicsMode;
config["progInc"] = options.progInc.ToString();
config["progDec"] = options.progDec.ToString();
config["normInc"] = options.normInc.ToString();
config["normDec"] = options.normDec.ToString();
config["radiInc"] = options.radiInc.ToString();
config["radiDec"] = options.radiDec.ToString();
config["timeInc"] = options.timeInc.ToString();
config["timeDec"] = options.timeDec.ToString();
config["pageIncrement"] = options.pageIncrement.ToString();
config["pageConics"] = options.pageConics.ToString();
config["hideWindow"] = options.hideWindow.ToString();
config["addWidget"] = options.addWidget.ToString();
config["mainWindowX"] = (int)options.mainWindowPos.x;
config["mainWindowY"] = (int)options.mainWindowPos.y;
config["optWindowX"] = (int)options.optionsWindowPos.x;
config["optWindowY"] = (int)options.optionsWindowPos.y;
config["keyWindowX"] = (int)options.keymapperWindowPos.x;
config["keyWindowY"] = (int)options.keymapperWindowPos.y;
config["clockWindowX"] = (int)options.clockWindowPos.x;
config["clockWindowY"] = (int)options.clockWindowPos.y;
config["conicsWindowX"] = (int)options.conicsWindowPos.x;
config["conicsWindowY"] = (int)options.conicsWindowPos.y;
config["tripWindowX"] = (int)options.tripWindowPos.x;
config["tripWindowY"] = (int)options.tripWindowPos.y;
config["showClock"] = options.showClock;
config["showEAngle"] = options.showEAngle;
config["showConicsAlways"] = options.showConicsAlways;
config["showOrbitInfo"] = options.showOrbitInfo;
config["showUTControls"] = options.showUTControls;
config["showManeuverPager"] = options.showManeuverPager;
config["removeUsedNodes"] = options.removeUsedNodes;
config["usedNodeThreshold"] = options.usedNodeThreshold;
config["largeUTIncrement"] = options.largeUTIncrement;
config.save();
}
private enum KEYS : byte {
PROGINC,
PROGDEC,
NORMINC,
NORMDEC,
RADIINC,
RADIDEC,
TIMEINC,
TIMEDEC,
PAGEINC,
PAGECON,
HIDEWINDOW,
ADDWIDGET
};
}
}