Skip to content

Commit

Permalink
Added orbit controls, updated to Space Warp GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
XYZ3211 committed Mar 2, 2023
1 parent a5cf496 commit 6e8daf7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 23 deletions.
116 changes: 94 additions & 22 deletions ManeuverNodeController/ManeuverNodeControllerMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using KSP.Game;
using KSP.Sim.impl;
using KSP.Sim.Maneuver;
using SpaceWarp.API;
using SpaceWarp.API.Mods;
using KSP.UI.Binding;

namespace ManeuverNodeController
{
Expand All @@ -20,10 +22,11 @@ public class ManeuverNodeControllerMod : Mod
private string radialString = "0";
private string smallStepString = "5";
private string bigStepString = "100";
private string timeStepString = "5";
private double smallStep, bigStep, timeStep;
private string timeSmallStepString = "5";
private string timeLargeStepString = "25";
private double smallStep, bigStep, timeSmallStep, timeLargeStep;

private bool pInc1, pInc2, pDec1, pDec2, nInc1, nInc2, nDec1, nDec2, rInc1, rInc2, rDec1, rDec2, timeInc, timeDec;
private bool pInc1, pInc2, pDec1, pDec2, nInc1, nInc2, nDec1, nDec2, rInc1, rInc2, rDec1, rDec2, timeInc1, timeInc2, timeDec1, timeDec2, orbitInc, orbitDec;
private bool advancedMode;

private ManeuverNodeData currentNode = null;
Expand All @@ -41,9 +44,24 @@ public override void OnInitialized()
{
Destroy(this);
}

loaded = true;

gameObject.hideFlags = HideFlags.HideAndDontSave;
DontDestroyOnLoad(gameObject);

SpaceWarpManager.RegisterAppButton(
"Maneuver Node Cont.",
"BTN-ManeuverNodeController",
SpaceWarpManager.LoadIcon(),
ToggleButton);
}

void ToggleButton(bool toggle)
{
interfaceEnabled = toggle;
GameObject.Find("BTN-MNC")?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(toggle);
}

void Awake()
{
windowRect = new Rect((Screen.width * 0.7f) - (windowWidth / 2), (Screen.height / 2) - (windowHeight / 2), 0, 0);
Expand All @@ -54,21 +72,25 @@ void Update()
if (Input.GetKey(KeyCode.LeftAlt) && Input.GetKeyDown(KeyCode.N))
{
interfaceEnabled = !interfaceEnabled;
Logger.Info("UI toggled with hotkey");
}
}

void OnGUI()
{
if (interfaceEnabled)
{
GUI.skin = SpaceWarpManager.Skin;

windowRect = GUILayout.Window(
GUIUtility.GetControlID(FocusType.Passive),
windowRect,
FillWindow,
"Maneuver Node Controller",
"<color=#696DFF>// MANEUVER NODE CONTROLLER</color>",
GUILayout.Height(0),
GUILayout.Width(350));
}

}

private void FillWindow(int windowID)
Expand Down Expand Up @@ -101,21 +123,28 @@ private void FillWindow(int windowID)
}
else
{

GUILayout.BeginHorizontal();
GUILayout.Label($"Total Maneuver dV (m/s): {currentNode.BurnRequiredDV.ToString("n2")}");
GUILayout.Label($"Total Maneuver dV (m/s): ");
GUILayout.FlexibleSpace();
GUILayout.Label(currentNode.BurnRequiredDV.ToString("n2"));
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label($"Prograde dV (m/s): {currentNode.BurnVector.z.ToString("n2")}");
GUILayout.Label($"Prograde dV (m/s): ");
GUILayout.FlexibleSpace();
GUILayout.Label(currentNode.BurnVector.z.ToString("n2"));
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label($"Normal dV (m/s): {currentNode.BurnVector.y.ToString("n2")}");
GUILayout.Label($"Normal dV (m/s): ");
GUILayout.FlexibleSpace();
GUILayout.Label(currentNode.BurnVector.y.ToString("n2"));
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label($"Radial dV (m/s): {currentNode.BurnVector.x.ToString("n2")}");
GUILayout.Label($"Radial dV (m/s): ");
GUILayout.FlexibleSpace();
GUILayout.Label(currentNode.BurnVector.x.ToString("n2"));
GUILayout.EndHorizontal();

GUILayout.Box("", horizontalDivider);
Expand Down Expand Up @@ -160,8 +189,8 @@ private void drawAdvancedMode()
{
ManeuverNodeData nodeData = GameManager.Instance.Game.SpaceSimulation.Maneuvers.GetNodesForVessel(GameManager.Instance.Game.ViewController.GetActiveVehicle(true).Guid)[0];
//nodeData.BurnVector = burnParams;
GameManager.Instance.Game.UniverseModel.FindVesselComponent(nodeData.RelatedSimID)?.SimulationObject.FindComponent<ManeuverPlanComponent>().UpdateChangeOnNode(nodeData, burnParams);
GameManager.Instance.Game.UniverseModel.FindVesselComponent(nodeData.RelatedSimID)?.SimulationObject.FindComponent<ManeuverPlanComponent>().RefreshManeuverNodeState(0);
game.UniverseModel.FindVesselComponent(nodeData.RelatedSimID)?.SimulationObject.FindComponent<ManeuverPlanComponent>().UpdateChangeOnNode(nodeData, burnParams);
game.UniverseModel.FindVesselComponent(nodeData.RelatedSimID)?.SimulationObject.FindComponent<ManeuverPlanComponent>().RefreshManeuverNodeState(0);
Logger.Info(nodeData.ToString());
}
}
Expand Down Expand Up @@ -213,25 +242,49 @@ private void drawSimpleMode()
GUILayout.Box("", horizontalDivider);

