forked from BahamutoD/VesselMover
-
Notifications
You must be signed in to change notification settings - Fork 5
/
VesselMoverToolbar.cs
318 lines (280 loc) · 11.3 KB
/
VesselMoverToolbar.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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KSP.UI.Screens;
namespace VesselMover
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class VesselMoverToolbar : MonoBehaviour
{
public static bool hasAddedButton = false;
public static bool toolbarGuiEnabled = false;
public static bool addCrewMembers = true;
public static bool selectCrewMembers = false;
public static bool ShowUI = true;
private static bool latch;
internal static GUIStyle ButtonToggledStyle;
// ReSharper disable once PossibleNullReferenceException
internal static GUIStyle ToolTipStyle;
internal static List<ProtoCrewMember> SelectedCrewMembers = new List<ProtoCrewMember>();
private Rect toolbarRect;
private float toolbarWidth = 280;
private float toolbarHeight = 0;
private float toolbarMargin = 6;
private float toolbarLineHeight = 20;
private float contentWidth;
private Vector2 toolbarPosition;
private Rect svRectScreenSpace;
private Rect svCrewScreenSpace;
private bool showMoveHelp = false;
private float helpHeight;
private Rect _crewSelectRect;
private float _crewSelectWidth = 300;
private float _crewSelectHeight = 300;
private static Vector2 _crewSelectPosition;
private static Vector2 _displayViewerPosition = Vector2.zero;
private static Rect Position;
private void Start()
{
GameEvents.onHideUI.Add(OnHideUI);
GameEvents.onShowUI.Add(OnShowUI);
toolbarPosition = new Vector2(Screen.width - toolbarWidth - 80, 39);
toolbarRect = new Rect(toolbarPosition.x, toolbarPosition.y, toolbarWidth, toolbarHeight);
_crewSelectPosition = new Vector2(Screen.width/2 - _crewSelectWidth/2, Screen.height/2 - _crewSelectHeight /2);
_crewSelectRect = new Rect(_crewSelectPosition.x, _crewSelectPosition.y, _crewSelectWidth, _crewSelectHeight);
contentWidth = toolbarWidth - (2 * toolbarMargin);
SelectedCrewMembers = new List<ProtoCrewMember>();
AddToolbarButton();
}
private void OnDestroy()
{
GameEvents.onHideUI.Remove(OnHideUI);
GameEvents.onShowUI.Remove(OnShowUI);
}
private void OnGUI()
{
SetGuiStyles();
if (ShowUI && addCrewMembers && VesselSpawn.IsSelectingCrew)
{
_crewSelectRect = GUILayout.Window(401239, _crewSelectRect, CrewSelectionWindow, "Select Crew", HighLogic.Skin.window);
if (!latch) Debug.Log(_crewSelectRect.ToString());
latch = true;
}
if (!ShowUI || !toolbarGuiEnabled || !VesselMove.Instance || !VesselSpawn.instance ||
VesselSpawn.instance.openingCraftBrowser || VesselSpawn.IsSelectingCrew) return;
toolbarRect = GUI.Window(401240, toolbarRect, ToolbarWindow, "Vessel Mover", HighLogic.Skin.window);
if (!VesselMove.Instance.IsMovingVessel)
{
if (!MouseIsInRect(svRectScreenSpace)) return;
Vector2 mousePos = MouseGUIPos();
//Rect warningRect = new Rect(mousePos.x + 5, mousePos.y + 20, 200, 60);
ShowToolTip(mousePos, "WARNING: Experimental. Launch clamps may be broken.");
}
else if (showMoveHelp)
{
GUI.Window(401241, new Rect(toolbarRect.x, toolbarRect.y + toolbarRect.height, toolbarRect.width, helpHeight), MoveHelp, "Controls", HighLogic.Skin.window);
}
}
private void ToolbarWindow(int windowID)
{
float line = 0;
line += 1.25f;
if (FlightGlobals.ActiveVessel && (FlightGlobals.ActiveVessel.LandedOrSplashed || VesselMove.Instance.IsMovingVessel))
{
if (!VesselMove.Instance.IsMovingVessel)
{
if (GUI.Button(LineRect(ref line, 1.5f), "Move Vessel", HighLogic.Skin.button))
{
VesselMove.Instance.StartMove(FlightGlobals.ActiveVessel, true);
}
line += 0.2f;
Rect spawnVesselRect = LineRect(ref line);
svRectScreenSpace = new Rect(spawnVesselRect);
svRectScreenSpace.x += toolbarRect.x;
svRectScreenSpace.y += toolbarRect.y;
if (GUI.Button(spawnVesselRect, "Spawn Vessel", HighLogic.Skin.button))
{
VesselSpawn.instance.StartVesselSpawn();
}
line += .75f;
Rect crewRect1 = LineRect(ref line);
Rect crewRect2 = new Rect(crewRect1.x + crewRect1.width/2 + 5f, crewRect1.y, crewRect1.width / 2, crewRect1.height);
crewRect1.width = crewRect1.width / 2;
svCrewScreenSpace = new Rect(crewRect1);
svCrewScreenSpace.x += toolbarRect.x;
svCrewScreenSpace.y += toolbarRect.y;
addCrewMembers = GUI.Toggle(crewRect1, addCrewMembers, "Spawn Crew");
if (!addCrewMembers) GUI.enabled = false;
selectCrewMembers = GUI.Toggle(crewRect2, selectCrewMembers, "Choose Crew");
GUI.enabled = true;
showMoveHelp = false;
}
else
{
if (GUI.Button(LineRect(ref line, 2), "Place Vessel", HighLogic.Skin.button))
{
VesselMove.Instance.EndMove();
}
line += 0.3f;
if (GUI.Button(LineRect(ref line, 2), "Drop Vessel", HighLogic.Skin.button))
{
VesselMove.Instance.DropMove();
}
line += 0.3f;
if (GUI.Button(LineRect(ref line), "Help", HighLogic.Skin.button))
{
showMoveHelp = !showMoveHelp;
}
}
}
else
{
GUIStyle centerLabelStyle = new GUIStyle(HighLogic.Skin.label);
centerLabelStyle.alignment = TextAnchor.UpperCenter;
GUI.Label(LineRect(ref line), "You need to be landed to use this!", centerLabelStyle);
}
toolbarRect.height = (line * toolbarLineHeight) + (toolbarMargin * 2);
GUI.DragWindow(new Rect(0, 0, Screen.width, 30));
VMUtils.RepositionWindow(ref toolbarRect);
}
private void CrewSelectionWindow(int windowID)
{
KerbalRoster kerbalRoster = HighLogic.CurrentGame.CrewRoster;
GUILayout.BeginVertical();
_displayViewerPosition = GUILayout.BeginScrollView(_displayViewerPosition, GUI.skin.box, GUILayout.Height(250), GUILayout.Width(280));
IEnumerator<ProtoCrewMember> kerbals = kerbalRoster.Kerbals(ProtoCrewMember.RosterStatus.Available).GetEnumerator();
while (kerbals.MoveNext())
{
ProtoCrewMember crewMember = kerbals.Current;
if (crewMember == null) continue;
bool selected = SelectedCrewMembers.Contains(crewMember);
GUIStyle buttonStyle = selected ? ButtonToggledStyle : HighLogic.Skin.button;
selected = GUILayout.Toggle(selected, $"{crewMember.name}, {crewMember.gender}, {crewMember.trait}", buttonStyle);
if (selected && !SelectedCrewMembers.Contains(crewMember))
{
SelectedCrewMembers.Clear();
SelectedCrewMembers.Add(crewMember);
}
else if (!selected && SelectedCrewMembers.Contains(crewMember))
{
SelectedCrewMembers.Clear();
}
}
kerbals.Dispose();
GUILayout.EndScrollView();
GUILayout.Space(20);
if (GUILayout.Button("Select", HighLogic.Skin.button))
{
VesselSpawn.SelectedCrewData = SelectedCrewMembers;
VesselSpawn.IsSelectingCrew = false;
VesselSpawn.IsCrewSelected = true;
}
GUILayout.EndVertical();
}
private Rect LineRect(ref float currentLine, float heightFactor = 1)
{
Rect rect = new Rect(toolbarMargin, toolbarMargin + (currentLine * toolbarLineHeight), contentWidth, toolbarLineHeight * heightFactor);
currentLine += heightFactor + 0.1f;
return rect;
}
private void MoveHelp(int windowID)
{
float line = 0;
line += 1.25f;
LineLabel("Movement: " + GameSettings.PITCH_DOWN.primary.ToString() + " " +
GameSettings.PITCH_UP.primary.ToString() + " " +
GameSettings.YAW_LEFT.primary.ToString() + " " +
GameSettings.YAW_RIGHT.primary.ToString(), ref line);
LineLabel("Roll: " + GameSettings.ROLL_LEFT.primary.ToString() + " " +
GameSettings.ROLL_RIGHT.primary.ToString(), ref line);
LineLabel("Pitch: " + GameSettings.TRANSLATE_DOWN.primary.ToString() + " " +
GameSettings.TRANSLATE_UP.primary.ToString(), ref line);
LineLabel("Yaw: " + GameSettings.TRANSLATE_LEFT.primary.ToString() + " " +
GameSettings.TRANSLATE_RIGHT.primary.ToString(), ref line);
LineLabel("Auto rotate rocket: " + GameSettings.TRANSLATE_BACK.primary.ToString(), ref line);
LineLabel("Auto rotate plane: " + GameSettings.TRANSLATE_FWD.primary.ToString(), ref line);
LineLabel("Change movement mode: Tab", ref line);
LineLabel("Reset Altitude: " + GameSettings.THROTTLE_CUTOFF.primary.ToString(), ref line);
LineLabel("Adjust Altitude: " + GameSettings.THROTTLE_UP.primary.ToString() + " " +
GameSettings.THROTTLE_DOWN.primary.ToString(), ref line);
helpHeight = (line * toolbarLineHeight) + (toolbarMargin * 2);
}
private void LineLabel(string label, ref float line)
{
GUI.Label(LineRect(ref line), label, HighLogic.Skin.label);
}
private void AddToolbarButton()
{
if (HighLogic.LoadedSceneIsFlight)
{
if (!hasAddedButton)
{
Texture buttonTexture = GameDatabase.Instance.GetTexture("VesselMover/Textures/icon", false);
ApplicationLauncher.Instance.AddModApplication(ShowToolbarGUI, HideToolbarGUI, Dummy, Dummy, Dummy, Dummy, ApplicationLauncher.AppScenes.FLIGHT, buttonTexture);
hasAddedButton = true;
}
}
}
public void ShowToolbarGUI()
{
VesselMoverToolbar.toolbarGuiEnabled = true;
}
public void HideToolbarGUI()
{
VesselMoverToolbar.toolbarGuiEnabled = false;
}
private void Dummy()
{ }
public static bool MouseIsInRect(Rect rect)
{
return rect.Contains(MouseGUIPos());
}
public static Vector2 MouseGUIPos()
{
return new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 0);
}
public void OnHideUI()
{
ShowUI = false;
}
public void OnShowUI()
{
ShowUI = true;
}
internal static void ShowToolTip(Vector2 toolTipPos, string toolTip)
{
Vector2 size = ToolTipStyle.CalcSize(new GUIContent(toolTip));
Position = new Rect(toolTipPos.x + 5, toolTipPos.y + 5, size.x, size.y);
RepositionToolTip();
GUI.Window(0, Position, EmptyWindow, toolTip, ToolTipStyle);
GUI.BringWindowToFront(0);
}
internal void SetGuiStyles()
{
ButtonToggledStyle = new GUIStyle(HighLogic.Skin.button);
ButtonToggledStyle.normal.background = ButtonToggledStyle.onActive.background;
ToolTipStyle = new GUIStyle(GUI.skin.textArea)
{
border = new RectOffset(4, 4, 4, 4),
padding = new RectOffset(5, 5, 5, 5),
alignment = TextAnchor.MiddleLeft,
fontStyle = FontStyle.Italic,
wordWrap = false,
normal = { textColor = Color.green },
hover = { textColor = Color.green }
};
ToolTipStyle.hover.background = ToolTipStyle.normal.background;
}
private static void RepositionToolTip()
{
if (Position.xMax > Screen.width)
Position.x = Screen.width - Position.width;
if (Position.yMax > Screen.height)
Position.y = Screen.height - Position.height;
}
private static void EmptyWindow(int windowId)
{
}
}
}