Skip to content

Commit 348d067

Browse files
committed
Added fade to hover and fixed import & export for NetworkProfilerWindow
1 parent bda4dbd commit 348d067

File tree

1 file changed

+88
-32
lines changed

1 file changed

+88
-32
lines changed

MLAPI-Editor/MLAPIProfiler.cs

Lines changed: 88 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ GUIStyle wrapStyle
2525
return style;
2626
}
2727
}
28+
29+
float hoverAlpha = 0f;
2830
float updateDelay = 1f;
2931
int captureCount = 100;
3032
float showMax = 0;
3133
float showMin = 0;
3234
AnimationCurve curve = AnimationCurve.Constant(0, 1, 0);
3335
readonly List<ProfilerTick> currentTicks = new List<ProfilerTick>();
3436
float lastDrawn = 0;
35-
struct ProfilerContainer
37+
class ProfilerContainer
3638
{
3739
public ProfilerTick[] ticks;
3840
}
@@ -46,7 +48,7 @@ private void StartRecording()
4648
{
4749
if (NetworkProfiler.IsRunning)
4850
StopRecording();
49-
51+
5052
if (NetworkProfiler.Ticks != null && NetworkProfiler.Ticks.Count >= 2)
5153
curve = AnimationCurve.Constant(NetworkProfiler.Ticks.ElementAt(0).Frame, NetworkProfiler.Ticks.ElementAt(NetworkProfiler.Ticks.Count - 1).Frame, 0);
5254
else
@@ -56,57 +58,78 @@ private void StartRecording()
5658
NetworkProfiler.Start(captureCount);
5759
}
5860

61+
private void ClearDrawing()
62+
{
63+
curve = AnimationCurve.Constant(0, 1, 0);
64+
lastDrawn = 0;
65+
}
66+
5967
private void ChangeRecordState()
6068
{
6169
if (NetworkProfiler.IsRunning) StopRecording();
6270
else StartRecording();
6371
}
6472

73+
TickEvent eventHover = null;
74+
double lastSetup = 0;
6575
private void OnGUI()
6676
{
6777
bool recording = NetworkProfiler.IsRunning;
78+
float deltaTime = (float)(EditorApplication.timeSinceStartup - lastSetup);
79+
lastSetup = EditorApplication.timeSinceStartup;
6880

6981
//Draw top bar
7082
EditorGUILayout.BeginVertical();
7183
EditorGUILayout.BeginHorizontal();
7284
if (GUILayout.Button(recording ? "Stop" : "Capture")) ChangeRecordState();
7385

86+
if (GUILayout.Button("Clear")) ClearDrawing();
87+
EditorGUILayout.Space();
88+
EditorGUILayout.Space();
89+
EditorGUILayout.Space();
90+
7491
if (GUILayout.Button("Import datafile"))
7592
{
76-
ProfilerTick[] ticks = BinarySerializer.Deserialize<ProfilerContainer>(File.ReadAllBytes(EditorUtility.OpenFilePanel("Choose a NetworkProfiler file", "", ""))).ticks;
77-
if (ticks.Length >= 2)
78-
{
79-
curve = AnimationCurve.Constant(ticks[0].EventId, ticks[(ticks.Length - 1)].EventId, 0);
80-
showMax = ticks.Length;
81-
showMin = ticks.Length - Mathf.Clamp(100, 0, ticks.Length);
82-
}
83-
else
84-
curve = AnimationCurve.Constant(0, 1, 0);
85-
currentTicks.Clear();
86-
for (int i = 0; i < ticks.Length; i++)
93+
string path = EditorUtility.OpenFilePanel("Choose a NetworkProfiler file", "", "");
94+
if (!string.IsNullOrEmpty(path))
8795
{
88-
currentTicks.Add(ticks[i]);
89-
90-
uint bytes = 0;
91-
if (ticks[i].Events.Count > 0)
96+
ProfilerTick[] ticks = BinarySerializer.Deserialize<ProfilerContainer>(File.ReadAllBytes(path)).ticks;
97+
if (ticks.Length >= 2)
98+
{
99+
curve = AnimationCurve.Constant(ticks[0].EventId, ticks[(ticks.Length - 1)].EventId, 0);
100+
showMax = ticks.Length;
101+
showMin = ticks.Length - Mathf.Clamp(100, 0, ticks.Length);
102+
}
103+
else
104+
curve = AnimationCurve.Constant(0, 1, 0);
105+
currentTicks.Clear();
106+
for (int i = 0; i < ticks.Length; i++)
92107
{
93-
for (int j = 0; j < ticks[i].Events.Count; j++)
108+
currentTicks.Add(ticks[i]);
109+
110+
uint bytes = 0;
111+
if (ticks[i].Events.Count > 0)
94112
{
95-
TickEvent tickEvent = ticks[i].Events[j];
96-
bytes += tickEvent.Bytes;
113+
for (int j = 0; j < ticks[i].Events.Count; j++)
114+
{
115+
TickEvent tickEvent = ticks[i].Events[j];
116+
bytes += tickEvent.Bytes;
117+
}
97118
}
119+
curve.AddKey(ticks[i].EventId, bytes);
98120
}
99-
curve.AddKey(ticks[i].EventId, bytes);
100121
}
101122
}
102123

103124
if (GUILayout.Button("Export datafile"))
104125
{
105-
int ticksInRange = 0;
106-
for (int i = 0; i < currentTicks.Count; i++) if (currentTicks[i].EventId >= showMin && currentTicks[i].EventId <= showMin) ticksInRange++;
126+
int max = (int)showMax;
127+
int min = (int)showMin;
128+
int ticksInRange = max - min;
107129
ProfilerTick[] ticks = new ProfilerTick[ticksInRange];
108-
for (int i = 0; i < currentTicks.Count; i++) if (currentTicks[i].EventId >= showMin && currentTicks[i].EventId <= showMin) ticks[i] = currentTicks[i];
109-
File.WriteAllBytes(EditorUtility.SaveFilePanel("Save NetworkProfiler data", "", "networkProfilerData", ""), BinarySerializer.Serialize(new ProfilerContainer() { ticks = ticks }));
130+
for (int i = min; i < max; i++) ticks[i - min] = currentTicks[i];
131+
string path = EditorUtility.SaveFilePanel("Save NetworkProfiler data", "", "networkProfilerData", "");
132+
if (!string.IsNullOrEmpty(path)) File.WriteAllBytes(path, BinarySerializer.Serialize(new ProfilerContainer() { ticks = ticks }));
110133
}
111134

112135
EditorGUILayout.EndHorizontal();
@@ -164,7 +187,7 @@ private void OnGUI()
164187
}
165188