GUILayout.BeginHorizontal();
GUILayout.Label("Time Step (seconds): ", GUILayout.Width(windowWidth / 2));
timeStepString = GUILayout.TextField(timeStepString);
double.TryParse(timeStepString, out timeStep);
GUILayout.Label("Small Time Step (seconds): ", GUILayout.Width(windowWidth / 2));
timeSmallStepString = GUILayout.TextField(timeSmallStepString);
double.TryParse(timeSmallStepString, out timeSmallStep);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("Large Time Step (seconds): ", GUILayout.Width(windowWidth / 2));
timeLargeStepString = GUILayout.TextField(timeLargeStepString);
double.TryParse(timeLargeStepString, out timeLargeStep);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
timeDec = GUILayout.Button("<", GUILayout.Width(windowWidth / 7));
timeDec2 = GUILayout.Button("<<", GUILayout.Width(windowWidth / 9));
timeDec1 = GUILayout.Button("<", GUILayout.Width(windowWidth / 9));
GUILayout.FlexibleSpace();
GUILayout.Label("Time", labelStyle);
GUILayout.FlexibleSpace();
timeInc = GUILayout.Button(">", GUILayout.Width(windowWidth / 7));
timeInc1 = GUILayout.Button(">", GUILayout.Width(windowWidth / 9));
timeInc2 = GUILayout.Button(">>", GUILayout.Width(windowWidth / 9));
GUILayout.EndHorizontal();

GUILayout.Box("", horizontalDivider);

GUILayout.BeginHorizontal();
GUILayout.Label("Maneuver Node in: ");
GUILayout.FlexibleSpace();
GUILayout.Label($"{((currentNode.Time - game.UniverseModel.UniversalTime) / game.UniverseModel.FindVesselComponent(currentNode.RelatedSimID).Orbit.period).ToString("n0")} orbit(s) ");
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
orbitDec = GUILayout.Button("-", GUILayout.Width(windowWidth / 7));
GUILayout.FlexibleSpace();
GUILayout.Label("Orbit", labelStyle);
GUILayout.FlexibleSpace();
orbitInc = GUILayout.Button("+", GUILayout.Width(windowWidth / 7));
GUILayout.EndHorizontal();

handleButtons();
}

private void handleButtons()
{
if (pInc1 || pInc2 || pDec1 || pDec2 || nInc1 || nInc2 || nDec1 || nDec2 || rInc1 || rInc2 || rDec1 || rDec2 || timeDec || timeInc)
if (pInc1 || pInc2 || pDec1 || pDec2 || nInc1 || nInc2 || nDec1 || nDec2 || rInc1 || rInc2 || rDec1 || rDec2 || timeDec1 || timeDec2 || timeInc1 || timeInc2 || orbitDec || orbitInc)
{
if (currentNode != null)
{
Expand Down Expand Up @@ -284,13 +337,32 @@ private void handleButtons()
{
burnParams.x -= bigStep;
}
else if (timeDec)
else if (timeDec1)
{
currentNode.Time -= timeSmallStep;
}
else if (timeDec2)
{
currentNode.Time -= timeLargeStep;
}
else if (timeInc1)
{
currentNode.Time += timeSmallStep;
}
else if (timeInc2)
{
currentNode.Time += timeLargeStep;
}
else if (orbitDec)
{
currentNode.Time -= timeStep;
if (game.UniverseModel.FindVesselComponent(currentNode.RelatedSimID).Orbit.period < (currentNode.Time- game.UniverseModel.UniversalTime))
{
currentNode.Time -= game.UniverseModel.FindVesselComponent(currentNode.RelatedSimID).Orbit.period;
}
}
else if (timeInc)
else if (orbitInc)
{
currentNode.Time += timeStep;
currentNode.Time += game.UniverseModel.FindVesselComponent(currentNode.RelatedSimID).Orbit.period;
}

game.UniverseModel.FindVesselComponent(currentNode.RelatedSimID)?.SimulationObject.FindComponent<ManeuverPlanComponent>().UpdateChangeOnNode(currentNode, burnParams);
Expand Down
2 changes: 1 addition & 1 deletion modinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Maneuver Node Controller",
"description": "Provides an interface to finely tune maneuver nodes.",
"source": "https://github.com/XYZ3211/ManeuverNodeController",
"version": "0.01",
"version": "0.03",
"dependencies": [],
"ksp2_version": {
"min": "0",
Expand Down

0 comments on commit 6e8daf7

Please sign in to comment.