/// <summary>The method invoked when the player presses an input button.</summary> /// <typeparam name="TKey">The input type.</typeparam> /// <param name="key">The pressed input.</param> /// <param name="map">The configured input mapping.</param> private void ReceiveKeyPress <TKey>(TKey key, InputMapConfiguration <TKey> map) { if (!map.IsValidKey(key)) { return; } // perform bound action GameHelper.InterceptErrors("handling your input", $"handling input '{key}'", () => { if (key.Equals(map.ToggleLookup)) { this.ToggleLookup(); } if (key.Equals(map.ScrollUp)) { (Game1.activeClickableMenu as LookupMenu)?.ScrollUp(this.Config.ScrollAmount); } else if (key.Equals(map.ScrollDown)) { (Game1.activeClickableMenu as LookupMenu)?.ScrollDown(this.Config.ScrollAmount); } else if (key.Equals(map.ToggleDebug)) { this.DebugInterface.Enabled = !this.DebugInterface.Enabled; } }); }
/********* ** Private methods *********/ /**** ** Event handlers ****/ /// <summary>The method invoked when the player loads the game.</summary> private void ReceiveGameLoaded() { // check for an updated version if (this.Config.CheckForUpdates) { Task.Factory.StartNew(() => { GameHelper.InterceptErrors("checking for a newer version", () => { using (ICumulativeLog log = this.GetTaskLog()) { log.Append("Lookup Anything checking for update... "); GitRelease release = UpdateHelper.GetLatestReleaseAsync("Pathoschild/LookupAnything").Result; if (release.IsNewerThan(this.CurrentVersion)) { log.AppendLine($"update to version {release.Name} available."); this.NewRelease = release; } else { log.AppendLine("no update available."); } } }); }); } }
/// <summary>Load the file containing metadata that's not available from the game directly.</summary> private void LoadMetadata() { GameHelper.InterceptErrors("loading metadata", () => { string content = File.ReadAllText(Path.Combine(this.PathOnDisk, this.DatabaseFileName)); this.Metadata = JsonConvert.DeserializeObject <Metadata>(content); }); }
/// <summary>Show the lookup UI for the current target.</summary> private void HideLookup() { GameHelper.InterceptErrors("closing the menu", () => { if (Game1.activeClickableMenu is LookupMenu) { Game1.playSound("bigDeSelect"); // match default behaviour when closing a menu Game1.activeClickableMenu = null; } }); }
/// <summary>The method invoked when the player closes a displayed menu.</summary> /// <param name="closedMenu">The menu which the player just closed.</param> private void ReceiveMenuClosed(IClickableMenu closedMenu) { // restore the previous menu if it was hidden to show the lookup UI GameHelper.InterceptErrors("restoring the previous menu", () => { if (closedMenu is LookupMenu && this.PreviousMenu != null) { Game1.activeClickableMenu = this.PreviousMenu; this.PreviousMenu = null; } }); }
/// <summary>The method invoked when the player presses an input button.</summary> /// <typeparam name="TKey">The input type.</typeparam> /// <param name="key">The pressed input.</param> /// <param name="map">The configured input mapping.</param> private void ReceiveKeyRelease <TKey>(TKey key, InputMapConfiguration <TKey> map) { if (!map.IsValidKey(key)) { return; } // perform bound action GameHelper.InterceptErrors("handling your input", $"handling input '{key}'", () => { if (key.Equals(map.ToggleLookup)) { this.HideLookup(); } }); }
/// <summary>Show the lookup UI for the current target.</summary> private void ShowLookup() { GameHelper.InterceptErrors("looking that up", () => { using (ICumulativeLog log = this.GetTaskLog()) { log.Append("Lookup Anything received a lookup request. "); // validate version string versionError = GameHelper.ValidateGameVersion(); if (versionError != null) { GameHelper.ShowErrorMessage(versionError); Log.Error(versionError); return; } // get target ISubject subject = null; if (Game1.activeClickableMenu != null) { log.Append($"Searching the open '{Game1.activeClickableMenu.GetType().Name}' menu... "); subject = this.TargetFactory.GetSubjectFrom(Game1.activeClickableMenu, GameHelper.GetScreenCoordinatesFromCursor()); } else { log.Append("Searching the world... "); subject = this.TargetFactory.GetSubjectFrom(Game1.currentLocation, Game1.currentCursorTile, GameHelper.GetScreenCoordinatesFromCursor()); } if (subject == null) { log.AppendLine("no target found."); return; } // show lookup UI log.AppendLine($"showing {subject.GetType().Name}::{subject.Type}::{subject.Name}."); this.PreviousMenu = Game1.activeClickableMenu; Game1.activeClickableMenu = new LookupMenu(subject, this.Metadata); } }); }
/**** ** Error handling ****/ /// <summary>Intercept errors thrown by the action.</summary> /// <param name="verb">The verb describing where the error occurred (e.g. "looking that up"). This is displayed on the screen, so it should be simple and avoid characters that might not be available in the sprite font.</param> /// <param name="action">The action to invoke.</param> /// <param name="onError">A callback invoked if an error is intercepted.</param> public static void InterceptErrors(string verb, Action action, Action <Exception> onError = null) { GameHelper.InterceptErrors(verb, null, action, onError); }