diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-15 18:48:31 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-15 18:48:31 -0500 |
commit | a9b99c12069bfabca81a74c83eda7f1325c2522a (patch) | |
tree | bf2c2b5abde7ada6d5cf6a802d586a99a3b38887 /src/SMAPI | |
parent | 95ad954fa4b761dd32eefa072622cb1168c4d028 (diff) | |
download | SMAPI-a9b99c12069bfabca81a74c83eda7f1325c2522a.tar.gz SMAPI-a9b99c12069bfabca81a74c83eda7f1325c2522a.tar.bz2 SMAPI-a9b99c12069bfabca81a74c83eda7f1325c2522a.zip |
allow resetting a PerScreen<T> field
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Utilities/PerScreen.cs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/SMAPI/Utilities/PerScreen.cs b/src/SMAPI/Utilities/PerScreen.cs index 1498488b..60406d6b 100644 --- a/src/SMAPI/Utilities/PerScreen.cs +++ b/src/SMAPI/Utilities/PerScreen.cs @@ -11,10 +11,10 @@ namespace StardewModdingAPI.Utilities /********* ** Fields *********/ - /// <summary>Create the initial value for a player.</summary> + /// <summary>Create the initial value for a screen.</summary> private readonly Func<T> CreateNewState; - /// <summary>The tracked values for each player.</summary> + /// <summary>The tracked values for each screen.</summary> private readonly IDictionary<int, T> States = new Dictionary<int, T>(); /// <summary>The last <see cref="Context.LastRemovedScreenId"/> value for which this instance was updated.</summary> @@ -24,8 +24,8 @@ namespace StardewModdingAPI.Utilities /********* ** Accessors *********/ - /// <summary>The value for the current player.</summary> - /// <remarks>The value is initialized the first time it's requested for that player, unless it's set manually first.</remarks> + /// <summary>The value for the current screen.</summary> + /// <remarks>The value is initialized the first time it's requested for that screen, unless it's set manually first.</remarks> public T Value { get => this.GetValueForScreen(Context.ScreenId); @@ -41,7 +41,7 @@ namespace StardewModdingAPI.Utilities : this(null) { } /// <summary>Construct an instance.</summary> - /// <param name="createNewState">Create the initial state for a player screen.</param> + /// <param name="createNewState">Create the initial state for a screen.</param> public PerScreen(Func<T> createNewState) { this.CreateNewState = createNewState ?? (() => default); @@ -66,6 +66,12 @@ namespace StardewModdingAPI.Utilities this.States[screenId] = value; } + /// <summary>Remove all active values.</summary> + public void ResetAllScreens() + { + this.RemoveScreens(p => true); + } + /********* ** Private methods @@ -77,9 +83,16 @@ namespace StardewModdingAPI.Utilities return; this.LastRemovedScreenId = Context.LastRemovedScreenId; + this.RemoveScreens(id => !Context.HasScreenId(id)); + } + + /// <summary>Remove screens matching a condition.</summary> + /// <param name="shouldRemove">Returns whether a screen ID should be removed.</param> + private void RemoveScreens(Func<int, bool> shouldRemove) + { foreach (var pair in this.States.ToArray()) { - if (!Context.HasScreenId(pair.Key)) + if (shouldRemove(pair.Key)) this.States.Remove(pair.Key); } } |