Skip to content

Commit d2b4f1f

Browse files
committed
Add Torque to StageInfoOab
- Add Torque to StageInfoOab window - Add Settings window for StageInfoOab - Torque is toggleable
1 parent b8b5591 commit d2b4f1f

File tree

3 files changed

+196
-26
lines changed

3 files changed

+196
-26
lines changed

MicroEngineerProject/MicroEngineer/MicroEngineerMod.cs

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class MicroEngineerMod : BaseSpaceWarpPlugin
1818
{
1919
private bool _showGuiFlight;
2020
private bool _showGuiOAB;
21+
private bool _showGuiSettingsOAB;
2122

2223
#region Editing window
2324
private bool showEditWindow = false;
@@ -47,6 +48,8 @@ public class MicroEngineerMod : BaseSpaceWarpPlugin
4748
// If game input is enabled or disabled (used for locking controls when user is editing a text field
4849
private bool _gameInputState = true;
4950

51+
private float _lastUpdate = 0;
52+
5053
public override void OnInitialized()
5154
{
5255
MicroStyles.InitializeStyles();
@@ -58,18 +61,7 @@ public override void OnInitialized()
5861
// Load window positions and states from disk, if file exists
5962
MicroUtility.LoadLayout(MicroWindows);
6063

61-
// Preserve backward compatibility with 0.6.0. If user previously saved the layout and then upgraded without deleting the original folder, then StageInfoOAB window will be wiped by LoadLayout(). So, we add it manually now.
62-
if (MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB) == null)
63-
InitializeStageInfoOABWindow();
64-
65-
// Preserve backward compatibility with SpaceWarp 1.0.1
66-
if (MicroUtility.IsModOlderThan("SpaceWarp", 1, 1, 0))
67-
{
68-
Logger.LogInfo("Space Warp older version detected. Loading old MicroStyles.");
69-
MicroStyles.SetStylesForOldSpaceWarpSkin();
70-
}
71-
else
72-
Logger.LogInfo("Space Warp new version detected. Loading new MicroStyles.");
64+
BackwardCompatibilityInitializations();
7365

7466
// Register Flight and OAB buttons
7567
Appbar.RegisterAppButton(
@@ -95,6 +87,30 @@ public override void OnInitialized()
9587
});
9688
}
9789

90+
private void BackwardCompatibilityInitializations()
91+
{
92+
// Preserve backward compatibility with 0.6.0. If user previously saved the layout and then upgraded without deleting the original folder, then StageInfoOAB window will be wiped by LoadLayout(). So, we add it manually now.
93+
if (MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB) == null)
94+
InitializeStageInfoOABWindow();
95+
96+
// Preserve backward compatibility with SpaceWarp 1.0.1
97+
if (MicroUtility.IsModOlderThan("SpaceWarp", 1, 1, 0))
98+
{
99+
Logger.LogInfo("Space Warp older version detected. Loading old MicroStyles.");
100+
MicroStyles.SetStylesForOldSpaceWarpSkin();
101+
}
102+
else
103+
Logger.LogInfo("Space Warp new version detected. Loading new MicroStyles.");
104+
105+
// Preserve backward compatibility with 0.7.2. If user previously saved the layout and then upgraded without deleting the original folder, then the Torque entry won't be in the loaded StageOAB window. So, we add it manually now.
106+
MicroWindow stageOabWindow = MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB);
107+
if (stageOabWindow.Entries.Find(e => e.Name == "Torque") == null)
108+
{
109+
stageOabWindow.Entries.Add(this.MicroEntries.Find(e => e.Name == "Torque"));
110+
MicroUtility.SaveLayout(this.MicroWindows);
111+
}
112+
}
113+
98114
/// <summary>
99115
/// Subscribe to Messages KSP2 is using
100116
/// </summary>
@@ -179,20 +195,37 @@ private void RefreshStagingDataOAB(MessageCenterMessage obj)
179195

