From ca249644dddddd407966b53b34aa28ceb0e5947b Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Mon, 10 Apr 2023 15:33:19 +0530 Subject: [PATCH] Fixed bug for TextBoxEntryMenu --- stardew-access/HarmonyPatches.cs | 20 +- stardew-access/ModEntry.cs | 13 +- .../Patches/MiscPatches/Game1Patch.cs | 6 + .../MiscPatches/IClickableMenuPatch.cs | 189 ++++++++---------- .../Patches/MiscPatches/TextBoxPatch.cs | 5 +- .../Patches/MiscPatches/TextEntryMenuPatch.cs | 23 +++ .../PurchaseAnimalsMenuPatch.cs | 14 +- 7 files changed, 143 insertions(+), 127 deletions(-) create mode 100644 stardew-access/Patches/MiscPatches/TextEntryMenuPatch.cs diff --git a/stardew-access/HarmonyPatches.cs b/stardew-access/HarmonyPatches.cs index 23d1484..c3567c9 100644 --- a/stardew-access/HarmonyPatches.cs +++ b/stardew-access/HarmonyPatches.cs @@ -241,11 +241,6 @@ namespace stardew_access prefix: new HarmonyMethod(typeof(PurchaseAnimalsMenuPatch), nameof(PurchaseAnimalsMenuPatch.DrawPatch)) ); - harmony.Patch( - original: AccessTools.Method(typeof(PurchaseAnimalsMenu), nameof(PurchaseAnimalsMenu.receiveKeyPress), new Type[] { typeof(Keys) }), - prefix: new HarmonyMethod(typeof(PurchaseAnimalsMenuPatch), nameof(PurchaseAnimalsMenuPatch.RecieveKeyPressPatch)) - ); - harmony.Patch( original: AccessTools.Method(typeof(AnimalQueryMenu), nameof(AnimalQueryMenu.draw), new Type[] { typeof(SpriteBatch) }), postfix: new HarmonyMethod(typeof(AnimalQueryMenuPatch), nameof(AnimalQueryMenuPatch.DrawPatch)) @@ -296,6 +291,21 @@ namespace stardew_access original: AccessTools.Method(typeof(TextBox), nameof(TextBox.Draw)), prefix: new HarmonyMethod(typeof(TextBoxPatch), nameof(TextBoxPatch.DrawPatch)) ); + + harmony.Patch( + original: AccessTools.Method(typeof(TextEntryMenu), nameof(TextEntryMenu.draw), new Type[] { typeof(SpriteBatch) }), + prefix: new HarmonyMethod(typeof(TextEntryMenuPatch), nameof(TextEntryMenuPatch.DrawPatch)) + ); + + harmony.Patch( + original: AccessTools.Method(typeof(TextEntryMenu), nameof(TextEntryMenu.Close)), + prefix: new HarmonyMethod(typeof(TextEntryMenuPatch), nameof(TextEntryMenuPatch.ClosePatch)) + ); + + harmony.Patch( + original: AccessTools.Method(typeof(Game1), nameof(Game1.closeTextEntry)), + prefix: new HarmonyMethod(typeof(Game1Patch), nameof(Game1Patch.CloseTextEntryPatch)) + ); } } } diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index 18a55de..7e89a4d 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -13,7 +13,7 @@ namespace stardew_access { #region Global Vars & Properties - #pragma warning disable CS8603 +#pragma warning disable CS8603 private static int prevDate = -99; private static ModConfig? config; private Harmony? harmony; @@ -129,10 +129,21 @@ namespace stardew_access helper.Events.Input.ButtonPressed += this.OnButtonPressed; helper.Events.GameLoop.UpdateTicked += this.onUpdateTicked; helper.Events.GameLoop.DayStarted += this.onDayStarted; + helper.Events.Display.MenuChanged += this.onMenuChanged; AppDomain.CurrentDomain.DomainUnload += OnExit; AppDomain.CurrentDomain.ProcessExit += OnExit; } + private void onMenuChanged(object? sender, MenuChangedEventArgs e) + { + TextBoxPatch.activeTextBoxes = ""; + if (e.OldMenu != null) + { + MainClass.DebugLog($"Switched from {e.OldMenu.GetType().ToString()} menu, performing cleanup..."); + IClickableMenuPatch.Cleanup(e.OldMenu); + } + } + /// Returns the Screen Reader class for other mods to use. public override object GetApi() { diff --git a/stardew-access/Patches/MiscPatches/Game1Patch.cs b/stardew-access/Patches/MiscPatches/Game1Patch.cs index 319acde..2859839 100644 --- a/stardew-access/Patches/MiscPatches/Game1Patch.cs +++ b/stardew-access/Patches/MiscPatches/Game1Patch.cs @@ -13,6 +13,7 @@ namespace stardew_access.Patches { try { + MainClass.DebugLog($"Closing {Game1.activeClickableMenu.GetType().ToString()} menu, performing cleanup..."); IClickableMenuPatch.Cleanup(Game1.activeClickableMenu); } catch (Exception e) @@ -21,6 +22,11 @@ namespace stardew_access.Patches } } + internal static void CloseTextEntryPatch() + { + TextBoxPatch.activeTextBoxes = ""; + } + internal static bool PlaySoundPatch(string cueName) { try diff --git a/stardew-access/Patches/MiscPatches/IClickableMenuPatch.cs b/stardew-access/Patches/MiscPatches/IClickableMenuPatch.cs index 7069fb5..549413c 100644 --- a/stardew-access/Patches/MiscPatches/IClickableMenuPatch.cs +++ b/stardew-access/Patches/MiscPatches/IClickableMenuPatch.cs @@ -199,6 +199,7 @@ namespace stardew_access.Patches { try { + MainClass.DebugLog($"Closed {__instance.GetType().ToString()} menu, performing cleanup..."); Cleanup(__instance); } catch (Exception e) @@ -209,114 +210,88 @@ namespace stardew_access.Patches internal static void Cleanup(IClickableMenu menu) { - if (menu is TitleMenu) + switch (menu) { - TitleMenuPatch.Cleanup(); - } - else if (menu is CoopMenu) - { - CoopMenuPatch.Cleanup(); - } - else if (menu is LoadGameMenu) - { - LoadGameMenuPatch.Cleanup(); - } - else if (menu is AdvancedGameOptions) - { - AdvancedGameOptionsPatch.Cleanup(); - } - else if (menu is LetterViewerMenu) - { - LetterViwerMenuPatch.Cleanup(); - } - else if (menu is LevelUpMenu) - { - LevelUpMenuPatch.Cleanup(); - } - else if (menu is Billboard) - { - BillboardPatch.Cleanup(); - } - else if (menu is GameMenu) - { - GameMenuPatch.Cleanup(); - ExitPagePatch.Cleanup(); - OptionsPagePatch.Cleanup(); - SocialPagePatch.Cleanup(); - InventoryPagePatch.Cleanup(); - CraftingPagePatch.Cleanup(); - } - else if (menu is JunimoNoteMenu) - { - JunimoNoteMenuPatch.Cleanup(); - } - else if (menu is ShopMenu) - { - ShopMenuPatch.Cleanup(); - } - else if (menu is ItemGrabMenu) - { - ItemGrabMenuPatch.Cleanup(); - } - else if (menu is GeodeMenu) - { - GeodeMenuPatch.Cleanup(); - } - else if (menu is CarpenterMenu) - { - CarpenterMenuPatch.Cleanup(); - } - else if (menu is PurchaseAnimalsMenu) - { - PurchaseAnimalsMenuPatch.Cleanup(); - } - else if (menu is AnimalQueryMenu) - { - AnimalQueryMenuPatch.Cleanup(); - } - else if (menu is DialogueBox) - { - DialogueBoxPatch.Cleanup(); - } - else if (menu is JojaCDMenu) - { - JojaCDMenuPatch.Cleanup(); - } - else if (menu is QuestLog) - { - QuestLogPatch.Cleaup(); - } - else if (menu is TailoringMenu) - { - TailoringMenuPatch.Cleanup(); - } - else if (menu is ForgeMenu) - { - ForgeMenuPatch.Cleanup(); - } - else if (menu is ItemListMenu) - { - ItemListMenuPatch.Cleanup(); - } - else if (menu is FieldOfficeMenu) - { - FieldOfficeMenuPatch.Cleanup(); - } - else if (menu is MuseumMenu) - { - MuseumMenuPatch.Cleanup(); - } - else if (menu is PondQueryMenu) - { - PondQueryMenuPatch.Cleanup(); - } - else if (menu is GeodeMenu) - { - GeodeMenuPatch.Cleanup(); - } - else if (menu is SpecialOrdersBoard) - { - SpecialOrdersBoardPatch.Cleanup(); + case TitleMenu: + TitleMenuPatch.Cleanup(); + break; + case CoopMenu: + CoopMenuPatch.Cleanup(); + break; + case LoadGameMenu: + LoadGameMenuPatch.Cleanup(); + break; + case AdvancedGameOptions: + AdvancedGameOptionsPatch.Cleanup(); + break; + case LetterViewerMenu: + LetterViwerMenuPatch.Cleanup(); + break; + case LevelUpMenu: + LevelUpMenuPatch.Cleanup(); + break; + case Billboard: + BillboardPatch.Cleanup(); + break; + case GameMenu: + GameMenuPatch.Cleanup(); + ExitPagePatch.Cleanup(); + OptionsPagePatch.Cleanup(); + SocialPagePatch.Cleanup(); + InventoryPagePatch.Cleanup(); + CraftingPagePatch.Cleanup(); + break; + case JunimoNoteMenu: + JunimoNoteMenuPatch.Cleanup(); + break; + case ShopMenu: + ShopMenuPatch.Cleanup(); + break; + case ItemGrabMenu: + ItemGrabMenuPatch.Cleanup(); + break; + case GeodeMenu: + GeodeMenuPatch.Cleanup(); + break; + case CarpenterMenu: + CarpenterMenuPatch.Cleanup(); + break; + case PurchaseAnimalsMenu: + PurchaseAnimalsMenuPatch.Cleanup(); + break; + case AnimalQueryMenu: + AnimalQueryMenuPatch.Cleanup(); + break; + case DialogueBox: + DialogueBoxPatch.Cleanup(); + break; + case JojaCDMenu: + JojaCDMenuPatch.Cleanup(); + break; + case QuestLog: + QuestLogPatch.Cleaup(); + break; + case TailoringMenu: + TailoringMenuPatch.Cleanup(); + break; + case ForgeMenu: + ForgeMenuPatch.Cleanup(); + break; + case ItemListMenu: + ItemListMenuPatch.Cleanup(); + break; + case FieldOfficeMenu: + FieldOfficeMenuPatch.Cleanup(); + break; + case MuseumMenu: + MuseumMenuPatch.Cleanup(); + break; + case PondQueryMenu: + PondQueryMenuPatch.Cleanup(); + break; + case SpecialOrdersBoard: + SpecialOrdersBoardPatch.Cleanup(); + break; } InventoryUtils.Cleanup(); diff --git a/stardew-access/Patches/MiscPatches/TextBoxPatch.cs b/stardew-access/Patches/MiscPatches/TextBoxPatch.cs index cc3de3e..988db56 100644 --- a/stardew-access/Patches/MiscPatches/TextBoxPatch.cs +++ b/stardew-access/Patches/MiscPatches/TextBoxPatch.cs @@ -2,7 +2,7 @@ namespace stardew_access.Patches { internal class TextBoxPatch { - internal static string textBoxQuery = " "; + internal static string textBoxQuery = ""; internal static string activeTextBoxes = ""; internal static bool isAnyTextBoxActive => activeTextBoxes != ""; @@ -13,7 +13,7 @@ namespace stardew_access.Patches string uniqueIdentifier = $"{__instance.X}:{__instance.Y}:{__instance.Height}:{__instance.Width}"; if (!__instance.Selected) { - if (activeTextBoxes.Contains(uniqueIdentifier)) activeTextBoxes = activeTextBoxes.Replace(uniqueIdentifier, ""); + if (activeTextBoxes.Contains(uniqueIdentifier)) activeTextBoxes = activeTextBoxes.Replace(uniqueIdentifier, ""); return; } @@ -24,6 +24,7 @@ namespace stardew_access.Patches if (isEscPressed) { + if (activeTextBoxes.Contains(uniqueIdentifier)) activeTextBoxes = activeTextBoxes.Replace(uniqueIdentifier, ""); __instance.Selected = false; } diff --git a/stardew-access/Patches/MiscPatches/TextEntryMenuPatch.cs b/stardew-access/Patches/MiscPatches/TextEntryMenuPatch.cs new file mode 100644 index 0000000..b5c10ad --- /dev/null +++ b/stardew-access/Patches/MiscPatches/TextEntryMenuPatch.cs @@ -0,0 +1,23 @@ + +namespace stardew_access.Patches +{ + internal class TextEntryMenuPatch + { + internal static void DrawPatch(StardewValley.Menus.TextEntryMenu __instance, StardewValley.Menus.TextBox ____target) + { + try + { + TextBoxPatch.DrawPatch(____target); + } + catch (Exception e) + { + MainClass.ErrorLog($"An error occured in DrawPatch() in TextEntryPatch:\n{e.Message}\n{e.StackTrace}"); + } + } + + internal static void ClosePatch() + { + TextBoxPatch.activeTextBoxes = ""; + } + } +} diff --git a/stardew-access/Patches/OtherMenuPatches/PurchaseAnimalsMenuPatch.cs b/stardew-access/Patches/OtherMenuPatches/PurchaseAnimalsMenuPatch.cs index 8a8b41a..b4b0035 100644 --- a/stardew-access/Patches/OtherMenuPatches/PurchaseAnimalsMenuPatch.cs +++ b/stardew-access/Patches/OtherMenuPatches/PurchaseAnimalsMenuPatch.cs @@ -66,7 +66,7 @@ namespace stardew_access.Patches } if (purchaseAnimalMenuQuery == toSpeak) return; - + purchaseAnimalMenuQuery = toSpeak; if (firstTimeInNamingMenu) @@ -98,21 +98,11 @@ namespace stardew_access.Patches } if (purchaseAnimalMenuQuery == toSpeak) return; - + purchaseAnimalMenuQuery = toSpeak; MainClass.ScreenReader.Say(toSpeak, true); } - internal static bool RecieveKeyPressPatch(PurchaseAnimalsMenu __instance, Microsoft.Xna.Framework.Input.Keys key) - { - if (TextBoxPatch.isAnyTextBoxActive && Game1.options.doesInputListContain(Game1.options.menuButton, key) && __instance.readyToClose()) - { - return false; - } - - return true; - } - internal static void Cleanup() { purchaseAnimalMenuQuery = "";