1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using System.Windows.Forms; |
---|
6 | using Microsoft.Xna.Framework; |
---|
7 | using Microsoft.Xna.Framework.Input; |
---|
8 | |
---|
9 | using Keys = System.Windows.Forms.Keys; |
---|
10 | |
---|
11 | namespace CastleMaster.Input |
---|
12 | { |
---|
13 | public class InputHandler |
---|
14 | { |
---|
15 | private class Key |
---|
16 | { |
---|
17 | private bool lastState = false, currentState = false, nextState = false; |
---|
18 | |
---|
19 | public bool IsDown { get { return currentState; } } |
---|
20 | |
---|
21 | public bool HasBeenPressed { get { return !lastState && currentState; } } |
---|
22 | |
---|
23 | public void Update() |
---|
24 | { |
---|
25 | lastState = currentState; |
---|
26 | currentState = nextState; |
---|
27 | } |
---|
28 | |
---|
29 | public void Toggle(bool state) |
---|
30 | { |
---|
31 | nextState = state; |
---|
32 | } |
---|
33 | |
---|
34 | public void Release() |
---|
35 | { |
---|
36 | currentState = false; |
---|
37 | nextState = false; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | private static Dictionary<Keys, Key> registeredKeys; |
---|
42 | private static Dictionary<MouseButtons, Key> registeredMouseButtons; |
---|
43 | private static int x, y; |
---|
44 | |
---|
45 | private Control windowControl; |
---|
46 | |
---|
47 | public InputHandler(GameWindow window) |
---|
48 | { |
---|
49 | registeredKeys = new Dictionary<Keys, Key>(); |
---|
50 | registeredMouseButtons = new Dictionary<MouseButtons, Key>(); |
---|
51 | |
---|
52 | windowControl = Control.FromHandle(window.Handle); |
---|
53 | windowControl.PreviewKeyDown += new PreviewKeyDownEventHandler(OnKeyDown); |
---|
54 | windowControl.KeyUp += new KeyEventHandler(OnKeyUp); |
---|
55 | windowControl.MouseDown += new MouseEventHandler(OnMouseDown); |
---|
56 | windowControl.MouseUp += new MouseEventHandler(OnMouseUp); |
---|
57 | } |
---|
58 | |
---|
59 | private void OnMouseUp(object sender, MouseEventArgs e) |
---|
60 | { |
---|
61 | Key k; |
---|
62 | if (registeredMouseButtons.TryGetValue(e.Button, out k)) k.Toggle(false); |
---|
63 | } |
---|
64 | |
---|
65 | private void OnMouseDown(object sender, MouseEventArgs e) |
---|
66 | { |
---|
67 | Key k; |
---|
68 | if (registeredMouseButtons.TryGetValue(e.Button, out k)) k.Toggle(true); |
---|
69 | } |
---|
70 | |
---|
71 | private void OnKeyUp(object sender, KeyEventArgs e) |
---|
72 | { |
---|
73 | Key k; |
---|
74 | if (registeredKeys.TryGetValue(e.KeyCode, out k)) k.Toggle(false); |
---|
75 | } |
---|
76 | |
---|
77 | private void OnKeyDown(object sender, PreviewKeyDownEventArgs e) |
---|
78 | { |
---|
79 | Key k; |
---|
80 | if (registeredKeys.TryGetValue(e.KeyCode, out k)) k.Toggle(true); |
---|
81 | } |
---|
82 | |
---|
83 | public void Update() |
---|
84 | { |
---|
85 | foreach (Key k in registeredKeys.Values) |
---|
86 | k.Update(); |
---|
87 | |
---|
88 | MouseState state = Mouse.GetState(); |
---|
89 | x = state.X; |
---|
90 | y = state.Y; |
---|
91 | |
---|
92 | foreach (Key k in registeredMouseButtons.Values) |
---|
93 | k.Update(); |
---|
94 | } |
---|
95 | |
---|
96 | public void RegisterKeyboardKey(Keys key) |
---|
97 | { |
---|
98 | registeredKeys.Add(key, new Key()); |
---|
99 | } |
---|
100 | |
---|
101 | public void RegisterMouseKey(MouseButtons mb) |
---|
102 | { |
---|
103 | registeredMouseButtons.Add(mb, new Key()); |
---|
104 | } |
---|
105 | |
---|
106 | public void ReleaseAll() |
---|
107 | { |
---|
108 | foreach (Key k in registeredKeys.Values) |
---|
109 | k.Release(); |
---|
110 | |
---|
111 | foreach (Key k in registeredMouseButtons.Values) |
---|
112 | k.Release(); |
---|
113 | } |
---|
114 | |
---|
115 | public static bool IsKeyDown(Keys key) |
---|
116 | { |
---|
117 | Key k; |
---|
118 | return registeredKeys.TryGetValue(key, out k) && k.IsDown; |
---|
119 | } |
---|
120 | |
---|
121 | public static bool HasKeyBeenPressed(Keys key) |
---|
122 | { |
---|
123 | Key k; |
---|
124 | return registeredKeys.TryGetValue(key, out k) && k.HasBeenPressed; |
---|
125 | } |
---|
126 | |
---|
127 | public static bool IsMouseButtonDown(MouseButtons mb) |
---|
128 | { |
---|
129 | Key k; |
---|
130 | return registeredMouseButtons.TryGetValue(mb, out k) && k.IsDown; |
---|
131 | } |
---|
132 | |
---|
133 | public static bool HasMouseButtonBeenPressed(MouseButtons mb) |
---|
134 | { |
---|
135 | Key k; |
---|
136 | return registeredMouseButtons.TryGetValue(mb, out k) && k.HasBeenPressed; |
---|
137 | } |
---|
138 | |
---|
139 | public static int MouseX { get { return x; } } |
---|
140 | |
---|
141 | public static int MouseY { get { return y; } } |
---|
142 | } |
---|
143 | } |
---|