summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Input/MouseStateBuilder.cs
blob: 59956feb67eabc864416f06dc87ffe3537c7c7d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;

namespace StardewModdingAPI.Framework.Input
{
    /// <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 IDictionary<SButton, ButtonState> ButtonStates;

        /// <summary>The mouse wheel scroll value.</summary>
        private int ScrollWheelValue;


        /*********
        ** Accessors
        *********/
        /// <summary>The X cursor position.</summary>
        public int X { get; private set; }

        /// <summary>The Y cursor position.</summary>
        public int Y { get; private set; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <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.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.Value.X;
            this.Y = state.Value.Y;
            this.ScrollWheelValue = state.Value.ScrollWheelValue;

            return this;
        }

        /// <summary>Override the states for a set of buttons.</summary>
        /// <param name="overrides">The button state overrides.</param>
        public MouseStateBuilder OverrideButtons(IDictionary<SButton, SButtonState> overrides)
        {
            foreach (var pair in overrides)
            {
                if (this.ButtonStates.ContainsKey(pair.Key))
                {
                    this.State = null;
                    this.ButtonStates[pair.Key] = pair.Value.IsDown() ? ButtonState.Pressed : ButtonState.Released;
                }
            }

            return this;
        }

        /// <summary>Get the currently pressed buttons.</summary>
        public IEnumerable<SButton> GetPressedButtons()
        {
            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;
        }
    }
}