From 2d7465321712f3e4640b75311c666f5faedde7e3 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Date: Thu, 12 May 2022 15:28:09 +0530 Subject: [PATCH] Read tile pauses when narrating a warning message --- stardew-access/Features/ReadTile.cs | 37 +++++++++++++++++++++--- stardew-access/Features/Warnings.cs | 6 ++-- stardew-access/ModEntry.cs | 44 +++++++++++++++++++++-------- 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/stardew-access/Features/ReadTile.cs b/stardew-access/Features/ReadTile.cs index 5017cb7..a7a5912 100644 --- a/stardew-access/Features/ReadTile.cs +++ b/stardew-access/Features/ReadTile.cs @@ -6,15 +6,44 @@ namespace stardew_access.Features { public class ReadTile { - public static bool isReadingTile = false; - public static Vector2 prevTile; + private bool isBusy; // To pause execution of run method between fixed intervals + private int delay; // Length of each interval (in ms) + private bool shouldPause; // To pause the execution + private Vector2 prevTile; public ReadTile() { - isReadingTile = false; + isBusy = false; + delay = 100; } - public static void run(bool manuallyTriggered = false, bool playersPosition = false) + public void update() + { + if (this.isBusy) + return; + + if (this.shouldPause) + return; + + if (!MainClass.Config.ReadTile) + return; + + this.isBusy = true; + this.run(); + Task.Delay(delay).ContinueWith(_ => { this.isBusy = false; }); + } + + /// + /// Pauses the read tile for the provided time. + /// + /// The amount of time we want to pause the execution (in ms).
Default is 2500 (2.5s). + public void pause(int time = 2500) + { + this.shouldPause = true; + Task.Delay(time).ContinueWith(_ => { this.shouldPause = false; }); + } + + public void run(bool manuallyTriggered = false, bool playersPosition = false) { try { diff --git a/stardew-access/Features/Warnings.cs b/stardew-access/Features/Warnings.cs index 7f3eb74..da54da7 100644 --- a/stardew-access/Features/Warnings.cs +++ b/stardew-access/Features/Warnings.cs @@ -27,8 +27,9 @@ namespace stardew_access.Features if ((stamina <= 50 && prevStamina > 50) || (stamina <= 25 && prevStamina > 25) || (stamina <= 10 && prevStamina > 10)) { - MainClass.DebugLog(toSpeak); MainClass.ScreenReader.Say(toSpeak, true); + // Pause the read tile feature to prevent interruption in warning message + MainClass.ReadTileFeature.pause(); } prevStamina = stamina; @@ -44,8 +45,9 @@ namespace stardew_access.Features if ((health <= 50 && prevHealth > 50) || (health <= 25 && prevHealth > 25) || (health <= 10 && prevHealth > 10)) { - MainClass.DebugLog(toSpeak); MainClass.ScreenReader.Say(toSpeak, true); + // Pause the read tile feature to prevent interruption in warning message + MainClass.ReadTileFeature.pause(); } prevHealth = health; diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index c79e472..768a365 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -21,6 +21,8 @@ namespace stardew_access private static IScreenReader? screenReader; private static IModHelper? modHelper; private static TileViewer? tileViewer; + private static Warnings? warnings; + private static ReadTile? readTile; internal static ModConfig Config { get => config; set => config = value; } public static IModHelper? ModHelper { get => modHelper; } @@ -64,7 +66,7 @@ namespace stardew_access set => screenReader = value; } - public static TileViewer TileViewer + public static TileViewer TileViewerFeature { get { @@ -74,6 +76,26 @@ namespace stardew_access } } + public static ReadTile ReadTileFeature + { + get + { + if (readTile == null) + readTile = new ReadTile(); + return readTile; + } + } + + public static Warnings WarningsFeature + { + get + { + if (warnings == null) + warnings = new Warnings(); + + return warnings; + } + } #endregion /********* @@ -120,7 +142,8 @@ namespace stardew_access public void OnExit(object? sender, EventArgs? e) { - // Don't if this ever gets called or not but, just in case if it does. + // This closes the connection with the screen reader, important for linux + // Don't know if this ever gets called or not but, just in case if it does. if (ScreenReader != null) ScreenReader.CloseScreenReader(); } @@ -143,14 +166,11 @@ namespace stardew_access Other.narrateCurrentLocation(); //handle TileCursor update logic - TileViewer.update(); + TileViewerFeature.update(); - if (!ReadTile.isReadingTile && Config.ReadTile) - { - ReadTile.isReadingTile = true; - ReadTile.run(); - Task.Delay(100).ContinueWith(_ => { ReadTile.isReadingTile = false; }); - } + WarningsFeature.update(); + + ReadTileFeature.update(); if (!RadarFeature.isRunning && Config.Radar) { @@ -258,19 +278,19 @@ namespace stardew_access // Manual read tile at player's position if (Config.ReadStandingTileKey.JustPressed()) { - ReadTile.run(manuallyTriggered: true, playersPosition: true); + ReadTileFeature.run(manuallyTriggered: true, playersPosition: true); return; } // Manual read tile at looking tile if (Config.ReadTileKey.JustPressed()) { - ReadTile.run(manuallyTriggered: true); + ReadTileFeature.run(manuallyTriggered: true); return; } // Tile viewing cursor keys - TileViewer.HandleInput(); + TileViewerFeature.HandleInput(); } public static void ErrorLog(string message)