/
GuiUtils.cs
1157 lines (955 loc) · 40.3 KB
/
GuiUtils.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.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Profiling;
using Object = UnityEngine.Object;
namespace MuMech
{
public interface IEditable
{
string text { get; set; }
}
//An EditableDouble stores a double value and a text string. The user can edit the string.
//Whenever the text is edited, it is parsed and the parsed value is stored in val. As a
//convenience, a multiplier can be specified so that the stored value is actually
//(multiplier * parsed value). If the parsing fails, the parsed flag is set to false and
//the stored value is unchanged. There are implicit conversions between EditableDouble and
//double so that if you are not doing text input you can treat an EditableDouble like a double.
public class EditableDoubleMult : IEditable
{
[Persistent]
protected double _val;
public virtual double val
{
get => _val;
set
{
_val = value;
_text = (_val / multiplier).ToString();
}
}
public readonly double multiplier;
public bool parsed;
[Persistent]
protected string _text;
public virtual string text
{
get => _text;
set
{
_text = value;
_text = Regex.Replace(_text, @"[^\d+-.]", ""); //throw away junk characters
double parsedValue;
parsed = double.TryParse(_text, out parsedValue);
if (parsed) _val = parsedValue * multiplier;
}
}
public EditableDoubleMult() : this(0) { }
public EditableDoubleMult(double val, double multiplier = 1)
{
this.val = val;
this.multiplier = multiplier;
_text = (val / multiplier).ToString();
}
public static implicit operator double(EditableDoubleMult x)
{
return x.val;
}
}
public class EditableDouble : EditableDoubleMult
{
public EditableDouble(double val)
: base(val)
{
}
public static implicit operator EditableDouble(double x)
{
return new EditableDouble(x);
}
}
public class EditableTime : EditableDouble
{
public EditableTime() : this(0) { }
public EditableTime(double seconds)
: base(seconds)
{
_text = GuiUtils.TimeToDHMS(seconds);
}
public override double val
{
get => _val;
set
{
_val = value;
_text = GuiUtils.TimeToDHMS(_val);
}
}
public override string text
{
get => _text;
set
{
_text = value;
_text = Regex.Replace(_text, @"[^\d+-.ydhms ,]", ""); //throw away junk characters
double parsedValue;
parsed = double.TryParse(_text, out parsedValue);
if (parsed) _val = parsedValue;
if (!parsed)
{
parsed = GuiUtils.TryParseDHMS(_text, out parsedValue);
if (parsed) _val = parsedValue;
}
}
}
public static implicit operator EditableTime(double x)
{
return new EditableTime(x);
}
}
public class EditableAngle
{
[Persistent]
public EditableDouble degrees = 0;
[Persistent]
public EditableDouble minutes = 0;
[Persistent]
public EditableDouble seconds = 0;
[Persistent]
public bool negative;
public EditableAngle(double angle)
{
angle = MuUtils.ClampDegrees180(angle);
negative = angle < 0;
angle = Math.Abs(angle);
degrees = (int)angle;
angle -= degrees;
minutes = (int)(60 * angle);
angle -= minutes / 60;
seconds = Math.Round(3600 * angle);
}
public static implicit operator double(EditableAngle x)
{
return (x.negative ? -1 : 1) * (x.degrees + x.minutes / 60.0 + x.seconds / 3600.0);
}
public static implicit operator EditableAngle(double x)
{
return new EditableAngle(x);
}
public enum Direction { NS, EW }
public void DrawEditGUI(Direction direction)
{
GUILayout.BeginHorizontal();
degrees.text = GUILayout.TextField(degrees.text, GUILayout.Width(30));
GUILayout.Label("°", GUILayout.ExpandWidth(false));
minutes.text = GUILayout.TextField(minutes.text, GUILayout.Width(30));
GUILayout.Label("'", GUILayout.ExpandWidth(false));
seconds.text = GUILayout.TextField(seconds.text, GUILayout.Width(30));
GUILayout.Label("\"", GUILayout.ExpandWidth(false));
string dirString = direction == Direction.NS ? negative ? "S" : "N" : negative ? "W" : "E";
if (GUILayout.Button(dirString, GUILayout.Width(25))) negative = !negative;
GUILayout.EndHorizontal();
}
}
public class EditableInt : IEditable
{
[Persistent]
private int _val;
public int val
{
get => _val;
set
{
_val = value;
_text = value.ToString();
}
}
private bool _parsed;
[Persistent]
private string _text;
public virtual string text
{
get => _text;
set
{
_text = value;
_text = Regex.Replace(_text, @"[^\d+-]", ""); //throw away junk characters
_parsed = int.TryParse(_text, out int parsedValue);
if (_parsed) val = parsedValue;
}
}
public EditableInt(int val)
{
this.val = val;
_text = val.ToString();
}
public static implicit operator int(EditableInt x)
{
return x.val;
}
public static implicit operator EditableInt(int x)
{
return new EditableInt(x);
}
}
public class EditableIntList : IEditable
{
[Persistent]
public readonly List<int> val = new List<int>();
[Persistent]
private string _text = "";
public string text
{
get => _text;
set
{
_text = value;
_text = Regex.Replace(_text, @"[^\d-,]", ""); //throw away junk characters
val.Clear();
// supports "1,2,3" and "1-3"
foreach (string x in _text.Split(','))
{
string[] y = x.Split('-');
if (!int.TryParse(y[0].Trim(), out int start)) continue;
if (!int.TryParse(y[y.Length - 1].Trim(), out int end)) continue;
for (int n = start; n <= end; n++)
val.Add(n);
}
}
}
}
public class ZombieGUILoader : MonoBehaviour
{
private void OnGUI()
{
GuiUtils.CopyDefaultSkin();
if (GuiUtils.skin == null) GuiUtils.skin = GuiUtils.defaultSkin;
Destroy(gameObject);
}
}
public static class GuiUtils
{
private static GUIStyle _yellowOnHover;
public static GUIStyle yellowOnHover
{
get
{
if (_yellowOnHover == null)
{
_yellowOnHover = new GUIStyle(GUI.skin.label);
_yellowOnHover.hover.textColor = Color.yellow;
var t = new Texture2D(1, 1);
t.SetPixel(0, 0, new Color(0, 0, 0, 0));
t.Apply();
_yellowOnHover.hover.background = t;
}
return _yellowOnHover;
}
}
private static GUIStyle _yellowLabel;
public static GUIStyle yellowLabel
{
get
{
if (_yellowLabel == null)
{
_yellowLabel = new GUIStyle(GUI.skin.label);
_yellowLabel.normal.textColor = Color.yellow;
_yellowLabel.hover.textColor = Color.yellow;
}
return _yellowLabel;
}
}
private static GUIStyle _redLabel;
public static GUIStyle redLabel
{
get
{
if (_redLabel == null)
{
_redLabel = new GUIStyle(GUI.skin.label);
_redLabel.normal.textColor = Color.red;
_redLabel.hover.textColor = Color.red;
}
return _redLabel;
}
}
private static GUIStyle _greenLabel;
public static GUIStyle greenLabel
{
get
{
if (_greenLabel == null)
{
_greenLabel = new GUIStyle(GUI.skin.label);
_greenLabel.normal.textColor = Color.green;
_greenLabel.hover.textColor = Color.green;
}
return _greenLabel;
}
}
private static GUIStyle _orangeLabel;
public static GUIStyle orangeLabel
{
get
{
if (_orangeLabel == null)
{
_orangeLabel = new GUIStyle(GUI.skin.label);
_orangeLabel.normal.textColor = Color.green;
_orangeLabel.hover.textColor = Color.green;
}
return _orangeLabel;
}
}
private static GUIStyle _middleCenterLabel;
public static GUIStyle middleCenterLabel
{
get
{
if (_middleCenterLabel == null)
{
_middleCenterLabel = new GUIStyle(GUI.skin.label);
_middleCenterLabel.alignment = TextAnchor.MiddleCenter;
}
return _middleCenterLabel;
}
}
private static GUIStyle _middleRightLabel;
public static GUIStyle middleRightLabel
{
get
{
if (_middleRightLabel == null)
{
_middleRightLabel = new GUIStyle(GUI.skin.label);
_middleRightLabel.alignment = TextAnchor.MiddleRight;
}
return _middleRightLabel;
}
}
private static GUIStyle _UpperCenterLabel;
public static GUIStyle UpperCenterLabel
{
get
{
if (_UpperCenterLabel == null)
{
_UpperCenterLabel = new GUIStyle(GUI.skin.label);
_UpperCenterLabel.alignment = TextAnchor.UpperCenter;
}
return _UpperCenterLabel;
}
}
private static GUIStyle _labelNoWrap;
public static GUIStyle LabelNoWrap
{
get
{
if (_labelNoWrap == null)
{
_labelNoWrap = new GUIStyle(GUI.skin.label) { wordWrap = false };
}
return _labelNoWrap;
}
}
private static GUIStyle _greenToggle;
public static GUIStyle greenToggle
{
get
{
if (_greenToggle == null)
{
_greenToggle = new GUIStyle(GUI.skin.toggle);
_greenToggle.onHover.textColor = Color.green;
_greenToggle.onNormal.textColor = Color.green;
}
return _greenToggle;
}
}
private static GUIStyle _redToggle;
public static GUIStyle redToggle
{
get
{
if (_redToggle == null)
{
_redToggle = new GUIStyle(GUI.skin.toggle);
_redToggle.onHover.textColor = Color.red;
_redToggle.onNormal.textColor = Color.red;
}
return _redToggle;
}
}
private static GUIStyle _yellowToggle;
public static GUIStyle yellowToggle
{
get
{
if (_yellowToggle == null)
{
_yellowToggle = new GUIStyle(GUI.skin.toggle);
_yellowToggle.onHover.textColor = Color.yellow;
_yellowToggle.onNormal.textColor = Color.yellow;
}
return _yellowToggle;
}
}
public enum SkinType { Default, MechJeb1, Compact }
public static GUISkin skin;
public static float scale = 1;
public static int scaledScreenWidth = 1;
public static int scaledScreenHeight = 1;
public static bool dontUseDropDownMenu = false;
public static bool showAdvancedWindowSettings = false;
public static GUISkin defaultSkin;
public static GUISkin compactSkin;
public static GUISkin transparentSkin;
public static void SetGUIScale(double s)
{
scale = Mathf.Clamp((float)s, 0.2f, 5f);
scaledScreenHeight = Mathf.RoundToInt(Screen.height / scale);
scaledScreenWidth = Mathf.RoundToInt(Screen.width / scale);
}
public static void CopyDefaultSkin()
{
GUI.skin = null;
defaultSkin = Object.Instantiate(GUI.skin);
}
public static void CopyCompactSkin()
{
GUI.skin = null;
compactSkin = Object.Instantiate(GUI.skin);
skin.name = "KSP Compact";
compactSkin.label.margin = new RectOffset(1, 1, 1, 1);
compactSkin.label.padding = new RectOffset(0, 0, 2, 2);
compactSkin.button.margin = new RectOffset(1, 1, 1, 1);
compactSkin.button.padding = new RectOffset(4, 4, 2, 2);
compactSkin.toggle.margin = new RectOffset(1, 1, 1, 1);
compactSkin.toggle.padding = new RectOffset(15, 0, 2, 0);
compactSkin.textField.margin = new RectOffset(1, 1, 1, 1);
compactSkin.textField.padding = new RectOffset(2, 2, 2, 2);
compactSkin.textArea.margin = new RectOffset(1, 1, 1, 1);
compactSkin.textArea.padding = new RectOffset(2, 2, 2, 2);
compactSkin.window.margin = new RectOffset(0, 0, 0, 0);
compactSkin.window.padding = new RectOffset(5, 5, 20, 5);
}
public static void CopyTransparentSkin()
{
GUI.skin = null;
transparentSkin = Object.Instantiate(GUI.skin);
var t = new Texture2D(1, 1);
t.SetPixel(0, 0, new Color(0, 0, 0, 0));
t.Apply();
transparentSkin.window.normal.background = t;
transparentSkin.window.onNormal.background = t;
transparentSkin.window.padding = new RectOffset(5, 5, 5, 5);
}
public static void LoadSkin(SkinType skinType)
{
if (defaultSkin == null) CopyDefaultSkin();
if (compactSkin == null) CopyCompactSkin();
if (transparentSkin == null) CopyTransparentSkin();
switch (skinType)
{
case SkinType.Default:
skin = defaultSkin;
break;
case SkinType.MechJeb1:
skin = AssetBase.GetGUISkin("KSP window 2");
break;
case SkinType.Compact:
skin = compactSkin;
break;
}
}
private static GUILayoutOption _layoutExpandWidth, _layoutNoExpandWidth;
public static GUILayoutOption LayoutExpandWidth => _layoutExpandWidth ??= GUILayout.ExpandWidth(true);
public static GUILayoutOption LayoutNoExpandWidth => _layoutNoExpandWidth ??= GUILayout.ExpandWidth(false);
public static GUILayoutOption ExpandWidth(bool b)
{
return b ? LayoutExpandWidth : LayoutNoExpandWidth;
}
private static readonly Dictionary<float, GUILayoutOption> _layoutWidthDict = new Dictionary<float, GUILayoutOption>(16);
public static GUILayoutOption LayoutWidth(float width)
{
if (_layoutWidthDict.TryGetValue(width, out GUILayoutOption option)) return option;
option = GUILayout.Width(width);
_layoutWidthDict.Add(width, option);
return option;
}
public static void SimpleTextField(IEditable ed, float width = 100, bool expandWidth = false)
{
Profiler.BeginSample("SimpleTextField");
string res = !expandWidth
? GUILayout.TextField(ed.text, LayoutWidth(width), ExpandWidth(expandWidth))
: GUILayout.TextField(ed.text, LayoutWidth(width));
if (res != null && !res.Equals(ed.text))
ed.text = res;
Profiler.EndSample();
}
#nullable enable
public static void SimpleTextBox(string? leftLabel, IEditable ed, string? rightLabel = null, float width = 100,
GUIStyle? leftLabelStyle = null, bool horizontalFraming = true)
{
Profiler.BeginSample("SimpleTextBox");
if (horizontalFraming) GUILayout.BeginHorizontal();
if (!string.IsNullOrEmpty(leftLabel))
{
leftLabelStyle ??= GUI.skin.label;
GUILayout.Label(leftLabel, leftLabelStyle);
}
SimpleTextField(ed, width, true);
if (!string.IsNullOrEmpty(rightLabel)) GUILayout.Label(rightLabel);
if (horizontalFraming) GUILayout.EndHorizontal();
Profiler.EndSample();
}
public static void ToggledTextBox(ref bool toggle, string toggleText, IEditable ed, string? rightLabel = null, GUIStyle? toggleStyle = null,
float width = 100)
{
Profiler.BeginSample("ToggledTextField");
GUILayout.BeginHorizontal();
toggle = toggleStyle != null ? GUILayout.Toggle(toggle, toggleText, toggleStyle) : GUILayout.Toggle(toggle, toggleText);
SimpleTextField(ed, width);
if (!string.IsNullOrEmpty(rightLabel)) GUILayout.Label(rightLabel);
GUILayout.EndHorizontal();
Profiler.EndSample();
}
public static bool ButtonTextBox(string buttonText, IEditable ed, string? rightLabel = null, GUIStyle? buttonStyle = null, float width = 100)
{
Profiler.BeginSample("ButtonTextBox");
GUILayout.BeginHorizontal();
bool pressed = buttonStyle != null ? GUILayout.Button(buttonText, buttonStyle) : GUILayout.Button(buttonText);
SimpleTextField(ed, width);
if (!string.IsNullOrEmpty(rightLabel)) GUILayout.Label(rightLabel);
GUILayout.EndHorizontal();
Profiler.EndSample();
return pressed;
}
#nullable restore
public static void SimpleLabel(string leftLabel, string rightLabel = "")
{
GUILayout.BeginHorizontal();
GUILayout.Label(leftLabel, ExpandWidth(true));
if (!string.IsNullOrEmpty(rightLabel))
GUILayout.Label(rightLabel, ExpandWidth(false));
GUILayout.EndHorizontal();
}
public static void SimpleLabelInt(string leftLabel, int rightValue)
{
SimpleLabel(leftLabel, rightValue.ToString());
}
public static int ArrowSelector(int index, int numIndices, Action centerGuiAction)
{
if (numIndices == 0) return index;
GUILayout.BeginHorizontal();
//if (numIndices > 1 && GUILayout.Button("◀", GUILayout.ExpandWidth(false))) index = (index - 1 + numIndices) % numIndices; // Seems those are gone from KSP font
if (numIndices > 1 && GUILayout.Button("<", GUILayout.ExpandWidth(false))) index = (index - 1 + numIndices) % numIndices;
centerGuiAction();
//if (numIndices > 1 && GUILayout.Button("▶", GUILayout.ExpandWidth(false))) index = (index + 1) % numIndices;
if (numIndices > 1 && GUILayout.Button(">", GUILayout.ExpandWidth(false))) index = (index + 1) % numIndices;
GUILayout.EndHorizontal();
return index;
}
private static GUIStyle _arrowSelectorStyeGuiStyleExpand;
public static GUIStyle arrowSelectorStyeGuiStyleExpand
{
get
{
if (_arrowSelectorStyeGuiStyleExpand == null)
{
_arrowSelectorStyeGuiStyleExpand = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, stretchWidth = true };
}
return _arrowSelectorStyeGuiStyleExpand;
}
}
private static GUIStyle _arrowSelectorStyeGuiStyleNoExpand;
public static GUIStyle arrowSelectorStyeGuiStyleNoExpand
{
get
{
if (_arrowSelectorStyeGuiStyleNoExpand == null)
{
_arrowSelectorStyeGuiStyleNoExpand = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, stretchWidth = false };
}
return _arrowSelectorStyeGuiStyleNoExpand;
}
}
public static int ArrowSelector(int index, int modulo, string label, bool expandWidth = true)
{
Action drawLabel = () => GUILayout.Label(label, expandWidth ? arrowSelectorStyeGuiStyleExpand : arrowSelectorStyeGuiStyleNoExpand);
return ArrowSelector(index, modulo, drawLabel);
}
public static int HoursPerDay => GameSettings.KERBIN_TIME ? 6 : 24;
public static int DaysPerYear => GameSettings.KERBIN_TIME ? 426 : 365;
public static string TimeToDHMS(double seconds, int decimalPlaces = 0)
{
if (double.IsInfinity(seconds) || double.IsNaN(seconds)) return "Inf";
string ret = "";
bool showSecondsDecimals = decimalPlaces > 0;
try
{
string[] units = { "y", "d", "h", "m", "s" };
long[] intervals = { KSPUtil.dateTimeFormatter.Year, KSPUtil.dateTimeFormatter.Day, 3600, 60, 1 };
if (seconds < 0)
{
ret += "-";
seconds *= -1;
}
for (int i = 0; i < units.Length; i++)
{
long n = (long)(seconds / intervals[i]);
bool first = ret.Length < 2;
if (!first || n != 0 || (i == units.Length - 1 && ret == ""))
{
if (!first) ret += " ";
if (showSecondsDecimals && seconds < 60 && i == units.Length - 1)
ret += seconds.ToString("00." + new string('0', decimalPlaces));
else if (first) ret += n.ToString();
else ret += n.ToString(i == 1 ? "000" : "00");
ret += units[i];
}
seconds -= n * intervals[i];
}
}
catch (Exception)
{
return "NaN";
}
return ret;
}
public static bool TryParseDHMS(string s, out double seconds)
{
string[] units = { "y", "d", "h", "m", "s" };
int[] intervals = { KSPUtil.dateTimeFormatter.Year, KSPUtil.dateTimeFormatter.Day, 3600, 60, 1 };
s = s.Trim(' ');
bool minus = s.StartsWith("-");
seconds = 0;
bool parsedSomething = false;
for (int i = 0; i < units.Length; i++)
{
s = s.Trim(' ', ',', '-');
int unitPos = s.IndexOf(units[i]);
if (unitPos != -1)
{
double value;
if (!double.TryParse(s.Substring(0, unitPos), out value)) return false;
seconds += value * intervals[i];
s = s.Substring(unitPos + 1);
parsedSomething = true;
}
}
if (minus) seconds = -seconds;
return parsedSomething;
}
public static double ArcDistance(Vector3 From, Vector3 To)
{
double a = (FlightGlobals.ActiveVessel.mainBody.transform.position - From).magnitude;
double b = (FlightGlobals.ActiveVessel.mainBody.transform.position - To).magnitude;
double c = Vector3d.Distance(From, To);
double ang = Math.Acos((a * a + b * b - c * c) / (2f * a * b));
return ang * FlightGlobals.ActiveVessel.mainBody.Radius;
}
public static double FromToETA(Vector3 From, Vector3 To, double Speed = 0)
{
return ArcDistance(From, To) / (Speed > 0 ? Speed : FlightGlobals.ActiveVessel.horizontalSrfSpeed);
}
public static bool MouseIsOverWindow(MechJebCore core)
{
//try to check if the mouse is over any active DisplayModule
foreach (DisplayModule m in core.GetComputerModules<DisplayModule>())
{
if (m.Enabled && m.showInCurrentScene && !m.IsOverlay
&& m.windowPos.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y) / scale))
{
return true;
}
}
return false;
}
public static Coordinates GetMouseCoordinates(CelestialBody body)
{
Ray mouseRay = PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition);
mouseRay.origin = ScaledSpace.ScaledToLocalSpace(mouseRay.origin);
Vector3d relOrigin = mouseRay.origin - body.position;
Vector3d relSurfacePosition;
double curRadius = body.pqsController.radiusMax;
double lastRadius = 0;
double error = 0;
int loops = 0;
float st = Time.time;
while (loops < 50)
{
if (PQS.LineSphereIntersection(relOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
{
Vector3d surfacePoint = body.position + relSurfacePosition;
double alt = body.pqsController.GetSurfaceHeight(QuaternionD.AngleAxis(body.GetLongitude(surfacePoint), Vector3d.down) *
QuaternionD.AngleAxis(body.GetLatitude(surfacePoint), Vector3d.forward) *
Vector3d.right);
error = Math.Abs(curRadius - alt);
if (error < (body.pqsController.radiusMax - body.pqsController.radiusMin) / 100)
{
return new Coordinates(body.GetLatitude(surfacePoint), MuUtils.ClampDegrees180(body.GetLongitude(surfacePoint)));
}
lastRadius = curRadius;
curRadius = alt;
loops++;
}
else
{
if (loops == 0)
{
break;
}
// Went too low, needs to try higher
curRadius = (lastRadius * 9 + curRadius) / 10;
loops++;
}
}
return null;
}
public class ComboBox
{
// Easy to use combobox class
// ***** For users *****
// Call the Box method with the latest selected item, list of text entries
// and an object identifying who is making the request.
// The result is the newly selected item.
// There is currently no way of knowing when a choice has been made
// Position of the popup
private static Rect rect;
// Identifier of the caller of the popup, null if nobody is waiting for a value
private static object popupOwner;
private static string[] entries;
private static bool popupActive;
// Result to be returned to the owner
private static int selectedItem;
// Unity identifier of the window, just needs to be unique
private static readonly int id = GUIUtility.GetControlID(FocusType.Passive);
// ComboBox GUI Style
private static readonly GUIStyle style;
static ComboBox()
{
style = new GUIStyle(GUI.skin.window);
style.normal.background = null;
style.onNormal.background = null;
style.border.top = style.border.bottom;
style.padding.top = style.padding.bottom;
}
public static void DrawGUI()
{
if (popupOwner == null || rect.height == 0 || !popupActive)
return;
if (style.normal.background == null)
{
style.normal.background = MechJebBundlesManager.comboBoxBackground;
style.onNormal.background = MechJebBundlesManager.comboBoxBackground;
}
// Make sure the rectangle is fully on screen
rect.x = Math.Max(0, Math.Min(rect.x, scaledScreenWidth - rect.width));
rect.y = Math.Max(0, Math.Min(rect.y, scaledScreenHeight - rect.height));
rect = GUILayout.Window(id, rect, identifier =>
{
selectedItem = GUILayout.SelectionGrid(-1, entries, 1, yellowOnHover);
if (GUI.changed)
popupActive = false;
}, "", style);
//Cancel the popup if we click outside
if (Event.current.type == EventType.MouseDown && !rect.Contains(Event.current.mousePosition))
popupOwner = null;
}
public static int Box(int selectedItem, string[] entries, object caller, bool expandWidth = true)
{
// Trivial cases (0-1 items)
if (entries.Length == 0)
return 0;
if (entries.Length == 1)
{
GUILayout.Label(entries[0]);
return 0;
}
if (selectedItem >= entries.Length)
selectedItem = entries.Length - 1;
if (dontUseDropDownMenu)
return ArrowSelector(selectedItem, entries.Length, entries[selectedItem], expandWidth);
// A choice has been made, update the return value
if (popupOwner == caller && !popupActive)
{
popupOwner = null;
selectedItem = ComboBox.selectedItem;
GUI.changed = true;
}
bool guiChanged = GUI.changed;
if (GUILayout.Button("↓ " + entries[selectedItem] + " ↓", GUILayout.ExpandWidth(expandWidth)))
{
// We will set the changed status when we return from the menu instead
GUI.changed = guiChanged;
// Update the global state with the new items
popupOwner = caller;
popupActive = true;
ComboBox.entries = entries;
// Magic value to force position update during repaint event
rect = new Rect(0, 0, 0, 0);
}
// The GetLastRect method only works during repaint event, but the Button will return false during repaint
if (Event.current.type == EventType.Repaint && popupOwner == caller && rect.height == 0)
{
rect = GUILayoutUtility.GetLastRect();
// But even worse, I can't find a clean way to convert from relative to absolute coordinates
Vector2 mousePos = Input.mousePosition;
mousePos.y = Screen.height - mousePos.y;
Vector2 clippedMousePos = Event.current.mousePosition;
rect.x = (rect.x + mousePos.x) / scale - clippedMousePos.x;
rect.y = (rect.y + mousePos.y) / scale - clippedMousePos.y;
}
return selectedItem;
}
}
}
public class Coordinates
{
public double latitude;
public double longitude;
public Coordinates(double latitude, double longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
public static string ToStringDecimal(double latitude, double longitude, bool newline = false, int precision = 3)
{
double clampedLongitude = MuUtils.ClampDegrees180(longitude);
double latitudeAbs = Math.Abs(latitude);
double longitudeAbs = Math.Abs(clampedLongitude);
return latitudeAbs.ToString("F" + precision) + "° " + (latitude > 0 ? "N" : "S") + (newline ? "\n" : ", ")
+ longitudeAbs.ToString("F" + precision) + "° " + (clampedLongitude > 0 ? "E" : "W");
}
public string ToStringDecimal(bool newline = false, int precision = 3)
{
return ToStringDecimal(latitude, longitude, newline, precision);
}
public static string ToStringDMS(double latitude, double longitude, bool newline = false)
{