180196
public void Update()
181197
{
182-
MicroUtility.RefreshActiveVesselAndCurrentManeuver();
198+
MicroUtility.RefreshGameManager();
183199

184-
// Do not perform flight UI updates if we're outside of flight and map scene
185-
if (MicroUtility.GameState == null || (MicroUtility.GameState.GameState != GameState.FlightView && MicroUtility.GameState.GameState != GameState.Map3DView))
186-
return;
187-
188-
if (MicroUtility.ActiveVessel == null)
189-
return;
190-
191-
// Grab all Flight entries from all active windows and refresh their data
192-
foreach (MicroEntry entry in MicroWindows
193-
.Where(w => w.IsFlightActive)
194-
.SelectMany(w => w.Entries ?? Enumerable.Empty<MicroEntry>()).ToList())
195-
entry.RefreshData();
200+
201+
// Perform OAB updates only if we're in OAB
202+
if (MicroUtility.GameState != null && MicroUtility.GameState.IsObjectAssembly)
203+
{
204+
// Do updates every 1 sec
205+
if (Time.time - _lastUpdate > 1)
206+
{
207+
_lastUpdate = Time.time;
208+
209+
Torque torque = (Torque)MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB).Entries.Find(e => e.Name == "Torque");
210+
if (torque.IsActive)
211+
torque.RefreshData();
212+
}
213+
}
214+
215+
// Perform flight UI updates only if we're in Flight or Map view
216+
if (MicroUtility.GameState != null && (MicroUtility.GameState.GameState == GameState.FlightView || MicroUtility.GameState.GameState == GameState.Map3DView))
217+
{
218+
MicroUtility.RefreshActiveVesselAndCurrentManeuver();
219+
220+
if (MicroUtility.ActiveVessel == null)
221+
return;
222+
223+
// Grab all Flight entries from all active windows and refresh their data
224+
foreach (MicroEntry entry in MicroWindows
225+
.Where(w => w.IsFlightActive)
226+
.SelectMany(w => w.Entries ?? Enumerable.Empty<MicroEntry>()).ToList())
227+
entry.RefreshData();
228+
}
196229
}
197230
#endregion
198231

@@ -785,6 +818,22 @@ private void OnGUI_OAB()
785818
GUILayout.Height(0)
786819
);
787820
}
821+
822+
// Draw Settings window for the StageInfoOAB
823+
if(_showGuiSettingsOAB)
824+
{
825+
Rect stageInfoOabRect = MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB).EditorRect;
826+
Rect settingsRect = new Rect(stageInfoOabRect.x + stageInfoOabRect.width, stageInfoOabRect.y, 0, 0);
827+
828+
settingsRect = GUILayout.Window(
829+
GUIUtility.GetControlID(FocusType.Passive),
830+
settingsRect,
831+
DrawSettingsOabWindow,
832+
"",
833+
MicroStyles.SettingsOabStyle,
834+
GUILayout.Height(0)
835+
);
836+
}
788837
}
789838

790839
private void DrawStageInfoOAB(int windowID)
@@ -793,6 +842,9 @@ private void DrawStageInfoOAB(int windowID)
793842
List<MicroEntry> stageInfoOabEntries = stageInfoOabWindow.Entries;
794843

795844
GUILayout.BeginHorizontal();
845+
if (SettingsButton(MicroStyles.SettingsOABRect))
846+
_showGuiSettingsOAB = !_showGuiSettingsOAB;
847+
796848
if (CloseButton(MicroStyles.CloseBtnStagesOABRect))
797849
{
798850
stageInfoOabWindow.IsEditorActive = false;
@@ -810,6 +862,20 @@ private void DrawStageInfoOAB(int windowID)
810862
GUILayout.Label("m/s", MicroStyles.UnitLabelStyle);
811863
GUILayout.EndHorizontal();
812864

865+
// Draw Torque
866+
Torque torque = (Torque)stageInfoOabEntries.Find(e => e.Name == "Torque");
867+
if (torque.IsActive)
868+
{
869+
GUILayout.Space(MicroStyles.SpacingAfterEntry);
870+
GUILayout.BeginHorizontal();
871+
GUILayout.Label("Torque", MicroStyles.NameLabelStyle);
872+
GUILayout.FlexibleSpace();
873+
GUILayout.Label(torque.ValueDisplay, MicroStyles.ValueLabelStyle);
874+
GUILayout.Space(5);
875+
GUILayout.Label(torque.Unit, MicroStyles.UnitLabelStyle);
876+
GUILayout.EndHorizontal();
877+
}
878+
813879
// Draw Stage table header
814880
GUILayout.BeginHorizontal();
815881
GUILayout.Label("Stage", MicroStyles.NameLabelStyle, GUILayout.Width(40));
@@ -895,6 +961,21 @@ private void DrawCelestialBodySelection(int id)
895961

896962
GUILayout.EndVertical();
897963
}
964+
965+
/// <summary>
966+
/// Opens a Settings window for OAB
967+
/// </summary>
968+
private void DrawSettingsOabWindow(int id)
969+
{
970+
if (CloseButton(MicroStyles.CloseBtnSettingsOABRect))
971+
_showGuiSettingsOAB = false;
972+
973+
MicroWindow stageInfoOabWindow = MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB);
974+
List<MicroEntry> stageInfoOabEntries = stageInfoOabWindow.Entries;
975+
Torque torqueEntry = (Torque)stageInfoOabEntries.Find(e => e.Name == "Torque");
976+
977+
torqueEntry.IsActive = GUILayout.Toggle(torqueEntry.IsActive, "Display Torque (experimental)\nTurn on CoT & CoM for this", MicroStyles.SectionToggleStyle);
978+
}
898979
#endregion
899980

