This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
/
KCT_GUI.cs
1921 lines (1771 loc) · 92.9 KB
/
KCT_GUI.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Reflection;
using KSP.UI.Screens;
namespace KerbalConstructionTime
{
public static partial class KCT_GUI
{
public static bool showMainGUI, showEditorGUI, showSOIAlert, showLaunchAlert, showTimeRemaining,
showBuildList, showClearLaunch, showShipRoster, showCrewSelect, showSettings, showUpgradeWindow,
showBLPlus, showRename, showFirstRun, showLaunchSiteSelector;
public static bool clicked = false;
public static GUIDataSaver guiDataSaver = new GUIDataSaver();
private static bool unlockEditor;
private static Vector2 scrollPos;
private static Rect iconPosition = new Rect(Screen.width / 4, Screen.height - 30, 50, 30);//110
private static Rect mainWindowPosition = new Rect(Screen.width / 3.5f, Screen.height / 3.5f, 350, 200);
public static Rect editorWindowPosition = new Rect(Screen.width / 3.5f, Screen.height / 3.5f, 275, 135);
private static Rect SOIAlertPosition = new Rect(Screen.width / 3, Screen.height / 3, 250, 100);
public static Rect centralWindowPosition = new Rect((Screen.width - 150) / 2, (Screen.height - 50) / 2, 150, 50);
//private static Rect launchAlertPosition = new Rect((Screen.width-75)/2, (Screen.height-100)/2, 150, 100);
public static Rect timeRemainingPosition = new Rect((Screen.width - 90) / 4, Screen.height - 85, 90, 55);
public static Rect buildListWindowPosition = new Rect(Screen.width - 400, 40, 400, 1);
private static Rect crewListWindowPosition = new Rect((Screen.width - 360) / 2, (Screen.height / 4), 360, 1);
private static Rect settingsPosition = new Rect((3 * Screen.width / 8), (Screen.height / 4), 300, 1);
private static Rect upgradePosition = new Rect((Screen.width - 260) / 2, (Screen.height / 4), 260, 1);
private static Rect bLPlusPosition = new Rect(Screen.width - 500, 40, 100, 1);
public static GUISkin windowSkin;// = HighLogic.UISkin;// = new GUIStyle(HighLogic.Skin.window);
//public static UISkinDef windowSkin;
private static bool isKSCLocked = false, isEditorLocked = false;
public delegate bool boolDelegatePCMString(ProtoCrewMember pcm, string partName);
public static boolDelegatePCMString AvailabilityChecker;
public static bool UseAvailabilityChecker = false;
private static List<GameScenes> validScenes = new List<GameScenes> { GameScenes.FLIGHT, GameScenes.EDITOR, GameScenes.SPACECENTER, GameScenes.TRACKSTATION };
public static void SetGUIPositions()
{
GUISkin oldSkin = GUI.skin;
if (HighLogic.LoadedScene == GameScenes.SPACECENTER && windowSkin == null)
windowSkin = GUI.skin;
GUI.skin = windowSkin;
if (validScenes.Contains(HighLogic.LoadedScene)) //&& KCT_GameStates.settings.enabledForSave)//!(HighLogic.CurrentGame.Mode == Game.Modes.SANDBOX && !KCT_GameStates.settings.SandboxEnabled))
{
/*if (!ToolbarManager.ToolbarAvailable && GUI.Button(iconPosition, "KCT", GUI.skin.button))
{
onClick();
}*/
if (ToolbarManager.ToolbarAvailable && KCT_GameStates.kctToolbarButton != null)
{
KCT_GameStates.kctToolbarButton.TexturePath = KCT_Utilities.GetButtonTexture(); //Set texture, allowing for flashing of icon.
}
if (showSettings)
//settingsPosition = GUILayout.Window(8955, settingsPosition, KCT_GUI.DrawSettings, "KCT Settings", HighLogic.Skin.window);
presetPosition = GUILayout.Window(8955, presetPosition, KCT_GUI.DrawPresetWindow, "KCT Settings", HighLogic.Skin.window);
if (!KCT_PresetManager.Instance.ActivePreset.generalSettings.Enabled)
return;
if (showMainGUI)
mainWindowPosition = GUILayout.Window(8950, mainWindowPosition, KCT_GUI.DrawMainGUI, "Kerbal Construction Time", HighLogic.Skin.window);
if (showEditorGUI)
editorWindowPosition = GUILayout.Window(8953, editorWindowPosition, KCT_GUI.DrawEditorGUI, "Kerbal Construction Time", HighLogic.Skin.window);
if (showSOIAlert)
SOIAlertPosition = GUILayout.Window(8951, SOIAlertPosition, KCT_GUI.DrawSOIAlertWindow, "SOI Change", HighLogic.Skin.window);
if (showLaunchAlert)
centralWindowPosition = GUILayout.Window(8951, centralWindowPosition, KCT_GUI.DrawLaunchAlert, "KCT", HighLogic.Skin.window);
if (showBuildList)
buildListWindowPosition = GUILayout.Window(8950, buildListWindowPosition, KCT_GUI.DrawBuildListWindow, "Build List", HighLogic.Skin.window);
if (showClearLaunch)
centralWindowPosition = GUILayout.Window(8952, centralWindowPosition, KCT_GUI.DrawClearLaunch, "Launch site not clear!", HighLogic.Skin.window);
if (showShipRoster)
crewListWindowPosition = GUILayout.Window(8955, crewListWindowPosition, KCT_GUI.DrawShipRoster, "Select Crew", HighLogic.Skin.window);
if (showCrewSelect)
crewListWindowPosition = GUILayout.Window(8954, crewListWindowPosition, KCT_GUI.DrawCrewSelect, "Select Crew", HighLogic.Skin.window);
if (showUpgradeWindow)
upgradePosition = GUILayout.Window(8952, upgradePosition, KCT_GUI.DrawUpgradeWindow, "Upgrades", HighLogic.Skin.window);
if (showBLPlus)
bLPlusPosition = GUILayout.Window(8953, bLPlusPosition, KCT_GUI.DrawBLPlusWindow, "Options", HighLogic.Skin.window);
if (showRename)
centralWindowPosition = GUILayout.Window(8954, centralWindowPosition, KCT_GUI.DrawRenameWindow, "Rename", HighLogic.Skin.window);
if (showFirstRun)
centralWindowPosition = GUILayout.Window(8954, centralWindowPosition, KCT_GUI.DrawFirstRun, "Kerbal Construction Time", HighLogic.Skin.window);
if (showPresetSaver)
presetNamingWindowPosition = GUILayout.Window(8952, presetNamingWindowPosition, KCT_GUI.DrawPresetSaveWindow, "Save as New Preset", HighLogic.Skin.window);
if (showLaunchSiteSelector)
centralWindowPosition = GUILayout.Window(8952, centralWindowPosition, DrawLaunchSiteChooser, "Select Site", HighLogic.Skin.window);
if (unlockEditor)
{
EditorLogic.fetch.Unlock("KCTGUILock");
unlockEditor = false;
}
//Disable KSC things when certain windows are shown.
if (showFirstRun || showRename || showUpgradeWindow || showSettings || showCrewSelect || showShipRoster || showClearLaunch)
{
if (!isKSCLocked)
{
InputLockManager.SetControlLock(ControlTypes.KSC_FACILITIES, "KCTKSCLock");
isKSCLocked = true;
}
}
else //if (!showBuildList)
{
if (isKSCLocked)
{
InputLockManager.RemoveControlLock("KCTKSCLock");
isKSCLocked = false;
}
}
GUI.skin = oldSkin;
}
}
public static bool PrimarilyDisabled { get { return (KCT_PresetManager.PresetLoaded() && (!KCT_PresetManager.Instance.ActivePreset.generalSettings.Enabled || !KCT_PresetManager.Instance.ActivePreset.generalSettings.BuildTimes)); } }
private static void CheckKSCLock()
{
//On mouseover code for build list inspired by Engineer's editor mousover code
Vector2 mousePos = Input.mousePosition;
mousePos.y = Screen.height - mousePos.y;
if (HighLogic.LoadedScene == GameScenes.SPACECENTER && !isKSCLocked)
{
if ((showBuildList && buildListWindowPosition.Contains(mousePos)) || (showBLPlus && bLPlusPosition.Contains(mousePos)))
{
InputLockManager.SetControlLock(ControlTypes.KSC_FACILITIES, "KCTKSCLock");
isKSCLocked = true;
}
//KCTDebug.Log("KSC Locked");
}
else if (HighLogic.LoadedScene == GameScenes.SPACECENTER && isKSCLocked)
{
if (!(showBuildList && buildListWindowPosition.Contains(mousePos)) && !(showBLPlus && bLPlusPosition.Contains(mousePos)))
{
InputLockManager.RemoveControlLock("KCTKSCLock");
isKSCLocked = false;
}
//KCTDebug.Log("KSC UnLocked");
}
}
private static void CheckEditorLock()
{
//On mouseover code for editor inspired by Engineer's editor mousover code
Vector2 mousePos = Input.mousePosition;
mousePos.y = Screen.height - mousePos.y;
if ((showEditorGUI && editorWindowPosition.Contains(mousePos)) && !isEditorLocked)
{
EditorLogic.fetch.Lock(true, false, true, "KCTEditorMouseLock");
isEditorLocked = true;
//KCTDebug.Log("KSC Locked");
}
else if (!(showEditorGUI && editorWindowPosition.Contains(mousePos)) && isEditorLocked)
{
EditorLogic.fetch.Unlock("KCTEditorMouseLock");
isEditorLocked = false;
//KCTDebug.Log("KSC UnLocked");
}
}
public static void ClickOff()
{
KCTDebug.Log("ClickOff");
clicked = false;
onClick();
}
public static void ClickOn()
{
KCTDebug.Log("ClickOn");
clicked = true;
onClick();
}
public static void ClickToggle()
{
clicked = !clicked;
onClick();
}
public static void onClick()
{
// clicked = !clicked;
if (ToolbarManager.ToolbarAvailable && KCT_GameStates.kctToolbarButton != null)
if (KCT_GameStates.kctToolbarButton.Important) KCT_GameStates.kctToolbarButton.Important = false;
/* if (!KCT_GameStates.settings.enabledForSave)
{
ShowSettings();
return;
}*/
if (PrimarilyDisabled && (HighLogic.LoadedScene == GameScenes.SPACECENTER))
{
if (clicked)
ShowSettings();
else
showSettings = false;
}
else if (HighLogic.LoadedScene == GameScenes.FLIGHT && !PrimarilyDisabled)
{
//showMainGUI = !showMainGUI;
buildListWindowPosition.height = 1;
showBuildList = clicked;
showBLPlus = false;
//listWindow = -1;
ResetBLWindow();
}
else if ((HighLogic.LoadedScene == GameScenes.EDITOR) && !PrimarilyDisabled)
{
editorWindowPosition.height = 1;
showEditorGUI = clicked;
KCT_GameStates.showWindows[1] = showEditorGUI;
}
else if ((HighLogic.LoadedScene == GameScenes.SPACECENTER || HighLogic.LoadedScene == GameScenes.TRACKSTATION) && !PrimarilyDisabled)
{
buildListWindowPosition.height = 1;
showBuildList = clicked;
showBLPlus = false;
//listWindow = -1;
ResetBLWindow();
KCT_GameStates.showWindows[0] = showBuildList;
}
if (!KCT_GameStates.settings.PreferBlizzyToolbar)
{
if (KCT_Events.instance != null && KCT_Events.instance.KCTButtonStock != null)
{
if (showBuildList || showSettings || showEditorGUI)
{
KCT_Events.instance.KCTButtonStock.SetTrue(false);
}
else
{
KCT_Events.instance.KCTButtonStock.SetFalse(false);
}
}
}
}
public static void onHoverOn()
{
KCTDebug.Log("onHoverOn: Clicked = " + clicked);
if (!PrimarilyDisabled)
{
if (HighLogic.LoadedScene == GameScenes.SPACECENTER || HighLogic.LoadedSceneIsFlight)
{
if (!showBuildList)
ResetBLWindow();
showBuildList = true;
}
}
}
public static void onHoverOff()
{
KCTDebug.Log("onHoverOff: Clicked = " + clicked);
if (!PrimarilyDisabled && !clicked)
{
if (HighLogic.LoadedScene == GameScenes.SPACECENTER || HighLogic.LoadedSceneIsFlight)
{
showBuildList = false;
}
}
}
public static void hideAll()
{
showEditorGUI = false;
showLaunchAlert = false;
showMainGUI = false;
showSOIAlert = false;
showTimeRemaining = false;
showBuildList = false;
showClearLaunch = false;
showShipRoster = false;
showCrewSelect = false;
showSettings = false;
showUpgradeWindow = false;
showBLPlus = false;
showRename = false;
showFirstRun = false;
showPresetSaver = false;
showLaunchSiteSelector = false;
//ClickOff();
/* if (!KCT_GameStates.settings.PreferBlizzyToolbar)
{
if (KCT_Events.instance != null && KCT_Events.instance.KCTButtonStock != null)
{
KCT_Events.instance.KCTButtonStock.SetFalse(false);
}
}*/
clicked = false;
//VABSelected = false;
//SPHSelected = false;
//TechSelected = false;
//listWindow = -1;
ResetBLWindow();
}
public static void DrawMainGUI(int windowID) //Deprecated to all hell now I think
{
//GUIStyle mySty = new GUIStyle(GUI.skin.button);
//mySty.normal.textColor = mySty.focused.textColor = Color.white;
//mySty.hover.textColor = mySty.active.textColor = Color.yellow;
//mySty.onNormal.textColor = mySty.onFocused.textColor = mySty.onHover.textColor = mySty.onActive.textColor = Color.green;
//mySty.padding = new RectOffset(16, 16, 8, 8);
//sets the layout for the GUI, which is pretty much just some debug stuff for me.
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
//GUILayout.Label("#Parts", GUILayout.ExpandHeight(true));
GUILayout.Label("Build Time (s)", GUILayout.ExpandHeight(true));
GUILayout.Label("Build Time Remaining: ", GUILayout.ExpandHeight(true));
GUILayout.Label("UT: ", GUILayout.ExpandHeight(true));
if (GUILayout.Button("Warp until ready."))
{
// if (FlightGlobals.ActiveVessel.id != KCT_GameStates.activeVessel.vessel.id)
{
// FlightGlobals.SetActiveVessel(KCT_GameStates.activeVessel.vessel);
}
KCT_GameStates.canWarp = true;
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
//GUILayout.Label(KCT_GameStates.activeVessel.vessel.Parts.Count.ToString(), GUILayout.ExpandHeight(true));
// GUILayout.Label(KCT_GameStates.activeVessel.buildTime.ToString(), GUILayout.ExpandHeight(true));
//GUILayout.Label(KCT_Utilities.GetFormatedTime(KCT_GameStates.activeVessel.finishDate - KCT_GameStates.UT), GUILayout.ExpandHeight(true));
// GUILayout.Label(MagiCore.Utilities.GetFormattedTime(KCT_GameStates.activeVessel.buildTime - KCT_GameStates.activeVessel.progress), GUILayout.ExpandHeight(true));
GUILayout.Label(MagiCore.Utilities.GetFormattedTime(KCT_GameStates.UT).ToString(), GUILayout.ExpandHeight(true));
if (GUILayout.Button("Stop warp"))
{
KCT_GameStates.canWarp = false;
TimeWarp.SetRate(0, true);
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
if (!Input.GetMouseButtonDown(1) && !Input.GetMouseButtonDown(2))
GUI.DragWindow();
}
public static bool showInventory = false, useInventory = false;
//private static string currentCategoryString = "NONE";
//private static int currentCategoryInt = -1;
public static string buildRateForDisplay;
private static int rateIndexHolder = 0;
public static Dictionary<string, int> PartsInUse = new Dictionary<string, int>();
private static double finishedShipBP = -1;
public static void DrawEditorGUI(int windowID)
{
if (EditorLogic.fetch == null)
{
return;
}
if (editorWindowPosition.width < 275) //the size keeps getting changed for some reason, so this will avoid that
{
editorWindowPosition.width = 275;
editorWindowPosition.height = 1;
}
GUILayout.BeginVertical();
//GUILayout.Label("Current KSC: " + KCT_GameStates.ActiveKSC.KSCName);
if (!KCT_GameStates.EditorShipEditingMode) //Build mode
{
double buildTime = KCT_GameStates.EditorBuildTime;
KCT_BuildListVessel.ListType type = EditorLogic.fetch.launchSiteName == "LaunchPad" ? KCT_BuildListVessel.ListType.VAB : KCT_BuildListVessel.ListType.SPH;
//GUILayout.Label("Total Build Points (BP):", GUILayout.ExpandHeight(true));
//GUILayout.Label(Math.Round(buildTime, 2).ToString(), GUILayout.ExpandHeight(true));
GUILayout.BeginHorizontal();
GUILayout.Label("Build Time at ");
if (buildRateForDisplay == null) buildRateForDisplay = KCT_Utilities.GetBuildRate(0, type, null).ToString();
buildRateForDisplay = GUILayout.TextField(buildRateForDisplay, GUILayout.Width(75));
GUILayout.Label(" BP/s:");
List<double> rates = new List<double>();
if (type == KCT_BuildListVessel.ListType.VAB) rates = KCT_Utilities.BuildRatesVAB(null);
else rates = KCT_Utilities.BuildRatesSPH(null);
double bR;
if (double.TryParse(buildRateForDisplay, out bR))
{
if (GUILayout.Button("*", GUILayout.ExpandWidth(false)))
{
rateIndexHolder = (rateIndexHolder + 1) % rates.Count;
bR = rates[rateIndexHolder];
if (bR > 0)
buildRateForDisplay = bR.ToString();
else
{
rateIndexHolder = (rateIndexHolder + 1) % rates.Count;
bR = rates[rateIndexHolder];
buildRateForDisplay = bR.ToString();
}
}
GUILayout.EndHorizontal();
GUILayout.Label(MagiCore.Utilities.GetFormattedTime(buildTime / bR));
}
else
{
GUILayout.EndHorizontal();
GUILayout.Label("Invalid Build Rate");
}
if (KCT_GameStates.EditorRolloutCosts > 0)
GUILayout.Label("Rollout Cost: " + Math.Round(KCT_GameStates.EditorRolloutCosts, 1));
//bool useHolder = useInventory;
//useInventory = GUILayout.Toggle(useInventory, " Use parts from inventory?");
//if (useInventory != useHolder) KCT_Utilities.RecalculateEditorBuildTime(EditorLogic.fetch.ship);
if (!KCT_GameStates.settings.OverrideLaunchButton)
{
GUILayout.BeginHorizontal();
if (GUILayout.Button("Build"))
{
KCT_Utilities.AddVesselToBuildList();
//SwitchCurrentPartCategory();
KCT_Utilities.RecalculateEditorBuildTime(EditorLogic.fetch.ship);
}
GUILayout.EndHorizontal();
}
if (GUILayout.Button("Show/Hide Build List"))
{
showBuildList = !showBuildList;
}
//if (GUILayout.Button("Part Inventory"))
//{
// showInventory = !showInventory;
// editorWindowPosition.width = 275;
// editorWindowPosition.height = 135;
//}
}
else //Edit mode
{
if (showInventory) //The part inventory is not shown in the editor mode
{
showInventory = false;
editorWindowPosition.width = 275;
editorWindowPosition.height = 1;
}
KCT_BuildListVessel ship = KCT_GameStates.editedVessel;
if (finishedShipBP < 0 && ship.isFinished)
finishedShipBP = KCT_Utilities.GetBuildTime(ship.ExtractedPartNodes);
double origBP = ship.isFinished ? finishedShipBP : ship.buildPoints; //If the ship is finished, recalculate times. Else, use predefined times.
double buildTime = KCT_GameStates.EditorBuildTime;
double difference = Math.Abs(buildTime - origBP);
double progress;
if (ship.isFinished) progress = origBP;
else progress = ship.progress;
double newProgress = Math.Max(0, progress - (1.1 * difference));
//GUILayout.Label("Original: " + Math.Max(0, Math.Round(progress, 2)) + "/" + Math.Round(origBP, 2) + " BP (" + Math.Max(0, Math.Round(100 * (progress / origBP), 2)) + "%)");
GUILayout.Label("Original: " + Math.Max(0, Math.Round(100 * (progress / origBP), 2)) + "%");
//GUILayout.Label("Edited: " + Math.Round(newProgress, 2) + "/" + Math.Round(buildTime, 2) + " BP (" + Math.Round(100 * newProgress / buildTime, 2) + "%)");
GUILayout.Label("Edited: " + Math.Round(100 * newProgress / buildTime, 2) + "%");
KCT_BuildListVessel.ListType type = EditorLogic.fetch.launchSiteName == "LaunchPad" ? KCT_BuildListVessel.ListType.VAB : KCT_BuildListVessel.ListType.SPH;
GUILayout.BeginHorizontal();
GUILayout.Label("Build Time at ");
if (buildRateForDisplay == null) buildRateForDisplay = KCT_Utilities.GetBuildRate(0, type, null).ToString();
buildRateForDisplay = GUILayout.TextField(buildRateForDisplay, GUILayout.Width(75));
GUILayout.Label(" BP/s:");
List<double> rates = new List<double>();
if (ship.type == KCT_BuildListVessel.ListType.VAB) rates = KCT_Utilities.BuildRatesVAB(null);
else rates = KCT_Utilities.BuildRatesSPH(null);
double bR;
if (double.TryParse(buildRateForDisplay, out bR))
{
if (GUILayout.Button("*", GUILayout.ExpandWidth(false)))
{
rateIndexHolder = (rateIndexHolder + 1) % rates.Count;
bR = rates[rateIndexHolder];
buildRateForDisplay = bR.ToString();
}
GUILayout.EndHorizontal();
GUILayout.Label(MagiCore.Utilities.GetFormattedTime(Math.Abs(buildTime - newProgress) / bR));
}
else
{
GUILayout.EndHorizontal();
GUILayout.Label("Invalid Build Rate");
}
GUILayout.BeginHorizontal();
if (GUILayout.Button("Save Edits"))
{
finishedShipBP = -1;
KCT_Utilities.AddFunds(ship.cost, TransactionReasons.VesselRollout);
KCT_BuildListVessel newShip = KCT_Utilities.AddVesselToBuildList();
if (newShip == null)
{
KCT_Utilities.SpendFunds(ship.cost, TransactionReasons.VesselRollout);
return;
}
ship.RemoveFromBuildList();
newShip.progress = newProgress;
KCTDebug.Log("Finished? " + ship.isFinished);
if (ship.isFinished)
newShip.cannotEarnScience = true;
GamePersistence.SaveGame("persistent", HighLogic.SaveFolder, SaveMode.OVERWRITE);
KCT_GameStates.EditorShipEditingMode = false;
InputLockManager.RemoveControlLock("KCTEditExit");
InputLockManager.RemoveControlLock("KCTEditLoad");
InputLockManager.RemoveControlLock("KCTEditNew");
InputLockManager.RemoveControlLock("KCTEditLaunch");
EditorLogic.fetch.Unlock("KCTEditorMouseLock");
KCTDebug.Log("Edits saved.");
HighLogic.LoadScene(GameScenes.SPACECENTER);
}
if (GUILayout.Button("Cancel Edits"))
{
KCTDebug.Log("Edits cancelled.");
finishedShipBP = -1;
KCT_GameStates.EditorShipEditingMode = false;
InputLockManager.RemoveControlLock("KCTEditExit");
InputLockManager.RemoveControlLock("KCTEditLoad");
InputLockManager.RemoveControlLock("KCTEditNew");
InputLockManager.RemoveControlLock("KCTEditLaunch");
EditorLogic.fetch.Unlock("KCTEditorMouseLock");
ScrapYardWrapper.ProcessVessel(KCT_GameStates.editedVessel.ExtractedPartNodes);
HighLogic.LoadScene(GameScenes.SPACECENTER);
}
GUILayout.EndHorizontal();
if (GUILayout.Button("Fill Tanks"))
{
foreach (Part p in EditorLogic.fetch.ship.parts)
{
//fill as part prefab would be filled?
if (KCT_Utilities.PartIsProcedural(p))
{
foreach (PartResource rsc in p.Resources)
{
rsc.amount = rsc.maxAmount;
}
}
else
{
foreach (PartResource rsc in p.Resources)
{
PartResource templateRsc = p.partInfo.partPrefab.Resources.FirstOrDefault(r => r.resourceName == rsc.resourceName);
if (templateRsc != null)
rsc.amount = templateRsc.amount;
}
}
}
}
}
GUILayout.EndVertical();
if (!Input.GetMouseButtonDown(1) && !Input.GetMouseButtonDown(2))
GUI.DragWindow();
CheckEditorLock();
ClampWindow(ref editorWindowPosition, strict: false);
}
public static void DrawSOIAlertWindow(int windowID)
{
GUILayout.BeginVertical();
GUILayout.Label(" Warp stopped due to SOI change.", GUILayout.ExpandHeight(true));
GUILayout.Label("Vessel name: " + KCT_GameStates.lastSOIVessel, GUILayout.ExpandHeight(true));
if (GUILayout.Button("Close"))
{
showSOIAlert = false;
}
GUILayout.EndVertical();
if (!Input.GetMouseButtonDown(1) && !Input.GetMouseButtonDown(2))
GUI.DragWindow();
}
public static void DrawLaunchAlert(int windowID)
{
GUILayout.BeginVertical();
if (GUILayout.Button("Build" + (KCT_GameStates.settings.WindowMode != 1 ? " Vessel" : "")))
{
KCT_Utilities.AddVesselToBuildList();
//SwitchCurrentPartCategory();
KCT_Utilities.RecalculateEditorBuildTime(EditorLogic.fetch.ship);
showLaunchAlert = false;
unlockEditor = true;
KCT_GUI.centralWindowPosition.width = 150;
}
if (GUILayout.Button("Cancel"))
{
showLaunchAlert = false;
centralWindowPosition.height = 1;
unlockEditor = true;
KCT_GUI.centralWindowPosition.width = 150;
}
GUILayout.EndVertical();
if (KCT_GameStates.settings.WindowMode != 1)
CenterWindow(ref centralWindowPosition);
}
public static void ResetBLWindow(bool deselectList = true)
{
buildListWindowPosition.height = 1;
buildListWindowPosition.width = 500;
if (deselectList)
SelectList("None");
// listWindow = -1;
}
private static void ScrapVessel()
{
InputLockManager.RemoveControlLock("KCTPopupLock");
//List<KCT_BuildListVessel> buildList = b.
KCT_BuildListVessel b = KCT_Utilities.FindBLVesselByID(IDSelected);// = listWindow == 0 ? KCT_GameStates.VABList[IndexSelected] : KCT_GameStates.SPHList[IndexSelected];
if (b == null)
{
KCTDebug.Log("Tried to remove a vessel that doesn't exist!");
return;
}
KCTDebug.Log("Scrapping " + b.shipName);
if (!b.isFinished)
{
List<ConfigNode> parts = b.ExtractedPartNodes;
//double costCompleted = 0;
//foreach (ConfigNode p in parts)
//{
// costCompleted += KCT_Utilities.GetPartCostFromNode(p);
//}
//costCompleted = (costCompleted * b.ProgressPercent() / 100);
b.RemoveFromBuildList();
//only add parts that were already a part of the inventory
if (ScrapYardWrapper.Available)
{
List<ConfigNode> partsToReturn = new List<ConfigNode>();
foreach (ConfigNode partNode in parts)
{
if (ScrapYardWrapper.PartIsFromInventory(partNode))
{
partsToReturn.Add(partNode);
}
}
if (partsToReturn.Any())
{
ScrapYardWrapper.AddPartsToInventory(partsToReturn, false);
}
}
}
else
{
b.RemoveFromBuildList();
//add parts to inventory
ScrapYardWrapper.AddPartsToInventory(b.ExtractedPartNodes, false); //don't count as a recovery
}
ScrapYardWrapper.SetProcessedStatus(ScrapYardWrapper.GetPartID(b.ExtractedPartNodes[0]), false);
KCT_Utilities.AddFunds(b.cost, TransactionReasons.VesselRollout);
}
public static void DummyVoid() { InputLockManager.RemoveControlLock("KCTPopupLock"); }
private static bool IsCrewable(List<Part> ship)
{
foreach (Part p in ship)
if (p.CrewCapacity > 0) return true;
return false;
}
private static int FirstCrewable(List<Part> ship)
{
for (int i = 0; i < ship.Count; i++)
{
Part p = ship[i];
//Debug.Log(p.partInfo.name+":"+p.CrewCapacity);
if (p.CrewCapacity > 0) return i;
}
return -1;
}
public static void DrawClearLaunch(int windowID)
{
GUILayout.BeginVertical();
if (GUILayout.Button("Recover Flight and Proceed"))
{
List<ProtoVessel> list = ShipConstruction.FindVesselsLandedAt(HighLogic.CurrentGame.flightState, KCT_GameStates.launchedVessel.launchSite);
foreach (ProtoVessel pv in list)
ShipConstruction.RecoverVesselFromFlight(pv, HighLogic.CurrentGame.flightState);
if (!IsCrewable(KCT_GameStates.launchedVessel.ExtractedParts))
KCT_GameStates.launchedVessel.Launch();
else
{
showClearLaunch = false;
centralWindowPosition.height = 1;
AssignInitialCrew();
showShipRoster = true;
}
centralWindowPosition.height = 1;
}
if (GUILayout.Button("Cancel"))
{
showClearLaunch = false;
centralWindowPosition.height = 1;
}
GUILayout.EndVertical();
CenterWindow(ref centralWindowPosition);
}
/// <summary>
/// Assigns the initial crew to the roster, based on desired roster in the editor
/// </summary>
public static void AssignInitialCrew()
{
KCT_GameStates.launchedCrew.Clear();
pseudoParts = KCT_GameStates.launchedVessel.GetPseudoParts();
parts = KCT_GameStates.launchedVessel.ExtractedParts;
KCT_GameStates.launchedCrew = new List<CrewedPart>();
foreach (PseudoPart pp in pseudoParts)
KCT_GameStates.launchedCrew.Add(new CrewedPart(pp.uid, new List<ProtoCrewMember>()));
//try to assign kerbals from the desired manifest
if (!UseAvailabilityChecker && KCT_GameStates.launchedVessel.DesiredManifest?.Count > 0 && KCT_GameStates.launchedVessel.DesiredManifest.Exists(c => c != null))
{
KCTDebug.Log("Assigning desired crew manifest.");
List<ProtoCrewMember> available = GetAvailableCrew(string.Empty);
Queue<ProtoCrewMember> finalCrew = new Queue<ProtoCrewMember>();
//try to assign crew from the desired manifest
foreach (string name in KCT_GameStates.launchedVessel.DesiredManifest)
{
//assign the kerbal with that name to each seat, in order. Let's try that
ProtoCrewMember crew = null;
if (!string.IsNullOrEmpty(name))
{
crew = available.Find(c => c.name.Equals(name, StringComparison.CurrentCultureIgnoreCase));
if (crew != null && crew.rosterStatus != ProtoCrewMember.RosterStatus.Available) //only take those that are available
{
crew = null;
}
}
finalCrew.Enqueue(crew);
}
//check if any of these crew are even available, if not then go back to CrewFirstAvailable
if (finalCrew.FirstOrDefault(c => c != null) == null)
{
KCTDebug.Log("Desired crew not available, falling back to default.");
CrewFirstAvailable();
return;
}
//Put the crew where they belong
for (int i = 0; i < parts.Count; i++)
{
Part part = parts[i];
for (int seat = 0; seat < part.CrewCapacity; seat++)
{
if (finalCrew.Count > 0)
{
ProtoCrewMember crewToInsert = finalCrew.Dequeue();
KCTDebug.Log("Assigning " + (crewToInsert?.name ?? "null"));
KCT_GameStates.launchedCrew[i].crewList.Add(crewToInsert); //even add the nulls, then they should match 1 to 1
}
}
}
}
else
{
CrewFirstAvailable();
}
}
private static int partIndexToCrew;
private static int indexToCrew;
//private static List<String> partNames;
private static List<PseudoPart> pseudoParts;
private static List<Part> parts;
public static bool randomCrew, autoHire;
public static List<ProtoCrewMember> AvailableCrew;
public static List<ProtoCrewMember> PossibleCrewForPart = new List<ProtoCrewMember>();
public static void DrawShipRoster(int windowID)
{
System.Random rand = new System.Random();
GUILayout.BeginVertical(GUILayout.ExpandHeight(true), GUILayout.MaxHeight(Screen.height / 2));
GUILayout.BeginHorizontal();
randomCrew = GUILayout.Toggle(randomCrew, " Randomize Filling");
autoHire = GUILayout.Toggle(autoHire, " Auto-Hire Applicants");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (AvailableCrew == null)
{
AvailableCrew = GetAvailableCrew(string.Empty);
}
if (GUILayout.Button("Fill All"))
{
//foreach (AvailablePart p in KCT_GameStates.launchedVessel.GetPartNames())
for (int j = 0; j < parts.Count; j++)
{
Part p = parts[j];//KCT_Utilities.GetAvailablePartByName(KCT_GameStates.launchedVessel.GetPartNames()[j]).partPrefab;
if (p.CrewCapacity > 0)
{
if (UseAvailabilityChecker)
{
PossibleCrewForPart.Clear();
foreach (ProtoCrewMember pcm in AvailableCrew)
if (AvailabilityChecker(pcm, p.partInfo.name))
PossibleCrewForPart.Add(pcm);
}
else
PossibleCrewForPart = AvailableCrew;
//if (!KCT_GameStates.launchedCrew.Keys.Contains(p.uid))
//KCT_GameStates.launchedCrew.Add(new List<ProtoCrewMember>());
for (int i = 0; i < p.CrewCapacity; i++)
{
if (KCT_GameStates.launchedCrew[j].crewList.Count <= i)
{
if (PossibleCrewForPart.Count > 0)
{
int index = randomCrew ? new System.Random().Next(PossibleCrewForPart.Count) : 0;
ProtoCrewMember crewMember = PossibleCrewForPart[index];
if (crewMember != null)
{
KCT_GameStates.launchedCrew[j].crewList.Add(crewMember);
PossibleCrewForPart.RemoveAt(index);
if (PossibleCrewForPart != AvailableCrew)
AvailableCrew.Remove(crewMember);
}
}
else if (autoHire)
{
if (HighLogic.CurrentGame.CrewRoster.Applicants.Count() == 0)
HighLogic.CurrentGame.CrewRoster.GetNextApplicant();
int index = randomCrew ? rand.Next(HighLogic.CurrentGame.CrewRoster.Applicants.Count() - 1) : 0;
ProtoCrewMember hired = HighLogic.CurrentGame.CrewRoster.Applicants.ElementAt(index);
HighLogic.CurrentGame.CrewRoster.HireApplicant(hired);
List<ProtoCrewMember> activeCrew;
activeCrew = KCT_GameStates.launchedCrew[j].crewList;
if (activeCrew.Count > i)
{
activeCrew.Insert(i, hired);
if (activeCrew[i + 1] == null)
activeCrew.RemoveAt(i + 1);
}
else
{
for (int k = activeCrew.Count; k < i; k++)
{
activeCrew.Insert(k, null);
}
activeCrew.Insert(i, hired);
}
KCT_GameStates.launchedCrew[j].crewList = activeCrew;
}
}
else if (KCT_GameStates.launchedCrew[j].crewList[i] == null)
{
if (PossibleCrewForPart.Count > 0)
{
int index = randomCrew ? new System.Random().Next(PossibleCrewForPart.Count) : 0;
ProtoCrewMember crewMember = PossibleCrewForPart[index];
if (crewMember != null)
{
KCT_GameStates.launchedCrew[j].crewList[i] = crewMember;
PossibleCrewForPart.RemoveAt(index);
if (PossibleCrewForPart != AvailableCrew)
AvailableCrew.Remove(crewMember);
}
}
else if (autoHire)
{
if (HighLogic.CurrentGame.CrewRoster.Applicants.Count() == 0)
HighLogic.CurrentGame.CrewRoster.GetNextApplicant();
int index = randomCrew ? rand.Next(HighLogic.CurrentGame.CrewRoster.Applicants.Count() - 1) : 0;
ProtoCrewMember hired = HighLogic.CurrentGame.CrewRoster.Applicants.ElementAt(index);
HighLogic.CurrentGame.CrewRoster.HireApplicant(hired);
List<ProtoCrewMember> activeCrew;
activeCrew = KCT_GameStates.launchedCrew[j].crewList;
if (activeCrew.Count > i)
{
activeCrew.Insert(i, hired);
if (activeCrew[i + 1] == null)
activeCrew.RemoveAt(i + 1);
}
else
{
for (int k = activeCrew.Count; k < i; k++)
{
activeCrew.Insert(k, null);
}
activeCrew.Insert(i, hired);
}
KCT_GameStates.launchedCrew[j].crewList = activeCrew;
}
}
}
}
}
}
if (GUILayout.Button("Clear All"))
{
foreach (CrewedPart cP in KCT_GameStates.launchedCrew)
{
cP.crewList.Clear();
}
PossibleCrewForPart.Clear();
AvailableCrew = GetAvailableCrew(string.Empty);
}
GUILayout.EndHorizontal();
int numberItems = 0;
foreach (Part p in parts)
{
//Part p = KCT_Utilities.GetAvailablePartByName(s).partPrefab;
if (p.CrewCapacity > 0)
{
numberItems += 1 + p.CrewCapacity;
}
}
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Height(numberItems * 25 + 10), GUILayout.MaxHeight(Screen.height / 2));
for (int j = 0; j < parts.Count; j++)
{
//Part p = KCT_Utilities.GetAvailablePartByName(KCT_GameStates.launchedVessel.GetPartNames()[j]).partPrefab;
Part p = parts[j];
if (p.CrewCapacity > 0)
{
if (UseAvailabilityChecker)
{
PossibleCrewForPart.Clear();
foreach (ProtoCrewMember pcm in AvailableCrew)
if (AvailabilityChecker(pcm, p.partInfo.name))
PossibleCrewForPart.Add(pcm);
}
else
PossibleCrewForPart = AvailableCrew;
GUILayout.BeginHorizontal();
GUILayout.Label(p.partInfo.title.Length <= 25 ? p.partInfo.title : p.partInfo.title.Substring(0, 25));
if (GUILayout.Button("Fill", GUILayout.Width(75)))
{
if (KCT_GameStates.launchedCrew.Find(part => part.partID == p.craftID) == null)
KCT_GameStates.launchedCrew.Add(new CrewedPart(p.craftID, new List<ProtoCrewMember>()));
for (int i = 0; i < p.CrewCapacity; i++)
{
if (KCT_GameStates.launchedCrew[j].crewList.Count <= i)
{
if (PossibleCrewForPart.Count > 0)
{
int index = randomCrew ? new System.Random().Next(PossibleCrewForPart.Count) : 0;
ProtoCrewMember crewMember = PossibleCrewForPart[index];
if (crewMember != null)
{
KCT_GameStates.launchedCrew[j].crewList.Add(crewMember);
PossibleCrewForPart.RemoveAt(index);
if (PossibleCrewForPart != AvailableCrew)
AvailableCrew.Remove(crewMember);
}
}
else if (autoHire)
{
if (HighLogic.CurrentGame.CrewRoster.Applicants.Count() == 0)
HighLogic.CurrentGame.CrewRoster.GetNextApplicant();
int index = randomCrew ? rand.Next(HighLogic.CurrentGame.CrewRoster.Applicants.Count() - 1) : 0;
ProtoCrewMember hired = HighLogic.CurrentGame.CrewRoster.Applicants.ElementAt(index);
HighLogic.CurrentGame.CrewRoster.HireApplicant(hired);
List<ProtoCrewMember> activeCrew;
activeCrew = KCT_GameStates.launchedCrew[j].crewList;
if (activeCrew.Count > i)
{