summaryrefslogtreecommitdiff
path: root/src/Noddybox.Emulation.Xna.Input/Joystick/JoystickDriver.cs
blob: dc838315f4b56306d2ddee121275b408e8d36617 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// This file is part of the Noddybox.Emulation C# suite.
//
// Noddybox.Emulation is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Noddybox.Emulation is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Noddybox.Emulation.  If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (c) 2012 Ian Cowburn
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Noddybox.Emulation.Xna.Input.Joystick
{
    /// <summary>
    /// Displays and drives the joystick.  Unlike the keyboard this is not
    /// not done event driven, but as a clasic Joystick interface.
    /// </summary>
    public class JoystickDriver
    {
        #region Event arguments

        /// <summary>
        /// Event data when the digital joystick state changes
        /// </summary>
        public class DigitalJoystickEventArgs : EventArgs
        {
            /// <summary>
            /// The joystick state.
            /// </summary>
            public DigitalJoystickState State {get; private set;}

            public bool[] Buttons {get; private set;}

            private DigitalJoystickEventArgs(DigitalJoystickState state, bool[] buttons)
            {
                State = state;
                Buttons = buttons;
            }
        }

        /// <summary>
        /// Event data when the analogue joystick state changes
        /// </summary>
        public class AnalogueJoystickEventArgs : EventArgs
        {
            /// <summary>
            /// The joystick state.
            /// </summary>
            public Vector2 State {get; private set;}

            public bool[] Buttons {get; private set;}

            private AnalogueJoystickEventArgs(Vector2 state, bool[] buttons)
            {
                State = state;
                Buttons = buttons;
            }
        }

        #endregion

        #region Private data

        private InputManager manager;
        private bool subscribed;

        private Texture2D backgroundImage;
        private Texture2D joystickImage;
        private Texture2D buttonImage;

        private DigitalJoystickState digital;
        private Vector2 analogue;
        private bool[] button;

        private Vector2 backgroundPosition;
        private Vector2 joystickPosition;
        private Vector2[] buttonPositions;
        private float deadzone;
        private float stickSize;
        private int numButtons;

        private Vector2 joystickOffset;
        private Vector2 buttonOffset;
        private int joystickSize;
        private int buttonSize;

        private JoystickLock joylock;

        #endregion

        #region Private members

        /// <summary>
        /// Updates the joystick.
        /// </summary>
        public void TouchScreenHandler(object sender, InputManager.TouchLocationEventArgs e)
        {
            if (e.Location.State == TouchLocationState.Pressed)
            {
                for(int f = 0; !e.Handled && f < numButtons; f++)
                {
                    if ((buttonPositions[f] - e.Location.Position).Length() < buttonSize)
                    {
                        e.Handled = true;
                        button[f] = true;
                    }
                }
            }
            else if (e.Location.State == TouchLocationState.Released)
            {
                for(int f = 0; !e.Handled && f < numButtons; f++)
                {
                    if ((buttonPositions[f] - e.Location.Position).Length() < buttonSize)
                    {
                        e.Handled = true;
                        button[f] = false;
                    }
                }
            }
        }

        #endregion

        #region Public members

        /// <summary>
        /// Stops consuming inputs.
        /// </summary>
        public void StopJoytstickUpdates()
        {
            if (subscribed)
            {
                manager.TouchEvent -= TouchScreenHandler;
                subscribed = false;
            }
        }

        /// <summary>
        /// Starts consuming inputs.
        /// </summary>
        public void StartJoystickUpdates()
        {
            if (!subscribed)
            {
                manager.TouchEvent += TouchScreenHandler;
                subscribed = true;
            }
        }

        /// <summary>
        /// Draw the joystick
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(backgroundImage, backgroundPosition, Color.White);

            spriteBatch.Draw(joystickImage, joystickPosition + analogue,
                             null, Color.White, 0f, joystickOffset, 1f, SpriteEffects.None, 0f);

            for(int f = 0; f < numButtons; f++)
            {
                spriteBatch.Draw(buttonImage, buttonPositions[f],
                                    null, button[f] ? Color.LightGray : Color.White, 0f, buttonOffset, button[f] ? 0.9f : 1f, SpriteEffects.None, 0f);
            }
        }

        #endregion

        #region Events

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="manager">The input manager to attach to.</param>
        /// <param name="graphics">The graphics device to associate textures with.</param>
        /// <param name="joystickType">The type of joystick.</param>
        /// <param name="backgroundImage">The image for the joystick background.</param>
        /// <param name="joystickImage">The image for the joystick knob.</param>
        /// <param name="buttonImage">The image for a joystick button.</param>
        /// <param name="backgroundPosition">Where to draw and offset the joystick background to.</param>
        /// <param name="joystickPosition">Where to centre the joystick. Note this is absolute.</param>
        /// <param name="buttonPositions">Positions for the buttons.</param>
        /// <param name="deadzone">Deadzone radius where joystick movement is not picked up.</param>
        /// <param name="stickSize">The maximum radius that the joystick can move.</param>
        public JoystickDriver(InputManager manager, GraphicsDevice graphics, JoystickType joystickType,
                              Texture2D backgroundImage, Texture2D joystickImage, Texture2D buttonImage,
                              Vector2 backgroundPosition, Vector2 joystickPosition, Vector2[] buttonPositions,
                              float deadzone, float stickSize)
        {
            this.manager = manager;
            this.backgroundImage = backgroundImage;
            this.joystickImage = joystickImage;
            this.buttonImage = buttonImage;

            this.backgroundPosition = backgroundPosition;
            this.joystickPosition = joystickPosition + backgroundPosition;

            this.joystickSize = Math.Min(joystickImage.Width, joystickImage.Height) / 2;
            this.joystickOffset = new Vector2(joystickImage.Width / 2f, joystickImage.Height / 2f);
            
            if (buttonImage != null)
            {
                this.buttonSize = Math.Min(buttonImage.Width, buttonImage.Height) / 2;
                this.buttonOffset = new Vector2(buttonImage.Width / 2f, buttonImage.Height / 2f);
                this.numButtons = buttonPositions.Length;

                this.button = new bool[numButtons];
                this.buttonPositions = new Vector2[numButtons];

                for(int f = 0; f < numButtons; f++)
                {
                    this.buttonPositions[f] = backgroundPosition + buttonPositions[f] + buttonOffset;
                }
            }
            else
            {
                this.buttonSize = 0;
                this.buttonOffset = Vector2.Zero;
                this.numButtons = 0;
            }

            this.deadzone = deadzone;
            this.stickSize = stickSize;

            this.digital = DigitalJoystickState.Centre;
            this.joylock = JoystickLock.EightWay;
            this.analogue = Vector2.Zero;

            StartJoystickUpdates();
        }

        #endregion
    }
}