900981
/// <summary>
@@ -907,6 +988,16 @@ private bool CloseButton(Rect rect)
907988
return GUI.Button(rect, "X", MicroStyles.CloseBtnStyle);
908989
}
909990

991+
/// <summary>
992+
/// Draws a Settings butoon (≡)
993+
/// </summary>
994+
/// <param name="settingsOABRect"></param>
995+
/// <returns></returns>
996+
private bool SettingsButton(Rect rect)
997+
{
998+
return GUI.Button(rect, "≡", MicroStyles.SettingsBtnStyle);
999+
}
1000+
9101001
#region Window and data initialization
9111002
/// <summary>
9121003
/// Builds the list of all Entries
@@ -979,6 +1070,7 @@ private void InitializeEntries()
9791070
MicroEntries.Add(new TotalDeltaVASL_OAB());
9801071
MicroEntries.Add(new TotalDeltaVActual_OAB());
9811072
MicroEntries.Add(new TotalDeltaVVac_OAB());
1073+
MicroEntries.Add(new Torque());
9821074
MicroEntries.Add(new StageInfo_OAB());
9831075
#endregion
9841076
}

MicroEngineerProject/MicroEngineer/MicroEntries.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,68 @@ public override string ValueDisplay
13541354
}
13551355
}
13561356

1357+
/// <summary>
1358+
/// Calculates torque from the Center of Thrust and Center of Mass
1359+
/// </summary>
1360+
public class Torque : MicroEntry
1361+
{
1362+
[JsonProperty]
1363+
internal bool IsActive = false;
1364+
1365+
public Torque()
1366+
{
1367+
Name = "Torque";
1368+
Description = "Thrust torque that is generated by not having Thrust Vector and Center of Mass aligned. Turn on the Center of Thrust and Center of Mass VAB indicators to get an accurate value.";
1369+
Category = MicroEntryCategory.OAB;
1370+
Unit = null;
1371+
Formatting = null;
1372+
}
1373+
1374+
public override void RefreshData()
1375+
{
1376+
if (!this.IsActive)
1377+
return;
1378+
1379+
Vector3d com = GameManager.Instance?.Game?.OAB?.Current?.Stats?.MainAssembly?.CenterOfProperties?.CoM ?? Vector3d.zero;
1380+
Vector3d cot = GameManager.Instance?.Game?.OAB?.Current?.Stats?.MainAssembly?.CenterOfProperties?.CoT ?? Vector3d.zero;
1381+
1382+
if (com == Vector3d.zero || cot == Vector3d.zero)
1383+
return;
1384+
1385+
List<DeltaVEngineInfo> engines = GameManager.Instance?.Game?.OAB?.Current?.Stats?.MainAssembly?.VesselDeltaV?.EngineInfo;
1386+
if (engines == null || engines.Count == 0)
1387+
return;
1388+
1389+
Vector3d force = new Vector3d();
1390+
1391+
foreach (var engine in engines)
1392+
{
1393+
force += engine.ThrustVectorVac;
1394+
}
1395+
1396+
var leverArm = cot - com;
1397+
1398+
Vector3d torque = Vector3d.Cross(force, (Vector3d)leverArm);
1399+
1400+
this.EntryValue = torque.magnitude;
1401+
this.Unit = (double)EntryValue >= 1.0 ? "kNm" : "Nm";
1402+
}
1403+
1404+
public override string ValueDisplay
1405+
{
1406+
get
1407+
{
1408+
if (EntryValue == null || !this.IsActive)
1409+
return "-";
1410+
1411+
if ((double)EntryValue >= 1.0)
1412+
return $"{String.Format("{0:F2}", EntryValue)}";
1413+
1414+
return Math.Abs((double)EntryValue) > double.Epsilon ? $"{String.Format("{0:F2}", (double)EntryValue * 1000.0)}" : $"{String.Format("{0:F0}", (double)EntryValue)}";
1415+
}
1416+
}
1417+
}
1418+
13571419
/// <summary>
13581420
/// Holds stage info parameters for each stage. Also keeps information about the celestial body user selected in the window.
13591421
/// </summary>

