forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntennaFragment.cs
More file actions
315 lines (280 loc) · 12.5 KB
/
Copy pathAntennaFragment.cs
File metadata and controls
315 lines (280 loc) · 12.5 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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace RemoteTech.UI
{
public class AntennaFragment : IFragment, IDisposable
{
public class Entry
{
public String Text { get; set; }
public Guid Guid { get; set; }
public Color Color;
public List<Entry> SubEntries { get; private set; }
public bool Expanded { get; set; }
public int Depth { get; set; }
public Entry()
{
SubEntries = new List<Entry>();
Expanded = true;
}
}
public IAntenna Antenna {
get { return mAntenna; }
set { if (mAntenna != value) { mAntenna = value; RefreshPlanets(); Refresh(); } }
}
private IAntenna mAntenna;
private Vector2 mScrollPosition = Vector2.zero;
/// <summary>The tree of (real or virtual) targets displayed in this fragment.</summary>
/// <invariant>No Entry object appears in the tree pointed to by mRootEntry more than once.</invariant>
private Entry mRootEntry = new Entry();
/// <summary>The Entry corresponding to the currently selected target, if any.</summary>
private Entry mSelection;
/// <summary>The Entry corresponding to the currently selected target, if any.</summary>
private Entry mCurrentMouseOverEntry = null;
/// <summary>Callback trigger for mouse over a list entry</summary>
public Action onMouseOverListEntry = delegate { };
/// <summary>Callback trigger for mouse out of a list entry</summary>
public Action onMouseOutListEntry = delegate { };
/// <summary>Current entry of the mouse</summary>
public Entry mouseOverEntry { get { return mCurrentMouseOverEntry; } private set { mCurrentMouseOverEntry = value; } }
/// <summary>Flag to trigger the onMouseover event</summary>
public bool triggerMouseOverListEntry = false;
/// <summary>The Entries corresponding to loaded celestial bodies.</summary>
private Dictionary<CelestialBody, Entry> mEntries; // Current planet list
private int refreshCounter = 0;
public AntennaFragment(IAntenna antenna)
{
Antenna = antenna;
RTCore.Instance.Satellites.OnRegister += Refresh;
RTCore.Instance.Satellites.OnUnregister += Refresh;
RTCore.Instance.Antennas.OnUnregister += Refresh;
RefreshPlanets();
Refresh();
}
public void Dispose()
{
triggerMouseOverListEntry = false;
if (RTCore.Instance != null)
{
RTCore.Instance.Satellites.OnRegister -= Refresh;
RTCore.Instance.Satellites.OnUnregister -= Refresh;
RTCore.Instance.Antennas.OnUnregister -= Refresh;
}
}
public void Draw()
{
// Allow update for non-triggering changes (e.g., changing map view filters or changing a vessel's type)
// This is the best way I could find to do periodic refreshes;
// RTCore.Instance.InvokeRepeating() would require a search for instances
// of AntennaFragment, and would keep running after all target windows
// closed. Replace with something less clunky later! -- Starstrider42
if (++refreshCounter >= 100) {
Refresh();
refreshCounter = 0;
}
mScrollPosition = GUILayout.BeginScrollView(mScrollPosition);
Color pushColor = GUI.backgroundColor;
// starstriders changes
//Color pushCtColor = GUI.contentColor;
//Color pushBgColor = GUI.backgroundColor;
TextAnchor pushAlign = GUI.skin.button.alignment;
try {
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
// Depth-first tree traversal.
Stack<Entry> dfs = new Stack<Entry>();
foreach (Entry child in mRootEntry.SubEntries)
{
dfs.Push(child);
}
// Set the inital mouseover to the selected entry
mouseOverEntry = mSelection;
while (dfs.Count > 0)
{
Entry current = dfs.Pop();
GUI.backgroundColor = current.Color;
GUILayout.BeginHorizontal();
{
GUILayout.Space(current.Depth * (GUI.skin.button.margin.left + 24));
if (current.SubEntries.Count > 0)
{
RTUtil.Button(current.Expanded ? " <" : " >",
() =>
{
current.Expanded = !current.Expanded;
}, GUILayout.Width(24));
}
RTUtil.StateButton(current.Text, mSelection == current ? 1 : 0, 1,
(s) =>
{
mSelection = current;
Antenna.Target = mSelection.Guid;
});
// Mouse is over the button
if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) && triggerMouseOverListEntry)
{
// reset current entry
mouseOverEntry = null;
if (current.Text.ToLower() != "active vessel" && current.Text.ToLower() != "no target")
{
mouseOverEntry = current;
}
onMouseOverListEntry.Invoke();
}
}
GUILayout.EndHorizontal();
if (current.Expanded)
{
foreach (Entry child in current.SubEntries)
{
dfs.Push(child);
}
}
}
} finally {
GUILayout.EndScrollView();
GUI.skin.button.alignment = pushAlign;
GUI.backgroundColor = pushColor;
}
}
public void Refresh(IAntenna sat) { if (sat == Antenna) { Antenna = null; } }
/// <summary>Rebuilds list of target vessels</summary>
/// <description>Rebuilds the list of target vessels, preserving the rest of the target list state. Does
/// not alter planets or special targets, call RefreshPlanets() for that.</description>
/// <param name="sat">The satellite whose status has just changed.</param>
public void Refresh(ISatellite sat) { Refresh(); }
/// <summary>Rebuilds list of target vessels</summary>
/// <description>Rebuilds the list of target vessels, preserving the rest of the target list state. Does
/// not alter planets or special targets, call RefreshPlanets() for that.</description>
public void Refresh()
{
// Clear the satellites
RemoveVessels(mRootEntry);
if (Antenna == null) return;
// Add the satellites
foreach (ISatellite s in RTCore.Instance.Network)
{
if (s.Guid == Antenna.Guid) continue;
if (s.parentVessel != null && !MapViewFiltering.CheckAgainstFilter(s.parentVessel)) {
continue;
}
Entry current = new Entry()
{
Text = s.Name,
Guid = s.Guid,
Color = Color.white,
};
mEntries[s.Body].SubEntries.Add(current);
if (s.Guid == Antenna.Target)
{
mSelection = current;
}
}
// Set a local depth variable so we can refer to it when rendering.
Stack<Entry> dfs = new Stack<Entry>();
foreach (Entry child in mRootEntry.SubEntries)
{
child.Depth = 0;
dfs.Push(child);
}
while (dfs.Count > 0)
{
Entry current = dfs.Pop();
foreach (Entry child in current.SubEntries)
{
child.Depth = current.Depth + 1;
dfs.Push(child);
}
}
}
/// <summary>Full refresh of target list</summary>
/// <description>Rebuilds the list of target buttons from scratch, including special targets and
/// planets. Does not build vessel list, call Refresh() for that.</description>
/// <remarks>Calling this function wipes all information about which submenus were open or closed.</remarks>
private void RefreshPlanets() {
mEntries = new Dictionary<CelestialBody, Entry>();
mRootEntry = new Entry();
mSelection = new Entry()
{
Text = "No Target",
Guid = new Guid(RTSettings.Instance.NoTargetGuid),
Color = Color.white,
Depth = 0,
};
mRootEntry.SubEntries.Add(mSelection);
if (Antenna == null) return;
var activeVesselEntry = new Entry()
{
Text = "Active Vessel",
Guid = NetworkManager.ActiveVesselGuid,
Color = Color.white,
Depth = 0,
};
mRootEntry.SubEntries.Add(activeVesselEntry);
if (Antenna.Target == activeVesselEntry.Guid)
{
mSelection = activeVesselEntry;
}
// Add the planets
foreach (var cb in RTCore.Instance.Network.Planets)
{
if (!mEntries.ContainsKey(cb.Value))
{
mEntries[cb.Value] = new Entry();
}
Entry current = mEntries[cb.Value];
current.Text = cb.Value.bodyName;
current.Guid = cb.Key;
current.Color = cb.Value.GetOrbitDriver() != null ? cb.Value.GetOrbitDriver().orbitColor : Color.yellow;
current.Color.a = 1.0f;
if (cb.Value.referenceBody != cb.Value)
{
CelestialBody parent = cb.Value.referenceBody;
if (!mEntries.ContainsKey(parent))
{
mEntries[parent] = new Entry();
}
mEntries[parent].SubEntries.Add(current);
}
else
{
mRootEntry.SubEntries.Add(current);
}
if (cb.Key == Antenna.Target)
{
mSelection = current;
}
}
// Sort the lists based on semi-major axis. In reverse because of how we render it.
foreach (var entryPair in mEntries)
{
entryPair.Value.SubEntries.Sort((b, a) =>
{
return RTCore.Instance.Network.Planets[a.Guid].orbit.semiMajorAxis.CompareTo(
RTCore.Instance.Network.Planets[b.Guid].orbit.semiMajorAxis);
});
}
mRootEntry.SubEntries.Reverse();
}
/// <summary>Removes all buttons representing specific vessels, while preserving celestial bodies
/// and special targets</summary>
/// <param name="root">The top of the tree from which to remove vessels.</param>
private void RemoveVessels(Entry root) {
List<Entry> vesselList = new List<Entry>();
foreach (Entry subentry in root.SubEntries) {
// Is it a vessel?
if (subentry.Guid != Guid.Empty && subentry.Guid != NetworkManager.ActiveVesselGuid
&& !mEntries.ContainsValue(subentry)) {
// List<T> iterator is invalidated by modifications, so do all deletions later
vesselList.Add(subentry);
} else {
RemoveVessels(subentry);
}
}
// Do deletions without relying on an iterator for root.SubEntries
foreach (Entry vessel in vesselList) {
root.SubEntries.Remove(vessel);
}
}
}
}