166189
//Draw main board
167-
TickEvent eventHover = null;
190+
bool hover = false;
168191
int nonEmptyTicks = 0;
169192
int largestTickCount = 0;
170193
int totalTicks = ((int)showMax - (int)showMin);
@@ -209,7 +232,11 @@ private void OnGUI()
209232
TickEvent tickEvent = tick.Events[j];
210233
Rect dataRect = new Rect(currentX, currentY, widthPerTick, heightPerEvent);
211234

212-
if (dataRect.Contains(Event.current.mousePosition)) eventHover = tickEvent;
235+
if (dataRect.Contains(Event.current.mousePosition))
236+
{
237+
hover = true;
238+
eventHover = tickEvent;
239+
}
213240

214241
if (j == tick.Events.Count - 1)
215242
dataRect.height -= 45f;
@@ -231,17 +258,32 @@ private void OnGUI()
231258
currentX += widthPerTick;
232259
}
233260

261+
//Calculate alpha
262+
if (hover)
263+
{
264+
hoverAlpha += deltaTime * 10f;
265+
266+
if (hoverAlpha > 1f) hoverAlpha = 1f;
267+
else if (hoverAlpha < 0f) hoverAlpha = 0f;
268+
}
269+
else
270+
{
271+
hoverAlpha -= deltaTime * 10f;
272+
if (hoverAlpha > 1f) hoverAlpha = 1f;
273+
else if (hoverAlpha < 0f) hoverAlpha = 0f;
274+
}
275+
234276
//Draw hover thingy
235277
if (eventHover != null)
236278
{
237279
Rect rect = new Rect(Event.current.mousePosition, new Vector2(500, 100));
238-
EditorGUI.DrawRect(rect, EditorColor);
280+
EditorGUI.DrawRect(rect, GetEditorColorWithAlpha(hoverAlpha));
239281

240282
float heightPerField = (rect.height - 5) / 4;
241-
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + 5, rect.width, rect.height), "EventType: " + eventHover.EventType.ToString());
242-
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 1 + 5, rect.width, rect.height), "Size: " + eventHover.Bytes + "B");
243-
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 2 + 5, rect.width, rect.height), "Channel: " + eventHover.ChannelName);
244-
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 3 + 5, rect.width, rect.height), "MessageType: " + eventHover.MessageType);
283+
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + 5, rect.width, rect.height), "EventType: " + eventHover.EventType.ToString(), GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
284+
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 1 + 5, rect.width, rect.height), "Size: " + eventHover.Bytes + "B", GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
285+
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 2 + 5, rect.width, rect.height), "Channel: " + eventHover.ChannelName, GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
286+
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 3 + 5, rect.width, rect.height), "MessageType: " + eventHover.MessageType, GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
245287
}
246288

247289
Repaint();
@@ -269,6 +311,20 @@ private Color EditorColor
269311
return EditorGUIUtility.isProSkin ? new Color32(56, 56, 56, 255) : new Color32(194, 194, 194, 255);
270312
}
271313
}
314+
315+
private Color GetEditorColorWithAlpha(float alpha)
316+
{
317+
return EditorGUIUtility.isProSkin ? new Color(0.22f, 0.22f, 0.22f, alpha) : new Color(0.76f, 0.76f, 0.76f, alpha);
318+
}
319+
320+
private GUIStyle GetStyleWithTextAlpha(GUIStyle style, float alpha)
321+
{
322+
Color textColor = style.normal.textColor;
323+
textColor.a = alpha;
324+
GUIStyle newStyle = new GUIStyle(style);
325+
newStyle.normal.textColor = textColor;
326+
return newStyle;
327+
}
272328
}
273329

274330
}

0 commit comments

Comments
 (0)