Improved and organised code in InventoryPagePatch.cs

master
Mohammad Shoaib Khan 2023-02-24 20:45:35 +05:30
parent 15380e5b76
commit 0fa90e4c74
No known key found for this signature in database
GPG Key ID: D8040D966320B620
3 changed files with 122 additions and 178 deletions

View File

@ -14,169 +14,38 @@ 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())
handleKeyBinds();
// If no slot or button is hovered
Cleanup();
}
catch (Exception e)
{
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));
@ -201,15 +70,85 @@ namespace stardew_access.Patches
MainClass.ScreenReader.Say(toSpeak, true);
}
}
catch (Exception e)
private static bool narrateHoveredButton(InventoryPage __instance, int x, int y)
{
MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}");
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 = "";
}

View File

@ -118,8 +118,7 @@ namespace stardew_access.Patches
MenuPatches.pondQueryMenuQuery = " ";
}
InventoryUtils.hoveredItemQueryKey = "";
InventoryUtils.prevSlotIndex = -999;
InventoryUtils.Cleanup();
TextBoxPatch.activeTextBoxes = "";
}
}

View File

@ -202,5 +202,11 @@ namespace stardew_access.Patches
if (MainClass.Config.DisableInventoryVerbosity) return "";
return " not usable here";
}
internal static void Cleanup()
{
hoveredItemQueryKey = "";
prevSlotIndex = -999;
}
}
}