Skip to content

Commit

Permalink
feat:💄 Improve metrics UI
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Jan 5, 2025
1 parent 34bde4f commit 9aad810
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 175 deletions.
8 changes: 4 additions & 4 deletions ResoniteMetricsCounter/Metrics/MetricsCounter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Elements.Core;
using Elements.Core;
using FrooxEngine;
using FrooxEngine.ProtoFlux;
using ResoniteMetricsCounter.Serialization;
Expand Down Expand Up @@ -61,12 +61,12 @@ private bool ShouldSkipImpl(IWorldElement element)
}

var slot = element.GetSlotFast();
if (slot is null || IgnoredHierarchy is null)
if (slot is null)
{
return false;
return true;
}

return IgnoredHierarchy.IsChildOf(slot, includeSelf: true);
return IgnoredHierarchy?.IsChildOf(slot, includeSelf: true) ?? false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using ResoniteMetricsCounter.UIX.Pages;
using ResoniteMetricsCounter.Utils;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace ResoniteMetricsCounter.UIX.Item;

internal abstract class MetricItemBase<T>

internal abstract class MetricPageItemBase<T>
{
private const float DEFAULT_ITEM_SIZE = 32;
private const float DEFAULT_PADDING = 4;

private readonly Slot slot;
private readonly Sync<colorX> metricTint;
private readonly ReferenceProxySource referenceProxySource;
private readonly RectTransform metricRect;

protected readonly List<Sync<string>> LabelFields = new();
protected abstract List<IMetricsPage.ColumnDefinition> Columns { get; }

public MetricItemBase(Slot container)
public MetricPageItemBase(Slot container, List<MetricColumnDefinition> columns)
{
var uiBuilder = new UIBuilder(container);

uiBuilder.Style.MinHeight = DEFAULT_ITEM_SIZE;
uiBuilder.Style.TextAutoSizeMin = 0;
uiBuilder.Style.TextAutoSizeMax = 24;
uiBuilder.Style.TextColor = RadiantUI_Constants.TEXT_COLOR;

uiBuilder.Style.MinHeight = Constants.ROWHEIGHT;
slot = uiBuilder.Panel(RadiantUI_Constants.Neutrals.DARK).Slot;

slot.AttachComponent<Button>();
Expand All @@ -38,27 +32,11 @@ public MetricItemBase(Slot container)
metricRect = metricImage.RectTransform;
metricTint = metricImage.Tint;

uiBuilder.HorizontalLayout(DEFAULT_PADDING);

uiBuilder.Style.ForceExpandWidth = uiBuilder.Style.ForceExpandHeight = false;

LabelFields.Capacity = Columns.Count;
for (int i = 0; i < Columns.Count; i++)
LabelFields.Capacity = columns.Count;
foreach (var label in MetricColumnDefinition.Build(uiBuilder, columns))
{
var column = Columns[i];
uiBuilder.Style.FlexibleWidth = column.FlexWidth;
uiBuilder.Style.MinWidth = column.MinWidth;
LabelFields.Add(uiBuilder.Text(column.Label, alignment: column.Alignment).Content);
LabelFields.Add(label.Content);
}

//uiBuilder.PushStyle();
//uiBuilder.Style.FlexibleWidth = -1;
//uiBuilder.Style.PreferredWidth = uiBuilder.Style.MinWidth = DEFAULT_ITEM_SIZE;

//var deactivateButton = uiBuilder.Button(OfficialAssets.Common.Icons.Cross, RadiantUI_Constants.Hero.RED);
//deactivateButton.LocalPressed += (_, _) => slot.Destroy();

//uiBuilder.PopStyle();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -87,7 +65,7 @@ public bool Update(in T metric, long maxTicks, long elapsedTicks, long frameCoun
UpdateColumn(metric, LabelFields[i], i, maxTicks, elapsedTicks, frameCount);
}

metricTint.Value = MathX.Lerp(RadiantUI_Constants.DarkLight.GREEN, RadiantUI_Constants.DarkLight.RED, maxRatio);
metricTint.Value = MathX.Lerp(RadiantUI_Constants.DarkLight.GREEN, RadiantUI_Constants.DarkLight.RED, MathX.Sqrt(maxRatio));
metricRect.AnchorMax.Value = new float2(maxRatio, 1.0f);

var reference = GetReference(metric);
Expand Down
51 changes: 51 additions & 0 deletions ResoniteMetricsCounter/UIX/MetricColumnDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using ResoniteMetricsCounter.Utils;
using System;
using System.Collections.Generic;

namespace ResoniteMetricsCounter.UIX;
internal struct MetricColumnDefinition
{
public string Label;
public float FlexWidth;
public float MinWidth;
public Alignment Alignment;

public MetricColumnDefinition(string label, Alignment alignment = Alignment.MiddleLeft, float flexWidth = -1, float minWidth = -1)
{
Label = label;
FlexWidth = flexWidth;
MinWidth = minWidth;
Alignment = alignment;
}

public static IEnumerable<Text> Build(UIBuilder uiBuilder, IEnumerable<MetricColumnDefinition> columns, Action<HorizontalLayout>? containerModifier = null)
{
uiBuilder.PushStyle();

uiBuilder.Style.MinHeight = Constants.ROWHEIGHT;
uiBuilder.Style.TextColor = RadiantUI_Constants.TEXT_COLOR;
uiBuilder.Style.TextAlignment = Alignment.MiddleCenter;
uiBuilder.Style.ForceExpandWidth = false;

var horizontalLayout = uiBuilder.HorizontalLayout(Constants.SPACING, Constants.PADDING);
if (containerModifier != null)
{
containerModifier(horizontalLayout);
}

foreach (var column in columns)
{
uiBuilder.Style.FlexibleWidth = column.FlexWidth;
uiBuilder.Style.MinWidth = column.MinWidth;
uiBuilder.Panel();
yield return uiBuilder.Text(column.Label);
uiBuilder.NestOut();
}
uiBuilder.NestOut();

uiBuilder.PopStyle();
}
}
8 changes: 4 additions & 4 deletions ResoniteMetricsCounter/UIX/MetricsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
namespace ResoniteMetricsCounter.UIX;
public sealed class MetricsPanel
{
private readonly List<KeyValuePair<string, IMetricsPage>> pages = new()
private readonly List<KeyValuePair<string, MetricsPageBase>> pages = new()
{
new("Detailed", new DetailedMetricsPanelPage()),
new("ObjectRoot", new ObjectRootPage()),
new("Detailed", new DetailedPage()),
new("Hierarchy", new HierarchyPage()),
};

public const float DEFAULTITEMSIZE = 32;
Expand Down Expand Up @@ -231,7 +231,7 @@ private void BuildPageButtonUI(in UIBuilder uiBuilder, string label, bool defaul
};
}

private static void BuildPageUI(UIBuilder uiBuilder, in IMetricsPage page, in string label, bool active)
private static void BuildPageUI(UIBuilder uiBuilder, in MetricsPageBase page, in string label, bool active)
{
var slot = uiBuilder.Next(label);
slot.ActiveSelf = active;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FrooxEngine;
using FrooxEngine.UIX;
using ResoniteMetricsCounter.Metrics;
using ResoniteMetricsCounter.UIX.Item;
using ResoniteMetricsCounter.Utils;
Expand All @@ -9,23 +8,11 @@

namespace ResoniteMetricsCounter.UIX.Pages;

internal sealed class DetailedMetricsPanelPage : IMetricsPage
internal sealed class DetailedPage : MetricsPageBase
{
private static List<IMetricsPage.ColumnDefinition> Columns => new() {
new("Object Root", flexWidth: 1.0f),
new("Parent", flexWidth: 1.0f),
new("Slot", flexWidth: 1.0f),
new("Component", flexWidth: 1.0f),
new("Stage", flexWidth: 0.5f),
new("Time", minWidth: 32*3),
new("%", minWidth: 32*3),
};

private sealed class Item : MetricItemBase<StageMetric<IWorldElement>>
private sealed class Item : MetricPageItemBase<StageMetric<IWorldElement>>
{
protected override List<IMetricsPage.ColumnDefinition> Columns => DetailedMetricsPanelPage.Columns;

public Item(Slot container) : base(container)
public Item(Slot container, List<MetricColumnDefinition> columns) : base(container, columns)
{
}

Expand Down Expand Up @@ -67,37 +54,18 @@ protected override void UpdateColumn(in StageMetric<IWorldElement> metric, Sync<
}
}

protected override List<MetricColumnDefinition> Columns => new() {
new("Object Root", flexWidth: Constants.FLEX),
new("Parent", flexWidth: Constants.FLEX),
new("Slot", flexWidth: Constants.FLEX),
new("Component", flexWidth: Constants.FLEX),
new("Stage", flexWidth: Constants.FLEX),
new("Time", minWidth: Constants.FIXEDWIDTH),
new("%", minWidth: Constants.FIXEDWIDTH),
};


private Slot? container;
private List<Item?>? items;

public bool IsActive()
{
return container?.IsActive ?? false;
}

public void BuildUI(UIBuilder uiBuilder)
{
container = uiBuilder.VerticalLayout(spacing: 8).Slot;

uiBuilder.PushStyle();

var hori = uiBuilder.HorizontalLayout();
hori.Slot.OrderOffset = long.MinValue;

foreach (var column in Columns)
{
uiBuilder.Style.FlexibleWidth = column.FlexWidth;
uiBuilder.Style.MinWidth = column.MinWidth;
uiBuilder.Text(column.Label);
}
uiBuilder.NestOut();

uiBuilder.PopStyle();
}

public void Update(in MetricsCounter metricsCounter, int maxItems)
public override void Update(in MetricsCounter metricsCounter, int maxItems)
{
if (container is null || container.IsDisposed)
{
Expand All @@ -123,7 +91,7 @@ public void Update(in MetricsCounter metricsCounter, int maxItems)
var i = 0;
foreach (var metric in metricsCounter.ByElement.Metrics.OrderByDescending(m => m.Ticks).Take(maxItems))
{
var item = items[i] ?? (items[i] = new Item(container));
var item = items[i] ?? (items[i] = new Item(container, Columns));

if (!item.Update(metric, maxTicks, totalTicks, frameCount))
{
Expand All @@ -132,12 +100,4 @@ public void Update(in MetricsCounter metricsCounter, int maxItems)
i++;
}
}

public void Dispose()
{
if (container is not null && !container.IsDisposed)
{
container.Destroy();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using FrooxEngine;
using ResoniteMetricsCounter.Metrics;
using ResoniteMetricsCounter.UIX.Item;
using System.Collections.Generic;
Expand All @@ -9,23 +7,14 @@

namespace ResoniteMetricsCounter.UIX.Pages;

internal sealed class ObjectRootPage : IMetricsPage
internal sealed class HierarchyPage : MetricsPageBase
{
private static List<IMetricsPage.ColumnDefinition> Columns => new() {
new ("Object Root", flexWidth: 1.0f),
new ("Time", minWidth: 32 * 3),
new ("%", minWidth: 32 * 3),
};

private sealed class Item : MetricItemBase<Metric<Slot>>
private sealed class Item : MetricPageItemBase<Metric<Slot>>
{
protected override List<IMetricsPage.ColumnDefinition> Columns => ObjectRootPage.Columns;

public Item(Slot container) : base(container)
public Item(Slot container, List<MetricColumnDefinition> columns) : base(container, columns)
{
}


protected override IWorldElement? GetReference(in Metric<Slot> metric)
{
return metric.Target;
Expand All @@ -52,45 +41,15 @@ protected override void UpdateColumn(in Metric<Slot> metric, Sync<string> column
}
}

private Slot? container;
private readonly List<Item?> items = new();


public void BuildUI(UIBuilder uiBuilder)
{
container = uiBuilder.VerticalLayout(RadiantUI_Constants.GRID_PADDING).Slot;

uiBuilder.PushStyle();

var hori = uiBuilder.HorizontalLayout(RadiantUI_Constants.GRID_PADDING);
hori.Slot.OrderOffset = long.MinValue;
foreach (var column in Columns)
{
uiBuilder.Style.FlexibleWidth = column.FlexWidth;
uiBuilder.Style.MinWidth = column.MinWidth;
uiBuilder.Text(column.Label, alignment: Alignment.MiddleCenter);
}
uiBuilder.NestOut();

uiBuilder.PopStyle();

container.Destroyed += (_) => Dispose();
}

public void Dispose()
{
if (container is not null && !container.IsDisposed)
{
container.Destroy();
}
}
protected override List<MetricColumnDefinition> Columns => new() {
new ("Hierarchy", flexWidth: 1.0f),
new ("Time", minWidth: 32 * 3),
new ("%", minWidth: 32 * 3),
};

public bool IsActive()
{
return container is not null && !container.IsDisposed && container.IsActive;
}
private readonly List<Item?> items = new();

public void Update(in MetricsCounter metricsCounter, int maxItems)
public override void Update(in MetricsCounter metricsCounter, int maxItems)
{
if (container is null || container.IsDisposed)
{
Expand All @@ -109,7 +68,7 @@ public void Update(in MetricsCounter metricsCounter, int maxItems)
int i = 0;
foreach (var metric in metricsCounter.ByObjectRoot.Metrics.OrderByDescending(m => m.Ticks).Take(maxItems))
{
var item = items[i] ??= new Item(container!);
var item = items[i] ??= new Item(container, Columns);
if (i == 0)
{
maxTicks = metric.Ticks;
Expand Down
Loading

0 comments on commit 9aad810

Please sign in to comment.