forked from wmvanvliet/Kerbulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameGlue.cs
377 lines (314 loc) · 12.5 KB
/
GameGlue.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
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System;
using UnityEngine;
using KSP;
using KSP.IO;
using KSP.UI.Screens;
namespace Kerbulator {
#region Starter Classes
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class KerbulatorFlight : GameGlue { }
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class KerbulatorEditor : GameGlue { }
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class KerbulatorSpaceCenter : GameGlue { }
[KSPAddon(KSPAddon.Startup.TrackingStation, false)]
public class KerbulatorTrackingStation : GameGlue { }
#endregion
/// <summary>Glue code when plugin is loaded in KSP and game
/// assembly is available.</summary>
public class GameGlue : MonoBehaviour, IGlue {
private KerbulatorGUI gui = null;
private ApplicationLauncherButton mainButton = null;
private IButton blizzyButton = null;
private bool mainWindowEnabled = false;
private KerbulatorOptions options = null;
private Dictionary<string, bool> locks = new Dictionary<string, bool>();
/// <summary>Called by Unity when the Plugin is loaded</summary>
void Awake() {
Debug.Log("[Kerbulator] Start");
options = LoadConfig();
gui = new KerbulatorGUI(this, false, false, options);
if(!ToolbarManager.ToolbarAvailable) {
GameEvents.onGUIApplicationLauncherReady.Add(InitToolbarButton);
GameEvents.onGUIApplicationLauncherUnreadifying.Add(OnGuiApplicationLauncherUnreadifying);
}
Debug.Log("[Kerbulator] Start done");
}
void Start() {
if(ToolbarManager.ToolbarAvailable) {
// Create a toolbar button using Blizzy's toolbar
InitBlizzyButton();
}
KACWrapper.InitKACWrapper();
}
void OnDisable() {
SaveConfig();
}
void SaveConfig() {
// Save config
PluginConfiguration config = KSP.IO.PluginConfiguration.CreateForType<Kerbulator>(null);
config["mainWindowX"] = (int)options.mainWindowPos.x;
config["mainWindowY"] = (int)options.mainWindowPos.y;
config["mainWindowWidth"] = (int)options.mainWindowPos.width;
config["mainWindowHeight"] = (int)options.mainWindowPos.height;
config["editWindowX"] = (int)options.editWindowPos.x;
config["editWindowY"] = (int)options.editWindowPos.y;
config["editWindowWidth"] = (int)options.editWindowPos.width;
config["editWindowHeight"] = (int)options.editWindowPos.height;
config["runWindowX"] = (int)options.runWindowPos.x;
config["runWindowY"] = (int)options.runWindowPos.y;
config["runWindowWidth"] = (int)options.runWindowPos.width;
config["runWindowHeight"] = (int)options.runWindowPos.height;
config["repeatWindowX"] = (int)options.repeatWindowPos.x;
config["repeatWindowY"] = (int)options.repeatWindowPos.y;
config["repeatWindowWidth"] = (int)options.repeatWindowPos.width;
config["repeatWindowHeight"] = (int)options.repeatWindowPos.height;
config.save();
}
KerbulatorOptions LoadConfig() {
PluginConfiguration config = KSP.IO.PluginConfiguration.CreateForType<Kerbulator>(null);
config.load();
options = new KerbulatorOptions();
try {
options.mainWindowPos.x = config.GetValue<int>("mainWindowX", 0);
options.mainWindowPos.y = config.GetValue<int>("mainWindowY", 60);
options.mainWindowPos.width = config.GetValue<int>("mainWindowWidth", 280);
options.mainWindowPos.height = config.GetValue<int>("mainWindowHeight", 400);
options.editWindowPos.x = config.GetValue<int>("editWindowX", 280);
options.editWindowPos.y = config.GetValue<int>("editWindowY", 60);
options.editWindowPos.width = config.GetValue<int>("editWindowWidth", 500);
options.editWindowPos.height = config.GetValue<int>("editWindowHeight", 400);
options.runWindowPos.x = config.GetValue<int>("runWindowX", 0);
options.runWindowPos.y = config.GetValue<int>("runWindowY", 470);
options.runWindowPos.width = config.GetValue<int>("runWindowWidth", 200);
options.runWindowPos.height = config.GetValue<int>("runWindowHeight", 200);
options.repeatWindowPos.x = config.GetValue<int>("repeatWindowX", 200);
options.repeatWindowPos.y = config.GetValue<int>("repeatWindowY", 470);
options.repeatWindowPos.width = config.GetValue<int>("repeatWindowWidth", 200);
options.repeatWindowPos.height = config.GetValue<int>("repeatWindowHeight", 100);
} catch(ArgumentException) {
}
return options;
}
/// <summary>Creates a toolbar button for KSP's toolbar</summary>
void InitToolbarButton() {
Debug.Log("[Kerbulator] InitToolbarButton");
if(!ApplicationLauncher.Ready || mainButton != null)
return;
Debug.Log("[Kerbulator] AddModApplication");
mainButton = ApplicationLauncher.Instance.AddModApplication(
// Callback when enabled
() => {
gui.ChangeState(true);
},
// Callback when disabled
() => {
gui.ChangeState(false);
},
// Unused callbacks
null,
null,
null,
null,
// Visible in these scenes
ApplicationLauncher.AppScenes.ALWAYS,
// Button texture
GetTexture("kerbulator_38")
);
Debug.Log("[Kerbulator] Done!");
}
/// <summary>Creates a toolbar button for Blizzy's toolbar</summary>
void InitBlizzyButton() {
mainWindowEnabled = false;
blizzyButton = ToolbarManager.Instance.add("Kerbulator", "Kerbulator");
blizzyButton.TexturePath = "Kerbulator/Textures/kerbulator";
blizzyButton.ToolTip = "Open a powerful calculator";
blizzyButton.Visibility = new GameScenesVisibility(
GameScenes.FLIGHT, GameScenes.SPACECENTER, GameScenes.EDITOR, GameScenes.TRACKSTATION
);
blizzyButton.OnClick += (e) => { gui.ChangeState(!mainWindowEnabled); };
}
public void OnGuiApplicationLauncherUnreadifying(GameScenes SceneToLoad) {
RemoveToolbarButton();
}
public void RemoveToolbarButton() {
Debug.Log("[Kerbulator] RemoveToolbarButton");
if(!ToolbarManager.ToolbarAvailable) {
GameEvents.onGUIApplicationLauncherReady.Remove(InitToolbarButton);
GameEvents.onGUIApplicationLauncherUnreadifying.Remove(OnGuiApplicationLauncherUnreadifying);
if(mainButton != null)
ApplicationLauncher.Instance.RemoveModApplication(mainButton);
mainButton = null;
} else {
if(blizzyButton != null) {
blizzyButton.Destroy();
blizzyButton = null;
}
}
}
/// <summary>Called by Unity to draw the GUI</summary>
public void OnGUI() {
//Debug.Log("[Kerbulator] OnGUI");
if(gui != null)
gui.OnGUI();
}
public void OnApplicationFocus(bool focused) {
Debug.Log("[Kerbulator] OnApplicationFocus");
if(gui != null)
gui.OnApplicationFocus(focused);
}
/// <summary>Add/Update some useful globals</summary>
public void AddGlobals(Kerbulator kalc) {
Globals.Add(kalc); // UNITY
}
public void PlaceNodes(List<string> ids, List<object> maneuverNodes, List<System.Object> output) {
//Places a node using the old system for backwards capability
PlaceNode(ids, output);
foreach(object[] maneuver in maneuverNodes) {
double dr, dn, dp;
dp = (double)maneuver[0];
dn = (double)maneuver[1];
dr = (double)maneuver[2];
double UT = (double)maneuver[3] + Planetarium.GetUniversalTime();
Vector3d dV = new Vector3d(dr, dn, dp);
CreateManeuverNode(UT, dV);
}
}
private void PlaceNode(List<string> ids, List<object> output) {
double dr = 0, dn = 0, dp = 0;
double UT = 0;
// Look at the resulting variables and create a maneuver node with them
for(int i = 0; i < ids.Count; i++) {
if(output[i].GetType() != typeof(double))
continue;
string id = ids[i];
double val = (double)output[i];
if(id == "Δv_r" || id == "dv_r")
dr = val;
else if(id == "Δv_n" || id == "dv_n")
dn = val;
else if(id == "Δv_p" || id == "dv_p")
dp = val;
else if(id == "Δt" || id == "dt")
UT = val + Planetarium.GetUniversalTime();
}
Vector3d dV = new Vector3d(dr, dn, dp);
if(dV.sqrMagnitude > 0)
CreateManeuverNode(UT, dV);
}
private void CreateManeuverNode(double UT, Vector3d dV) {
Vessel vessel = FlightGlobals.ActiveVessel;
if(vessel == null)
return;
//placing a maneuver node with bad dV values can really mess up the game, so try to protect against that
//and log an exception if we get a bad dV vector:
for (int i = 0; i < 3; i++) {
if (double.IsNaN(dV[i]) || double.IsInfinity(dV[i])) {
throw new Exception("Kerbulator: bad dV: " + dV);
}
}
if (double.IsNaN(UT) || double.IsInfinity(UT)) {
throw new Exception("Kerbulator: bad UT: " + UT);
}
//It seems that sometimes the game can freak out if you place a maneuver node in the past, so this
//protects against that.
UT = Math.Max(UT, Planetarium.GetUniversalTime());
ManeuverNode mn = vessel.patchedConicSolver.AddManeuverNode(UT);
mn.OnGizmoUpdated(dV, UT);
}
public Texture2D GetTexture(string id) {
return GameDatabase.Instance.GetTexture("Kerbulator/Textures/"+ id, false);
}
/// <summary>Called by Unity when plugin is unloaded</summary>
public void OnDestroy() {
Debug.Log("[Kerbulator] Destroy");
if(gui != null)
gui.OnDestroy();
gui = null;
RemoveToolbarButton();
}
public void ChangeState(bool open) {
mainWindowEnabled = open;
}
public void RunAsCoroutine(IEnumerator f) {
StartCoroutine(f);
}
public void AddAlarms(string name, List<string> ids, List<System.Object> alarms, List<System.Object> output) {
Debug.Log("[Kerbulator] AddAlarm " + name);
if(KACWrapper.APIReady) {
//This is used to get the alarm using the old method
System.Object oldNotationAlarm = GetAlarm(ids, output);
if(oldNotationAlarm != null)
alarms = alarms.Append(oldNotationAlarm).ToList();
for(int i = 0; i<alarms.Count; i ++) {
double alarmTime = (double)alarms[i];
// Create a raw alarm
String aID = KACWrapper.KAC.CreateAlarm(
KACWrapper.KACAPI.AlarmTypeEnum.Raw, $"name {i}", alarmTime
);
if(aID !="") {
// If the alarm was made get the object so we can update it
KACWrapper.KACAPI.KACAlarm a = KACWrapper.KAC.Alarms.First(z=>z.ID==aID);
// Now update some of the other properties
a.Notes = "This alarm was placed by the Kerbulator function "+ name;
a.AlarmAction = KACWrapper.KACAPI.AlarmActionEnum.KillWarp;
}
}
}
}
private System.Object GetAlarm(List<string> ids, List<System.Object> output) {
double UT = 0;
// Look at the resulting variables and create an alarm with them
for(int i = 0; i < ids.Count; i++) {
if(output[i].GetType() != typeof(double))
continue;
string id = ids[i];
double val = (double)output[i];
if(id == "Δt" || id == "dt")
UT = val + Planetarium.GetUniversalTime();
else if(id == "UT")
UT = val;
}
if(UT != 0)
return UT;
else
return null;
}
public bool CanAddNode() {
return HighLogic.LoadedScene == GameScenes.FLIGHT;
}
public bool CanAddAlarm() {
return KACWrapper.APIReady;
}
public string GetFunctionDir() {
return KSPUtil.ApplicationRootPath + "/PluginData/Kerbulator";
}
public void PreventClickthrough(Rect windowRect, string lockName) {
Vector2 mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
bool cursorOnWindow = windowRect.Contains(mousePos);
if(cursorOnWindow) {
if(HighLogic.LoadedSceneIsEditor) {
EditorLogic.fetch.Lock(true, true, true, lockName);
} else {
InputLockManager.SetControlLock(ControlTypes.All, lockName);
}
} else {
if(HighLogic.LoadedSceneIsEditor) {
EditorLogic.fetch.Unlock(lockName);
} else {
InputLockManager.RemoveControlLock(lockName);
}
}
}
public void EnsureLockReleased(string lockName) {
if(HighLogic.LoadedSceneIsEditor) {
EditorLogic.fetch.Unlock(lockName);
} else {
InputLockManager.RemoveControlLock(lockName);
}
}
}
}