forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkRenderer.cs
More file actions
394 lines (345 loc) · 16.2 KB
/
Copy pathNetworkRenderer.cs
File metadata and controls
394 lines (345 loc) · 16.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
using System;
using System.Linq;
using System.Collections.Generic;
using RemoteTech.SimpleTypes;
using RemoteTech.UI;
using UnityEngine;
using Debug = System.Diagnostics.Debug;
using KSP.Localization;
namespace RemoteTech
{
[Flags]
public enum MapFilter
{
None = 0,
Omni = 1,
Dish = 2,
Sphere = 4,
Cone = 8,
Planet = 8, // For backward compatibility with RemoteTech 1.4 and earlier
// Cone should be first, so that it's the one that appears in settings file
Path = 16,
MultiPath = 32
}
/// <summary>
/// RemoteTech UI network render in charre of drawing connection links in tracking station or flight map scenes.
/// </summary>
public class NetworkRenderer : MonoBehaviour
{
public MapFilter Filter {
get
{
return RTSettings.Instance.MapFilter;
}
set
{
RTSettings.Instance.MapFilter = value;
RTSettings.Instance.Save();
}
}
private static readonly Texture2D mTexMark;
private readonly HashSet<BidirectionalEdge<ISatellite>> mEdges = new HashSet<BidirectionalEdge<ISatellite>>();
private readonly List<NetworkLine> mLines = new List<NetworkLine>();
private readonly List<NetworkCone> mCones = new List<NetworkCone>();
private static float mLineWidth = 1f;
public bool ShowOmni { get { return (Filter & MapFilter.Omni) == MapFilter.Omni; } }
public bool ShowDish { get { return (Filter & MapFilter.Dish) == MapFilter.Dish; } }
public bool ShowPath { get { return (Filter & MapFilter.Path) == MapFilter.Path; } }
public bool ShowMultiPath { get { return (Filter & MapFilter.MultiPath) == MapFilter.MultiPath; } }
public bool ShowRange { get { return (Filter & MapFilter.Sphere) == MapFilter.Sphere; } }
public bool ShowCone { get { return (Filter & MapFilter.Cone) == MapFilter.Cone; } }
public GUIStyle smallStationText;
public GUIStyle smallStationHead;
static NetworkRenderer()
{
RTUtil.LoadImage(out mTexMark, "mark");
if(Versioning.version_major == 1)
{
switch(Versioning.version_minor)
{
case 4:
mLineWidth = 1f; //1f is matching to CommNet's line width
break;
default:
mLineWidth = 3f;
break;
}
}
}
public static NetworkRenderer CreateAndAttach()
{
var renderer = MapView.MapCamera.gameObject.GetComponent<NetworkRenderer>();
if (renderer)
{
Destroy(renderer);
}
renderer = MapView.MapCamera.gameObject.AddComponent<NetworkRenderer>();
RTCore.Instance.Network.OnLinkAdd += renderer.OnLinkAdd;
RTCore.Instance.Network.OnLinkRemove += renderer.OnLinkRemove;
RTCore.Instance.Satellites.OnUnregister += renderer.OnSatelliteUnregister;
renderer.smallStationHead = new GUIStyle(HighLogic.Skin.label)
{
fontSize = 12
};
renderer.smallStationText = new GUIStyle(HighLogic.Skin.label)
{
fontSize = 10,
normal = { textColor = Color.white }
};
return renderer;
}
public void OnPreCull()
{
if (MapView.MapIsEnabled)
{
UpdateNetworkEdges();
UpdateNetworkCones();
}
}
public void OnGUI()
{
if (Event.current.type == EventType.Repaint && MapView.MapIsEnabled)
{
foreach (ISatellite s in RTCore.Instance.Satellites.FindCommandStations().Concat(RTCore.Instance.Network.GroundStations.Values))
{
bool showOnMapview = true;
var worldPos = ScaledSpace.LocalToScaledSpace(s.Position);
if (MapView.MapCamera.transform.InverseTransformPoint(worldPos).z < 0f) continue;
Vector3 pos = PlanetariumCamera.Camera.WorldToScreenPoint(worldPos);
var screenRect = new Rect((pos.x - 8), (Screen.height - pos.y) - 8, 16, 16);
// Hide the current ISatellite if it is behind its body
if (RTSettings.Instance.HideGroundStationsBehindBody && IsOccluded(s.Position, s.Body))
showOnMapview = false;
if (RTSettings.Instance.HideGroundStationsOnDistance && !IsOccluded(s.Position, s.Body) && this.IsCamDistanceToWide(s.Position))
showOnMapview = false;
// orbiting remote stations are always shown
if(s.isVessel && !s.parentVessel.Landed)
showOnMapview = true;
if (showOnMapview)
{
Color pushColor = GUI.color;
// tint the white mark.png into the defined color
GUI.color = s.MarkColor;
// draw the mark.png
GUI.DrawTexture(screenRect, mTexMark, ScaleMode.ScaleToFit, true);
GUI.color = pushColor;
// Show Mouse over informations to the ground station
if (RTSettings.Instance.ShowMouseOverInfoGroundStations && s is MissionControlSatellite && screenRect.ContainsMouse())
{
Rect headline = screenRect;
Vector2 nameDim = this.smallStationHead.CalcSize(new GUIContent(s.Name));
headline.x -= nameDim.x + 10;
headline.y -= 3;
headline.width = nameDim.x;
headline.height = 14;
// draw headline of the station
GUI.Label(headline, s.Name, this.smallStationHead);
// loop antennas
String antennaRanges = String.Empty;
foreach (var antenna in s.Antennas)
{
if(antenna.Omni > 0)
{
antennaRanges += Localizer.Format("#RT_NetworkFB_Omni") + RTUtil.FormatSI(antenna.Omni,"m") + Environment.NewLine;//"Omni: "
}
if (antenna.Dish > 0)
{
antennaRanges += Localizer.Format("#RT_NetworkFB_Dish") + RTUtil.FormatSI(antenna.Dish, "m") + Environment.NewLine;//"Dish: "
}
}
if(!antennaRanges.Equals(String.Empty))
{
Rect antennas = screenRect;
GUIContent content = new GUIContent(antennaRanges);
Vector2 antennaDim = this.smallStationText.CalcSize(content);
float maxHeight = this.smallStationText.CalcHeight(content, antennaDim.x);
antennas.y += headline.height - 3;
antennas.x -= antennaDim.x + 10;
antennas.width = antennaDim.x;
antennas.height = maxHeight;
// draw antenna infos of the station
GUI.Label(antennas, antennaRanges, this.smallStationText);
}
}
}
}
}
}
/// <summary>
/// Checks whether the location is behind the body
/// Original code by regex from https://github.com/NathanKell/RealSolarSystem/blob/master/Source/KSCSwitcher.cs
/// </summary>
private bool IsOccluded(Vector3d loc, CelestialBody body)
{
Vector3d camPos = ScaledSpace.ScaledToLocalSpace(PlanetariumCamera.Camera.transform.position);
if (Vector3d.Angle(camPos - loc, body.position - loc) > 90) { return false; }
return true;
}
/// <summary>
/// Calculates the distance between the camera position and the ground station, and
/// returns true if the distance is >= DistanceToHideGroundStations from the settings file.
/// </summary>
/// <param name="loc">Position of the ground station</param>
/// <returns>True if the distance is to wide, otherwise false</returns>
private bool IsCamDistanceToWide(Vector3d loc)
{
Vector3d camPos = ScaledSpace.ScaledToLocalSpace(PlanetariumCamera.Camera.transform.position);
float distance = Vector3.Distance(camPos, loc);
// distance to wide?
if(distance >= RTSettings.Instance.DistanceToHideGroundStations)
return true;
return false;
}
private void UpdateNetworkCones()
{
List<IAntenna> antennas = (ShowCone ? RTCore.Instance.Antennas.Where(
ant => ant.Powered && ant.CanTarget && RTCore.Instance.Satellites[ant.Guid] != null
&& ant.Target != Guid.Empty)
: Enumerable.Empty<IAntenna>()).ToList();
int oldLength = mCones.Count;
int newLength = antennas.Count;
// Free any unused lines
for (int i = newLength; i < oldLength; i++)
{
GameObject.Destroy(mCones[i]);
mCones[i] = null;
}
mCones.RemoveRange(Math.Min(oldLength, newLength), Math.Max(oldLength - newLength, 0));
mCones.AddRange(Enumerable.Repeat((NetworkCone) null, Math.Max(newLength - oldLength, 0)));
for (int i = 0; i < newLength; i++)
{
var center = RTCore.Instance.Network.GetPositionFromGuid(antennas[i].Target);
Debug.Assert(center != null,
"center != null",
String.Format("GetPositionFromGuid returned a null value for the target {0}",
antennas[i].Target)
);
if (!center.HasValue) continue;
mCones[i] = mCones[i] ?? NetworkCone.Instantiate();
mCones[i].LineWidth = mLineWidth;
mCones[i].Antenna = antennas[i];
mCones[i].Color = Color.gray;
mCones[i].Active = ShowCone;
mCones[i].Center = center.Value;
}
}
private void UpdateNetworkEdges()
{
var edges = mEdges.Where(CheckVisibility).ToList();
int oldLength = mLines.Count;
int newLength = edges.Count;
// Free any unused lines
for (int i = newLength; i < oldLength; i++)
{
Destroy(mLines[i]);
mLines[i] = null;
}
mLines.RemoveRange(Math.Min(oldLength, newLength), Math.Max(oldLength - newLength, 0));
mLines.AddRange(Enumerable.Repeat<NetworkLine>(null, Math.Max(newLength - oldLength, 0)));
// Iterate over all satellites, updating or creating new lines.
var it = edges.GetEnumerator();
for (int i = 0; i < newLength; i++)
{
it.MoveNext();
mLines[i] = mLines[i] ?? NetworkLine.Instantiate();
mLines[i].LineWidth = mLineWidth;
mLines[i].Edge = it.Current;
mLines[i].Color = CheckColor(it.Current);
mLines[i].Active = true;
}
}
private bool CheckVisibility(BidirectionalEdge<ISatellite> edge)
{
var vessel = PlanetariumCamera.fetch.target.vessel;
var satellite = RTCore.Instance.Satellites[vessel];
if (satellite != null && ShowPath)
{
var connections = RTCore.Instance.Network[satellite];
if (connections.Any() && connections[0].Contains(edge))
return true;
}
if (ShowMultiPath && edge.A.Visible && edge.B.Visible) // purpose of edge-visibility condition is to prevent unnecessary performance off-screen
{
var satellites = RTCore.Instance.Network.ToArray();
for (int i = 0; i < satellites.Length; i++)
{
var connections = RTCore.Instance.Network[satellites[i]]; // get the working-connection path of every satellite
if (connections.Any() && connections[0].Contains(edge))
return true;
}
}
if (edge.Type == LinkType.Omni && !ShowOmni)
return false;
if (edge.Type == LinkType.Dish && !ShowDish)
return false;
if (!edge.A.Visible || !edge.B.Visible)
return false;
return true;
}
private Color CheckColor(BidirectionalEdge<ISatellite> edge)
{
var vessel = PlanetariumCamera.fetch.target.vessel;
var satellite = RTCore.Instance.Satellites[vessel];
if (satellite != null && ShowPath)
{
var connections = RTCore.Instance.Network[satellite];
if (connections.Any() && connections[0].Contains(edge))
return RTSettings.Instance.ActiveConnectionColor;
}
if (ShowMultiPath && edge.A.Visible && edge.B.Visible) // purpose of edge-visibility condition is to prevent unnecessary performance off-screen
{
var satellites = RTCore.Instance.Network.ToArray();
for (int i = 0; i < satellites.Length; i++)
{
var connections = RTCore.Instance.Network[satellites[i]]; // get the working-connection path of every satellite
if (connections.Any() && connections[0].Contains(edge))
return RTSettings.Instance.ActiveConnectionColor;
}
}
if (RTSettings.Instance.SignalRelayEnabled)
{
var satA = RTCore.Instance.Satellites[edge.A.Guid];
var satB = RTCore.Instance.Satellites[edge.B.Guid];
if ((satA != null && !satA.CanRelaySignal) || (satB != null && !satB.CanRelaySignal))
return RTSettings.Instance.DirectConnectionColor;
}
if (edge.Type == LinkType.Omni)
return RTSettings.Instance.OmniConnectionColor;
if (edge.Type == LinkType.Dish)
return RTSettings.Instance.DishConnectionColor;
return XKCDColors.Grey;
}
private void OnSatelliteUnregister(ISatellite s)
{
mEdges.RemoveWhere(e => e.A == s || e.B == s);
}
private void OnLinkAdd(ISatellite a, NetworkLink<ISatellite> link)
{
mEdges.Add(new BidirectionalEdge<ISatellite>(a, link.Target, link.Port));
}
private void OnLinkRemove(ISatellite a, NetworkLink<ISatellite> link)
{
mEdges.Remove(new BidirectionalEdge<ISatellite>(a, link.Target, link.Port));
}
public void Detach()
{
for (int i = 0; i < mLines.Count; i++)
{
GameObject.DestroyImmediate(mLines[i]);
}
mLines.Clear();
for (int i = 0; i < mCones.Count; i++)
{
GameObject.DestroyImmediate(mCones[i]);
}
mCones.Clear();
DestroyImmediate(this);
}
public void OnDestroy()
{
RTCore.Instance.Network.OnLinkAdd -= OnLinkAdd;
RTCore.Instance.Network.OnLinkRemove -= OnLinkRemove;
RTCore.Instance.Satellites.OnUnregister -= OnSatelliteUnregister;
}
}
}