From 4a39bf94ead74f66229d737855bf0d680bfadb37 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Fri, 24 Feb 2023 13:49:50 +0530 Subject: [PATCH] Moved shop menu patch --- stardew-access/HarmonyPatches.cs | 2 +- stardew-access/Patches/GameMenuPatches.cs | 107 --------------- stardew-access/Patches/IClickableMenuPatch.cs | 2 +- stardew-access/Patches/ShopMenuPatch.cs | 123 ++++++++++++++++++ 4 files changed, 125 insertions(+), 109 deletions(-) create mode 100644 stardew-access/Patches/ShopMenuPatch.cs diff --git a/stardew-access/HarmonyPatches.cs b/stardew-access/HarmonyPatches.cs index 72475db..1948818 100644 --- a/stardew-access/HarmonyPatches.cs +++ b/stardew-access/HarmonyPatches.cs @@ -100,7 +100,7 @@ namespace stardew_access harmony.Patch( original: AccessTools.Method(typeof(ShopMenu), nameof(ShopMenu.draw), new Type[] { typeof(SpriteBatch) }), - postfix: new HarmonyMethod(typeof(GameMenuPatches), nameof(GameMenuPatches.ShopMenuPatch)) + postfix: new HarmonyMethod(typeof(ShopMenuPatch), nameof(ShopMenuPatch.DrawPatch)) ); harmony.Patch( diff --git a/stardew-access/Patches/GameMenuPatches.cs b/stardew-access/Patches/GameMenuPatches.cs index f5d0ea2..bdca60c 100644 --- a/stardew-access/Patches/GameMenuPatches.cs +++ b/stardew-access/Patches/GameMenuPatches.cs @@ -15,7 +15,6 @@ namespace stardew_access.Patches internal static string inventoryPageQueryKey = ""; internal static string exitPageQueryKey = ""; internal static string optionsPageQueryKey = ""; - internal static string shopMenuQueryKey = ""; internal static string socialPageQuery = ""; internal static string profilePageQuery = ""; internal static int currentSelectedCraftingRecipe = -1; @@ -185,112 +184,6 @@ namespace stardew_access.Patches } } - internal static void ShopMenuPatch(ShopMenu __instance) - { - try - { - int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position - - if (MainClass.Config.SnapToFirstInventorySlotKey.JustPressed() && __instance.inventory.inventory.Count > 0) - { - __instance.inventory.inventory[0].snapMouseCursorToCenter(); - __instance.setCurrentlySnappedComponentTo(__instance.inventory.inventory[0].myID); - } - else if (MainClass.Config.SnapToFirstSecondaryInventorySlotKey.JustPressed() && __instance.forSaleButtons.Count > 0) - { - __instance.forSaleButtons[0].snapMouseCursorToCenter(); - __instance.setCurrentlySnappedComponentTo(__instance.forSaleButtons[0].myID); - } - - #region Narrate buttons in the menu - if (__instance.inventory.dropItemInvisibleButton != null && __instance.inventory.dropItemInvisibleButton.containsPoint(x, y)) - { - string toSpeak = "Drop Item"; - if (shopMenuQueryKey != toSpeak) - { - shopMenuQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - Game1.playSound("drop_item"); - } - return; - } - if (__instance.upArrow != null && __instance.upArrow.containsPoint(x, y)) - { - string toSpeak = "Up Arrow Button"; - if (shopMenuQueryKey != toSpeak) - { - shopMenuQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } - return; - } - if (__instance.downArrow != null && __instance.downArrow.containsPoint(x, y)) - { - string toSpeak = "Down Arrow Button"; - if (shopMenuQueryKey != toSpeak) - { - shopMenuQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } - return; - } - #endregion - - #region Narrate hovered item - if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice)) - { - shopMenuQueryKey = ""; - return; - } - #endregion - - #region Narrate hovered selling item - if (__instance.hoveredItem != null) - { - string name = __instance.hoveredItem.DisplayName; - string price = $"Buy Price: {__instance.hoverPrice} g"; - string description = __instance.hoveredItem.getDescription(); - string requirements = ""; - - #region Narrate required items for item - int itemIndex = -1, itemAmount = 5; - - if (__instance.itemPriceAndStock[__instance.hoveredItem].Length > 2) - itemIndex = __instance.itemPriceAndStock[__instance.hoveredItem][2]; - - if (__instance.itemPriceAndStock[__instance.hoveredItem].Length > 3) - itemAmount = __instance.itemPriceAndStock[__instance.hoveredItem][3]; - - if (itemIndex != -1) - { - string itemName = Game1.objectInformation[itemIndex].Split('/')[0]; - - if (itemAmount != -1) - requirements = $"Required: {itemAmount} {itemName}"; - else - requirements = $"Required: {itemName}"; - } - #endregion - - string toSpeak = $"{name}, {requirements}, {price}, \n\t{description}"; - if (shopMenuQueryKey != toSpeak) - { - shopMenuQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } - } - #endregion - } - catch (Exception e) - { - MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}"); - } - } - internal static void GameMenuPatch(GameMenu __instance) { try diff --git a/stardew-access/Patches/IClickableMenuPatch.cs b/stardew-access/Patches/IClickableMenuPatch.cs index ae98f9d..67c91a1 100644 --- a/stardew-access/Patches/IClickableMenuPatch.cs +++ b/stardew-access/Patches/IClickableMenuPatch.cs @@ -51,7 +51,7 @@ namespace stardew_access.Patches } else if (menu is ShopMenu) { - GameMenuPatches.shopMenuQueryKey = ""; + ShopMenuPatch.Cleanup(); } else if (menu is ItemGrabMenu) { diff --git a/stardew-access/Patches/ShopMenuPatch.cs b/stardew-access/Patches/ShopMenuPatch.cs new file mode 100644 index 0000000..4f565a9 --- /dev/null +++ b/stardew-access/Patches/ShopMenuPatch.cs @@ -0,0 +1,123 @@ +using StardewValley; +using StardewValley.Menus; + +namespace stardew_access.Patches +{ + internal class ShopMenuPatch + { + internal static string shopMenuQueryKey = ""; + internal static string hoveredItemQueryKey = ""; + + internal static void DrawPatch(ShopMenu __instance) + { + try + { + int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position + + if (MainClass.Config.SnapToFirstInventorySlotKey.JustPressed() && __instance.inventory.inventory.Count > 0) + { + __instance.inventory.inventory[0].snapMouseCursorToCenter(); + __instance.setCurrentlySnappedComponentTo(__instance.inventory.inventory[0].myID); + } + else if (MainClass.Config.SnapToFirstSecondaryInventorySlotKey.JustPressed() && __instance.forSaleButtons.Count > 0) + { + __instance.forSaleButtons[0].snapMouseCursorToCenter(); + __instance.setCurrentlySnappedComponentTo(__instance.forSaleButtons[0].myID); + } + + #region Narrate buttons in the menu + if (__instance.inventory.dropItemInvisibleButton != null && __instance.inventory.dropItemInvisibleButton.containsPoint(x, y)) + { + string toSpeak = "Drop Item"; + if (shopMenuQueryKey != toSpeak) + { + shopMenuQueryKey = toSpeak; + hoveredItemQueryKey = ""; + MainClass.ScreenReader.Say(toSpeak, true); + Game1.playSound("drop_item"); + } + return; + } + if (__instance.upArrow != null && __instance.upArrow.containsPoint(x, y)) + { + string toSpeak = "Up Arrow Button"; + if (shopMenuQueryKey != toSpeak) + { + shopMenuQueryKey = toSpeak; + hoveredItemQueryKey = ""; + MainClass.ScreenReader.Say(toSpeak, true); + } + return; + } + if (__instance.downArrow != null && __instance.downArrow.containsPoint(x, y)) + { + string toSpeak = "Down Arrow Button"; + if (shopMenuQueryKey != toSpeak) + { + shopMenuQueryKey = toSpeak; + hoveredItemQueryKey = ""; + MainClass.ScreenReader.Say(toSpeak, true); + } + return; + } + #endregion + + #region Narrate hovered item + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice)) + { + shopMenuQueryKey = ""; + return; + } + #endregion + + #region Narrate hovered selling item + if (__instance.hoveredItem != null) + { + string name = __instance.hoveredItem.DisplayName; + string price = $"Buy Price: {__instance.hoverPrice} g"; + string description = __instance.hoveredItem.getDescription(); + string requirements = ""; + + #region Narrate required items for item + int itemIndex = -1, itemAmount = 5; + + if (__instance.itemPriceAndStock[__instance.hoveredItem].Length > 2) + itemIndex = __instance.itemPriceAndStock[__instance.hoveredItem][2]; + + if (__instance.itemPriceAndStock[__instance.hoveredItem].Length > 3) + itemAmount = __instance.itemPriceAndStock[__instance.hoveredItem][3]; + + if (itemIndex != -1) + { + string itemName = Game1.objectInformation[itemIndex].Split('/')[0]; + + if (itemAmount != -1) + requirements = $"Required: {itemAmount} {itemName}"; + else + requirements = $"Required: {itemName}"; + } + #endregion + + string toSpeak = $"{name}, {requirements}, {price}, \n\t{description}"; + if (shopMenuQueryKey != toSpeak) + { + shopMenuQueryKey = toSpeak; + hoveredItemQueryKey = ""; + MainClass.ScreenReader.Say(toSpeak, true); + } + } + #endregion + } + catch (Exception e) + { + MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}"); + } + } + + internal static void Cleanup() + { + shopMenuQueryKey = ""; + hoveredItemQueryKey = ""; + } + } +}