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
265 lines (235 loc) · 10.1 KB
/
Copy pathNetworkRenderer.cs
File metadata and controls
265 lines (235 loc) · 10.1 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
using System;
using System.Linq;
using System.Collections.Generic;
using RemoteTech.SimpleTypes;
using RemoteTech.UI;
using UnityEngine;
using Debug = System.Diagnostics.Debug;
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
}
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>();
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 ShowRange { get { return (Filter & MapFilter.Sphere) == MapFilter.Sphere; } }
public bool ShowCone { get { return (Filter & MapFilter.Cone) == MapFilter.Cone; } }
static NetworkRenderer()
{
RTUtil.LoadImage(out mTexMark, "mark.png");
}
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;
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 = MapView.MapCamera.camera.WorldToScreenPoint(worldPos);
var screenRect = new Rect((pos.x - 8), (Screen.height - pos.y) - 8, 16, 16);
if (s is MissionControlSatellite && RTSettings.Instance.HideGroundStationsBehindBody)
{
// Hide the current ISatellite if it is behind its body
if (IsOccluded(s.Position, s.Body))
showOnMapview = false;
}
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;
}
}
}
}
/// <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;
}
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].Material = MapView.fetch.orbitLinesMaterial;
mCones[i].LineWidth = 2.0f;
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].Material = MapView.fetch.orbitLinesMaterial;
mLines[i].LineWidth = 3.0f;
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 (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 (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;
}
}
}