From 5296c4cabe0f813ae605f485a3755d454e1f4f39 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Tue, 21 Feb 2023 21:28:38 +0530 Subject: [PATCH] Added patch to disable left mouse sim key when a tex box is active Also speak the content of the text box when active --- stardew-access/HarmonyPatches.cs | 5 ++ stardew-access/ModEntry.cs | 6 +- .../Patches/BuildingNAnimalMenuPatches.cs | 77 ++++++++----------- stardew-access/Patches/TextBoxPatch.cs | 40 ++++++++++ 4 files changed, 82 insertions(+), 46 deletions(-) create mode 100644 stardew-access/Patches/TextBoxPatch.cs diff --git a/stardew-access/HarmonyPatches.cs b/stardew-access/HarmonyPatches.cs index dcdc7bd..692ce38 100644 --- a/stardew-access/HarmonyPatches.cs +++ b/stardew-access/HarmonyPatches.cs @@ -285,6 +285,11 @@ namespace stardew_access original: AccessTools.Method(typeof(InstanceGame), nameof(InstanceGame.Exit)), prefix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.ExitEventPatch)) ); + + harmony.Patch( + original: AccessTools.Method(typeof(TextBox), nameof(TextBox.Draw)), + prefix: new HarmonyMethod(typeof(TextBoxPatch), nameof(TextBoxPatch.DrawPatch)) + ); } } } diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index 7049d76..3233e58 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -29,6 +29,8 @@ namespace stardew_access internal static ModConfig Config { get => config; set => config = value; } public static IModHelper? ModHelper { get => modHelper; } + public static bool isAnyTextBoxActive = false; + public static StaticTiles STiles { get @@ -226,7 +228,7 @@ namespace stardew_access } // Alternate Keybinds - if (!isCustomizingCharacter && Game1.activeClickableMenu is not AnimalQueryMenu && Config.LeftClickAlternateKey.JustPressed()) // Excluding the character creation menu + if (!isCustomizingCharacter && !isAnyTextBoxActive && Config.LeftClickAlternateKey.JustPressed()) // Excluding the character creation menu { Game1.activeClickableMenu.receiveLeftClick(Game1.getMouseX(true), Game1.getMouseY(true)); } @@ -377,4 +379,4 @@ namespace stardew_access monitor.Log(message, LogLevel.Debug); } } -} \ No newline at end of file +} diff --git a/stardew-access/Patches/BuildingNAnimalMenuPatches.cs b/stardew-access/Patches/BuildingNAnimalMenuPatches.cs index 910e45b..d52e981 100644 --- a/stardew-access/Patches/BuildingNAnimalMenuPatches.cs +++ b/stardew-access/Patches/BuildingNAnimalMenuPatches.cs @@ -27,65 +27,54 @@ namespace stardew_access.Patches { try { + if (MainClass.isAnyTextBoxActive) return; + int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position bool isPrimaryInfoKeyPressed = MainClass.Config.PrimaryInfoKey.JustPressed(); // For narrating animal details - bool isEscPressed = Game1.input.GetKeyboardState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape); // For escaping/unselecting from the animal name text box string toSpeak = " ", details = " "; isOnFarm = ___movingAnimal; animalQueryMenu = __instance; animalBeingPurchasedOrMoved = ___animal; - if (___textBox.Selected) + if (isPrimaryInfoKeyPressed & !isNarratingAnimalInfo) { - toSpeak = ___textBox.Text; - - if (isEscPressed) + string name = ___animal.displayName; + string type = ___animal.displayType; + int age = (___animal.GetDaysOwned() + 1) / 28 + 1; + string ageText = (age <= 1) ? Game1.content.LoadString("Strings\\UI:AnimalQuery_Age1") : Game1.content.LoadString("Strings\\UI:AnimalQuery_AgeN", age); + string parent = ""; + if ((int)___animal.age.Value < (byte)___animal.ageWhenMature.Value) { - ___textBox.Selected = false; + ageText += Game1.content.LoadString("Strings\\UI:AnimalQuery_AgeBaby"); } - } - else - { - if (isPrimaryInfoKeyPressed & !isNarratingAnimalInfo) + if (___parentName != null) { - string name = ___animal.displayName; - string type = ___animal.displayType; - int age = (___animal.GetDaysOwned() + 1) / 28 + 1; - string ageText = (age <= 1) ? Game1.content.LoadString("Strings\\UI:AnimalQuery_Age1") : Game1.content.LoadString("Strings\\UI:AnimalQuery_AgeN", age); - string parent = ""; - if ((int)___animal.age.Value < (byte)___animal.ageWhenMature.Value) - { - ageText += Game1.content.LoadString("Strings\\UI:AnimalQuery_AgeBaby"); - } - if (___parentName != null) - { - parent = Game1.content.LoadString("Strings\\UI:AnimalQuery_Parent", ___parentName); - } - - details = $"Name: {name} Type: {type} \n\t Age: {ageText} {parent}"; - animalQueryMenuQuery = " "; - - isNarratingAnimalInfo = true; - Task.Delay(200).ContinueWith(_ => { isNarratingAnimalInfo = false; }); + parent = Game1.content.LoadString("Strings\\UI:AnimalQuery_Parent", ___parentName); } - if (__instance.okButton != null && __instance.okButton.containsPoint(x, y)) - toSpeak = "OK button"; - else if (__instance.sellButton != null && __instance.sellButton.containsPoint(x, y)) - toSpeak = $"Sell for {___animal.getSellPrice()}g button"; - else if (___confirmingSell && __instance.yesButton != null && __instance.yesButton.containsPoint(x, y)) - toSpeak = "Confirm selling animal"; - else if (___confirmingSell && __instance.noButton != null && __instance.noButton.containsPoint(x, y)) - toSpeak = "Cancel selling animal"; - else if (__instance.moveHomeButton != null && __instance.moveHomeButton.containsPoint(x, y)) - toSpeak = "Change home building button"; - else if (__instance.allowReproductionButton != null && __instance.allowReproductionButton.containsPoint(x, y)) - toSpeak = ((___animal.allowReproduction.Value) ? "Enabled" : "Disabled") + " allow reproduction button"; - else if (__instance.textBoxCC != null && __instance.textBoxCC.containsPoint(x, y)) - toSpeak = "Animal name text box"; + details = $"Name: {name} Type: {type} \n\t Age: {ageText} {parent}"; + animalQueryMenuQuery = " "; + + isNarratingAnimalInfo = true; + Task.Delay(200).ContinueWith(_ => { isNarratingAnimalInfo = false; }); } + if (__instance.okButton != null && __instance.okButton.containsPoint(x, y)) + toSpeak = "OK button"; + else if (__instance.sellButton != null && __instance.sellButton.containsPoint(x, y)) + toSpeak = $"Sell for {___animal.getSellPrice()}g button"; + else if (___confirmingSell && __instance.yesButton != null && __instance.yesButton.containsPoint(x, y)) + toSpeak = "Confirm selling animal"; + else if (___confirmingSell && __instance.noButton != null && __instance.noButton.containsPoint(x, y)) + toSpeak = "Cancel selling animal"; + else if (__instance.moveHomeButton != null && __instance.moveHomeButton.containsPoint(x, y)) + toSpeak = "Change home building button"; + else if (__instance.allowReproductionButton != null && __instance.allowReproductionButton.containsPoint(x, y)) + toSpeak = ((___animal.allowReproduction.Value) ? "Enabled" : "Disabled") + " allow reproduction button"; + else if (__instance.textBoxCC != null && __instance.textBoxCC.containsPoint(x, y)) + toSpeak = "Animal name text box"; + if (animalQueryMenuQuery != toSpeak) { animalQueryMenuQuery = toSpeak; @@ -711,4 +700,4 @@ namespace stardew_access.Patches return; } } -} \ No newline at end of file +} diff --git a/stardew-access/Patches/TextBoxPatch.cs b/stardew-access/Patches/TextBoxPatch.cs new file mode 100644 index 0000000..f940193 --- /dev/null +++ b/stardew-access/Patches/TextBoxPatch.cs @@ -0,0 +1,40 @@ +namespace stardew_access.Patches +{ + internal class TextBoxPatch + { + internal static string textBoxQuery = " "; + + internal static void DrawPatch(StardewValley.Menus.TextBox __instance) + { + try + { + bool isEscPressed = StardewValley.Game1.input.GetKeyboardState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape); // For escaping/unselecting from the animal name text box + string toSpeak = " "; + if (__instance.Selected) + { + MainClass.isAnyTextBoxActive = true; + toSpeak = __instance.Text; + + if (isEscPressed) + { + __instance.Selected = false; + } + } + else + { + MainClass.isAnyTextBoxActive = false; + } + + if (textBoxQuery != toSpeak) + { + textBoxQuery = toSpeak; + MainClass.ScreenReader.Say(toSpeak, true); + } + } + catch (Exception e) + { + MainClass.ErrorLog($"An error occured in DrawPatch() in TextBoxPatch:\n{e.Message}\n{e.StackTrace}"); + } + } + } +}