/
MechJebModuleRoverWindow.cs
170 lines (139 loc) · 7.52 KB
/
MechJebModuleRoverWindow.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
extern alias JetBrainsAnnotations;
using System.Collections;
using JetBrainsAnnotations::JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
namespace MuMech
{
[UsedImplicitly]
public class MechJebModuleRoverWindow : DisplayModule
{
public MechJebModuleRoverController autopilot;
public MechJebModuleRoverWindow(MechJebCore core) : base(core) { }
public override void OnStart(PartModule.StartState state) => autopilot = Core.GetComputerModule<MechJebModuleRoverController>();
public override string GetName() => Localizer.Format("#MechJeb_Rover_title"); // "Rover Autopilot"
public override string IconName() => "Rover Autopilot";
protected override GUILayoutOption[] WindowOptions() => new[] { GUILayout.Width(200), GUILayout.Height(50) };
protected override void WindowGUI(int windowID)
{
MechJebModuleCustomWindowEditor ed = Core.GetComputerModule<MechJebModuleCustomWindowEditor>();
bool alt = GameSettings.MODIFIER_KEY.GetKey();
if (GUI.Button(new Rect(WindowPos.width - 48, 0, 13, 20), "?", GuiUtils.YellowOnHover))
{
MechJebModuleWaypointHelpWindow help = Core.GetComputerModule<MechJebModuleWaypointHelpWindow>();
help.SelTopic = ((IList)help.Topics).IndexOf("Controller");
help.Enabled = help.SelTopic > -1 || help.Enabled;
}
ed.registry.Find(i => i.id == "Toggle:RoverController.ControlHeading").DrawItem();
GUILayout.BeginHorizontal();
ed.registry.Find(i => i.id == "Editable:RoverController.heading").DrawItem();
if (GUILayout.Button("-", GUILayout.Width(18))) { autopilot.heading.Val -= GameSettings.MODIFIER_KEY.GetKey() ? 5 : 1; }
if (GUILayout.Button("+", GUILayout.Width(18))) { autopilot.heading.Val += GameSettings.MODIFIER_KEY.GetKey() ? 5 : 1; }
GUILayout.EndHorizontal();
ed.registry.Find(i => i.id == "Value:RoverController.headingErr").DrawItem();
ed.registry.Find(i => i.id == "Toggle:RoverController.ControlSpeed").DrawItem();
GUILayout.BeginHorizontal();
ed.registry.Find(i => i.id == "Editable:RoverController.speed").DrawItem();
if (GUILayout.Button("-", GUILayout.Width(18))) { autopilot.speed.Val -= GameSettings.MODIFIER_KEY.GetKey() ? 5 : 1; }
if (GUILayout.Button("+", GUILayout.Width(18))) { autopilot.speed.Val += GameSettings.MODIFIER_KEY.GetKey() ? 5 : 1; }
GUILayout.EndHorizontal();
ed.registry.Find(i => i.id == "Value:RoverController.speedErr").DrawItem();
ed.registry.Find(i => i.id == "Toggle:RoverController.StabilityControl").DrawItem();
if (!Core.Settings.hideBrakeOnEject)
{
ed.registry.Find(i => i.id == "Toggle:RoverController.BrakeOnEject").DrawItem();
}
ed.registry.Find(i => i.id == "Toggle:RoverController.BrakeOnEnergyDepletion").DrawItem();
if (autopilot.BrakeOnEnergyDepletion)
{
ed.registry.Find(i => i.id == "Toggle:RoverController.WarpToDaylight").DrawItem();
}
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_Rover_label1"), GUILayout.ExpandWidth(true)); // "Target Speed"
GUILayout.Label(autopilot.tgtSpeed.ToString("F1"), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_Rover_label2"), GUILayout.ExpandWidth(true)); // "Waypoints"
GUILayout.Label("Index " + (autopilot.WaypointIndex + 1) + " of " + autopilot.Waypoints.Count, GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
// GUILayout.Label("Debug1: " + autopilot.debug1.ToString("F3"));
GUILayout.BeginHorizontal();
if (Core.Target != null && Core.Target.Target != null)
{
Vessel vssl = Core.Target.Target.GetVessel();
if (GUILayout.Button(Localizer.Format("#MechJeb_Rover_button1"))) // "To Target"
{
Core.GetComputerModule<MechJebModuleWaypointWindow>().SelIndex = -1;
autopilot.WaypointIndex = 0;
autopilot.Waypoints.Clear();
if (vssl != null) { autopilot.Waypoints.Add(new MechJebWaypoint(vssl, 25f)); }
else { autopilot.Waypoints.Add(new MechJebWaypoint(Core.Target.GetPositionTargetPosition())); }
autopilot.ControlHeading = autopilot.ControlSpeed = true;
Vessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, false);
autopilot.LoopWaypoints = alt;
}
if (GUILayout.Button(Localizer.Format("#MechJeb_Rover_button2"))) // "Add Target"
{
if (vssl != null) { autopilot.Waypoints.Add(new MechJebWaypoint(vssl, 25f)); }
else { autopilot.Waypoints.Add(new MechJebWaypoint(Core.Target.GetPositionTargetPosition())); }
}
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (autopilot.Waypoints.Count > 0)
{
if (!autopilot.ControlHeading || !autopilot.ControlSpeed)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_Rover_button3")))
{
// "Drive"
autopilot.WaypointIndex = Mathf.Max(0, alt ? 0 : autopilot.WaypointIndex);
autopilot.ControlHeading = autopilot.ControlSpeed = true;
// autopilot.LoopWaypoints = alt;
}
}
else if (GUILayout.Button(Localizer.Format("#MechJeb_Rover_button4")))
{
// "Stop"
// autopilot.WaypointIndex = -1; // more annoying than helpful
autopilot.ControlHeading = autopilot.ControlSpeed = autopilot.LoopWaypoints = false;
}
}
if (GUILayout.Button(Localizer.Format("#MechJeb_Rover_button5")))
{
// "Waypoints"
MechJebModuleWaypointWindow waypoints = Core.GetComputerModule<MechJebModuleWaypointWindow>();
waypoints.Enabled = !waypoints.Enabled;
if (waypoints.Enabled)
{
waypoints.Mode = MechJebModuleWaypointWindow.WaypointMode.ROVER;
}
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
base.WindowGUI(windowID);
}
public override void OnUpdate()
{
if (autopilot != null)
{
if (autopilot.ControlHeading || autopilot.ControlSpeed || autopilot.StabilityControl || autopilot.BrakeOnEnergyDepletion ||
autopilot.BrakeOnEject)
{
autopilot.Users.Add(this);
}
else
{
autopilot.Users.Remove(this);
}
}
}
protected override void OnModuleDisabled()
{
Core.GetComputerModule<MechJebModuleWaypointWindow>().Enabled = false;
Core.GetComputerModule<MechJebModuleWaypointHelpWindow>().Enabled = false;
base.OnModuleDisabled();
}
}
}