From 403a97b63323dc193914d6c4d6fdc9b925654f83 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Tue, 7 Mar 2023 12:03:08 +0530 Subject: [PATCH] Improved and organised code in AnimalQueryMenuPatch.cs --- .../Patches/AnimalQueryMenuPatch.cs | 103 ++++++++++-------- stardew-access/Patches/IClickableMenuPatch.cs | 3 +- 2 files changed, 60 insertions(+), 46 deletions(-) diff --git a/stardew-access/Patches/AnimalQueryMenuPatch.cs b/stardew-access/Patches/AnimalQueryMenuPatch.cs index 232d573..334e580 100644 --- a/stardew-access/Patches/AnimalQueryMenuPatch.cs +++ b/stardew-access/Patches/AnimalQueryMenuPatch.cs @@ -18,61 +18,76 @@ namespace stardew_access.Patches if (TextBoxPatch.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 - string toSpeak = " ", details = " "; isOnFarm = ___movingAnimal; animalQueryMenu = __instance; animalBeingMoved = ___animal; - if (isPrimaryInfoKeyPressed & !isNarratingAnimalInfo) - { - 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); - } + narrateAnimalDetailsOnKeyPress(___animal, ___parentName); - 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; - MainClass.ScreenReader.Say($"{details} {toSpeak}", true); - } + narrateHoveredButton(__instance, ___animal, ___confirmingSell, x, y); } catch (System.Exception e) { MainClass.ErrorLog($"An error occured in AnimalQueryMenuPatch()->DrawPatch():\n{e.Message}\n{e.StackTrace}"); } } + + private static void narrateAnimalDetailsOnKeyPress(FarmAnimal ___animal, string ___parentName) + { + bool isPrimaryInfoKeyPressed = MainClass.Config.PrimaryInfoKey.JustPressed(); + if (!isPrimaryInfoKeyPressed | isNarratingAnimalInfo) + return; + + 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); + } + + isNarratingAnimalInfo = true; + Task.Delay(200).ContinueWith(_ => { isNarratingAnimalInfo = false; }); // Adds delay + + MainClass.ScreenReader.Say($"Name: {name} Type: {type} \n\t Age: {ageText} {parent}", true); + } + + private static void narrateHoveredButton(AnimalQueryMenu __instance, FarmAnimal ___animal, bool ___confirmingSell, int x, int y) + { + string toSpeak = ""; + 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; + MainClass.ScreenReader.Say($"{toSpeak}", true); + } + } + + internal static void Cleanup() + { + AnimalQueryMenuPatch.animalQueryMenuQuery = ""; + AnimalQueryMenuPatch.animalQueryMenu = null; + } } } diff --git a/stardew-access/Patches/IClickableMenuPatch.cs b/stardew-access/Patches/IClickableMenuPatch.cs index 24a6184..da31c47 100644 --- a/stardew-access/Patches/IClickableMenuPatch.cs +++ b/stardew-access/Patches/IClickableMenuPatch.cs @@ -77,8 +77,7 @@ namespace stardew_access.Patches } else if (menu is AnimalQueryMenu) { - AnimalQueryMenuPatch.animalQueryMenuQuery = ""; - AnimalQueryMenuPatch.animalQueryMenu = null; + AnimalQueryMenuPatch.Cleanup(); } else if (menu is DialogueBox) {