diff --git a/MiraAPI/Hud/CustomMultiSelectMenu.cs b/MiraAPI/Hud/CustomMultiSelectMenu.cs index 5b405053..057b7205 100644 --- a/MiraAPI/Hud/CustomMultiSelectMenu.cs +++ b/MiraAPI/Hud/CustomMultiSelectMenu.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using MiraAPI.Patches.Stubs; +using MiraAPI.Utilities; using MiraAPI.Utilities.Assets; using UnityEngine; using UnityEngine.Events; @@ -101,6 +102,7 @@ public abstract class CustomMultiSelectMenu : CustomPhoneMenu entries, Action?> onClick, int totalSelections, bool shouldConfirm, bool canRepeat = false) { MinigameStubs.Begin(Component, null); + MeetingVoteAreaVisibility.HideNameplates(Component); this.totalSelections = totalSelections; this.shouldConfirm = shouldConfirm; @@ -111,6 +113,7 @@ public abstract class CustomMultiSelectMenu : CustomPhoneMenu { selectedEntries.Clear(); + MeetingVoteAreaVisibility.Restore(Component); onClick(null); })); @@ -172,6 +175,7 @@ public abstract class CustomMultiSelectMenu : CustomPhoneMenu LocalSettingsManager.Tabs.IndexOf(this) + 10; private Scroller? scroller; + private float MaxScroll { get; set; } /// /// Invoked when an option value is changed. @@ -107,6 +108,17 @@ public abstract class LocalSettingsTab(ConfigFile config) } } + internal void RefreshScrollBounds() + { + if (scroller == null) + { + return; + } + + scroller.SetBounds(new FloatRange(0, MaxScroll), new FloatRange(0, 0)); + scroller.ScrollToTop(); + } + /// /// Creates the tab and it's content. /// @@ -119,11 +131,13 @@ public abstract class LocalSettingsTab(ConfigFile config) tab.transform.DestroyChildren(); tab.gameObject.SetActive(false); - if (scroller == null) + if (scroller == null || scroller.transform.parent != tab.transform) { scroller = Helpers.CreateScroller(tab.transform, OptionsMenuPatches.MaskCollider); } + Labels.Clear(); + if (TabButton != null) { TabButton.Content = tab.gameObject; @@ -217,7 +231,11 @@ public abstract class LocalSettingsTab(ConfigFile config) contentIndex++; } - scroller.SetBounds(new FloatRange(0, (Settings.Count + Buttons.Count) * 0.5f - 5f), new FloatRange(0, 0)); + const float visibleHeight = 5f; + const float extraPadding = 0.25f; + var itemRange = (Settings.Count + Buttons.Count) * 0.5f - visibleHeight; + var offsetRange = contentOffset - visibleHeight + extraPadding; + MaxScroll = Mathf.Max(0f, itemRange, offsetRange); return tab; } diff --git a/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs b/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs index 39b84870..f6aaaf45 100644 --- a/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs +++ b/MiraAPI/Patches/LocalSettings/OptionsMenuPatches.cs @@ -1,7 +1,9 @@ -using System; +using System; +using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using BepInEx.Unity.IL2CPP.Utils.Collections; using HarmonyLib; using Il2CppInterop.Runtime.InteropTypes.Arrays; using MiraAPI.LocalSettings; @@ -180,7 +182,9 @@ public static class OptionsMenuPatches return true; } - CustomOpen(LocalSettingsManager.AvailableTabs[index - 10]); + var tab = LocalSettingsManager.AvailableTabs[index - 10]; + CustomOpen(tab); + __instance.StartCoroutine(RefreshScrollBoundsAfterOpen(__instance, tab).WrapToIl2Cpp()); return false; } @@ -226,6 +230,16 @@ public static class OptionsMenuPatches tab.Open(); } + private static IEnumerator RefreshScrollBoundsAfterOpen(OptionsMenuBehaviour menu, LocalSettingsTab tab) + { + yield return new WaitForEndOfFrame(); + + if (menu && tab.TabButton && tab.TabButton!.Content && tab.TabButton.Content.activeInHierarchy) + { + tab.RefreshScrollBounds(); + } + } + private static void CustomClose(LocalSettingsTab tab) { if (tab.TabButton) diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs index 36c3735a..aa6396df 100644 --- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs @@ -373,6 +373,7 @@ public static class RoleSettingMenuPatches } roleMenu.SetScrollBounds(); + roleMenu.scrollBar.ScrollToTop(); _quotaTabCoroutine = null; } diff --git a/MiraAPI/Patches/RoleGuidePatches.cs b/MiraAPI/Patches/RoleGuidePatches.cs index 929e5be8..dfb9bac4 100644 --- a/MiraAPI/Patches/RoleGuidePatches.cs +++ b/MiraAPI/Patches/RoleGuidePatches.cs @@ -279,6 +279,8 @@ public static class RoleGuidePatches MapBehaviour.Instance.Close(); } + MeetingVoteAreaVisibility.HideNameplates(__instance); + if (HudManager.InstanceExists) { ConsoleJoystick.SetMode_MenuAdditive(); @@ -390,6 +392,13 @@ public static class RoleGuidePatches return false; } + [HarmonyPostfix] + [HarmonyPatch(typeof(MatchInfoGuide), nameof(MatchInfoGuide.Close))] + private static void ClosePostfix(MatchInfoGuide __instance) + { + MeetingVoteAreaVisibility.Restore(__instance); + } + private static void SetUpTabGrids(MatchInfoTabPos position) { tabButtonContainer.startCorner = GridLayoutGroup.Corner.UpperLeft; diff --git a/MiraAPI/Patches/Voting/MeetingHudPatches.cs b/MiraAPI/Patches/Voting/MeetingHudPatches.cs index a41b7e41..d9232f38 100644 --- a/MiraAPI/Patches/Voting/MeetingHudPatches.cs +++ b/MiraAPI/Patches/Voting/MeetingHudPatches.cs @@ -106,6 +106,7 @@ internal static class MeetingHudPatches [HarmonyPatch(nameof(MeetingHud.OnDestroy))] public static void OnDestroyPatch(MeetingHud __instance) { + MeetingVoteAreaVisibility.RestoreAll(); MiraEventManager.InvokeEvent(new EndMeetingEvent(__instance)); } @@ -113,6 +114,7 @@ internal static class MeetingHudPatches [HarmonyPatch(nameof(MeetingHud.VotingComplete))] public static void VotingCompletePatch(MeetingHud __instance) { + MeetingVoteAreaVisibility.RestoreAll(); foreach (var meetingAbility in MeetingButtonManager.UntargetedButtons.Where(x => x.Enabled(PlayerControl.LocalPlayer.Data.Role) && (x.DisableUponVoting || x.HideUponWrapUp))) { diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs index 6caa973c..823cdce4 100644 --- a/MiraAPI/Utilities/Helpers.cs +++ b/MiraAPI/Utilities/Helpers.cs @@ -936,7 +936,7 @@ public static class Helpers { const float contentTop = 0.95f; const float horizontalOffset = -0.2f; - const float headerContentGap = 0.2f; + const float groupHeaderContentGap = 0.05f; const int contentSortingOrder = 151; columns = Mathf.Clamp(columns, 1, 6); @@ -1024,7 +1024,7 @@ public static class Helpers groupColumns[group] = i % columns; bandHeights[band] = Mathf.Max( bandHeights[band], - rowPitch * (groupedSettings[i].Count() + 1) + headerContentGap + 0.25f); + rowPitch * (groupedSettings[i].Count() + 1) + groupHeaderContentGap + 0.25f); } var bandOffset = 0f; @@ -1040,7 +1040,6 @@ public static class Helpers } } - var contentBottom = float.PositiveInfinity; var previousGroup = (AbstractOptionGroup?)null; var rowIndex = 0; var column = 0; @@ -1089,12 +1088,12 @@ public static class Helpers if (headerSource) { - const float headerScale = 0.46f; + const float groupHeaderScale = 0.42f; var headerWidth = groupsAsColumns - ? columnWidth / headerScale - : (columns * columnWidth + (columns - 1) * columnGap) / headerScale; + ? columnWidth / groupHeaderScale + : (columns * columnWidth + (columns - 1) * columnGap) / groupHeaderScale; var header = Object.Instantiate(headerSource!, page.transform, false); - header.transform.localScale = Vector3.one * headerScale; + header.transform.localScale = Vector3.one * groupHeaderScale; if (groupsAsColumns) { var totalWidth = columns * columnWidth + (columns - 1) * columnGap; @@ -1155,12 +1154,6 @@ public static class Helpers renderer.maskInteraction = SpriteMaskInteraction.VisibleInsideMask; renderer.material.SetInt(PlayerMaterial.MaskLayer, 50); renderer.sortingOrder = contentSortingOrder; - if (renderer.enabled && renderer.sprite) - { - contentBottom = Mathf.Min( - contentBottom, - inner.InverseTransformPoint(renderer.bounds.min).y); - } } foreach (var text in header.GetComponentsInChildren(true)) @@ -1171,9 +1164,6 @@ public static class Helpers if (text.enabled) { text.ForceMeshUpdate(); - contentBottom = Mathf.Min( - contentBottom, - inner.InverseTransformPoint(text.bounds.min).y); } } @@ -1191,7 +1181,7 @@ public static class Helpers horizontalOffset; var centerY = groupsAsColumns && settings[i].Group is { } rowGroup ? contentTop - groupTopOffsets[rowGroup] - - (rowPitch * (groupRows[rowGroup] + 1)) - headerContentGap - (rowHeight / 2f) + (rowPitch * (groupRows[rowGroup] + 1)) - groupHeaderContentGap - (rowHeight / 2f) : contentTop - (rowHeight / 2f) - (rowIndex * rowPitch) - groupSpacing; var row = Object.Instantiate(rowPrefab, page.transform); row.enabled = false; @@ -1238,27 +1228,6 @@ public static class Helpers renderer.maskInteraction = SpriteMaskInteraction.VisibleInsideMask; renderer.material.SetInt(PlayerMaterial.MaskLayer, 50); renderer.sortingOrder = contentSortingOrder; - if (renderer.enabled && - renderer.sprite && - renderer.transform != row.PlusBtn.transform && - !renderer.transform.IsChildOf(row.PlusBtn.transform) && - renderer.transform != row.MinusBtn.transform && - !renderer.transform.IsChildOf(row.MinusBtn.transform)) - { - contentBottom = Mathf.Min( - contentBottom, - inner.InverseTransformPoint(renderer.bounds.min).y); - } - } - - foreach (var text in row.GetComponentsInChildren(true)) - { - if (text.enabled) - { - contentBottom = Mathf.Min( - contentBottom, - inner.InverseTransformPoint(text.bounds.min).y); - } } row.gameObject.SetActive(true); @@ -1277,6 +1246,26 @@ public static class Helpers } } + page.SetActive(true); + var contentBottom = float.PositiveInfinity; + foreach (var text in page.GetComponentsInChildren(true)) + { + if (text.enabled && text.gameObject.activeInHierarchy) + { + text.ForceMeshUpdate(); + } + } + + foreach (var renderer in page.GetComponentsInChildren(true)) + { + if (renderer.enabled && renderer.gameObject.activeInHierarchy) + { + contentBottom = Mathf.Min( + contentBottom, + inner.InverseTransformPoint(renderer.bounds.min).y); + } + } + var maskArea = parent.transform.FindChild("MaskArea")?.GetComponent(); var maskBottom = maskArea ? inner.InverseTransformPoint(maskArea!.bounds.min).y @@ -1288,7 +1277,6 @@ public static class Helpers ? contentOverflow + 0.05f : 0f; parent.SetYBoundsMax(Mathf.Clamp(scrollRange, 0f, 999f)); - page.SetActive(true); return page; } diff --git a/MiraAPI/Utilities/MeetingVoteAreaVisibility.cs b/MiraAPI/Utilities/MeetingVoteAreaVisibility.cs new file mode 100644 index 00000000..eeffe511 --- /dev/null +++ b/MiraAPI/Utilities/MeetingVoteAreaVisibility.cs @@ -0,0 +1,74 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace MiraAPI.Utilities; + +internal static class MeetingVoteAreaVisibility +{ + private static readonly Dictionary originalRendererStates = []; + private static readonly HashSet activeOwners = []; + + internal static void HideNameplates(UnityEngine.Object owner) + { + var meetingHud = MeetingHud.Instance; + if (!owner || + !meetingHud || + meetingHud.state is not (MeetingHud.MeetingStates.Discussion or + MeetingHud.MeetingStates.NotVoted or + MeetingHud.MeetingStates.Voted)) + { + return; + } + + activeOwners.Add(owner.GetInstanceID()); + foreach (var playerVoteArea in meetingHud.playerStates) + { + if (!playerVoteArea) + { + continue; + } + + var buttons = playerVoteArea.Buttons; + foreach (var renderer in playerVoteArea.GetComponentsInChildren(true)) + { + if (!renderer || + (buttons && + (renderer.transform == buttons.transform || renderer.transform.IsChildOf(buttons.transform)))) + { + continue; + } + + if (!originalRendererStates.ContainsKey(renderer)) + { + originalRendererStates.Add(renderer, renderer.enabled); + } + + renderer.enabled = false; + } + } + } + + internal static void Restore(UnityEngine.Object owner) + { + if (!owner || !activeOwners.Remove(owner.GetInstanceID()) || activeOwners.Count > 0) + { + return; + } + + RestoreAll(); + } + + internal static void RestoreAll() + { + activeOwners.Clear(); + foreach (var rendererState in originalRendererStates) + { + if (rendererState.Key) + { + rendererState.Key.enabled = rendererState.Value; + } + } + + originalRendererStates.Clear(); + } +}