Fixed dialogue narration

master
shoaib11120 2021-12-09 15:00:40 +05:30
parent 21c2a3309e
commit fdc8af92eb
1 changed files with 57 additions and 23 deletions

View File

@ -7,6 +7,8 @@ using HarmonyLib;
using StardewValley.Menus; using StardewValley.Menus;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System.Text; using System.Text;
using StardewValley.BellsAndWhistles;
using Microsoft.Xna.Framework;
namespace stardew_access namespace stardew_access
{ {
@ -17,6 +19,8 @@ namespace stardew_access
Harmony harmony; Harmony harmony;
public static IMonitor monitor; public static IMonitor monitor;
private static string prevText = ""; private static string prevText = "";
private static DialogueBox? dialogueBox = null;
private int index = 0;
/********* /*********
** Public methods ** Public methods
*********/ *********/
@ -35,13 +39,9 @@ namespace stardew_access
// Add patches // Add patches
harmony.Patch( harmony.Patch(
original: AccessTools.Constructor(typeof(DialogueBox), new Type[] { typeof(Dialogue) }), original: AccessTools.Method(typeof(DialogueBox), nameof(DialogueBox.draw), new Type[] {typeof(SpriteBatch)}),
postfix: new HarmonyMethod(typeof(MainClass), nameof(MainClass.dialogBoxPostfix)) 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( harmony.Patch(
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 >) }), 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 >) }),
@ -49,6 +49,46 @@ namespace stardew_access
); );
helper.Events.Input.ButtonPressed += this.OnButtonPressed; helper.Events.Input.ButtonPressed += this.OnButtonPressed;
helper.Events.GameLoop.OneSecondUpdateTicked += this.OnOneSecondUpdateTicked;
}
private static void dialogBoxPostfix(DialogueBox __instance, SpriteBatch b)
{
try
{
Dialogue dialogue = __instance.characterDialogue;
string speakerName = dialogue.speaker.Name;
List<string> dialogues = dialogue.dialogues;
int dialogueIndex = dialogue.currentDialogueIndex;
monitor.Log("" + dialogue.isCurrentStringContinuedOnNextScreen, LogLevel.Debug);
if (prevText != $"{speakerName} said, {dialogues[dialogueIndex]}")
{
prevText = $"{speakerName} said, {dialogues[dialogueIndex]}";
screenReader.Speak($"{speakerName} said, {dialogues[dialogueIndex]}", false);
}
}
catch (Exception e)
{
monitor.Log($"Unable to narrate dialog:\n{e.StackTrace}", LogLevel.Error);
}
}
private void OnOneSecondUpdateTicked(object sender, OneSecondUpdateTickedEventArgs e)
{
if (Game1.activeClickableMenu != null)
{
string name = Game1.activeClickableMenu.getCurrentlySnappedComponent().name;
string label = Game1.activeClickableMenu.getCurrentlySnappedComponent().label;
string toSpeeak = $"{name} {label}";
if (prevText != toSpeeak)
{
prevText = toSpeeak;
screenReader.Speak(toSpeeak, true);
}
}
} }
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) 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)
@ -138,21 +178,6 @@ namespace stardew_access
} }
} }
private static void dialogBoxPostfix(Dialogue dialogue)
{
try
{
string speakerName = dialogue.speaker.Name;
List<string> dialogues = dialogue.dialogues;
int dialogueIndex = dialogue.currentDialogueIndex;
screenReader.Speak($"{speakerName} said, {dialogues[dialogueIndex]}", false);
} catch (Exception e)
{
monitor.Log($"Unable to narrate dialog:\n{e.StackTrace}", LogLevel.Error);
}
}
private void initializeScreenReader() private void initializeScreenReader()
{ {
NvdaOutput nvdaOutput = null; NvdaOutput nvdaOutput = null;
@ -192,15 +217,24 @@ namespace stardew_access
screenReader = sapiOutput; screenReader = sapiOutput;
} }
private void OnButtonPressed(object sender, ButtonPressedEventArgs e) private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
{ {
if (Equals(e.Button, SButton.K))
{
if (Game1.activeClickableMenu != null)
{
string name = Game1.activeClickableMenu.getCurrentlySnappedComponent().label;
screenReader.Speak(name, true);
}
}
// ignore if player hasn't loaded a save yet // ignore if player hasn't loaded a save yet
if (!Context.IsWorldReady) if (!Context.IsWorldReady)
return; return;
// Narrate Health And Energy // Narrate Health And Energy
if (Equals(e.Button, SButton.R)) if (Equals(e.Button, SButton.I))
{ {
screenReader.Speak($"Health is {CurrentPlayer.getHealth()} and Stamina is {CurrentPlayer.getStamina()}"); screenReader.Speak($"Health is {CurrentPlayer.getHealth()} and Stamina is {CurrentPlayer.getStamina()}");
} }