summaryrefslogtreecommitdiff
path: root/src/Noddybox.Emulation.Xna.Keyboard/KeyboardDriver.cs
blob: 1b6f61c4868a858d7a68ef6df4ec4d4d4ab6a402 (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
// 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 Noddybox.Emulation.Keyboard.Schema;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Noddybox.Emulation.Xna.Keyboard
{
    /// <summary>
    /// Displays and drives the keyboard.
    /// </summary>
    /// <typeparam name="T">The enumerated type matching the symbols in the used keyboard definition.</typeparam>
    public class KeyboardDriver<T> where T: struct,IConvertible
    {
        #region Event arguments

        /// <summary>
        /// Event data when a key is pressed or released.
        /// </summary>
        public class KeyPressEventArgs : EventArgs
        {
            /// <summary>
            /// The key that was pressed.
            /// </summary>
            public T Key {get;set;}

            /// <summary>
            /// True if the key was pressed.  False if released.
            /// </summary>
            public bool Pressed {get; set;}
        }

        /// <summary>
        /// Event data when the screen is touched away from a key.
        /// </summary>
        public class TouchLocationEventArgs : EventArgs
        {
            /// <summary>
            /// The touch.
            /// </summary>
            public TouchLocation Location {get; set;}
        }

        #endregion

        #region Private data

        private class KeyState
        {
            public T Symbol {get;set;}
            public bool IsPressed {get; set;}
            public bool IsSticky {get; set;}
            public int TouchId {get; set;}
        }

        private Rectangle[] keymapPos;
        private KeyState[] keymapState;
        private int keymapSize;
        private KeyboardDefinition keyboard;
        private Texture2D image;
        private Vector2 position;
        private Texture2D overlay;

        #endregion

        #region Private members

        #endregion

        #region Public members

        /// <summary>
        /// Updates the keyboard.  Note that this will consume all the key press events.
        /// </summary>
        public void Update()
        {
            foreach (TouchLocation t in TouchPanel.GetState())
            {
                int x = (int)t.Position.X;
                int y = (int)t.Position.Y;
                int f = 0;
                KeyState key = null;


                if (t.State == TouchLocationState.Pressed)
                {
                    for(f = 0; f < keymapSize && key == null; f++)
                    {
                        if (keymapPos[f].Contains(x, y))
                        {
                            key = keymapState[f];
                        }
                    }

                    if (key != null)
                    {
                        key.TouchId = t.Id;

                        if (key.IsSticky)
                        {
                            key.IsPressed = !key.IsPressed;
                        }
                        else
                        {
                            key.IsPressed = true;
                        }

                        if (KeyEvent != null)
                        {
                            KeyEvent(this, new KeyPressEventArgs() {Key = key.Symbol, Pressed = key.IsPressed});
                        }
                    }
                }
                else if (t.State == TouchLocationState.Released)
                {
                    for(f = 0; f < keymapSize && key == null; f++)
                    {
                        if (keymapState[f].TouchId == t.Id)
                        {
                            key = keymapState[f];
                        }
                    }

                    if (key != null)
                    {
                        if (!key.IsSticky)
                        {
                            key.IsPressed = false;

                            if (KeyEvent != null)
                            {
                                KeyEvent(this, new KeyPressEventArgs() {Key = key.Symbol, Pressed = key.IsPressed});
                            }
                        }
                    }
                }

                // If no key handled, pass on the event
                //
                if (key == null && keymapState.Where(r => r.TouchId == t.Id).Count() == 0)
                {
                    if (TouchEvent != null)
                    {
                        TouchEvent(this, new TouchLocationEventArgs() {Location = t});
                    }
                }
            }
        }

        /// <summary>
        /// Draw the keyboard
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(image, position, Color.White);

            for(int f = 0; f < keymapSize; f++)
            {
                if (keymapState[f].IsPressed)
                {
                    spriteBatch.Draw(overlay, keymapPos[f], new Color(200, 200, 0, 25));
                }
            }
        }

        #endregion

        #region Events

        public EventHandler<KeyPressEventArgs> KeyEvent;
        public EventHandler<TouchLocationEventArgs> TouchEvent;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphics">The graphics device to associate textures with.</param>
        /// <param name="image">The image for the keyboard.</param>
        /// <param name="position">Where to draw and offset the keyboard to.</param>
        /// <param name="keyboard">The keyboard to drive this from.</param>
        public KeyboardDriver(GraphicsDevice graphics, Texture2D image, Vector2 position, KeyboardDefinition keyboard)
        {
            this.keyboard = keyboard;
            this.image = image;
            this.position = position;

            keymapSize = keyboard.Definitions.Count;
            keymapPos = new Rectangle[keymapSize];
            keymapState = new KeyState[keymapSize];

            for(int f = 0; f < keymapSize; f++)
            {
                KeyboardKey k = keyboard.Definitions[f];

                keymapPos[f] = new Rectangle(k.X, k.Y, k.Width, k.Height);

                keymapState[f] = new KeyState()
                {
                    Symbol = (T)Enum.Parse(typeof(T), k.KeySymbol, false),
                    IsPressed = false,
                    IsSticky = k.Sticky
                };
            }

            Color c = Color.White;
            overlay = new Texture2D(graphics, 2, 2, false, SurfaceFormat.Color);
            overlay.SetData<Color>(new Color[] {c, c, c, c});
        }

        #endregion
    }
}