Improved and organised code in shop menu patch

master
Mohammad Shoaib Khan 2023-03-08 12:21:50 +05:30
parent 8523026ed0
commit 80963c4dc6
No known key found for this signature in database
GPG Key ID: D8040D966320B620
4 changed files with 77 additions and 84 deletions

View File

@ -16,12 +16,12 @@ namespace stardew_access
#region Dialogue Patches #region Dialogue Patches
harmony.Patch( harmony.Patch(
original: AccessTools.Method(typeof(DialogueBox), nameof(DialogueBox.draw), new Type[] { typeof(SpriteBatch) }), original: AccessTools.Method(typeof(DialogueBox), nameof(DialogueBox.draw), new Type[] { typeof(SpriteBatch) }),
postfix: new HarmonyMethod(typeof(DialoguePatches), nameof(DialoguePatches.DialoguePatch)) postfix: new HarmonyMethod(typeof(DialogueBoxPatch), nameof(DialogueBoxPatch.DrawPatch))
); );
harmony.Patch( harmony.Patch(
original: AccessTools.Method(typeof(DialogueBox), nameof(DialogueBox.receiveLeftClick)), original: AccessTools.Method(typeof(DialogueBox), nameof(DialogueBox.receiveLeftClick)),
postfix: new HarmonyMethod(typeof(DialoguePatches), nameof(DialoguePatches.RecieveLeftClickPatch)) postfix: new HarmonyMethod(typeof(DialogueBoxPatch), nameof(DialogueBoxPatch.RecieveLeftClickPatch))
); );
harmony.Patch( harmony.Patch(

View File

@ -1,17 +1,14 @@
using Microsoft.Xna.Framework.Graphics; using StardewValley;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Menus; using StardewValley.Menus;
namespace stardew_access.Patches namespace stardew_access.Patches
{ {
internal class DialoguePatches internal class DialogueBoxPatch
{ {
private static string currentDialogue = ""; private static string currentDialogue = "";
private static string previousSpeakerName = "";
private static bool isDialogueAppearingFirstTime = true; private static bool isDialogueAppearingFirstTime = true;
internal static void DialoguePatch(DialogueBox __instance, SpriteBatch b) internal static void DrawPatch(DialogueBox __instance)
{ {
try try
{ {

View File

@ -283,7 +283,7 @@ namespace stardew_access.Patches
} }
else if (menu is DialogueBox) else if (menu is DialogueBox)
{ {
DialoguePatches.Cleanup(); DialogueBoxPatch.Cleanup();
} }
else if (menu is JojaCDMenu) else if (menu is JojaCDMenu)
{ {

View File

@ -25,60 +25,64 @@ namespace stardew_access.Patches
__instance.setCurrentlySnappedComponentTo(__instance.inventory.inventory[0].myID); __instance.setCurrentlySnappedComponentTo(__instance.inventory.inventory[0].myID);
} }
#region Narrate buttons in the menu if (narrateHoveredButton(__instance, x, y)) return;
if (__instance.inventory.dropItemInvisibleButton != null && __instance.inventory.dropItemInvisibleButton.containsPoint(x, y))
{
string toSpeak = "Drop Item";
if (shopMenuQueryKey != toSpeak)
{
shopMenuQueryKey = toSpeak;
hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true);
Game1.playSound("drop_item");
}
return;
}
if (__instance.upArrow != null && __instance.upArrow.containsPoint(x, y))
{
string toSpeak = "Up Arrow Button";
if (shopMenuQueryKey != toSpeak)
{
shopMenuQueryKey = toSpeak;
hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true);
}
return;
}
if (__instance.downArrow != null && __instance.downArrow.containsPoint(x, y))
{
string toSpeak = "Down Arrow Button";
if (shopMenuQueryKey != toSpeak)
{
shopMenuQueryKey = toSpeak;
hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true);
}
return;
}
#endregion
#region Narrate hovered item
if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice)) if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice))
{ {
shopMenuQueryKey = ""; shopMenuQueryKey = "";
return; return;
} }
#endregion
#region Narrate hovered selling item narrateHoveredSellingItem(__instance);
if (__instance.hoveredItem != null) }
catch (Exception e)
{ {
MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}");
}
}
private static bool narrateHoveredButton(ShopMenu __instance, int x, int y)
{
string toSpeak = "";
bool isDropItemButton = false;
if (__instance.inventory.dropItemInvisibleButton != null && __instance.inventory.dropItemInvisibleButton.containsPoint(x, y))
{
toSpeak = "Drop Item";
isDropItemButton = true;
}
else if (__instance.upArrow != null && __instance.upArrow.containsPoint(x, y))
{
toSpeak = "Up Arrow Button";
}
else if (__instance.downArrow != null && __instance.downArrow.containsPoint(x, y))
{
toSpeak = "Down Arrow Button";
}
else
{
return false;
}
if (shopMenuQueryKey == toSpeak) return true;
shopMenuQueryKey = toSpeak;
hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true);
if (isDropItemButton) Game1.playSound("drop_item");
return true;
}
private static void narrateHoveredSellingItem(ShopMenu __instance)
{
if (__instance.hoveredItem == null) return;
string name = __instance.hoveredItem.DisplayName; string name = __instance.hoveredItem.DisplayName;
string price = $"Buy Price: {__instance.hoverPrice} g"; string price = $"Buy Price: {__instance.hoverPrice} g";
string description = __instance.hoveredItem.getDescription(); string description = __instance.hoveredItem.getDescription();
string requirements = ""; string requirements = "";
#region Narrate required items for item #region get required items for item
int itemIndex = -1, itemAmount = 5; int itemIndex = -1, itemAmount = 5;
if (__instance.itemPriceAndStock[__instance.hoveredItem].Length > 2) if (__instance.itemPriceAndStock[__instance.hoveredItem].Length > 2)
@ -99,20 +103,12 @@ namespace stardew_access.Patches
#endregion #endregion
string toSpeak = $"{name}, {requirements}, {price}, \n\t{description}"; string toSpeak = $"{name}, {requirements}, {price}, \n\t{description}";
if (shopMenuQueryKey != toSpeak) if (shopMenuQueryKey == toSpeak) return;
{
shopMenuQueryKey = toSpeak; shopMenuQueryKey = toSpeak;
hoveredItemQueryKey = ""; hoveredItemQueryKey = "";
MainClass.ScreenReader.Say(toSpeak, true); MainClass.ScreenReader.Say(toSpeak, true);
} }
}
#endregion
}
catch (Exception e)
{
MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}");
}
}
internal static void Cleanup() internal static void Cleanup()
{ {