mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
Filter/hide roles by search in cleaner way
This commit is contained in:
1 parent
57471289e1
commit
b6414fe5a0
1 file changed
+109
-13
@@ -7,6 +7,7 @@ using System.Linq;
|
|||||||
using AmongUs.GameOptions;
|
using AmongUs.GameOptions;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using Innersloth.Assets;
|
using Innersloth.Assets;
|
||||||
|
using MiraAPI.LocalSettings;
|
||||||
using MiraAPI.Modifiers;
|
using MiraAPI.Modifiers;
|
||||||
using MiraAPI.Modifiers.Types;
|
using MiraAPI.Modifiers.Types;
|
||||||
using MiraAPI.Roles;
|
using MiraAPI.Roles;
|
||||||
@@ -28,7 +29,9 @@ public static class RoleGuidePatches
|
|||||||
{
|
{
|
||||||
private const int RolesTabIndex = 2;
|
private const int RolesTabIndex = 2;
|
||||||
private const int ModifiersTabIndex = 3;
|
private const int ModifiersTabIndex = 3;
|
||||||
private const float SearchDebounceSeconds = 0.3f;
|
private const float MinGuideScaleFactor = 0.5f;
|
||||||
|
private const float MaxGuideScaleFactor = 1f;
|
||||||
|
private const float SearchDebounceSeconds = 0.05f;
|
||||||
|
|
||||||
private static readonly List<DetailedPanel> RoleEntries = [];
|
private static readonly List<DetailedPanel> RoleEntries = [];
|
||||||
private static readonly List<DetailedPanel> ModifierEntries = [];
|
private static readonly List<DetailedPanel> ModifierEntries = [];
|
||||||
@@ -113,13 +116,29 @@ public static class RoleGuidePatches
|
|||||||
[HarmonyPatch(typeof(MatchInfoGuide), nameof(MatchInfoGuide.Awake))]
|
[HarmonyPatch(typeof(MatchInfoGuide), nameof(MatchInfoGuide.Awake))]
|
||||||
public static void Awake(MatchInfoGuide __instance)
|
public static void Awake(MatchInfoGuide __instance)
|
||||||
{
|
{
|
||||||
__instance.transitionOpen.targetSize = 1.3f;
|
__instance.transitionOpen.targetSize = Helpers.MatchInfoGuideBaseScale;
|
||||||
if (_guide != __instance)
|
if (_guide != __instance)
|
||||||
{
|
{
|
||||||
ResetState();
|
ResetState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void ApplyGuideScale(MatchInfoGuide instance, float scaleFactor)
|
||||||
|
{
|
||||||
|
if (!instance || !instance.transitionOpen)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var targetSize = Helpers.MatchInfoGuideBaseScale *
|
||||||
|
Mathf.Clamp(scaleFactor, MinGuideScaleFactor, MaxGuideScaleFactor);
|
||||||
|
instance.transitionOpen.targetSize = targetSize;
|
||||||
|
if (instance.MatchInfoParent.activeSelf)
|
||||||
|
{
|
||||||
|
instance.transitionOpen.transform.localScale = Vector3.one * targetSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Clears all cached state tied to a previous <see cref="MatchInfoGuide"/> instance.
|
/// Clears all cached state tied to a previous <see cref="MatchInfoGuide"/> instance.
|
||||||
/// Every game spawns a fresh guide, so anything we cached from the old one is a destroyed Unity object.
|
/// Every game spawns a fresh guide, so anything we cached from the old one is a destroyed Unity object.
|
||||||
@@ -171,6 +190,7 @@ public static class RoleGuidePatches
|
|||||||
[HarmonyPatch(typeof(MatchInfoGuide), nameof(MatchInfoGuide.Awake))]
|
[HarmonyPatch(typeof(MatchInfoGuide), nameof(MatchInfoGuide.Awake))]
|
||||||
private static void AwakePostfix(MatchInfoGuide __instance)
|
private static void AwakePostfix(MatchInfoGuide __instance)
|
||||||
{
|
{
|
||||||
|
ApplyGuideScale(__instance, LocalSettingsTabSingleton<MiraApiSettings>.Instance.MatchInfoGuideScaleFactor.Value);
|
||||||
Coroutines.Start(CoPreload(__instance));
|
Coroutines.Start(CoPreload(__instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,6 +252,8 @@ public static class RoleGuidePatches
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ApplyGuideScale(__instance, LocalSettingsTabSingleton<MiraApiSettings>.Instance.MatchInfoGuideScaleFactor.Value);
|
||||||
|
|
||||||
if (Minigame.Instance != null)
|
if (Minigame.Instance != null)
|
||||||
{
|
{
|
||||||
Minigame.Instance.Close();
|
Minigame.Instance.Close();
|
||||||
@@ -746,6 +768,8 @@ public static class RoleGuidePatches
|
|||||||
public int Amount { get; set; }
|
public int Amount { get; set; }
|
||||||
public int Chance { get; set; }
|
public int Chance { get; set; }
|
||||||
public float Likelihood { get; set; }
|
public float Likelihood { get; set; }
|
||||||
|
public bool CanShow { get; set; }
|
||||||
|
public bool CountsReady { get; set; }
|
||||||
public bool Visible { get; set; }
|
public bool Visible { get; set; }
|
||||||
|
|
||||||
public string GetSortKey(SortingGroups grouping)
|
public string GetSortKey(SortingGroups grouping)
|
||||||
@@ -935,14 +959,18 @@ public static class RoleGuidePatches
|
|||||||
entry.Amount = amount;
|
entry.Amount = amount;
|
||||||
entry.Chance = chance;
|
entry.Chance = chance;
|
||||||
entry.Likelihood = amount * chance;
|
entry.Likelihood = amount * chance;
|
||||||
entry.Visible = ShouldShowPanel(forceShow, amount == 0 || chance == 0, isHidden);
|
entry.CanShow = ShouldShowPanel(forceShow, amount == 0 || chance == 0, isHidden);
|
||||||
|
entry.CountsReady = true;
|
||||||
|
entry.Visible = entry.CanShow;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<DetailedPanel> UpdateEntries(List<DetailedPanel> entries, bool modifiers)
|
private static List<DetailedPanel> UpdateEntries(List<DetailedPanel> entries, bool modifiers)
|
||||||
{
|
{
|
||||||
|
var searchText = GetSearchText();
|
||||||
foreach (var entry in entries)
|
foreach (var entry in entries)
|
||||||
{
|
{
|
||||||
ComputeCounts(entry);
|
ComputeCounts(entry);
|
||||||
|
entry.Visible = entry.CanShow && (modifiers || MatchesSearch(entry, searchText));
|
||||||
if (entry.Panel)
|
if (entry.Panel)
|
||||||
{
|
{
|
||||||
SetPanelCount(entry.Panel!, entry.Amount, entry.Chance, entry.ModId);
|
SetPanelCount(entry.Panel!, entry.Amount, entry.Chance, entry.ModId);
|
||||||
@@ -953,16 +981,7 @@ public static class RoleGuidePatches
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var searchText = GetSearchText();
|
var shown = SortEntries(entries, searchText);
|
||||||
var sorted = entries.Where(p => p.Visible)
|
|
||||||
.OrderByDescending(p => p.Title.Equals(searchText, StringComparison.OrdinalIgnoreCase))
|
|
||||||
.ThenByDescending(p =>
|
|
||||||
p.Title.Contains(searchText, StringComparison.OrdinalIgnoreCase) ||
|
|
||||||
p.Category.Contains(searchText, StringComparison.OrdinalIgnoreCase));
|
|
||||||
var shown = (_sortMethod is SortingMethod.Alphabetical
|
|
||||||
? sorted.ThenBy(p => p.GetSortKey(_sortGrouping))
|
|
||||||
: sorted.ThenByDescending(p => p.GetSortKey(_sortGrouping)))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
var instance = MatchInfoGuide.Instance;
|
var instance = MatchInfoGuide.Instance;
|
||||||
if (modifiers)
|
if (modifiers)
|
||||||
@@ -979,6 +998,26 @@ public static class RoleGuidePatches
|
|||||||
return shown;
|
return shown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<DetailedPanel> SortEntries(List<DetailedPanel> entries, string searchText)
|
||||||
|
{
|
||||||
|
var sorted = entries.Where(p => p.Visible)
|
||||||
|
.OrderByDescending(p => p.Title.Equals(searchText, StringComparison.OrdinalIgnoreCase))
|
||||||
|
.ThenByDescending(p =>
|
||||||
|
p.Title.Contains(searchText, StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
p.Category.Contains(searchText, StringComparison.OrdinalIgnoreCase));
|
||||||
|
return (_sortMethod is SortingMethod.Alphabetical
|
||||||
|
? sorted.ThenBy(p => p.GetSortKey(_sortGrouping))
|
||||||
|
: sorted.ThenByDescending(p => p.GetSortKey(_sortGrouping)))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool MatchesSearch(DetailedPanel entry, string searchText)
|
||||||
|
{
|
||||||
|
return searchText.Length == 0 ||
|
||||||
|
entry.Title.Contains(searchText, StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
entry.Category.Contains(searchText, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
private static void RefreshActiveTab(bool scrollToTop)
|
private static void RefreshActiveTab(bool scrollToTop)
|
||||||
{
|
{
|
||||||
StartPopulate();
|
StartPopulate();
|
||||||
@@ -1009,9 +1048,66 @@ public static class RoleGuidePatches
|
|||||||
}
|
}
|
||||||
|
|
||||||
_appliedSearch = text;
|
_appliedSearch = text;
|
||||||
|
if (TryRefreshRoleSearch(text))
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
|
||||||
RefreshActiveTab(true);
|
RefreshActiveTab(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool TryRefreshRoleSearch(string searchText)
|
||||||
|
{
|
||||||
|
var guide = MatchInfoGuide.Instance;
|
||||||
|
if (!_guide || guide != _guide)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (guide.activeTabIndex != RolesTabIndex || _populate != null || !AreRolePanelsReady())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var entry in RoleEntries)
|
||||||
|
{
|
||||||
|
entry.Visible = entry.CanShow && MatchesSearch(entry, searchText);
|
||||||
|
if (entry.Panel!.gameObject.activeSelf != entry.Visible)
|
||||||
|
{
|
||||||
|
entry.Panel.gameObject.SetActive(entry.Visible);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var shown = SortEntries(RoleEntries, searchText);
|
||||||
|
foreach (var entry in shown)
|
||||||
|
{
|
||||||
|
entry.Panel!.transform.SetAsLastSibling();
|
||||||
|
}
|
||||||
|
|
||||||
|
guide.MatchInfoRoleScroller.SetYBoundsMax(GetGridScrollBounds(shown.Count));
|
||||||
|
guide.rolesEnabledMessage.SetActive(shown.Count == 0);
|
||||||
|
_rolesScroller.ScrollToTop();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool AreRolePanelsReady()
|
||||||
|
{
|
||||||
|
if (RoleEntries.Count == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var entry in RoleEntries)
|
||||||
|
{
|
||||||
|
if (!entry.CountsReady || !entry.Panel)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private static void ResetSearchBox()
|
private static void ResetSearchBox()
|
||||||
{
|
{
|
||||||
if (_searchDebounce != null)
|
if (_searchDebounce != null)
|
||||||
|
|||||||
Reference in new issue
Block a user