From fcadea8a8b4a78d44c6aff2f9c0d182cec7e6c1b Mon Sep 17 00:00:00 2001 From: shoaib11120 Date: Fri, 7 Jan 2022 16:04:37 +0530 Subject: [PATCH] Added keybinds to cycle through crafting recipies --- stardew-access/ModEntry.cs | 4 ++ stardew-access/Patches/GameMenuPatches.cs | 65 ++++++++++++++++++++++- stardew-access/Patches/MenuPatches.cs | 47 ++++++++-------- 3 files changed, 88 insertions(+), 28 deletions(-) diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index bc61c67..cfa32b7 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -200,6 +200,10 @@ namespace stardew_access original: AccessTools.Method(typeof(IClickableMenu), nameof(IClickableMenu.exitThisMenu)), postfix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.IClickableMenuOnExitPatch)) ); + harmony.Patch( + original: AccessTools.Method(typeof(Game1), nameof(Game1.exitActiveMenu)), + prefix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.Game1ExitActiveMenuPatch)) + ); #endregion #endregion diff --git a/stardew-access/Patches/GameMenuPatches.cs b/stardew-access/Patches/GameMenuPatches.cs index a8d03bf..11929bd 100644 --- a/stardew-access/Patches/GameMenuPatches.cs +++ b/stardew-access/Patches/GameMenuPatches.cs @@ -16,6 +16,8 @@ namespace stardew_access.Patches internal static string inventoryPageQueryKey = ""; internal static string exitPageQueryKey = ""; internal static string optionsPageQueryKey = ""; + internal static int currentSelectedCraftingRecipe = -1; + internal static bool isSelectingRecipe = false; internal static void GameMenuPatch(GameMenu __instance) { @@ -231,11 +233,30 @@ namespace stardew_access.Patches } } - internal static void CraftingPagePatch(CraftingPage __instance, CraftingRecipe ___hoverRecipe) + internal static void CraftingPagePatch(CraftingPage __instance, CraftingRecipe ___hoverRecipe, int ___currentCraftingPage) { 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 isCPressed = Game1.input.GetKeyboardState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.C); + bool isLeftShiftPressed = Game1.input.GetKeyboardState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftShift); + + if (isLeftShiftPressed && isIPressed && __instance.inventory != null) + { + // snap to first inventory slot + __instance.inventory.inventory[0].snapMouseCursorToCenter(); + currentSelectedCraftingRecipe = -1; + } + else if (!isLeftShiftPressed && isIPressed && __instance.inventory != null) + { + // snap to first crafting recipe + __instance.pagesOfCraftingRecipes[___currentCraftingPage].ElementAt(0).Key.snapMouseCursorToCenter(); + currentSelectedCraftingRecipe = 0; + } else if (isCPressed && !isSelectingRecipe) + { + _ = CycleThroughRecipies(__instance.pagesOfCraftingRecipes, ___currentCraftingPage); + } #region Narrate buttons in the menu if (__instance.upButton != null && __instance.upButton.containsPoint(x, y)) @@ -379,6 +400,20 @@ namespace stardew_access.Patches } } + private static async Task CycleThroughRecipies(List> pagesOfCraftingRecipes, int ___currentCraftingPage) + { + isSelectingRecipe = true; + + currentSelectedCraftingRecipe++; + if (currentSelectedCraftingRecipe < 0 || currentSelectedCraftingRecipe >= pagesOfCraftingRecipes[0].Count) + currentSelectedCraftingRecipe = 0; + + pagesOfCraftingRecipes[___currentCraftingPage].ElementAt(currentSelectedCraftingRecipe).Key.snapMouseCursorToCenter(); + + await Task.Delay(200); + isSelectingRecipe = false; + } + // This method is used to get the inventory items to check if the player has enough ingredients for a recipe // Taken from CraftingPage.cs -> 169 line internal static IList getContainerContents(List materialContainers) @@ -415,8 +450,34 @@ namespace stardew_access.Patches return; } + if (__instance.organizeButton != null && __instance.organizeButton.containsPoint(x, y)) + { + string toSpeak = "Organize Inventory Button"; + if (inventoryPageQueryKey != toSpeak) + { + inventoryPageQueryKey = toSpeak; + gameMenuQueryKey = ""; + hoveredItemQueryKey = ""; + ScreenReader.say(toSpeak, true); + } + return; + } + + if (__instance.trashCan != null && __instance.trashCan.containsPoint(x, y)) + { + string toSpeak = "Trash Can"; + if (inventoryPageQueryKey != toSpeak) + { + inventoryPageQueryKey = toSpeak; + gameMenuQueryKey = ""; + hoveredItemQueryKey = ""; + ScreenReader.say(toSpeak, true); + } + return; + } + #region Narrate equipment slots - for(int i=0; i<__instance.equipmentIcons.Count; i++) + for (int i=0; i<__instance.equipmentIcons.Count; i++) { if (__instance.equipmentIcons[i].containsPoint(x, y)) { diff --git a/stardew-access/Patches/MenuPatches.cs b/stardew-access/Patches/MenuPatches.cs index d31ee22..78ac66f 100644 --- a/stardew-access/Patches/MenuPatches.cs +++ b/stardew-access/Patches/MenuPatches.cs @@ -256,6 +256,27 @@ namespace stardew_access.Patches } } + internal static void Game1ExitActiveMenuPatch() + { + try + { + if (Game1.activeClickableMenu is GameMenu) + { + GameMenuPatches.gameMenuQueryKey = ""; + GameMenuPatches.craftingPageQueryKey = ""; + GameMenuPatches.inventoryPageQueryKey = ""; + GameMenuPatches.exitPageQueryKey = ""; + GameMenuPatches.optionsPageQueryKey = ""; + GameMenuPatches.currentSelectedCraftingRecipe = -1; + GameMenuPatches.isSelectingRecipe = false; + } + } + catch (Exception e) + { + MainClass.monitor.Log($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}", LogLevel.Error); + } + } + internal static void IClickableMenuOnExitPatch(IClickableMenu __instance) { try @@ -265,32 +286,6 @@ namespace stardew_access.Patches GameMenuPatches.geodeMenuQueryKey = ""; } - if (Game1.activeClickableMenu is GameMenu && (Game1.activeClickableMenu as GameMenu).GetCurrentPage() is InventoryPage) - { - GameMenuPatches.inventoryPageQueryKey = ""; - } - - if (Game1.activeClickableMenu is GameMenu && (Game1.activeClickableMenu as GameMenu).GetCurrentPage() is CraftingPage) - { - GameMenuPatches.craftingPageQueryKey = ""; - } - - if (Game1.activeClickableMenu is GameMenu && (Game1.activeClickableMenu as GameMenu).GetCurrentPage() is ExitPage) - { - GameMenuPatches.exitPageQueryKey = ""; - } - - if (Game1.activeClickableMenu is GameMenu && (Game1.activeClickableMenu as GameMenu).GetCurrentPage() is CraftingPage) - { - GameMenuPatches.craftingPageQueryKey = ""; - } - - // This should be after checking all pages/tabs in the GameMenu - if (Game1.activeClickableMenu is GameMenu) - { - GameMenuPatches.gameMenuQueryKey = ""; - } - if(__instance is ItemGrabMenu) { GameMenuPatches.itemGrabMenuQueryKey = "";