Refactored code CraftingPagePatch.cs

master
Mohammad Shoaib Khan 2023-03-03 21:03:37 +05:30
parent 0fa90e4c74
commit 774454ce0e
No known key found for this signature in database
GPG Key ID: D8040D966320B620
1 changed files with 172 additions and 172 deletions

View File

@ -17,6 +17,32 @@ namespace stardew_access.Patches
{ {
int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position
if (narrateMenuButtons(__instance, x, y))
{
return;
}
if (narrateHoveredRecipe(__instance, ___currentCraftingPage, ___hoverRecipe, x, y))
{
return;
}
if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y))
{
craftingPageQueryKey = "";
return;
}
handleKeyBinds(__instance, ___currentCraftingPage);
}
catch (Exception e)
{
MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}");
}
}
private static void handleKeyBinds(CraftingPage __instance, int ___currentCraftingPage)
{
if (MainClass.Config.SnapToFirstInventorySlotKey.JustPressed() && __instance.inventory.inventory.Count > 0) if (MainClass.Config.SnapToFirstInventorySlotKey.JustPressed() && __instance.inventory.inventory.Count > 0)
{ {
// snap to first inventory slot // snap to first inventory slot
@ -37,61 +63,74 @@ namespace stardew_access.Patches
CycleThroughRecipies(__instance.pagesOfCraftingRecipes, ___currentCraftingPage, __instance); CycleThroughRecipies(__instance.pagesOfCraftingRecipes, ___currentCraftingPage, __instance);
Task.Delay(200).ContinueWith(_ => { isSelectingRecipe = false; }); Task.Delay(200).ContinueWith(_ => { isSelectingRecipe = false; });
} }
}
private static bool narrateMenuButtons(CraftingPage __instance, int x, int y)
{
string? toSpeak = null;
bool isDropItemButton = false;
#region Narrate buttons in the menu
if (__instance.upButton != null && __instance.upButton.containsPoint(x, y)) if (__instance.upButton != null && __instance.upButton.containsPoint(x, y))
{ {
string toSpeak = "Previous Recipe List"; toSpeak = "Previous Recipe List";
if (craftingPageQueryKey != toSpeak) }
else if (__instance.downButton != null && __instance.downButton.containsPoint(x, y))
{
toSpeak = "Next Recipe List";
}
else if (__instance.trashCan != null && __instance.trashCan.containsPoint(x, y))
{
toSpeak = "Trash Can";
}
else if (__instance.dropItemInvisibleButton != null && __instance.dropItemInvisibleButton.containsPoint(x, y))
{
toSpeak = "Drop Item";
isDropItemButton = true;
}
else
{
return false;
}
if (toSpeak != null && craftingPageQueryKey != toSpeak)
{ {
craftingPageQueryKey = toSpeak; craftingPageQueryKey = toSpeak;
hoveredItemQueryKey = ""; hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true); MainClass.ScreenReader.Say(toSpeak, true);
} if (isDropItemButton) Game1.playSound("drop_item");
return;
} }
if (__instance.downButton != null && __instance.downButton.containsPoint(x, y)) return true;
}
private static bool narrateHoveredRecipe(CraftingPage __instance, int ___currentCraftingPage, CraftingRecipe ___hoverRecipe, int x, int y)
{ {
string toSpeak = "Next Recipe List"; if (___hoverRecipe == null)
if (craftingPageQueryKey != toSpeak)
{ {
craftingPageQueryKey = toSpeak; var isRecipeInFocus = false;
foreach (var item in __instance.pagesOfCraftingRecipes[___currentCraftingPage])
{
if (!item.Key.containsPoint(x, y))
continue;
isRecipeInFocus = true;
break;
}
if (!isRecipeInFocus)
return false;
string query = $"unknown recipe:{__instance.getCurrentlySnappedComponent().myID}";
if (craftingPageQueryKey != query)
{
craftingPageQueryKey = query;
hoveredItemQueryKey = ""; hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true); MainClass.ScreenReader.Say("unknown recipe", true);
} }
return; return true;
} }
if (__instance.trashCan.containsPoint(x, y))
{
string toSpeak = "Trash Can";
if (craftingPageQueryKey != toSpeak)
{
craftingPageQueryKey = toSpeak;
hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true);
}
return;
}
if (__instance.dropItemInvisibleButton.containsPoint(x, y))
{
string toSpeak = "Drop Item";
if (craftingPageQueryKey != toSpeak)
{
craftingPageQueryKey = toSpeak;
hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true);
Game1.playSound("drop_item");
}
return;
}
#endregion
#region Narrate hovered recipe
if (___hoverRecipe != null)
{
string name = ___hoverRecipe.DisplayName; string name = ___hoverRecipe.DisplayName;
int numberOfProduce = ___hoverRecipe.numberProducedPerCraft; int numberOfProduce = ___hoverRecipe.numberProducedPerCraft;
string description = ""; string description = "";
@ -166,47 +205,8 @@ namespace stardew_access.Patches
hoveredItemQueryKey = ""; hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true); MainClass.ScreenReader.Say(toSpeak, true);
} }
return;
}
else
{
var isRecipeInFocus = false;
foreach (var item in __instance.pagesOfCraftingRecipes[___currentCraftingPage])
{
if (item.Key.containsPoint(x, y))
{
isRecipeInFocus = true;
break;
}
}
if (isRecipeInFocus) return true;
{
string query = $"unknown recipe:{__instance.getCurrentlySnappedComponent().myID}";
if (craftingPageQueryKey != query)
{
craftingPageQueryKey = query;
hoveredItemQueryKey = "";
MainClass.ScreenReader.Say("unknown recipe", true);
}
return;
}
}
#endregion
#region Narrate hovered item
if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y))
{
craftingPageQueryKey = "";
return;
}
#endregion
}
catch (Exception e)
{
MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}");
}
} }
private static void CycleThroughRecipies(List<Dictionary<ClickableTextureComponent, CraftingRecipe>> pagesOfCraftingRecipes, int ___currentCraftingPage, CraftingPage __instance) private static void CycleThroughRecipies(List<Dictionary<ClickableTextureComponent, CraftingRecipe>> pagesOfCraftingRecipes, int ___currentCraftingPage, CraftingPage __instance)