From 0fa90e4c74f3d63fe37b32f84eb588295031d885 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Fri, 24 Feb 2023 20:45:35 +0530 Subject: [PATCH] Improved and organised code in InventoryPagePatch.cs --- .../GameMenuPatches/InventoryPagePatch.cs | 291 +++++++----------- stardew-access/Patches/IClickableMenuPatch.cs | 3 +- stardew-access/Patches/InventoryUtils.cs | 6 + 3 files changed, 122 insertions(+), 178 deletions(-) diff --git a/stardew-access/Patches/GameMenuPatches/InventoryPagePatch.cs b/stardew-access/Patches/GameMenuPatches/InventoryPagePatch.cs index bfc367b..8dc26b3 100644 --- a/stardew-access/Patches/GameMenuPatches/InventoryPagePatch.cs +++ b/stardew-access/Patches/GameMenuPatches/InventoryPagePatch.cs @@ -14,202 +14,141 @@ namespace stardew_access.Patches { int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position - #region Narrate buttons in the menu - if (__instance.inventory.dropItemInvisibleButton != null && __instance.inventory.dropItemInvisibleButton.containsPoint(x, y)) + if (narrateHoveredButton(__instance, x, y)) { - string toSpeak = "Drop Item"; - if (inventoryPageQueryKey != toSpeak) - { - inventoryPageQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - Game1.playSound("drop_item"); - } + return; } - if (__instance.organizeButton != null && __instance.organizeButton.containsPoint(x, y)) + if (narrateHoveredEquipmentSlot(__instance, x, y)) { - string toSpeak = "Organize Inventory Button"; - if (inventoryPageQueryKey != toSpeak) - { - inventoryPageQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } + return; } - if (__instance.trashCan != null && __instance.trashCan.containsPoint(x, y)) - { - string toSpeak = "Trash Can"; - if (inventoryPageQueryKey != toSpeak) - { - inventoryPageQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } - } - - if (__instance.organizeButton != null && __instance.organizeButton.containsPoint(x, y)) - { - string toSpeak = "Organize Button"; - if (inventoryPageQueryKey != toSpeak) - { - inventoryPageQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } - } - - if (__instance.junimoNoteIcon != null && __instance.junimoNoteIcon.containsPoint(x, y)) - { - - string toSpeak = "Community Center Button"; - if (inventoryPageQueryKey != toSpeak) - { - inventoryPageQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } - } - #endregion - - #region Narrate equipment slots - for (int i = 0; i < __instance.equipmentIcons.Count; i++) - { - if (__instance.equipmentIcons[i].containsPoint(x, y)) - { - string toSpeak = ""; - - #region Get name and description of the item - switch (__instance.equipmentIcons[i].name) - { - case "Hat": - { - if (Game1.player.hat.Value != null) - { - toSpeak = $"{Game1.player.hat.Value.DisplayName}, {Game1.player.hat.Value.getDescription()}"; - } - else - { - toSpeak = "Hat slot"; - } - } - break; - case "Left Ring": - { - if (Game1.player.leftRing.Value != null) - { - toSpeak = $"{Game1.player.leftRing.Value.DisplayName}, {Game1.player.leftRing.Value.getDescription()}"; - } - else - { - toSpeak = "Left Ring slot"; - } - } - break; - case "Right Ring": - { - if (Game1.player.rightRing.Value != null) - { - toSpeak = $"{Game1.player.rightRing.Value.DisplayName}, {Game1.player.rightRing.Value.getDescription()}"; - } - else - { - toSpeak = "Right ring slot"; - } - } - break; - case "Boots": - { - if (Game1.player.boots.Value != null) - { - toSpeak = $"{Game1.player.boots.Value.DisplayName}, {Game1.player.boots.Value.getDescription()}"; - } - else - { - toSpeak = "Boots slot"; - } - } - break; - case "Shirt": - { - if (Game1.player.shirtItem.Value != null) - { - toSpeak = $"{Game1.player.shirtItem.Value.DisplayName}, {Game1.player.shirtItem.Value.getDescription()}"; - } - else - { - toSpeak = "Shirt slot"; - } - } - break; - case "Pants": - { - if (Game1.player.pantsItem.Value != null) - { - toSpeak = $"{Game1.player.pantsItem.Value.DisplayName}, {Game1.player.pantsItem.Value.getDescription()}"; - } - else - { - toSpeak = "Pants slot"; - } - } - break; - } - #endregion - - if (inventoryPageQueryKey != toSpeak) - { - inventoryPageQueryKey = toSpeak; - hoveredItemQueryKey = ""; - MainClass.ScreenReader.Say(toSpeak, true); - } - } - } - #endregion - - #region Narrate hovered item if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) { inventoryPageQueryKey = ""; + return; } - #endregion - if (MainClass.Config.MoneyKey.JustPressed()) - { - string farmName = Game1.content.LoadString("Strings\\UI:Inventory_FarmName", Game1.player.farmName.Value); - string currentFunds = Game1.content.LoadString("Strings\\UI:Inventory_CurrentFunds" + (Game1.player.useSeparateWallets ? "_Separate" : ""), Utility.getNumberWithCommas(Game1.player.Money)); - string totalEarnings = Game1.content.LoadString("Strings\\UI:Inventory_TotalEarnings" + (Game1.player.useSeparateWallets ? "_Separate" : ""), Utility.getNumberWithCommas((int)Game1.player.totalMoneyEarned)); - int festivalScore = Game1.player.festivalScore; - int walnut = Game1.netWorldState.Value.GoldenWalnuts.Value; - int qiGems = Game1.player.QiGems; - int qiCoins = Game1.player.clubCoins; + handleKeyBinds(); - string toSpeak = $"{farmName}\n{currentFunds}\n{totalEarnings}"; - - if (festivalScore > 0) - toSpeak = $"{toSpeak}\nFestival Score: {festivalScore}"; - - if (walnut > 0) - toSpeak = $"{toSpeak}\nGolden Walnut: {walnut}"; - - if (qiGems > 0) - toSpeak = $"{toSpeak}\nQi Gems: {qiGems}"; - - if (qiCoins > 0) - toSpeak = $"{toSpeak}\nQi Club Coins: {qiCoins}"; - - MainClass.ScreenReader.Say(toSpeak, true); - } + // If no slot or button is hovered + Cleanup(); } catch (Exception e) { - MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}"); + MainClass.ErrorLog($"An error occured in InventoryPagePatch()->DrawPatch():\n{e.Message}\n{e.StackTrace}"); } } + private static void handleKeyBinds() + { + if (!MainClass.Config.MoneyKey.JustPressed()) + return; + + string farmName = Game1.content.LoadString("Strings\\UI:Inventory_FarmName", Game1.player.farmName.Value); + string currentFunds = Game1.content.LoadString("Strings\\UI:Inventory_CurrentFunds" + (Game1.player.useSeparateWallets ? "_Separate" : ""), Utility.getNumberWithCommas(Game1.player.Money)); + string totalEarnings = Game1.content.LoadString("Strings\\UI:Inventory_TotalEarnings" + (Game1.player.useSeparateWallets ? "_Separate" : ""), Utility.getNumberWithCommas((int)Game1.player.totalMoneyEarned)); + int festivalScore = Game1.player.festivalScore; + int walnut = Game1.netWorldState.Value.GoldenWalnuts.Value; + int qiGems = Game1.player.QiGems; + int qiCoins = Game1.player.clubCoins; + + string toSpeak = $"{farmName}\n{currentFunds}\n{totalEarnings}"; + + if (festivalScore > 0) + toSpeak = $"{toSpeak}\nFestival Score: {festivalScore}"; + + if (walnut > 0) + toSpeak = $"{toSpeak}\nGolden Walnut: {walnut}"; + + if (qiGems > 0) + toSpeak = $"{toSpeak}\nQi Gems: {qiGems}"; + + if (qiCoins > 0) + toSpeak = $"{toSpeak}\nQi Club Coins: {qiCoins}"; + + MainClass.ScreenReader.Say(toSpeak, true); + } + + private static bool narrateHoveredButton(InventoryPage __instance, int x, int y) + { + string? toSpeak = null; + bool isDropItemButton = false; + + if (__instance.inventory.dropItemInvisibleButton != null && __instance.inventory.dropItemInvisibleButton.containsPoint(x, y)) + { + toSpeak = "Drop Item"; + isDropItemButton = true; + } + else if (__instance.organizeButton != null && __instance.organizeButton.containsPoint(x, y)) + { + toSpeak = "Organize Inventory Button"; + } + else if (__instance.trashCan != null && __instance.trashCan.containsPoint(x, y)) + { + toSpeak = "Trash Can"; + } + else if (__instance.organizeButton != null && __instance.organizeButton.containsPoint(x, y)) + { + toSpeak = "Organize Button"; + } + else if (__instance.junimoNoteIcon != null && __instance.junimoNoteIcon.containsPoint(x, y)) + { + toSpeak = "Community Center Button"; + } + else + { + return false; + } + + if (toSpeak != null && inventoryPageQueryKey != toSpeak) + { + inventoryPageQueryKey = toSpeak; + hoveredItemQueryKey = ""; + MainClass.ScreenReader.Say(toSpeak, true); + if (isDropItemButton) Game1.playSound("drop_item"); + } + + return true; + } + + private static bool narrateHoveredEquipmentSlot(InventoryPage __instance, int mouseX, int mouseY) + { + for (int i = 0; i < __instance.equipmentIcons.Count; i++) + { + if (!__instance.equipmentIcons[i].containsPoint(mouseX, mouseY)) + continue; + + string toSpeak = getNameAndDescriptionOfItem(__instance.equipmentIcons[i].name); + + if (inventoryPageQueryKey != toSpeak) + { + inventoryPageQueryKey = toSpeak; + hoveredItemQueryKey = ""; + MainClass.ScreenReader.Say(toSpeak, true); + } + + return true; + } + + return false; + } + + private static string getNameAndDescriptionOfItem(string slotName) => slotName switch + { + "Hat" => (Game1.player.hat.Value != null) ? $"{Game1.player.hat.Value.DisplayName}, {Game1.player.hat.Value.getDescription()}" : "Hat slot", + "Left Ring" => (Game1.player.leftRing.Value != null) ? $"{Game1.player.leftRing.Value.DisplayName}, {Game1.player.leftRing.Value.getDescription()}" : "Left Ring slot", + "Right Ring" => (Game1.player.rightRing.Value != null) ? $"{Game1.player.rightRing.Value.DisplayName}, {Game1.player.rightRing.Value.getDescription()}" : "Right ring slot", + "Boots" => (Game1.player.boots.Value != null) ? $"{Game1.player.boots.Value.DisplayName}, {Game1.player.boots.Value.getDescription()}" : "Boots slot", + "Shirt" => (Game1.player.shirtItem.Value != null) ? $"{Game1.player.shirtItem.Value.DisplayName}, {Game1.player.shirtItem.Value.getDescription()}" : "Shirt slot", + "Pants" => (Game1.player.pantsItem.Value != null) ? $"{Game1.player.pantsItem.Value.DisplayName}, {Game1.player.pantsItem.Value.getDescription()}" : "Pants slot", + _ => "unkown slot" + }; + internal static void Cleanup() { + InventoryUtils.Cleanup(); inventoryPageQueryKey = ""; hoveredItemQueryKey = ""; } diff --git a/stardew-access/Patches/IClickableMenuPatch.cs b/stardew-access/Patches/IClickableMenuPatch.cs index 3260a80..24a6184 100644 --- a/stardew-access/Patches/IClickableMenuPatch.cs +++ b/stardew-access/Patches/IClickableMenuPatch.cs @@ -118,8 +118,7 @@ namespace stardew_access.Patches MenuPatches.pondQueryMenuQuery = " "; } - InventoryUtils.hoveredItemQueryKey = ""; - InventoryUtils.prevSlotIndex = -999; + InventoryUtils.Cleanup(); TextBoxPatch.activeTextBoxes = ""; } } diff --git a/stardew-access/Patches/InventoryUtils.cs b/stardew-access/Patches/InventoryUtils.cs index 5903f92..f4a7602 100644 --- a/stardew-access/Patches/InventoryUtils.cs +++ b/stardew-access/Patches/InventoryUtils.cs @@ -202,5 +202,11 @@ namespace stardew_access.Patches if (MainClass.Config.DisableInventoryVerbosity) return ""; return " not usable here"; } + + internal static void Cleanup() + { + hoveredItemQueryKey = ""; + prevSlotIndex = -999; + } } }