Added hud message narration

master
shoaib11120 2022-01-14 10:10:17 +05:30
parent db1cc7f58d
commit b19e9fda43
1 changed files with 67 additions and 15 deletions

View File

@ -8,9 +8,8 @@ using Microsoft.Xna.Framework.Graphics;
using stardew_access.Patches;
using AutoHotkey.Interop;
using Microsoft.Xna.Framework;
using StardewValley.TerrainFeatures;
using StardewValley.Locations;
using Microsoft.Xna.Framework.Audio;
using System.Text.RegularExpressions;
namespace stardew_access
{
@ -18,9 +17,10 @@ namespace stardew_access
public class MainClass : Mod
{
private Harmony? harmony;
private static bool readTile = true, snapMouse = true;
private static bool readTile = true, snapMouse = true, isNarratingHudMessage = false;
public static IMonitor? monitor;
AutoHotkeyEngine ahk;
public static string hudMessageQueryKey = "";
/*********
** Public methods
@ -45,7 +45,6 @@ namespace stardew_access
}
catch (Exception e)
{
monitor.Log($"Unable to initialize AutoHotKey:\n{e.Message}\n{e.StackTrace}", LogLevel.Error);
}
@ -173,6 +172,11 @@ namespace stardew_access
original: AccessTools.Method(typeof(LanguageSelectionMenu), nameof(LanguageSelectionMenu.draw), new Type[] { typeof(SpriteBatch) }),
postfix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.LanguageSelectionMenuPatch))
);
/*harmony.Patch(
original: AccessTools.Method(typeof(HUDMessage), nameof(HUDMessage.draw), new Type[] { typeof(SpriteBatch), typeof(int) }),
postfix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.HUDMessagePatch))
);*/
#endregion
#region Quest Patches
@ -236,6 +240,8 @@ namespace stardew_access
#endregion
#region Custom Drop Item Sound
try
{
CueDefinition sa_drop_item = new CueDefinition();
sa_drop_item.name = "sa_drop_item";
sa_drop_item.instanceLimit = 1;
@ -248,6 +254,11 @@ namespace stardew_access
}
sa_drop_item.SetSound(audio, Game1.audioEngine.GetCategoryIndex("Sound"), false);
Game1.soundBank.AddCue(sa_drop_item);
}
catch (Exception e)
{
MainClass.monitor.Log($"Unable to initialize custom sounds:\n{e.Message}\n{e.StackTrace}", LogLevel.Error);
}
#endregion
helper.Events.Input.ButtonPressed += this.OnButtonPressed;
@ -272,6 +283,11 @@ namespace stardew_access
if(!ReadTile.isReadingTile && readTile)
ReadTile.run();
if (!isNarratingHudMessage)
{
narrateHudMessages();
}
}
private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
@ -339,5 +355,41 @@ namespace stardew_access
Game1.setMousePosition(x, y);
}
public static async void narrateHudMessages()
{
isNarratingHudMessage = true;
try
{
if(Game1.hudMessages.Count > 0)
{
int lastIndex = Game1.hudMessages.Count - 1;
HUDMessage lastMessage = Game1.hudMessages[lastIndex];
if (!lastMessage.noIcon)
{
string toSpeak = lastMessage.Message;
string searchQuery = toSpeak;
searchQuery = Regex.Replace(toSpeak, @"[\d+]", string.Empty);
searchQuery.Trim();
if (hudMessageQueryKey != searchQuery)
{
hudMessageQueryKey = searchQuery;
ScreenReader.say(toSpeak, true);
}
}
}
}
catch (Exception e)
{
MainClass.monitor.Log($"Unable to narrate hud messages:\n{e.Message}\n{e.StackTrace}", LogLevel.Error);
}
await Task.Delay(1000);
isNarratingHudMessage = false;
}
}
}