using System.Collections.Generic; using Microsoft.Xna.Framework.Input; namespace StardewModdingAPI.Framework.Input { /// Manipulates mouse state. internal class MouseStateBuilder { /********* ** Fields *********/ /// The current button states. private readonly IDictionary ButtonStates; /// The X cursor position. private readonly int X; /// The Y cursor position. private readonly int Y; /// The mouse wheel scroll value. private readonly int ScrollWheelValue; /********* ** Public methods *********/ /// Construct an instance. /// The initial state. public MouseStateBuilder(MouseState state) { this.ButtonStates = new Dictionary { [SButton.MouseLeft] = state.LeftButton, [SButton.MouseMiddle] = state.MiddleButton, [SButton.MouseRight] = state.RightButton, [SButton.MouseX1] = state.XButton1, [SButton.MouseX2] = state.XButton2 }; this.X = state.X; this.Y = state.Y; this.ScrollWheelValue = state.ScrollWheelValue; } /// Override the states for a set of buttons. /// The button state overrides. public MouseStateBuilder OverrideButtons(IDictionary overrides) { foreach (var pair in overrides) { bool isDown = pair.Value.IsDown(); if (this.ButtonStates.ContainsKey(pair.Key)) this.ButtonStates[pair.Key] = isDown ? ButtonState.Pressed : ButtonState.Released; } return this; } /// Construct an equivalent mouse state. public MouseState ToMouseState() { 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] ); } } }