forked from KSPModStewards/SystemHeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemHeatOverlay.cs
More file actions
418 lines (351 loc) · 12.2 KB
/
Copy pathSystemHeatOverlay.cs
File metadata and controls
418 lines (351 loc) · 12.2 KB
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
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using SystemHeat;
using KSP.Localization;
using KSP.UI;
using Vectrosity;
namespace SystemHeat.UI
{
/// <summary>
/// The master controller for the system heat overlay.
/// </summary>
[KSPAddon(KSPAddon.Startup.FlightAndEditor, false)]
public class SystemHeatOverlay : MonoBehaviour
{
public static SystemHeatOverlay Instance { get; private set; }
public static GameObject UICanvas = null;
public static bool Drawn { get; private set; }
protected Transform overlayRoot;
protected SystemHeatSimulator simulator;
public List<OverlayLoop> overlayLoops;
protected List<OverlayPanel> overlayPanels;
protected void Awake()
{
Drawn = false;
Instance = this;
overlayLoops = new List<OverlayLoop>();
//overlayLoopVisibility = new Dictionary<int, bool>();
overlayPanels = new List<OverlayPanel>();
overlayRoot = (new GameObject("SHOverlayRoot")).GetComponent<Transform>();
Utils.Log("[SystemHeatOverlay]: Subscribing to events", LogType.Overlay);
GameEvents.onGameSceneLoadRequested.Add(new EventData<GameScenes>.OnEvent(onSceneChange));
if (HighLogic.LoadedSceneIsEditor)
{
GameEvents.onEditorScreenChange.Add(new EventData<EditorScreen>.OnEvent(onEditorScreenChange));
GameEvents.onEditorPartDeleted.Add(new EventData<Part>.OnEvent(onEditorPartDeleted));
GameEvents.onEditorPartPicked.Add(new EventData<Part>.OnEvent(onEditorPartPicked));
GameEvents.onEditorRestart.Add(new EventVoid.OnEvent(onEditorReset));
GameEvents.onEditorLoad.Add(new EventData<ShipConstruct, KSP.UI.Screens.CraftBrowserDialog.LoadType>.OnEvent(onEditorLoad));
GameEvents.onEditorStarted.Add(new EventVoid.OnEvent(onEditorStart));
}
else
{
GameEvents.OnMapEntered.Add(new EventVoid.OnEvent(onEnterMapView));
GameEvents.OnMapExited.Add(new EventVoid.OnEvent(onExitMapView));
}
}
protected void OnDestroy()
{
Utils.Log("[SystemHeatOverlay]: Unsubscribing to events", LogType.Overlay);
GameEvents.onGameSceneLoadRequested.Remove(onSceneChange);
GameEvents.onEditorScreenChange.Remove(onEditorScreenChange);
GameEvents.onEditorPartDeleted.Remove(onEditorPartDeleted);
GameEvents.onEditorPartPicked.Remove(onEditorPartPicked);
GameEvents.onEditorRestart.Remove(onEditorReset);
GameEvents.onEditorLoad.Remove(onEditorLoad);
GameEvents.onEditorStarted.Remove(onEditorStart);
GameEvents.OnMapEntered.Remove(onEnterMapView);
GameEvents.OnMapExited.Remove(onExitMapView);
ClearPanels();
DestroyOverlay();
Instance = null;
}
protected void Start()
{
}
protected void onEnterMapView()
{
Utils.Log("[SystemHeatOverlay]: Entered map view, clearing panels", LogType.Overlay);
VectorLine.SetCamera3D(PlanetariumCamera.Camera);
ClearPanels();
}
protected void onExitMapView()
{
Utils.Log("[SystemHeatOverlay]: Entered map view, clearing panels", LogType.Overlay);
VectorLine.SetCamera3D(FlightCamera.fetch.mainCamera);
}
protected void onEditorScreenChange(EditorScreen screen)
{
Utils.Log("[SystemHeatOverlay]: Editor Screen Changed, clearing panels", LogType.Overlay);
ClearPanels();
}
protected void onEditorLoad(ShipConstruct ship, KSP.UI.Screens.CraftBrowserDialog.LoadType loadType)
{
Utils.Log("[SystemHeatOverlay]: Editor Load", LogType.Overlay);
ClearPanels();
}
protected void onEditorReset()
{
Utils.Log("[SystemHeatOverlay]: Editor Reset", LogType.Overlay);
ClearPanels();
}
protected void onEditorStart()
{
Utils.Log("[SystemHeatOverlay]: Editor Start", LogType.Overlay);
ClearPanels();
}
protected void onSceneChange(GameScenes scene)
{
Utils.Log("[SystemHeatOverlay]: Changing Scenes, clearing panels", LogType.Overlay);
SetVisible(false);
ClearPanels();
}
protected void onEditorPartDeleted(Part part)
{
if (overlayPanels != null)
for (int i = overlayPanels.Count - 1; i >= 0; i--)
{
if (overlayPanels[i].heatModule.part == part)
{
Utils.Log(String.Format("[SystemHeatOverlay]: Destroying unusued overlay panel because it was deleted"), LogType.Overlay);
Destroy(overlayPanels[i].gameObject);
overlayPanels.RemoveAt(i);
}
}
}
protected void onEditorPartPicked(Part part)
{
if (overlayPanels != null)
for (int i = overlayPanels.Count - 1; i >= 0; i--)
{
if (overlayPanels[i].heatModule.part == part)
{
Utils.Log(String.Format("[SystemHeatOverlay]: Destroying unusued overlay panel because it was deleted"), LogType.Overlay);
Destroy(overlayPanels[i].gameObject);
overlayPanels.RemoveAt(i);
}
}
}
public void ClearPanels()
{
Utils.Log("[SystemHeatOverlay]: Cleared all panels", LogType.Overlay);
for (int i = 0; i < overlayPanels.Count; i++)
{
if (overlayPanels[i] != null)
Destroy(overlayPanels[i].gameObject);
}
overlayPanels.Clear();
}
public void ResetOverlay()
{
ClearPanels();
}
protected void DestroyOverlay()
{
if (overlayLoops != null)
{
foreach (OverlayLoop overLoop in overlayLoops)
{
if (overLoop != null)
overLoop.Destroy();
}
overlayLoops.Clear();
}
}
protected void LateUpdate()
{
if (simulator != null && !(HighLogic.LoadedSceneIsFlight && MapView.MapIsEnabled))
{
if (simulator.HeatLoops == null || simulator.HeatLoops.Count == 0 && overlayLoops.Count > 0)
{
Utils.Log(String.Format("[SystemHeatOverlay]: No loops, destroying overlay"), LogType.Overlay);
DestroyOverlay();
}
if (simulator.HeatLoops != null && Drawn)
{
// Update each loop, build a new loop when needed
foreach (HeatLoop loop in simulator.HeatLoops)
{
// if we have an overlay for this loop, update it
OverlayLoop curOverlay = overlayLoops.FirstOrDefault(x => x.heatLoop.ID == loop.ID);
if (curOverlay != null)
{
// if no modules, hide the loop
if (loop.LoopModules.Count <= 1 && curOverlay.Drawn)
{
Utils.Log(String.Format("[SystemHeatOverlay]: Loop has < 2 members, hiding"), LogType.Overlay);
curOverlay.SetVisible(false);
}
else if (loop.LoopModules.Count > 1)
{
curOverlay.SetVisible((SystemHeatUI.Instance.OverlayMasterState && SystemHeatUI.Instance.OverlayLoopState(loop.ID)));
if (curOverlay.Drawn)
{
curOverlay.Update(loop);
}
}
}
// else build a new loop
else
{
Utils.Log(String.Format("[SystemHeatOverlay]: Building a new overlay for loop {0}", loop.ID), LogType.Overlay);
overlayLoops.Add(new OverlayLoop(loop, overlayRoot, (SystemHeatUI.Instance.OverlayMasterState && SystemHeatUI.Instance.OverlayLoopState(loop.ID))));
}
foreach (ModuleSystemHeat system in loop.LoopModules)
{
int index = overlayPanels.FindIndex(f => f.heatModule == system);
if (index == -1)
{
Utils.Log($"[SystemHeatOverlay]: Building new OverlayPanel for system {system.moduleID}", LogType.Overlay);
// new panel instance
GameObject newUIPanel = (GameObject)Instantiate(SystemHeatAssets.OverlayPanelPrefab, Vector3.zero, Quaternion.identity);
newUIPanel.transform.SetParent(UIMasterController.Instance.actionCanvas.transform);
newUIPanel.transform.localPosition = Vector3.zero;
OverlayPanel panel = newUIPanel.AddComponent<OverlayPanel>();
panel.parentCanvas = UIMasterController.Instance.appCanvas;
if (system.moduleUsed)
{
panel.SetupLoop(loop, system, (SystemHeatUI.Instance.OverlayMasterState && SystemHeatUI.Instance.OverlayLoopState(loop.ID)));
}
else
{
panel.SetupLoop(loop, system, (false && SystemHeatUI.Instance.OverlayLoopState(loop.ID)));
}
overlayPanels.Add(panel);
}
else
{
// Update the panel
if (system.moduleUsed)
{
overlayPanels[index].UpdateLoop(loop, system, (SystemHeatUI.Instance.OverlayMasterState && SystemHeatUI.Instance.OverlayLoopState(loop.ID)));
}
else
{
overlayPanels[index].UpdateLoop(loop, system, (false && SystemHeatUI.Instance.OverlayLoopState(loop.ID)));
}
}
}
}
for (int i = overlayPanels.Count - 1; i >= 0; i--)
{
if (overlayPanels[i].heatModule == null)
{
Utils.Log(String.Format("[SystemHeatOverlay]: Destroying unusued overlay panel"), LogType.Overlay);
Destroy(overlayPanels[i].gameObject);
overlayPanels.RemoveAt(i);
}
}
foreach (OverlayLoop l in overlayLoops)
{
if (!simulator.HasLoop(l.heatLoop.ID))
{
l.SetVisible(false);
}
}
}
}
else
{
DestroyOverlay();
foreach (OverlayPanel panel in overlayPanels)
{
panel.SetVisibility(false);
}
}
}
public void AssignSimulator(SystemHeatSimulator sim)
{
simulator = sim;
}
public void SetVisible(bool visible)
{
Utils.Log(String.Format("[SystemHeatOverlay]: Visibility set to {0}", visible), LogType.Overlay);
Drawn = visible;
SetLoopVisiblity(visible);
SetPanelVisiblity(visible);
}
public void SetVisible(bool visible, int loopID)
{
Utils.Log(String.Format("[SystemHeatOverlay]: Visibility of loop {0} set to {1}", loopID, visible), LogType.Overlay);
Drawn = visible;
SetLoopVisiblity(visible, loopID);
SetPanelVisiblity(visible, loopID);
}
private void SetPanelVisiblity(bool visible)
{
for (int i = 0; i < overlayPanels.Count; i++)
{
if (overlayPanels[i] != null)
{
if (overlayPanels[i].heatModule.moduleUsed)
overlayPanels[i].SetVisibility(visible);
else
overlayPanels[i].SetVisibility(false);
}
}
}
private void SetPanelVisiblity(bool visible, int loopID)
{
for (int i = 0; i < overlayPanels.Count; i++)
{
if (overlayPanels[i] == null) continue;
if (overlayPanels[i].loop.ID == loopID)
{
if (overlayPanels[i].heatModule.moduleUsed)
overlayPanels[i].SetVisibility(visible);
else
overlayPanels[i].SetVisibility(false);
}
else
{
Drawn = Drawn || overlayPanels[i].active;
}
}
}
private void SetLoopVisiblity(bool visible)
{
if (simulator != null)
{
foreach (OverlayLoop loop in overlayLoops)
{
loop.SetVisible(visible);
}
}
else
{
foreach (OverlayLoop loop in overlayLoops)
{
loop.SetVisible(false);
}
}
}
private void SetLoopVisiblity(bool visible, int loopID)
{
if (simulator != null)
{
foreach (OverlayLoop loop in overlayLoops)
{
if (loop.heatLoop.ID == loopID)
loop.SetVisible(visible);
else
Drawn = Drawn || loop.Drawn;
}
}
else
{
}
}
public bool CheckLoopVisibility(int loopID)
{
foreach (OverlayLoop loop in overlayLoops)
{
if (loop.heatLoop.ID == loopID)
return loop.Drawn;
}
return true;
}
}
}