From 07fcc942960ce66790891f8054a486f0ca215cb8 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Fri, 10 Mar 2023 14:50:40 +0530 Subject: [PATCH] Moved forge menu patch to its own class --- stardew-access/HarmonyPatches.cs | 2 +- stardew-access/Patches/ForgeMenuPatch.cs | 116 ++++++++++++++++++ stardew-access/Patches/IClickableMenuPatch.cs | 2 +- stardew-access/Patches/MenuPatches.cs | 85 ------------- 4 files changed, 118 insertions(+), 87 deletions(-) create mode 100644 stardew-access/Patches/ForgeMenuPatch.cs diff --git a/stardew-access/HarmonyPatches.cs b/stardew-access/HarmonyPatches.cs index f894686..5e84c6b 100644 --- a/stardew-access/HarmonyPatches.cs +++ b/stardew-access/HarmonyPatches.cs @@ -184,7 +184,7 @@ namespace stardew_access harmony.Patch( original: AccessTools.Method(typeof(ForgeMenu), nameof(ForgeMenu.draw), new Type[] { typeof(SpriteBatch) }), - postfix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.ForgeMenuPatch)) + postfix: new HarmonyMethod(typeof(ForgeMenuPatch), nameof(ForgeMenuPatch.DrawPatch)) ); harmony.Patch( diff --git a/stardew-access/Patches/ForgeMenuPatch.cs b/stardew-access/Patches/ForgeMenuPatch.cs new file mode 100644 index 0000000..d5b6671 --- /dev/null +++ b/stardew-access/Patches/ForgeMenuPatch.cs @@ -0,0 +1,116 @@ +using stardew_access.Features; +using StardewValley; +using StardewValley.Menus; + +namespace stardew_access.Patches +{ + internal class ForgeMenuPatch + { + private static string forgeMenuQuery = ""; + + internal static void DrawPatch(ForgeMenu __instance) + { + try + { + int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position + + if (narrateHoveredButton(__instance, x, y)) return; + + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) + { + Cleanup(); + } + + } + catch (System.Exception e) + { + MainClass.ErrorLog($"An error occured in forge menu patch:\n{e.Message}\n{e.StackTrace}"); + } + } + + private static bool narrateHoveredButton(ForgeMenu __instance, int x, int y) + { + string toSpeak = ""; + bool isDropItemButton = false; + + if (__instance.leftIngredientSpot != null && __instance.leftIngredientSpot.containsPoint(x, y)) + { + if (__instance.leftIngredientSpot.item == null) + { + toSpeak = "Input weapon or tool here"; + } + else + { + Item item = __instance.leftIngredientSpot.item; + toSpeak = $"Weapon slot: {item.Stack} {item.DisplayName}"; + } + } + else if (__instance.rightIngredientSpot != null && __instance.rightIngredientSpot.containsPoint(x, y)) + { + if (__instance.rightIngredientSpot.item == null) + { + toSpeak = "Input gemstone here"; + } + else + { + Item item = __instance.rightIngredientSpot.item; + toSpeak = $"Gemstone slot: {item.Stack} {item.DisplayName}"; + } + } + else if (__instance.startTailoringButton != null && __instance.startTailoringButton.containsPoint(x, y)) + { + toSpeak = "Star forging button"; + } + else if (__instance.unforgeButton != null && __instance.unforgeButton.containsPoint(x, y)) + { + toSpeak = "Unforge button"; + } + else if (__instance.trashCan != null && __instance.trashCan.containsPoint(x, y)) + { + toSpeak = "Trashcan"; + } + else if (__instance.okButton != null && __instance.okButton.containsPoint(x, y)) + { + toSpeak = "ok button"; + } + else if (__instance.dropItemInvisibleButton != null && __instance.dropItemInvisibleButton.containsPoint(x, y)) + { + toSpeak = "drop item"; + isDropItemButton = true; + } + else if (__instance.equipmentIcons.Count > 0 && __instance.equipmentIcons[0].containsPoint(x, y)) + { + toSpeak = "Left ring Slot"; + + if (Game1.player.leftRing.Value != null) + toSpeak = $"{toSpeak}: {Game1.player.leftRing.Value.DisplayName}"; + } + else if (__instance.equipmentIcons.Count > 0 && __instance.equipmentIcons[1].containsPoint(x, y)) + { + toSpeak = "Right ring Slot"; + + if (Game1.player.rightRing.Value != null) + toSpeak = $"{toSpeak}: {Game1.player.rightRing.Value.DisplayName}"; + } + else + { + return false; + } + + if (forgeMenuQuery != toSpeak) + { + forgeMenuQuery = toSpeak; + MainClass.ScreenReader.Say(toSpeak, true); + + if (isDropItemButton) Game1.playSound("drop_item"); + } + + return true; + } + + internal static void Cleanup() + { + forgeMenuQuery = ""; + } + } +} diff --git a/stardew-access/Patches/IClickableMenuPatch.cs b/stardew-access/Patches/IClickableMenuPatch.cs index 35ffe98..6f8f121 100644 --- a/stardew-access/Patches/IClickableMenuPatch.cs +++ b/stardew-access/Patches/IClickableMenuPatch.cs @@ -292,7 +292,7 @@ namespace stardew_access.Patches } else if (menu is ForgeMenu) { - MenuPatches.forgeMenuQuery = " "; + ForgeMenuPatch.Cleanup(); } else if (menu is ItemListMenu) { diff --git a/stardew-access/Patches/MenuPatches.cs b/stardew-access/Patches/MenuPatches.cs index 20ca4c4..9a1b2d4 100644 --- a/stardew-access/Patches/MenuPatches.cs +++ b/stardew-access/Patches/MenuPatches.cs @@ -14,93 +14,8 @@ namespace stardew_access.Patches internal static bool isNarratingPondInfo = false; internal static string tailoringMenuQuery = " "; internal static string pondQueryMenuQuery = " "; - internal static string forgeMenuQuery = " "; internal static int prevSlotIndex = -999; - internal static void ForgeMenuPatch(ForgeMenu __instance) - { - try - { - int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position - string toSpeak = " "; - - if (__instance.leftIngredientSpot != null && __instance.leftIngredientSpot.containsPoint(x, y)) - { - if (__instance.leftIngredientSpot.item == null) - { - toSpeak = "Input weapon or tool here"; - } - else - { - Item item = __instance.leftIngredientSpot.item; - toSpeak = $"Weapon slot: {item.Stack} {item.DisplayName}"; - } - } - else if (__instance.rightIngredientSpot != null && __instance.rightIngredientSpot.containsPoint(x, y)) - { - if (__instance.rightIngredientSpot.item == null) - { - toSpeak = "Input gemstone here"; - } - else - { - Item item = __instance.rightIngredientSpot.item; - toSpeak = $"Gemstone slot: {item.Stack} {item.DisplayName}"; - } - } - else if (__instance.startTailoringButton != null && __instance.startTailoringButton.containsPoint(x, y)) - { - toSpeak = "Star forging button"; - } - else if (__instance.unforgeButton != null && __instance.unforgeButton.containsPoint(x, y)) - { - toSpeak = "Unforge button"; - } - else if (__instance.trashCan != null && __instance.trashCan.containsPoint(x, y)) - { - toSpeak = "Trashcan"; - } - else if (__instance.okButton != null && __instance.okButton.containsPoint(x, y)) - { - toSpeak = "ok button"; - } - else if (__instance.dropItemInvisibleButton != null && __instance.dropItemInvisibleButton.containsPoint(x, y)) - { - toSpeak = "drop item"; - } - else if (__instance.equipmentIcons.Count > 0 && __instance.equipmentIcons[0].containsPoint(x, y)) - { - toSpeak = "Left ring Slot"; - - if (Game1.player.leftRing.Value != null) - toSpeak = $"{toSpeak}: {Game1.player.leftRing.Value.DisplayName}"; - } - else if (__instance.equipmentIcons.Count > 0 && __instance.equipmentIcons[1].containsPoint(x, y)) - { - toSpeak = "Right ring Slot"; - - if (Game1.player.rightRing.Value != null) - toSpeak = $"{toSpeak}: {Game1.player.rightRing.Value.DisplayName}"; - } - - if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) - return; - - if (forgeMenuQuery != toSpeak) - { - forgeMenuQuery = toSpeak; - MainClass.ScreenReader.Say(toSpeak, true); - - if (__instance.dropItemInvisibleButton != null && __instance.dropItemInvisibleButton.containsPoint(x, y)) - Game1.playSound("drop_item"); - } - } - catch (System.Exception e) - { - MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}"); - } - } - internal static void PondQueryMenuPatch(PondQueryMenu __instance, StardewValley.Object ____fishItem, FishPond ____pond, string ____statusText, bool ___confirmingEmpty) { try