Skip to content

Commit

Permalink
Merge pull request #15 from esnya/refactor/ui-improvement
Browse files Browse the repository at this point in the history
feat: 💄 Improve UI
  • Loading branch information
esnya authored Jan 5, 2025
2 parents cc5641d + 9aad810 commit 04b6c19
Show file tree
Hide file tree
Showing 13 changed files with 529 additions and 360 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
2 changes: 1 addition & 1 deletion ResoniteMetricsCounter/Patch/Metric_Profiler_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrooxEngine;
using FrooxEngine;
using FrooxEngine.ProtoFlux;
using HarmonyLib;
using ResoniteModLoader;
Expand Down
110 changes: 0 additions & 110 deletions ResoniteMetricsCounter/UIX/Item/MetricItemBase.cs

This file was deleted.

83 changes: 83 additions & 0 deletions ResoniteMetricsCounter/UIX/Item/MetricPageItemBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Elements.Core;
using FrooxEngine;
using FrooxEngine.UIX;
using ResoniteMetricsCounter.Utils;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace ResoniteMetricsCounter.UIX.Item;


internal abstract class MetricPageItemBase<T>
{

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

protected readonly List<Sync<string>> LabelFields = new();

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

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

slot.AttachComponent<Button>();
referenceProxySource = slot.AttachComponent<ReferenceProxySource>();

var metricImage = uiBuilder.Image();
metricRect = metricImage.RectTransform;
metricTint = metricImage.Tint;

LabelFields.Capacity = columns.Count;
foreach (var label in MetricColumnDefinition.Build(uiBuilder, columns))
{
LabelFields.Add(label.Content);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected abstract long GetTicks(in T metric);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected abstract IWorldElement? GetReference(in T metric);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected abstract void UpdateColumn(in T metric, Sync<string> column, int i, long maxTicks, long elapsedTicks, long frameCount);

public bool Update(in T metric, long maxTicks, long elapsedTicks, long frameCount)
{
if (slot.IsDisposed)
{
return false;
}

var ticks = GetTicks(metric);
var maxRatio = (float)ticks / maxTicks;
slot.OrderOffset = -ticks;

for (int i = 0; i < LabelFields.Count; i++)
{
UpdateColumn(metric, LabelFields[i], i, maxTicks, elapsedTicks, frameCount);
}

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);
if (reference is null || reference.IsRemoved)
{
referenceProxySource.Enabled = false;
}
else
{
referenceProxySource.Reference.Target = reference;
}

return true;
}
}
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();
}
}
Loading

0 comments on commit 04b6c19

Please sign in to comment.