-
Notifications
You must be signed in to change notification settings - Fork 131
/
FlightGUI.cs
434 lines (362 loc) · 15.7 KB
/
FlightGUI.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
/*
Ferram Aerospace Research v0.15.9.1 "Liepmann"
=========================
Aerodynamics model for Kerbal Space Program
Copyright 2017, Michael Ferrara, aka Ferram4
This file is part of Ferram Aerospace Research.
Ferram Aerospace Research is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ferram Aerospace Research is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ferram Aerospace Research. If not, see <http://www.gnu.org/licenses/>.
Serious thanks: a.g., for tons of bugfixes and code-refactorings
stupid_chris, for the RealChuteLite implementation
Taverius, for correcting a ton of incorrect values
Tetryds, for finding lots of bugs and issues and not letting me get away with them, and work on example crafts
sarbian, for refactoring code for working with MechJeb, and the Module Manager updates
ialdabaoth (who is awesome), who originally created Module Manager
Regex, for adding RPM support
DaMichel, for some ferramGraph updates and some control surface-related features
Duxwing, for copy editing the readme
CompatibilityChecker by Majiir, BSD 2-clause http://opensource.org/licenses/BSD-2-Clause
Part.cfg changes powered by sarbian & ialdabaoth's ModuleManager plugin; used with permission
http://forum.kerbalspaceprogram.com/threads/55219
ModularFLightIntegrator by Sarbian, Starwaster and Ferram4, MIT: http://opensource.org/licenses/MIT
http://forum.kerbalspaceprogram.com/threads/118088
Toolbar integration powered by blizzy78's Toolbar plugin; used with permission
http://forum.kerbalspaceprogram.com/threads/60863
*/
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using KSP;
using KSP.UI.Screens;
using KSP.Localization;
using StringLeakTest;
using FerramAerospaceResearch.FARAeroComponents;
using ferram4;
namespace FerramAerospaceResearch.FARGUI.FARFlightGUI
{
public class FlightGUI : VesselModule
{
Vessel _vessel;
FARVesselAero _vesselAero;
static bool showGUI = false;
public static bool showAllGUI = true;
public static bool savedShowGUI = true;
static Rect mainGuiRect;
static Rect dataGuiRect;
static Rect settingsGuiRect;
static IButton blizzyFlightGUIButton;
static int activeFlightGUICount = 0;
static int frameCountForSaving = 0;
public static Dictionary<Vessel, FlightGUI> vesselFlightGUI;
private StringBuilder _strBuilder = new StringBuilder();
PhysicsCalcs _physicsCalcs;
VesselFlightInfo infoParameters;
public VesselFlightInfo InfoParameters
{
get { return infoParameters; }
}
FlightStatusGUI _flightStatusGUI;
StabilityAugmentation _stabilityAugmentation;
FlightDataGUI _flightDataGUI;
AeroVisualizationGUI _aeroVizGUI;
public AeroVisualizationGUI AeroVizGUI
{
get { return _aeroVizGUI; }
}
AirspeedSettingsGUI _airSpeedGUI;
public AirspeedSettingsGUI airSpeedGUI
{
get { return _airSpeedGUI; }
}
bool showFlightDataWindow = false;
bool showSettingsWindow = false;
internal static GUIStyle boxStyle = null;
internal static GUIStyle buttonStyle = null;
GUIDropDown<int> settingsWindow;
protected override void OnAwake()
{
if (vesselFlightGUI == null)
{
vesselFlightGUI = new Dictionary<Vessel, FlightGUI>();
}
}
protected override void OnStart()
{
base.OnStart();
if (!CompatibilityChecker.IsAllCompatible())
{
this.enabled = false;
return;
}
showGUI = savedShowGUI;
//since we're sharing the button, we need these shenanigans now
if (FARDebugAndSettings.FARDebugButtonStock && HighLogic.LoadedSceneIsFlight)
if (showGUI)
FARDebugAndSettings.FARDebugButtonStock.SetTrue(false);
else
FARDebugAndSettings.FARDebugButtonStock.SetFalse(false);
_vessel = GetComponent<Vessel>();
_vesselAero = GetComponent<FARVesselAero>();
_physicsCalcs = new PhysicsCalcs(_vessel, _vesselAero);
_flightStatusGUI = new FlightStatusGUI();
_stabilityAugmentation = new StabilityAugmentation(_vessel);
_flightDataGUI = new FlightDataGUI();
_aeroVizGUI = new AeroVisualizationGUI();
settingsWindow = new GUIDropDown<int>(new string[4] { Localizer.Format("FARFlightGUIWindowSelect0"), Localizer.Format("FARFlightGUIWindowSelect1"), Localizer.Format("FARFlightGUIWindowSelect2"), Localizer.Format("FARFlightGUIWindowSelect3") }, new int[4] { 0, 1, 2, 3 }, 0);
//boxStyle.padding = new RectOffset(4, 4, 4, 4);
if (vesselFlightGUI.ContainsKey(_vessel))
vesselFlightGUI[_vessel] = this;
else
vesselFlightGUI.Add(_vessel, this);
this.enabled = true;
if(FARDebugValues.useBlizzyToolbar)
GenerateBlizzyToolbarButton();
activeFlightGUICount++;
if(_vessel == FlightGlobals.ActiveVessel || FlightGlobals.ActiveVessel == null)
LoadConfigs();
GameEvents.onShowUI.Add(ShowUI);
GameEvents.onHideUI.Add(HideUI);
}
void OnDestroy()
{
FlightGUIDrawer.SetGUIActive(this,false);
GameEvents.onShowUI.Remove(ShowUI);
GameEvents.onHideUI.Remove(HideUI);
SaveConfigs();
if (_vessel)
{
vesselFlightGUI.Remove(_vessel);
}
_physicsCalcs = null;
if(_flightDataGUI != null)
_flightDataGUI.SaveSettings();
_flightDataGUI = null;
if(_stabilityAugmentation != null)
_stabilityAugmentation.SaveAndDestroy();
_stabilityAugmentation = null;
if(_airSpeedGUI != null)
_airSpeedGUI.SaveSettings();
_airSpeedGUI = null;
if (_aeroVizGUI != null)
_aeroVizGUI.SaveSettings();
_flightStatusGUI = null;
settingsWindow = null;
activeFlightGUICount--;
if (activeFlightGUICount <= 0)
{
activeFlightGUICount = 0;
if (blizzyFlightGUIButton != null)
ClearBlizzyToolbarButton();
}
savedShowGUI = showGUI;
}
public void SaveData()
{
if (_vessel == FlightGlobals.ActiveVessel)
{
SaveConfigs();
if(_airSpeedGUI != null)
_airSpeedGUI.SaveSettings();
if(_stabilityAugmentation != null)
_stabilityAugmentation.SaveSettings();
if(_flightDataGUI != null)
_flightDataGUI.SaveSettings();
if(_aeroVizGUI != null)
_aeroVizGUI.SaveSettings();
}
}
public static void SaveActiveData()
{
FlightGUI gui;
if (FlightGlobals.ready && FlightGlobals.ActiveVessel != null && vesselFlightGUI != null && vesselFlightGUI.TryGetValue(FlightGlobals.ActiveVessel, out gui))
{
if(gui != null)
gui.SaveData();
}
}
//Receives message from FARVesselAero through _vessel on the recalc being completed
public void UpdateAeroModules(List<FARAeroPartModule> newAeroModules, List<FARWingAerodynamicModel> legacyWingModels)
{
_physicsCalcs.UpdateAeroModules(newAeroModules, legacyWingModels);
}
//Receives a message from any FARWingAerodynamicModel or FARAeroPartModule that has failed to update the GUI
void AerodynamicFailureStatus()
{
if(_flightStatusGUI != null)
_flightStatusGUI.AerodynamicFailureStatus();
}
#region PhysicsAndOrientationBlock
void FixedUpdate()
{
if (_physicsCalcs == null)
return;
infoParameters = _physicsCalcs.UpdatePhysicsParameters();
_stabilityAugmentation.UpdatePhysicsInfo(infoParameters);
_flightStatusGUI.UpdateInfoParameters(infoParameters);
_flightDataGUI.UpdateInfoParameters(infoParameters);
}
void Update()
{
FlightGUIDrawer.SetGUIActive(this,(_vessel == FlightGlobals.ActiveVessel && showGUI && showAllGUI));
if (frameCountForSaving >= 120)
{
SaveActiveData();
frameCountForSaving = 0;
}
else
frameCountForSaving++;
}
#endregion
void LateUpdate()
{
//OnGUIAppLauncherReady();
if (_airSpeedGUI != null)
_airSpeedGUI.ChangeSurfVelocity();
else if (_vessel != null)
_airSpeedGUI = new AirspeedSettingsGUI(_vessel);
}
#region GUI Functions
public void DrawGUI()
{
GUI.skin = HighLogic.Skin;
if(boxStyle == null)
{
boxStyle = new GUIStyle(GUI.skin.box);
boxStyle.normal.textColor = boxStyle.focused.textColor = Color.white;
boxStyle.hover.textColor = boxStyle.active.textColor = Color.yellow;
boxStyle.onNormal.textColor = boxStyle.onFocused.textColor = boxStyle.onHover.textColor = boxStyle.onActive.textColor = Color.green;
boxStyle.padding = new RectOffset(2, 2, 2, 2);
}
if (buttonStyle == null)
{
buttonStyle = new GUIStyle(GUI.skin.button);
buttonStyle.normal.textColor = buttonStyle.focused.textColor = Color.white;
buttonStyle.hover.textColor = buttonStyle.active.textColor = buttonStyle.onActive.textColor = Color.yellow;
buttonStyle.onNormal.textColor = buttonStyle.onFocused.textColor = buttonStyle.onHover.textColor = Color.green;
buttonStyle.padding = new RectOffset(2, 2, 2, 2);
}
if (_vessel == FlightGlobals.ActiveVessel && showGUI && showAllGUI)
{
mainGuiRect = GUILayout.Window(this.GetHashCode(), mainGuiRect, MainFlightGUIWindow, "FAR, v0.15.9.1 'Liepmann'", GUILayout.MinWidth(230));
GUIUtils.ClampToScreen(mainGuiRect);
if (showFlightDataWindow)
{
dataGuiRect = GUILayout.Window(this.GetHashCode() + 1, dataGuiRect, FlightDataWindow, Localizer.Format("FARFlightDataTitle"), GUILayout.MinWidth(150));
GUIUtils.ClampToScreen(dataGuiRect);
}
if (showSettingsWindow)
{
settingsGuiRect = GUILayout.Window(this.GetHashCode() + 2, settingsGuiRect, SettingsWindow, Localizer.Format("FARFlightSettings"), GUILayout.MinWidth(200));
GUIUtils.ClampToScreen(settingsGuiRect);
}
}
}
void MainFlightGUIWindow(int windowId)
{
GUILayout.BeginVertical(GUILayout.Height(100));
GUILayout.BeginHorizontal();
_strBuilder.Length = 0;
_strBuilder.Append(Localizer.Format("FARAbbrevMach"));
_strBuilder.Append(": ");
_strBuilder.Concat((float)(_vesselAero.MachNumber),3).AppendLine();
_strBuilder.AppendFormat(Localizer.Format("FARFlightGUIReynolds"),_vesselAero.ReynoldsNumber);
GUILayout.Box(_strBuilder.ToString(), boxStyle, GUILayout.ExpandWidth(true));
GUILayout.EndHorizontal();
_strBuilder.Length = 0;
_strBuilder.Append(Localizer.Format("FARFlightGUIAtmDens"));
_strBuilder.Concat((float)(vessel.atmDensity),3);
GUILayout.Box(_strBuilder.ToString(), boxStyle, GUILayout.ExpandWidth(true));
_flightStatusGUI.Display();
showFlightDataWindow = GUILayout.Toggle(showFlightDataWindow, Localizer.Format("FARFlightGUIFltDataBtn"), buttonStyle, GUILayout.ExpandWidth(true));
showSettingsWindow = GUILayout.Toggle(showSettingsWindow, Localizer.Format("FARFlightGUIFltSettings"), buttonStyle, GUILayout.ExpandWidth(true));
GUILayout.Label(Localizer.Format("FARFlightGUIFltAssistance"));
_stabilityAugmentation.Display();
GUILayout.EndVertical();
GUI.DragWindow();
}
void FlightDataWindow(int windowId)
{
_flightDataGUI.DataDisplay();
GUI.DragWindow();
}
void SettingsWindow(int windowId)
{
GUILayout.Label(Localizer.Format("FARFlightSettingsLabel"));
settingsWindow.GUIDropDownDisplay();
int selection = settingsWindow.ActiveSelection;
switch (selection)
{
case 0:
if (_flightDataGUI.SettingsDisplay())
dataGuiRect.height = 0;
break;
case 1:
_stabilityAugmentation.SettingsDisplay();
break;
case 2:
_airSpeedGUI.AirSpeedSettings();
break;
case 3:
_aeroVizGUI.SettingsDisplay();
break;
}
GUI.DragWindow();
}
#endregion
#region AppLauncher
private static void ClearBlizzyToolbarButton()
{
blizzyFlightGUIButton.Destroy();
blizzyFlightGUIButton = null;
}
private void GenerateBlizzyToolbarButton()
{
if (blizzyFlightGUIButton == null)
{
blizzyFlightGUIButton = ToolbarManager.Instance.add("FerramAerospaceResearch", "FARFlightButtonBlizzy");
blizzyFlightGUIButton.TexturePath = "FerramAerospaceResearch/Textures/icon_button_blizzy";
blizzyFlightGUIButton.ToolTip = "FAR Flight Sys";
blizzyFlightGUIButton.OnClick += (e) => showGUI = !showGUI;
}
}
public static void onAppLaunchToggle()
{
showGUI = !showGUI;
}
private void HideUI()
{
showAllGUI = false;
}
private void ShowUI()
{
showAllGUI = true;
}
#endregion
void SaveConfigs()
{
if (FARDebugAndSettings.config != null)
{
KSP.IO.PluginConfiguration config = FARDebugAndSettings.config;
config.SetValue("flight_mainGuiRect", mainGuiRect);
config.SetValue("flight_dataGuiRect", dataGuiRect);
config.SetValue("flight_settingsGuiRect", settingsGuiRect);
}
}
void LoadConfigs()
{
KSP.IO.PluginConfiguration config = FARDebugAndSettings.config;
mainGuiRect = config.GetValue("flight_mainGuiRect", new Rect());
dataGuiRect = config.GetValue("flight_dataGuiRect", new Rect());
settingsGuiRect = config.GetValue("flight_settingsGuiRect", new Rect());
}
}
}