From 1e77ab54c447a3414a4f661e8b47033a5f98a4f8 Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Mon, 8 Apr 2024 22:40:39 +0200 Subject: [PATCH] Add session capacity highlighting --- FlexibleContactsSort/ContactsSortingConfig.cs | 4 +- FlexibleContactsSort/SessionCapacityConfig.cs | 26 +++++++++++ FlexibleContactsSort/SessionUserCapacity.cs | 45 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 FlexibleContactsSort/SessionCapacityConfig.cs create mode 100644 FlexibleContactsSort/SessionUserCapacity.cs diff --git a/FlexibleContactsSort/ContactsSortingConfig.cs b/FlexibleContactsSort/ContactsSortingConfig.cs index c76c843..3923964 100644 --- a/FlexibleContactsSort/ContactsSortingConfig.cs +++ b/FlexibleContactsSort/ContactsSortingConfig.cs @@ -20,9 +20,9 @@ internal sealed class ContactsSortingConfig : ConfigSection private readonly DefiningConfigKey _readMessageCooldownKey = new("ReadMessageCooldown", "Delay in seconds before a contact with freshly-read messages is counted as such. Set 0 to disable.", () => 120); public int AlphabeticPriority => _alphabeticPriorityKey.GetValue(); - public override string Description { get; } = "Contains options for how to sort the Contacts list."; + public override string Description => "Contains options for how to sort the Contacts list."; public int HeadlessPriority => _headlessPriorityKey.GetValue(); - public override string Id { get; } = "ContactsSorting"; + public override string Id => "ContactsSorting"; public int IncomingContactRequestPriority => _incomingContactRequestPriorityKey.GetValue(); public int JoinablePriority => _joinablePriorityKey.GetValue(); public int OnlineStatusPriority => _onlineStatusPriorityKey.GetValue(); diff --git a/FlexibleContactsSort/SessionCapacityConfig.cs b/FlexibleContactsSort/SessionCapacityConfig.cs new file mode 100644 index 0000000..df7c1df --- /dev/null +++ b/FlexibleContactsSort/SessionCapacityConfig.cs @@ -0,0 +1,26 @@ +using Elements.Core; +using FrooxEngine; +using MonkeyLoader.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlexibleContactsSort +{ + internal sealed class SessionCapacityConfig : ConfigSection + { + private readonly DefiningConfigKey _emptySessionColorKey = new("EmptySessionColor", "Color of the user count when only the host is there.", () => RadiantUI_Constants.Hero.GREEN); + private readonly DefiningConfigKey _fullSessionColorKey = new("FullSessionColor", "Color of the user count when the session is full.", () => RadiantUI_Constants.Hero.RED); + private readonly DefiningConfigKey _showUsageLevelWithColorGradientKey = new("ShowUsageLevelWithColorGradient", "Color the user count based on capacity usage.", () => true); + private readonly DefiningConfigKey _showUserCapacityInSessionListKey = new("ShowUserCapacityInSessionList", "Show the user capacity of contacts' joinable sessions.", () => true); + public override string Description => "Contains options for how to highlight contacts' session capacity."; + public colorX EmptySessionColor => _emptySessionColorKey.GetValue(); + public colorX FullSessionColor => _fullSessionColorKey.GetValue(); + public override string Id => "SessionCapacity"; + public bool ShowUsageLevelWithColorGradient => _showUsageLevelWithColorGradientKey.GetValue(); + public bool ShowUserCapacityInSessionList => _showUserCapacityInSessionListKey.GetValue(); + public override Version Version { get; } = new(1, 0, 0); + } +} \ No newline at end of file diff --git a/FlexibleContactsSort/SessionUserCapacity.cs b/FlexibleContactsSort/SessionUserCapacity.cs new file mode 100644 index 0000000..a2689cf --- /dev/null +++ b/FlexibleContactsSort/SessionUserCapacity.cs @@ -0,0 +1,45 @@ +using Elements.Core; +using FrooxEngine; +using HarmonyLib; +using MonkeyLoader.Patching; +using MonkeyLoader.Resonite; +using SkyFrost.Base; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlexibleContactsSort +{ + [HarmonyPatchCategory(nameof(SessionUserCapacity))] + [HarmonyPatch(typeof(SessionItem), nameof(SessionItem.Update))] + internal sealed class SessionUserCapacity : ConfiguredResoniteMonkey + { + protected override IEnumerable GetFeaturePatches() => Enumerable.Empty(); + + private static void Postfix(SessionItem __instance, SessionInfo session) + { + var format = "{0} ({1})"; + + if (ConfigSection.ShowUserCapacityInSessionList) + format += " / {2}"; + + if (ConfigSection.ShowUsageLevelWithColorGradient) + { + var usage = (float)session.JoinedUsers / session.MaximumUsers; + + var value = usage > 1 || !usage.IsValid() ? + new ColorHSV(ConfigSection.FullSessionColor).v - .2f + : MathX.Lerp(new ColorHSV(ConfigSection.EmptySessionColor).v, new ColorHSV(ConfigSection.FullSessionColor).v, usage); + + var color = MathX.Lerp(ConfigSection.EmptySessionColor, ConfigSection.FullSessionColor, usage).SetValue(value); + + format = $"{format}"; + } + + format = "Users: " + format; + __instance._userCount.Target.Content.Value = string.Format(format, session.ActiveUsers, session.JoinedUsers, session.MaximumUsers); + } + } +} \ No newline at end of file