From e80b7712f7e4c78a5b016289ca19efaf7d39cd8b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 8 Mar 2020 14:53:41 -0400 Subject: encapsulate logic for each input type --- src/SMAPI/Framework/Input/MouseStateBuilder.cs | 93 +++++++++++++++++--------- 1 file changed, 63 insertions(+), 30 deletions(-) (limited to 'src/SMAPI/Framework/Input/MouseStateBuilder.cs') 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 { - /// Manipulates mouse state. - internal class MouseStateBuilder + /// Manages mouse state. + internal class MouseStateBuilder : IInputStateBuilder { /********* ** Fields *********/ + /// The underlying mouse state. + private MouseState? State; + /// The current button states. - private readonly IDictionary ButtonStates; + private IDictionary ButtonStates; + + /// The mouse wheel scroll value. + private int ScrollWheelValue; + + /********* + ** Accessors + *********/ /// The X cursor position. - private readonly int X; + public int X { get; private set; } /// The Y cursor position. - private readonly int Y; - - /// The mouse wheel scroll value. - private readonly int ScrollWheelValue; + public int Y { get; private set; } /********* ** Public methods *********/ /// Construct an instance. - /// The initial state. - public MouseStateBuilder(MouseState state) + /// The initial state, or null to get the latest state. + public MouseStateBuilder(MouseState? state = null) + { + this.Reset(state); + } + + /// Reset the tracked state. + /// The state from which to reset, or null to get the latest state. + public MouseStateBuilder Reset(MouseState? state = null) { + this.State = state ??= Mouse.GetState(); + this.ButtonStates = new Dictionary { - [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; } /// Override the states for a set of buttons. @@ -56,19 +74,34 @@ namespace StardewModdingAPI.Framework.Input return this; } - /// Construct an equivalent mouse state. - public MouseState ToMouseState() + /// Get the currently pressed buttons. + public IEnumerable 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; + } + } + + /// Get the equivalent state. + 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; } } } -- cgit