-
Notifications
You must be signed in to change notification settings - Fork 96
/
SCAN_UI_Instruments.cs
666 lines (555 loc) · 15.3 KB
/
SCAN_UI_Instruments.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#region license
/*
* [Scientific Committee on Advanced Navigation]
* S.C.A.N. Satellite
*
* SCAN_UI_Instruments - UI control object for SCANsat instruments readout
*
* Copyright (c)2014 David Grandy <david.grandy@gmail.com>;
* Copyright (c)2014 technogeeky <technogeeky@gmail.com>;
* Copyright (c)2014 (Your Name Here) <your email here>; see LICENSE.txt for licensing details.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using SCANsat.SCAN_Toolbar;
using SCANsat.Unity.Interfaces;
using SCANsat.Unity.Unity;
using SCANsat.SCAN_Data;
using SCANsat.SCAN_UI.UI_Framework;
using SCANsat.SCAN_PartModules;
using FinePrint;
using FinePrint.Utilities;
using KSP.UI;
namespace SCANsat.SCAN_Unity
{
public class SCAN_UI_Instruments : ISCAN_Instruments
{
private bool _isVisible;
private bool _mouseInAnomaly;
private SCANremoteView _anomalyView;
private SCANtype sensors;
private SCANdata data;
private SCANanomaly nearest = null;
private Vessel v;
private List<SCANresourceDisplay> resourceScanners = new List<SCANresourceDisplay>();
private List<SCANresourceGlobal> resources = new List<SCANresourceGlobal>();
private int currentResource;
private double degreeOffset;
private double vlat, vlon;
private float lastUpdate = 0f;
private float updateInterval = 0.2f;
private double maxAnomalyDistance = 20000;
private double slopeAVG;
private StringBuilder infoString = new StringBuilder();
private string slopeString;
private int oldLines;
private int lines;
private Texture anomalyTex;
private SCAN_Instruments uiElement;
private static SCAN_UI_Instruments instance;
public static SCAN_UI_Instruments Instance
{
get { return instance; }
}
public SCAN_UI_Instruments()
{
instance = this;
data = SCANUtil.getData(FlightGlobals.currentMainBody);
if (data == null)
{
data = new SCANdata(FlightGlobals.currentMainBody);
SCANcontroller.controller.addToBodyData(FlightGlobals.currentMainBody, data);
}
planetConstants(FlightGlobals.currentMainBody);
v = FlightGlobals.ActiveVessel;
resources = SCANcontroller.setLoadedResourceList();
resetResourceList();
GameEvents.onVesselSOIChanged.Add(soiChange);
GameEvents.onVesselChange.Add(vesselChange);
GameEvents.onVesselWasModified.Add(vesselChange);
}
private void soiChange(GameEvents.HostedFromToAction<Vessel, CelestialBody> VC)
{
data = SCANUtil.getData(VC.to);
if (data == null)
{
data = new SCANdata(VC.to);
SCANcontroller.controller.addToBodyData(VC.to, data);
}
planetConstants(VC.to);
}
private void vesselChange(Vessel V)
{
v = FlightGlobals.ActiveVessel;
resetResourceList();
}
public void SetScale(float scale)
{
if (uiElement != null)
uiElement.SetScale(scale);
}
public void ProcessTooltips()
{
if (uiElement != null)
uiElement.ProcessTooltips();
}
public void Update()
{
if (!_isVisible || uiElement == null)
return;
vlat = SCANUtil.fixLatShift(v.latitude);
vlon = SCANUtil.fixLonShift(v.longitude);
sensors = SCANcontroller.controller.activeSensorsOnVessel(v.id);
if (SCANUtil.isCovered(vlon, vlat, data, SCANtype.AltimetryLoRes))
{
sensors |= SCANtype.AltimetryLoRes;
}
if (SCANUtil.isCovered(vlon, vlat, data, SCANtype.AltimetryHiRes))
{
sensors |= SCANtype.AltimetryHiRes;
}
if (SCANUtil.isCovered(vlon, vlat, data, SCANtype.Biome))
{
sensors |= SCANtype.Biome;
}
if (SCANUtil.isCovered(vlon, vlat, data, SCANtype.Anomaly))
{
sensors |= SCANtype.Anomaly;
}
foreach (SCANresourceGlobal s in resources)
{
if (SCANUtil.isCovered(vlon, vlat, data, s.SType))
sensors |= s.SType;
}
if (SCANUtil.isCovered(vlon, vlat, data, SCANtype.FuzzyResources))
sensors |= SCANtype.FuzzyResources;
infoString.Length = 0;
locationInfo();
altInfo();
biomeInfo();
resourceInfo();
anomalyInfo();
BTDTInfo();
if (oldLines != lines && ResourceButtons)
{
oldLines = lines;
uiElement.SetResourceButtons(lines);
}
uiElement.UpdateText(infoString.ToString());
}
public void OnDestroy()
{
if (uiElement != null)
{
uiElement.gameObject.SetActive(false);
MonoBehaviour.Destroy(uiElement.gameObject);
}
GameEvents.onVesselSOIChanged.Remove(soiChange);
GameEvents.onVesselChange.Remove(vesselChange);
GameEvents.onVesselWasModified.Remove(vesselChange);
}
public void Open()
{
uiElement = GameObject.Instantiate(SCAN_UI_Loader.InstrumentsPrefab).GetComponent<SCAN_Instruments>();
if (uiElement == null)
return;
uiElement.transform.SetParent(UIMasterController.Instance.dialogCanvas.transform, false);
infoString = StringBuilderCache.Acquire();
uiElement.SetInstruments(this);
_isVisible = true;
if (HighLogic.LoadedSceneIsFlight && SCAN_Settings_Config.Instance.StockToolbar && SCAN_Settings_Config.Instance.ToolbarMenu)
{
if (SCANappLauncher.Instance != null && SCANappLauncher.Instance.UIElement != null)
SCANappLauncher.Instance.UIElement.SetInstrumentToggle(true);
}
}
public void Close()
{
_isVisible = false;
oldLines = 0;
if (uiElement == null)
return;
uiElement.FadeOut();
if (HighLogic.LoadedSceneIsFlight && SCAN_Settings_Config.Instance.StockToolbar && SCAN_Settings_Config.Instance.ToolbarMenu)
{
if (SCANappLauncher.Instance != null && SCANappLauncher.Instance.UIElement != null)
SCANappLauncher.Instance.UIElement.SetInstrumentToggle(false);
}
}
public string Version
{
get { return SCANmainMenuLoader.SCANsatVersion; }
}
public string Readout
{
get { return infoString.ToString(); }
}
public string ResourceName
{
get { return resources[currentResource].Name; }
}
public string TypingText
{
get { return ""; }
}
public string AnomalyText
{
get { return ""; }
}
public bool IsVisible
{
get { return _isVisible; }
set
{
_isVisible = value;
if (!value)
{
Close();
infoString.Release();
}
}
}
public bool ResourceButtons
{
get { return SCANcontroller.MasterResourceCount > 1; }
}
public bool MouseAnomaly
{
get { return _mouseInAnomaly; }
set { _mouseInAnomaly = value; }
}
public bool TooltipsOn
{
get { return SCAN_Settings_Config.Instance.WindowTooltips; }
}
public float Scale
{
get { return SCAN_Settings_Config.Instance.UIScale; }
}
public Texture AnomalyCamera
{
get { return null; }
}
public Canvas TooltipCanvas
{
get { return UIMasterController.Instance.tooltipCanvas; }
}
public Vector2 Position
{
get { return SCAN_Settings_Config.Instance.InstrumentsPosition; }
set { SCAN_Settings_Config.Instance.InstrumentsPosition = value; }
}
public void ClampToScreen(RectTransform rect)
{
UIMasterController.ClampToScreen(rect, Vector2.zero);
}
public void NextResource()
{
currentResource += 1;
if (currentResource >= resources.Count)
currentResource = 0;
}
public void PreviousResource()
{
currentResource -= 1;
if (currentResource < 0)
currentResource = resources.Count - 1;
}
private void locationInfo()
{
infoString.AppendFormat("Lat: {0:F2}°, Lon: {1:F2}°", vlat, vlon);
lines = 1;
foreach (SCANwaypoint p in data.Waypoints)
{
if (p.LandingTarget)
{
double distance = SCANUtil.waypointDistance(vlat, vlon, v.altitude, p.Latitude, p.Longitude, v.altitude, data.Body);
if (distance <= 15000)
{
infoString.AppendLine();
infoString.AppendFormat("Target Dist: {0:N1}m", distance);
lines++;
}
continue;
}
if (p.Band == FlightBand.NONE)
continue;
if (p.Root != null)
{
if (p.Root.ContractState != Contracts.Contract.State.Active)
continue;
}
if (p.Param != null)
{
if (p.Param.State != Contracts.ParameterState.Incomplete)
continue;
}
double range = waypointRange(p);
if (SCANUtil.waypointDistance(vlat, vlon, v.altitude, p.Latitude, p.Longitude, v.altitude, data.Body) <= range)
{
infoString.AppendLine();
infoString.AppendFormat("Waypoint: {0}", p.Name);
lines++;
break;
}
}
}
private void altInfo()
{
double h = v.altitude;
double pqs = 0;
if (v.mainBody.pqsController != null)
{
pqs = v.PQSAltitude();
if (pqs > 0 || !v.mainBody.ocean)
h -= pqs;
}
if (h < 0)
h = v.altitude;
bool drawSlope = false;
switch (v.situation)
{
case Vessel.Situations.LANDED:
case Vessel.Situations.PRELAUNCH:
infoString.AppendLine();
infoString.AppendFormat("Terrain: {0:N1}m", pqs);
lines++;
drawSlope = true;
break;
case Vessel.Situations.SPLASHED:
double d = Math.Abs(pqs) - Math.Abs(h);
if ((sensors & SCANtype.Altimetry) != SCANtype.Nothing)
{
infoString.AppendLine();
infoString.AppendFormat("Depth: {0}", SCANuiUtil.distanceString(Math.Abs(d), 10000));
lines++;
}
else
{
d = ((int)(d / 100)) * 100;
infoString.AppendLine();
infoString.AppendFormat("Depth: {0}", SCANuiUtil.distanceString(Math.Abs(d), 10000));
lines++;
}
drawSlope = false;
break;
default:
if (h < 1000 || (sensors & SCANtype.AltimetryHiRes) != SCANtype.Nothing)
{
infoString.AppendLine();
infoString.AppendFormat("Altitude: {0}", SCANuiUtil.distanceString(h, 100000));
lines++;
drawSlope = true;
}
else if ((sensors & SCANtype.AltimetryLoRes) != SCANtype.Nothing)
{
h = ((int)(h / 500)) * 500;
infoString.AppendLine();
infoString.AppendFormat("Altitude: {0}", SCANuiUtil.distanceString(h, 100000));
lines++;
drawSlope = false;
}
break;
}
if (drawSlope)
{
//Calculate slope less frequently; the rapidly changing value makes it difficult to read otherwise
if (v.mainBody.pqsController != null)
{
float deltaTime = 1f;
if (Time.deltaTime != 0)
deltaTime = TimeWarp.deltaTime / Time.deltaTime;
if (deltaTime > 5)
deltaTime = 5;
if (((Time.time * deltaTime) - lastUpdate) > updateInterval)
{
lastUpdate = Time.time;
slopeAVG = SCANUtil.slope(pqs, v.mainBody, vlon, vlat, degreeOffset);
slopeString = string.Format("Slope: {0:F2}°", slopeAVG);
}
infoString.AppendLine();
infoString.Append(slopeString);
lines++;
}
}
}
private void biomeInfo()
{
if ((sensors & SCANtype.Biome) != SCANtype.Nothing && v.mainBody.BiomeMap != null)
{
infoString.AppendLine();
infoString.AppendFormat("Biome: {0}", SCANUtil.getBiomeName(v.mainBody, vlon, vlat));
lines++;
}
}
private void resourceInfo()
{
if (v.mainBody.pqsController == null)
return;
if (SCAN_Settings_Config.Instance.RequireNarrowBand)
{
bool tooHigh = false;
bool scanner = false;
foreach (SCANresourceDisplay s in resourceScanners)
{
if (s == null)
continue;
if (s.ResourceName != resources[currentResource].Name)
continue;
if (ResourceUtilities.GetAltitude(v) > s.MaxAbundanceAltitude && !v.Landed)
{
tooHigh = true;
continue;
}
scanner = true;
tooHigh = false;
break;
}
resourceLabel(resources[currentResource], tooHigh, scanner);
}
else
{
resourceLabel(resources[currentResource], false, true);
}
}
private void resourceLabel(SCANresourceGlobal r, bool high, bool onboard)
{
if ((sensors & r.SType) != SCANtype.Nothing)
{
if (high || !onboard)
{
infoString.AppendLine();
infoString.AppendFormat("{0}: {1:P0}", r.Name, SCANUtil.ResourceOverlay(vlat, vlon, r.Name, v.mainBody, SCAN_Settings_Config.Instance.BiomeLock));
}
else
{
infoString.AppendLine();
infoString.AppendFormat("{0}: {1:P2}", r.Name, SCANUtil.ResourceOverlay(vlat, vlon, r.Name, v.mainBody, SCAN_Settings_Config.Instance.BiomeLock));
}
}
else if ((sensors & SCANtype.FuzzyResources) != SCANtype.Nothing)
{
infoString.AppendLine();
infoString.AppendFormat("{0}: {1:P0}", r.Name, SCANUtil.ResourceOverlay(vlat, vlon, r.Name, v.mainBody, SCAN_Settings_Config.Instance.BiomeLock));
}
else if (ResourceButtons)
{
infoString.AppendLine();
infoString.AppendFormat("{0}: No Data", r.Name);
}
}
private void anomalyInfo()
{
if ((sensors & SCANtype.Anomaly) != SCANtype.Nothing)
{
nearest = null;
double nearest_dist = -1;
foreach (SCANanomaly a in data.Anomalies)
{
if (!a.Known)
continue;
double d = (a.Mod.transform.position - v.transform.position).magnitude;
if (d < nearest_dist || nearest_dist < 0)
{
if (d < maxAnomalyDistance)
{
nearest = a;
nearest_dist = d;
}
}
}
if (nearest != null)
{
infoString.AppendLine();
if (nearest.Detail)
infoString.Append(string.Format("{0}: {1}", nearest.Name, SCANuiUtil.distanceString(nearest_dist, 2000)));
else
infoString.Append(string.Format("Unknown Anomaly: {0}", SCANuiUtil.distanceString(nearest_dist, 2000)));
}
}
else
nearest = null;
}
private void BTDTInfo()
{
if ((sensors & SCANtype.AnomalyDetail) == SCANtype.Nothing || nearest == null || !nearest.Detail || nearest.Mod == null)
{
uiElement.SetDetailState(false);
if (_anomalyView != null)
_anomalyView.free();
_anomalyView = null;
return;
}
uiElement.SetDetailState(true);
if (_anomalyView == null)
{
_anomalyView = new SCANremoteView();
_anomalyView.setup(320, 240, nearest.Mod.gameObject);//, uiElement.EdgeDetectShader, uiElement.GrayScaleShader);
}
if (!_anomalyView.valid(nearest.Mod.gameObject))
{
if (_anomalyView != null)
_anomalyView.free();
_anomalyView = null;
return;
}
anomalyTex = _anomalyView.getTexture();
uiElement.UpdateAnomaly(anomalyTex);
string info = _anomalyView.getInfoString();
string aData = _anomalyView.getAnomalyDataString(_mouseInAnomaly);
uiElement.UpdateAnomalyText(info);
uiElement.UpdateAnomalyName(aData);
}
private string distanceString(double dist)
{
if (dist < 5000)
return string.Format("{0:N1}m", dist);
return string.Format("{0:N3}km", dist / 1000);
}
private double waypointRange(SCANwaypoint p)
{
double min = ContractDefs.Survey.MinimumTriggerRange;
double max = ContractDefs.Survey.MaximumTriggerRange;
switch (p.Band)
{
case FlightBand.GROUND:
return min;
case FlightBand.LOW:
return (min + max) / 2;
case FlightBand.HIGH:
return max;
default:
return max;
}
}
private void planetConstants(CelestialBody b)
{
double circum = b.Radius * 2 * Math.PI;
double eqDistancePerDegree = circum / 360;
degreeOffset = 5 / eqDistancePerDegree;
}
public void resetResourceList()
{
resourceScanners = new List<SCANresourceDisplay>();
if (v == null)
return;
foreach (SCANresourceDisplay s in v.FindPartModulesImplementing<SCANresourceDisplay>())
{
if (s == null)
continue;
if (resourceScanners.Contains(s))
continue;
resourceScanners.Add(s);
}
}
public void ResetPosition()
{
SCAN_Settings_Config.Instance.InstrumentsPosition = new Vector2(100, -500);
if (uiElement != null)
uiElement.SetPosition(SCAN_Settings_Config.Instance.InstrumentsPosition);
}
}
}