From cc9b66c2164ae6cd1842af1bfc5ef3cb1fe72306 Mon Sep 17 00:00:00 2001 From: Anna Date: Sat, 8 Aug 2020 06:05:23 -0400 Subject: [PATCH] feat: add options for behaviour when closed --- Peeping Tom/Configuration.cs | 2 ++ Peeping Tom/Plugin.cs | 42 +++++++++++++++--------------- Peeping Tom/PluginUI.cs | 15 ++++++++--- Peeping Tom/TargetWatcher.cs | 50 +++++++++++++++++++++--------------- 4 files changed, 64 insertions(+), 45 deletions(-) diff --git a/Peeping Tom/Configuration.cs b/Peeping Tom/Configuration.cs index 622efb1..8878be0 100644 --- a/Peeping Tom/Configuration.cs +++ b/Peeping Tom/Configuration.cs @@ -25,6 +25,7 @@ namespace PeepingTom { public bool DebugMarkers { get; set; } = false; public bool KeepHistory { get; set; } = true; + public bool HistoryWhenClosed { get; set; } = true; public int NumHistory { get; set; } = 5; public bool LogParty { get; set; } = true; @@ -36,6 +37,7 @@ namespace PeepingTom { public bool PlaySoundOnTarget { get; set; } = false; public string SoundPath { get; set; } = null; public float SoundCooldown { get; set; } = 10f; + public bool PlaySoundWhenClosed { get; set; } = false; public bool OpenOnLogin { get; set; } = false; public bool AllowMovement { get; set; } = true; diff --git a/Peeping Tom/Plugin.cs b/Peeping Tom/Plugin.cs index abad079..378c501 100644 --- a/Peeping Tom/Plugin.cs +++ b/Peeping Tom/Plugin.cs @@ -6,33 +6,33 @@ namespace PeepingTom { public class PeepingTomPlugin : IDalamudPlugin, IDisposable { public string Name => "Peeping Tom"; - private DalamudPluginInterface pi; - private Configuration config; - private PluginUI ui; + internal DalamudPluginInterface Interface { get; private set; } + internal Configuration config; + internal PluginUI ui; private HookManager hookManager; private TargetWatcher watcher; public void Initialize(DalamudPluginInterface pluginInterface) { - this.pi = pluginInterface ?? throw new ArgumentNullException(nameof(pluginInterface), "DalamudPluginInterface argument was null"); - this.config = this.pi.GetPluginConfig() as Configuration ?? new Configuration(); - this.config.Initialize(this.pi); - this.watcher = new TargetWatcher(this.pi, this.config); - this.ui = new PluginUI(this, this.config, this.pi, this.watcher); - this.hookManager = new HookManager(this.pi, this.ui, this.config); + this.Interface = pluginInterface ?? throw new ArgumentNullException(nameof(pluginInterface), "DalamudPluginInterface argument was null"); + this.config = this.Interface.GetPluginConfig() as Configuration ?? new Configuration(); + this.config.Initialize(this.Interface); + this.watcher = new TargetWatcher(this); + this.ui = new PluginUI(this, this.config, this.Interface, this.watcher); + this.hookManager = new HookManager(this.Interface, this.ui, this.config); - this.pi.CommandManager.AddHandler("/ppeepingtom", new CommandInfo(this.OnCommand) { + this.Interface.CommandManager.AddHandler("/ppeepingtom", new CommandInfo(this.OnCommand) { HelpMessage = "Use with no arguments to show the list. Use with \"c\" or \"config\" to show the config" }); - this.pi.CommandManager.AddHandler("/ptom", new CommandInfo(this.OnCommand) { + this.Interface.CommandManager.AddHandler("/ptom", new CommandInfo(this.OnCommand) { HelpMessage = "Alias for /ppeepingtom" }); - this.pi.CommandManager.AddHandler("/ppeep", new CommandInfo(this.OnCommand) { + this.Interface.CommandManager.AddHandler("/ppeep", new CommandInfo(this.OnCommand) { HelpMessage = "Alias for /ppeepingtom" }); - this.pi.Framework.OnUpdateEvent += this.watcher.OnFrameworkUpdate; - this.pi.UiBuilder.OnBuildUi += this.DrawUI; - this.pi.UiBuilder.OnOpenConfigUi += this.ConfigUI; + this.Interface.Framework.OnUpdateEvent += this.watcher.OnFrameworkUpdate; + this.Interface.UiBuilder.OnBuildUi += this.DrawUI; + this.Interface.UiBuilder.OnOpenConfigUi += this.ConfigUI; } private void OnCommand(string command, string args) { @@ -45,13 +45,13 @@ namespace PeepingTom { protected virtual void Dispose(bool includeManaged) { this.hookManager.Dispose(); - this.pi.Framework.OnUpdateEvent -= this.watcher.OnFrameworkUpdate; + this.Interface.Framework.OnUpdateEvent -= this.watcher.OnFrameworkUpdate; this.watcher.Dispose(); - this.pi.UiBuilder.OnBuildUi -= DrawUI; - this.pi.UiBuilder.OnOpenConfigUi -= ConfigUI; - this.pi.CommandManager.RemoveHandler("/ppeepingtom"); - this.pi.CommandManager.RemoveHandler("/ptom"); - this.pi.CommandManager.RemoveHandler("/ppeep"); + this.Interface.UiBuilder.OnBuildUi -= DrawUI; + this.Interface.UiBuilder.OnOpenConfigUi -= ConfigUI; + this.Interface.CommandManager.RemoveHandler("/ppeepingtom"); + this.Interface.CommandManager.RemoveHandler("/ptom"); + this.Interface.CommandManager.RemoveHandler("/ppeep"); this.ui.Dispose(); } diff --git a/Peeping Tom/PluginUI.cs b/Peeping Tom/PluginUI.cs index 248a717..75f1ba7 100644 --- a/Peeping Tom/PluginUI.cs +++ b/Peeping Tom/PluginUI.cs @@ -7,10 +7,7 @@ using Dalamud.Plugin; using ImGuiNET; using System; using System.Collections.Generic; -using System.Diagnostics; -using System.IO; using System.Linq; -using System.Media; using System.Numerics; using System.Runtime.InteropServices; @@ -195,6 +192,12 @@ namespace PeepingTom { this.config.Save(); } + bool playWhenClosed = this.config.PlaySoundWhenClosed; + if (ImGui.Checkbox("Play sound when window is closed", ref playWhenClosed)) { + this.config.PlaySoundWhenClosed = playWhenClosed; + this.config.Save(); + } + ImGui.EndTabItem(); } @@ -241,6 +244,12 @@ namespace PeepingTom { this.config.Save(); } + bool historyWhenClosed = this.config.HistoryWhenClosed; + if (ImGui.Checkbox("Record history when window is closed", ref historyWhenClosed)) { + this.config.HistoryWhenClosed = historyWhenClosed; + this.config.Save(); + } + int numHistory = this.config.NumHistory; if (ImGui.InputInt("Number of previous targeters to keep", ref numHistory)) { numHistory = Math.Max(0, Math.Min(10, numHistory)); diff --git a/Peeping Tom/TargetWatcher.cs b/Peeping Tom/TargetWatcher.cs index 4397802..5345792 100644 --- a/Peeping Tom/TargetWatcher.cs +++ b/Peeping Tom/TargetWatcher.cs @@ -11,14 +11,11 @@ using System.IO; using System.Linq; using System.Media; using System.Runtime.InteropServices; -using System.Text; using System.Threading; -using System.Threading.Tasks; namespace PeepingTom { class TargetWatcher : IDisposable { - private readonly DalamudPluginInterface pi; - private readonly Configuration config; + private readonly PeepingTomPlugin plugin; private long soundLastPlayed = 0; private int lastTargetAmount = 0; @@ -45,9 +42,8 @@ namespace PeepingTom { } } - public TargetWatcher(DalamudPluginInterface pi, Configuration config) { - this.pi = pi ?? throw new ArgumentNullException(nameof(pi), "DalamudPluginInterface cannot be null"); - this.config = config ?? throw new ArgumentNullException(nameof(config), "Configuration cannot be null"); + public TargetWatcher(PeepingTomPlugin plugin) { + this.plugin = plugin ?? throw new ArgumentNullException(nameof(plugin), "PeepingTomPlugin cannot be null"); } public Out WithCurrent(Func, Out> func) { @@ -58,7 +54,7 @@ namespace PeepingTom { } public void OnFrameworkUpdate(Framework framework) { - PlayerCharacter player = this.pi.ClientState.LocalPlayer; + PlayerCharacter player = this.plugin.Interface.ClientState.LocalPlayer; if (player == null) { return; } @@ -76,7 +72,7 @@ namespace PeepingTom { this.HandleHistory(current); // play sound if necessary - if (this.config.PlaySoundOnTarget && this.current.Length > this.lastTargetAmount && this.CanPlaySound()) { + if (this.CanPlaySound()) { this.soundLastPlayed = Stopwatch.GetTimestamp(); this.PlaySound(); } @@ -84,7 +80,7 @@ namespace PeepingTom { } private void HandleHistory(PlayerCharacter[] targeting) { - if (!this.config.KeepHistory) { + if (!this.plugin.Config.KeepHistory || (!this.plugin.Config.HistoryWhenClosed && !this.plugin.Ui.Visible)) { return; } @@ -97,24 +93,24 @@ namespace PeepingTom { } // only keep the configured number of previous targeters (ignoring ones that are currently targeting) - while (this.previousTargeters.Where(old => targeting.All(actor => actor.ActorId != old.ActorId)).Count() > this.config.NumHistory) { + while (this.previousTargeters.Where(old => targeting.All(actor => actor.ActorId != old.ActorId)).Count() > this.plugin.Config.NumHistory) { this.previousTargeters.RemoveAt(this.previousTargeters.Count - 1); } } private PlayerCharacter[] GetTargeting(Actor player) { - return this.pi.ClientState.Actors + return this.plugin.Interface.ClientState.Actors .Where(actor => actor.TargetActorID == player.ActorId && actor is PlayerCharacter) .Select(actor => actor as PlayerCharacter) - .Where(actor => this.config.LogParty || this.pi.ClientState.PartyList.All(member => member.Actor?.ActorId != actor.ActorId)) - .Where(actor => this.config.LogAlliance || !this.InAlliance(actor)) - .Where(actor => this.config.LogInCombat || !this.InCombat(actor)) - .Where(actor => this.config.LogSelf || actor.ActorId != player.ActorId) + .Where(actor => this.plugin.Config.LogParty || this.plugin.Interface.ClientState.PartyList.All(member => member.Actor?.ActorId != actor.ActorId)) + .Where(actor => this.plugin.Config.LogAlliance || !this.InAlliance(actor)) + .Where(actor => this.plugin.Config.LogInCombat || !this.InCombat(actor)) + .Where(actor => this.plugin.Config.LogSelf || actor.ActorId != player.ActorId) .ToArray(); } private byte GetStatus(Actor actor) { - IntPtr statusPtr = this.pi.TargetModuleScanner.ResolveRelativeAddress(actor.Address, 0x1901); + IntPtr statusPtr = this.plugin.Interface.TargetModuleScanner.ResolveRelativeAddress(actor.Address, 0x1901); return Marshal.ReadByte(statusPtr); } @@ -127,6 +123,18 @@ namespace PeepingTom { } private bool CanPlaySound() { + if (!this.plugin.Config.PlaySoundOnTarget) { + return false; + } + + if (this.current.Length <= this.lastTargetAmount) { + return false; + } + + if (!this.plugin.Config.PlaySoundWhenClosed && !this.plugin.Ui.Visible) { + return false; + } + if (this.soundLastPlayed == 0) { return true; } @@ -135,15 +143,15 @@ namespace PeepingTom { long diff = current - this.soundLastPlayed; // only play every 10 seconds? float secs = (float)diff / Stopwatch.Frequency; - return secs >= this.config.SoundCooldown; + return secs >= this.plugin.Config.SoundCooldown; } private void PlaySound() { SoundPlayer player; - if (this.config.SoundPath == null) { + if (this.plugin.Config.SoundPath == null) { player = new SoundPlayer(Properties.Resources.Target); } else { - player = new SoundPlayer(this.config.SoundPath); + player = new SoundPlayer(this.plugin.Config.SoundPath); } using (player) { try { @@ -158,7 +166,7 @@ namespace PeepingTom { private void SendError(string message) { Payload[] payloads = { new TextPayload($"[Who's Looking] {message}") }; - this.pi.Framework.Gui.Chat.PrintChat(new XivChatEntry { + this.plugin.Interface.Framework.Gui.Chat.PrintChat(new XivChatEntry { MessageBytes = new SeString(payloads).Encode(), Type = XivChatType.ErrorMessage });