MicroEngineerProject/MicroEngineer/MicroStyles.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ public static class MicroStyles
88
public static int WindowWidth = 290;
99
public static int WindowHeight = 1440;
1010
public static int WindowWidthStageOAB = 645;
11-
//public static int WindowWidthCelestialSelection = 200;
11+
public static int WindowWidthSettingsOAB = 300;
1212

1313
public static GUISkin SpaceWarpUISkin;
1414
public static GUIStyle MainWindowStyle;
1515
public static GUIStyle PopoutWindowStyle;
1616
public static GUIStyle EditWindowStyle;
1717
public static GUIStyle StageOABWindowStyle;
1818
public static GUIStyle CelestialSelectionStyle;
19+
public static GUIStyle SettingsOabStyle;
1920
public static GUIStyle PopoutBtnStyle;
2021
public static GUIStyle SectionToggleStyle;
2122
public static GUIStyle NameLabelStyle;
@@ -29,6 +30,7 @@ public static class MicroStyles
2930
public static GUIStyle WindowSelectionTextFieldStyle;
3031
public static GUIStyle WindowSelectionAbbrevitionTextFieldStyle;
3132
public static GUIStyle CloseBtnStyle;
33+
public static GUIStyle SettingsBtnStyle;
3234
public static GUIStyle CloseBtnStageOABStyle;
3335
public static GUIStyle NormalBtnStyle;
3436
public static GUIStyle CelestialBodyBtnStyle;
@@ -51,6 +53,8 @@ public static class MicroStyles
5153

5254
public static Rect CloseBtnRect = new Rect(MicroStyles.WindowWidth - 23, 6, 16, 16);
5355
public static Rect CloseBtnStagesOABRect = new Rect(MicroStyles.WindowWidthStageOAB - 23, 6, 16, 16);
56+
public static Rect CloseBtnSettingsOABRect = new Rect(MicroStyles.WindowWidthSettingsOAB - 23, 6, 16, 16);
57+
public static Rect SettingsOABRect = new Rect(MicroStyles.WindowWidthStageOAB - 50, 6, 16, 16);
5458
public static Rect EditWindowRect = new Rect(Screen.width * 0.5f - MicroStyles.WindowWidth / 2, Screen.height * 0.2f, MicroStyles.WindowWidth, 0);
5559

5660
public static void InitializeStyles()
@@ -88,6 +92,13 @@ public static void InitializeStyles()
8892
contentOffset = new Vector2(0, -22)
8993
};
9094

95+
SettingsOabStyle = new GUIStyle(SpaceWarpUISkin.window)
96+
{
97+
padding = new RectOffset(8, 8, 0, 16),
98+
contentOffset = new Vector2(0, -22),
99+
fixedWidth = WindowWidthSettingsOAB
100+
};
101+
91102
PopoutBtnStyle = new GUIStyle(SpaceWarpUISkin.button)
92103
{
93104
alignment = TextAnchor.MiddleCenter,
@@ -170,6 +181,11 @@ public static void InitializeStyles()
170181
fontSize = 8
171182
};
172183

184+
SettingsBtnStyle = new GUIStyle(SpaceWarpUISkin.button)
185+
{
186+
fontSize = 24
187+
};
188+
173189
NormalBtnStyle = new GUIStyle(SpaceWarpUISkin.button)
174190
{
175191
alignment = TextAnchor.MiddleCenter

0 commit comments

Comments
 (0)