From 09fdbd59aa2e963d0dd0a6bfb7c926edd1ca6c86 Mon Sep 17 00:00:00 2001 From: shoaib11120 Date: Tue, 7 Dec 2021 20:02:15 +0530 Subject: [PATCH] + Narrate Hovered Texts --- stardew-access/ModEntry.cs | 63 +++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index ef61583..deb3b9d 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -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 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 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,8 +101,7 @@ namespace stardew_access int dialogueIndex = dialogue.currentDialogueIndex; screenReader.Speak($"{speakerName} said, {dialogues[dialogueIndex]}", false); - monitor.Log($"Dialogue", LogLevel.Info); - }catch (Exception e) + } catch (Exception e) { monitor.Log($"Unable to narrate dialog:\n{e.StackTrace}", LogLevel.Error); }