From eabb8ae49951405176ae573e64049edd3bfbca64 Mon Sep 17 00:00:00 2001 From: shoaib11120 Date: Thu, 6 Jan 2022 19:41:28 +0530 Subject: [PATCH] Adding custom hovered item narration instead of hovered item --- stardew-access/ModEntry.cs | 4 +- stardew-access/Patches/DialoguePatches.cs | 12 +- stardew-access/Patches/GameMenuPatches.cs | 142 +++++++++++++++------- 3 files changed, 112 insertions(+), 46 deletions(-) diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index de19899..2f3f438 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -114,8 +114,8 @@ namespace stardew_access ); harmony.Patch( - original: AccessTools.Method(typeof(InventoryMenu), nameof(InventoryMenu.draw), new Type[] { typeof(SpriteBatch) }), - postfix: new HarmonyMethod(typeof(GameMenuPatches), nameof(GameMenuPatches.InventoryMenuPatch)) + original: AccessTools.Method(typeof(InventoryPage), nameof(InventoryPage.draw), new Type[] { typeof(SpriteBatch) }), + postfix: new HarmonyMethod(typeof(GameMenuPatches), nameof(GameMenuPatches.InventoryPagePatch)) ); harmony.Patch( diff --git a/stardew-access/Patches/DialoguePatches.cs b/stardew-access/Patches/DialoguePatches.cs index 15caf98..3e9cc6e 100644 --- a/stardew-access/Patches/DialoguePatches.cs +++ b/stardew-access/Patches/DialoguePatches.cs @@ -112,7 +112,7 @@ namespace stardew_access.Patches { try { - // Fix for delete button hover text not narrating + #region Skip narrating hover text for certain menus if (Game1.activeClickableMenu is TitleMenuPatches && !((Game1.activeClickableMenu as TitleMenu).GetChildMenu() is CharacterCustomization)) return; @@ -125,6 +125,16 @@ namespace stardew_access.Patches if (Game1.activeClickableMenu is GeodeMenu) return; + if (Game1.activeClickableMenu is GameMenu && (Game1.activeClickableMenu as GameMenu).GetCurrentPage() is InventoryPage) + return; + + if (Game1.activeClickableMenu is GameMenu && (Game1.activeClickableMenu as GameMenu).GetCurrentPage() is CraftingPage) + return; + + if (Game1.activeClickableMenu is ItemGrabMenu) + return; + #endregion + StringBuilder toSpeak = new StringBuilder(" "); #region Add item count before title diff --git a/stardew-access/Patches/GameMenuPatches.cs b/stardew-access/Patches/GameMenuPatches.cs index bf55959..5c699d3 100644 --- a/stardew-access/Patches/GameMenuPatches.cs +++ b/stardew-access/Patches/GameMenuPatches.cs @@ -78,48 +78,11 @@ namespace stardew_access.Patches ScreenReader.say(toSpeak, true); } return; - } + } #endregion #region Narrate hovered item - for (int i = 0; i < __instance.inventory.inventory.Count; i++) - { - if (__instance.inventory.inventory[i].containsPoint(x, y)) - { - string toSpeak = ""; - if ((i + 1) <= __instance.inventory.actualInventory.Count) - { - if (__instance.inventory.actualInventory[i] != null) - { - string name = __instance.inventory.actualInventory[i].DisplayName; - int stack = __instance.inventory.actualInventory[i].Stack; - - if (stack > 1) - toSpeak = $"{stack} {name}"; - else - toSpeak = $"{name}"; - - } - else - { - // For empty slot - toSpeak = "Empty Slot"; - } - } - else - { - // For empty slot - toSpeak = "Empty Slot"; - } - - if (geodeMenuQueryKey != $"{toSpeak}:{i}") - { - geodeMenuQueryKey = $"{toSpeak}:{i}"; - ScreenReader.say(toSpeak, true); - } - return; - } - } + narrateHoveredItemInInventory(__instance.inventory.inventory, __instance.inventory.actualInventory, x, y); #endregion } catch (Exception e) @@ -133,7 +96,18 @@ namespace stardew_access.Patches try { int x = Game1.getMousePosition(true).X, y = Game1.getMousePosition(true).Y; // Mouse x and y position + bool isIPressed = Game1.input.GetKeyboardState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.I); + bool isLeftShiftPressed = Game1.input.GetKeyboardState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftShift); + if(isLeftShiftPressed && isIPressed && __instance.inventory != null) + { + __instance.inventory.inventory[0].snapMouseCursorToCenter(); + }else if(!isLeftShiftPressed && isIPressed && __instance.ItemsToGrabMenu != null) + { + __instance.ItemsToGrabMenu.inventory[0].snapMouseCursorToCenter(); + } + + #region Narrate buttons in the menu if (__instance.okButton != null && __instance.okButton.containsPoint(x, y)) { ScreenReader.sayWithMenuChecker("Ok Button", true); @@ -150,7 +124,9 @@ namespace stardew_access.Patches ScreenReader.sayWithMenuChecker("Drop Item", true); return; } - + #endregion + + #region Narrate the last shipped item if in the shipping bin if (__instance.shippingBin && Game1.getFarm().lastItemShipped != null && __instance.lastShippedHolder.containsPoint(x, y)) { Item lastShippedItem = Game1.getFarm().lastItemShipped; @@ -161,7 +137,17 @@ namespace stardew_access.Patches ScreenReader.sayWithMenuChecker(toSpeak, true); return; - } + } + #endregion + + #region Narrate hovered item + if(narrateHoveredItemInInventory(__instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) + return; + + if(narrateHoveredItemInInventory(__instance.ItemsToGrabMenu.inventory, __instance.ItemsToGrabMenu.actualInventory, x, y, true)) + return; + + #endregion } catch (Exception e) { @@ -205,17 +191,21 @@ namespace stardew_access.Patches } } - internal static void InventoryMenuPatch(InventoryMenu __instance) + internal static void InventoryPagePatch(InventoryPage __instance) { try { int x = Game1.getMousePosition(true).X, y = Game1.getMousePosition(true).Y; // Mouse x and y position - if (__instance.dropItemInvisibleButton != null && __instance.dropItemInvisibleButton.containsPoint(x, y)) + if (__instance.inventory.dropItemInvisibleButton != null && __instance.inventory.dropItemInvisibleButton.containsPoint(x, y)) { ScreenReader.sayWithMenuChecker("Drop Item", true); return; } + + #region Narrate hovered item + narrateHoveredItemInInventory(__instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true); + #endregion } catch (Exception e) { @@ -292,5 +282,71 @@ namespace stardew_access.Patches MainClass.monitor.Log($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}", LogLevel.Error); } } + + internal static bool narrateHoveredItemInInventory(List inventory, IList actualInventory, int x, int y, bool isInInventoryPage = false) + { + #region Narrate hovered item + for (int i = 0; i < inventory.Count; i++) + { + if (inventory[i].containsPoint(x, y)) + { + string toSpeak = ""; + if ((i + 1) <= actualInventory.Count) + { + if (actualInventory[i] != null) + { + string name = actualInventory[i].DisplayName; + int stack = actualInventory[i].Stack; + string quality = ""; + + #region Add quality of item + if (actualInventory[i] is StardewValley.Object && (actualInventory[i] as StardewValley.Object).quality > 0) + { + int qualityIndex = (actualInventory[i] as StardewValley.Object).quality; + if (qualityIndex == 1) + { + quality = "Silver quality"; + } + else if (qualityIndex == 2 || qualityIndex == 3) + { + quality = "Gold quality"; + } + else if (qualityIndex >= 4) + { + quality = "Iridium quality"; + } + } + #endregion + + if (stack > 1) + toSpeak = $"{stack} {name} {quality}"; + else + toSpeak = $"{name} {quality}"; + + MainClass.monitor.Log(actualInventory[i].getHoverBoxText(actualInventory[i]), LogLevel.Debug); + } + else + { + // For empty slot + toSpeak = "Empty Slot"; + } + } + else + { + // For empty slot + toSpeak = "Empty Slot"; + } + + if (geodeMenuQueryKey != $"{toSpeak}:{i}") + { + geodeMenuQueryKey = $"{toSpeak}:{i}"; + ScreenReader.say(toSpeak, true); + } + return true; + } + } + #endregion + return false; + } } }