+ Narrate Hovered Texts

master
shoaib11120 2021-12-07 20:02:15 +05:30
parent 218e418945
commit 09fdbd59aa
1 changed files with 55 additions and 8 deletions

View File

@ -4,6 +4,9 @@ using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using HarmonyLib;
using StardewValley.Menus;
using Microsoft.Xna.Framework.Graphics;
using System.Text;
namespace stardew_access
{
@ -13,6 +16,7 @@ namespace stardew_access
public static IAccessibleOutput screenReader;
Harmony harmony;
public static IMonitor monitor;
private static string prevText = "";
/*********
** Public methods
*********/
@ -31,20 +35,64 @@ namespace stardew_access
// Add patches
harmony.Patch(
original: AccessTools.Constructor(typeof(StardewValley.Menus.DialogueBox), new Type[] { typeof(Dialogue) }),
postfix: new HarmonyMethod(typeof(MainClass), nameof(MainClass.Dialog_post))
original: AccessTools.Constructor(typeof(DialogueBox), new Type[] { typeof(Dialogue) }),
postfix: new HarmonyMethod(typeof(MainClass), nameof(MainClass.dialogBoxPostfix))
);
/*harmony.Patch(
original: AccessTools.Method(typeof(DialogueBox), nameof(DialogueBox.receiveLeftClick)),
postfix: new HarmonyMethod(typeof(MainClass), nameof(MainClass.dialogBoxRecieveLeftClickPostfix))
);*/
harmony.Patch(
original:,
postfix:
original: AccessTools.Method(typeof(IClickableMenu), nameof(IClickableMenu.drawHoverText), new Type[] { typeof(SpriteBatch), typeof(string), typeof(SpriteFont), typeof(int), typeof(int), typeof(int) , typeof(string) , typeof(int) , typeof(string[]) , typeof(Item) , typeof(int) , typeof(int), typeof(int), typeof(int), typeof(int), typeof(float), typeof(CraftingRecipe) , typeof(IList < Item >) }),
postfix: new HarmonyMethod(typeof(MainClass), nameof(MainClass.iClickableMenuDrawHoverTextPostFix))
);
helper.Events.Input.ButtonPressed += this.OnButtonPressed;
}
private static void Dialog_post(Dialogue dialogue)
private static void iClickableMenuDrawHoverTextPostFix(SpriteBatch b, string text, SpriteFont font, int xOffset = 0, int yOffset = 0, int moneyAmountToDisplayAtBottom = -1, string boldTitleText = null, int healAmountToDisplay = -1, string[] buffIconsToDisplay = null, Item hoveredItem = null, int currencySymbol = 0, int extraItemToShowIndex = -1, int extraItemToShowAmount = -1, int overrideX = -1, int overrideY = -1, float alpha = 1f, CraftingRecipe craftingIngredients = null, IList<Item> additional_craft_materials = null)
{
try
{
StringBuilder toSpeak = new StringBuilder();
if (hoveredItem != null)
toSpeak.AppendLine(hoveredItem.DisplayName);
toSpeak.AppendLine(text);
if (prevText != toSpeak.ToString())
{
prevText = toSpeak.ToString();
screenReader.Speak(toSpeak.ToString(), true);
}
}
catch (Exception e)
{
monitor.Log($"Unable to narrate dialog:\n{e.StackTrace}", LogLevel.Error);
}
}
private static void dialogBoxRecieveLeftClickPostfix(DialogueBox dialogueBox)
{
try
{
if(!dialogueBox.transitioning && dialogueBox.characterDialogue!=null)
{
string speakerName = dialogueBox.characterDialogue.speaker.Name;
List<string> dialogues = dialogueBox.characterDialogue.dialogues;
int dialogueIndex = dialogueBox.characterDialogue.currentDialogueIndex;
screenReader.Speak($"{speakerName} said, {dialogues[dialogueIndex]}", false);
}
} catch (Exception e)
{
monitor.Log($"Unable to narrate dialog:\n{e.StackTrace}", LogLevel.Error);
}
}
private static void dialogBoxPostfix(Dialogue dialogue)
{
try
{
@ -53,7 +101,6 @@ namespace stardew_access
int dialogueIndex = dialogue.currentDialogueIndex;
screenReader.Speak($"{speakerName} said, {dialogues[dialogueIndex]}", false);
monitor.Log($"Dialogue", LogLevel.Info);
} catch (Exception e)
{
monitor.Log($"Unable to narrate dialog:\n{e.StackTrace}", LogLevel.Error);