summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Input/MouseStateBuilder.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-03-08 14:53:41 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-03-08 14:53:41 -0400
commite80b7712f7e4c78a5b016289ca19efaf7d39cd8b (patch)
tree5ca5277b1755504a1e95b655b16ff696114d26b8 /src/SMAPI/Framework/Input/MouseStateBuilder.cs
parente39b9e0d699079edfbcf8595d7499aff894578b6 (diff)
downloadSMAPI-e80b7712f7e4c78a5b016289ca19efaf7d39cd8b.tar.gz
SMAPI-e80b7712f7e4c78a5b016289ca19efaf7d39cd8b.tar.bz2
SMAPI-e80b7712f7e4c78a5b016289ca19efaf7d39cd8b.zip
encapsulate logic for each input type
Diffstat (limited to 'src/SMAPI/Framework/Input/MouseStateBuilder.cs')
-rw-r--r--src/SMAPI/Framework/Input/MouseStateBuilder.cs93
1 files changed, 63 insertions, 30 deletions
diff --git a/src/SMAPI/Framework/Input/MouseStateBuilder.cs b/src/SMAPI/Framework/Input/MouseStateBuilder.cs
index 9c6f6f95..cff3e05e 100644
--- a/src/SMAPI/Framework/Input/MouseStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/MouseStateBuilder.cs
@@ -3,43 +3,61 @@ using Microsoft.Xna.Framework.Input;
namespace StardewModdingAPI.Framework.Input
{
- /// <summary>Manipulates mouse state.</summary>
- internal class MouseStateBuilder
+ /// <summary>Manages mouse state.</summary>
+ internal class MouseStateBuilder : IInputStateBuilder<MouseStateBuilder, MouseState>
{
/*********
** Fields
*********/
+ /// <summary>The underlying mouse state.</summary>
+ private MouseState? State;
+
/// <summary>The current button states.</summary>
- private readonly IDictionary<SButton, ButtonState> ButtonStates;
+ private IDictionary<SButton, ButtonState> ButtonStates;
+
+ /// <summary>The mouse wheel scroll value.</summary>
+ private int ScrollWheelValue;
+
+ /*********
+ ** Accessors
+ *********/
/// <summary>The X cursor position.</summary>
- private readonly int X;
+ public int X { get; private set; }
/// <summary>The Y cursor position.</summary>
- private readonly int Y;
-
- /// <summary>The mouse wheel scroll value.</summary>
- private readonly int ScrollWheelValue;
+ public int Y { get; private set; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
- /// <param name="state">The initial state.</param>
- public MouseStateBuilder(MouseState state)
+ /// <param name="state">The initial state, or <c>null</c> to get the latest state.</param>
+ public MouseStateBuilder(MouseState? state = null)
+ {
+ this.Reset(state);
+ }
+
+ /// <summary>Reset the tracked state.</summary>
+ /// <param name="state">The state from which to reset, or <c>null</c> to get the latest state.</param>
+ public MouseStateBuilder Reset(MouseState? state = null)
{
+ this.State = state ??= Mouse.GetState();
+
this.ButtonStates = new Dictionary<SButton, ButtonState>
{
- [SButton.MouseLeft] = state.LeftButton,
- [SButton.MouseMiddle] = state.MiddleButton,
- [SButton.MouseRight] = state.RightButton,
- [SButton.MouseX1] = state.XButton1,
- [SButton.MouseX2] = state.XButton2
+ [SButton.MouseLeft] = state.Value.LeftButton,
+ [SButton.MouseMiddle] = state.Value.MiddleButton,
+ [SButton.MouseRight] = state.Value.RightButton,
+ [SButton.MouseX1] = state.Value.XButton1,
+ [SButton.MouseX2] = state.Value.XButton2
};
- this.X = state.X;
- this.Y = state.Y;
- this.ScrollWheelValue = state.ScrollWheelValue;
+ this.X = state.Value.X;
+ this.Y = state.Value.Y;
+ this.ScrollWheelValue = state.Value.ScrollWheelValue;
+
+ return this;
}
/// <summary>Override the states for a set of buttons.</summary>
@@ -56,19 +74,34 @@ namespace StardewModdingAPI.Framework.Input
return this;
}
- /// <summary>Construct an equivalent mouse state.</summary>
- public MouseState ToMouseState()
+ /// <summary>Get the currently pressed buttons.</summary>
+ public IEnumerable<SButton> GetPressedButtons()
{
- return new MouseState(
- x: this.X,
- y: this.Y,
- scrollWheel: this.ScrollWheelValue,
- leftButton: this.ButtonStates[SButton.MouseLeft],
- middleButton: this.ButtonStates[SButton.MouseMiddle],
- rightButton: this.ButtonStates[SButton.MouseRight],
- xButton1: this.ButtonStates[SButton.MouseX1],
- xButton2: this.ButtonStates[SButton.MouseX2]
- );
+ foreach (var pair in this.ButtonStates)
+ {
+ if (pair.Value == ButtonState.Pressed)
+ yield return pair.Key;
+ }
+ }
+
+ /// <summary>Get the equivalent state.</summary>
+ public MouseState GetState()
+ {
+ if (this.State == null)
+ {
+ this.State = new MouseState(
+ x: this.X,
+ y: this.Y,
+ scrollWheel: this.ScrollWheelValue,
+ leftButton: this.ButtonStates[SButton.MouseLeft],
+ middleButton: this.ButtonStates[SButton.MouseMiddle],
+ rightButton: this.ButtonStates[SButton.MouseRight],
+ xButton1: this.ButtonStates[SButton.MouseX1],
+ xButton2: this.ButtonStates[SButton.MouseX2]
+ );
+ }
+
+ return this.State.Value;
}
}
}