summaryrefslogtreecommitdiff
path: root/src/SMAPI/Utilities/PerScreen.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-01-22 21:05:04 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-01-22 21:05:04 -0500
commitd0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6 (patch)
tree02896d970c7650d8f7c8b84f54e53eddab88b4ca /src/SMAPI/Utilities/PerScreen.cs
parent5953fc3bd083ae0a579d2da1ad833e6163848086 (diff)
parent733750fdc4f5d16069d95880144619c0e31e8a89 (diff)
downloadSMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.tar.gz
SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.tar.bz2
SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Utilities/PerScreen.cs')
-rw-r--r--src/SMAPI/Utilities/PerScreen.cs53
1 files changed, 36 insertions, 17 deletions
diff --git a/src/SMAPI/Utilities/PerScreen.cs b/src/SMAPI/Utilities/PerScreen.cs
index 89d08e87..20b8fbce 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,47 +41,66 @@ 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);
}
+ /// <summary>Get all active values by screen ID. This doesn't initialize the value for a screen ID if it's not created yet.</summary>
+ public IEnumerable<KeyValuePair<int, T>> GetActiveValues()
+ {
+ this.RemoveDeadScreens();
+ return this.States.ToArray();
+ }
+
/// <summary>Get the value for a given screen ID, creating it if needed.</summary>
/// <param name="screenId">The screen ID to check.</param>
- internal T GetValueForScreen(int screenId)
+ public T GetValueForScreen(int screenId)
{
- this.RemoveDeadPlayers();
+ this.RemoveDeadScreens();
return this.States.TryGetValue(screenId, out T state)
? state
: this.States[screenId] = this.CreateNewState();
}
- /// <summary>Set the value for a given screen ID, creating it if needed.</summary>
+ /// <summary>Set the value for a given screen ID.</summary>
/// <param name="screenId">The screen ID whose value set.</param>
/// <param name="value">The value to set.</param>
- internal void SetValueForScreen(int screenId, T value)
+ public void SetValueForScreen(int screenId, T value)
{
- this.RemoveDeadPlayers();
+ this.RemoveDeadScreens();
this.States[screenId] = value;
}
+ /// <summary>Remove all active values.</summary>
+ public void ResetAllScreens()
+ {
+ this.RemoveScreens(p => true);
+ }
+
/*********
** Private methods
*********/
- /// <summary>Remove players who are no longer have a split-screen index.</summary>
- /// <returns>Returns whether any players were removed.</returns>
- private void RemoveDeadPlayers()
+ /// <summary>Remove screens which are no longer active.</summary>
+ private void RemoveDeadScreens()
{
if (this.LastRemovedScreenId == Context.LastRemovedScreenId)
return;
-
this.LastRemovedScreenId = Context.LastRemovedScreenId;
- foreach (int id in this.States.Keys.ToArray())
+
+ 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(id))
- this.States.Remove(id);
+ if (shouldRemove(pair.Key))
+ this.States.Remove(pair.Key);
}
}
}