Jypeli 10
The simple game programming library
Keyboard.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2013 University of Jyväskylä, Department of Mathematical
4 * Information Technology.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#endregion
25
26/*
27 * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28 */
29
30using System;
31using Jypeli.Controls;
32using Microsoft.Xna.Framework;
33using Microsoft.Xna.Framework.Input;
34
35namespace Jypeli
36{
40 public class Keyboard : Controller<KeyboardState, Key>
41 {
45 public event Action<char> TextInput;
46
47 internal Keyboard()
48 {
49#if DESKTOP
50 Game.Instance.Window.TextInput += delegate( object sender, TextInputEventArgs args )
51 {
52 if ( TextInput != null ) TextInput( args.Character );
53 };
54#endif
55 }
56
57 internal override KeyboardState GetState()
58 {
59 return Microsoft.Xna.Framework.Input.Keyboard.GetState();
60 }
61
62 private ChangePredicate<KeyboardState> MakeTriggerRule( Key k, ButtonState state )
63 {
64 Keys key = (Keys)k;
65
66 switch ( state )
67 {
68 case ButtonState.Up:
69 return delegate( KeyboardState prev, KeyboardState curr ) { return ( curr.IsKeyUp( key ) ); };
70
71 case ButtonState.Down:
72 return delegate( KeyboardState prev, KeyboardState curr ) { return ( curr.IsKeyDown( key ) ); };
73
74 case ButtonState.Pressed:
75 return delegate( KeyboardState prev, KeyboardState curr ) { return ( prev.IsKeyUp( key ) && curr.IsKeyDown( key ) ); };
76
77 case ButtonState.Released:
78 return delegate( KeyboardState prev, KeyboardState curr ) { return ( prev.IsKeyDown( key ) && curr.IsKeyUp( key ) ); };
79 }
80
81 return AlwaysTrigger;
82 }
83
84 private string GetKeyName( Key k )
85 {
86 string keyStr = k.ToString();
87
88 if ( k == Key.OemQuotes ) keyStr = "ä";
89 if ( k == Key.OemTilde ) keyStr = "ö";
90 if ( k == Key.OemPlus || k == Key.Add ) keyStr = "+";
91 if ( k == Key.Subtract ) keyStr = "-";
92 if ( k == Key.Multiply ) keyStr = "*";
93 if ( k == Key.Divide ) keyStr = "/";
94 if ( k == Key.Aring ) keyStr = "å";
95 if ( k == Key.LessOrGreater ) keyStr = "<";
96
97 return "Keyboard " + keyStr;
98 }
99
106 {
107 Keys key = (Keys)k;
108 bool down = CurrentState.IsKeyDown( key );
109 bool lastdown = PrevState.IsKeyDown( key );
110
111 if ( lastdown && down )
112 return ButtonState.Down;
113 if ( !lastdown && down )
114 return ButtonState.Pressed;
115 if ( lastdown && !down )
116 return ButtonState.Released;
117
118 return ButtonState.Up;
119 }
120
127 public bool IsShiftDown()
128 {
129 return CurrentState.IsKeyDown( Keys.LeftShift ) || CurrentState.IsKeyDown( Keys.RightShift );
130 }
131
138 public bool IsCtrlDown()
139 {
140 return CurrentState.IsKeyDown( Keys.LeftControl ) || CurrentState.IsKeyDown( Keys.RightControl );
141 }
142
149 public bool IsAltDown()
150 {
151 return CurrentState.IsKeyDown( Keys.LeftAlt ) || CurrentState.IsKeyDown( Keys.RightAlt );
152 }
153
161 public Listener Listen( Key k, ButtonState state, Action handler, string helpText )
162 {
163 ChangePredicate<KeyboardState> rule = MakeTriggerRule( k, state );
164 return AddListener( rule, k, GetKeyName( k ), helpText, handler );
165 }
166
176 public Listener Listen<T>( Key k, ButtonState state, Action<T> handler, string helpText, T p )
177 {
178 ChangePredicate<KeyboardState> rule = MakeTriggerRule( k, state );
179 return AddListener( rule, k, GetKeyName( k ), helpText, handler, p );
180 }
181
193 public Listener Listen<T1, T2>( Key k, ButtonState state, Action<T1, T2> handler, string helpText, T1 p1, T2 p2 )
194 {
195 ChangePredicate<KeyboardState> rule = MakeTriggerRule( k, state );
196 return AddListener( rule, k, GetKeyName( k ), helpText, handler, p1, p2 );
197 }
198
212 public Listener Listen<T1, T2, T3>( Key k, ButtonState state, Action<T1, T2, T3> handler, string helpText, T1 p1, T2 p2, T3 p3 )
213 {
214 ChangePredicate<KeyboardState> rule = MakeTriggerRule( k, state );
215 return AddListener( rule, k, GetKeyName( k ), helpText, handler, p1, p2, p3 );
216 }
217
233 public Listener Listen<T1, T2, T3, T4>( Key k, ButtonState state, Action<T1, T2, T3, T4> handler, string helpText, T1 p1, T2 p2, T3 p3, T4 p4 )
234 {
235 ChangePredicate<KeyboardState> rule = MakeTriggerRule( k, state );
236 return AddListener( rule, k, GetKeyName( k ), helpText, handler, p1, p2, p3, p4 );
237 }
238
239 #region ListenWSAD
246 public void ListenWSAD( ButtonState state, Action<Vector> handler, String helpText )
247 {
248 Listen( Key.W, state, handler, helpText, Vector.UnitY );
249 Listen( Key.S, state, handler, helpText, -Vector.UnitY );
250 Listen( Key.A, state, handler, helpText, -Vector.UnitX );
251 Listen( Key.D, state, handler, helpText, Vector.UnitX );
252 }
253
262 public void ListenWSAD<T1>( ButtonState state, Action<Vector, T1> handler, String helpText, T1 p1 )
263 {
264 Listen( Key.W, state, handler, helpText, Vector.UnitY, p1 );
265 Listen( Key.S, state, handler, helpText, -Vector.UnitY, p1 );
266 Listen( Key.A, state, handler, helpText, -Vector.UnitX, p1 );
267 Listen( Key.D, state, handler, helpText, Vector.UnitX, p1 );
268 }
269
280 public void ListenWSAD<T1, T2>( ButtonState state, Action<Vector, T1, T2> handler, String helpText, T1 p1, T2 p2 )
281 {
282 Listen( Key.W, state, handler, helpText, Vector.UnitY, p1, p2 );
283 Listen( Key.S, state, handler, helpText, -Vector.UnitY, p1, p2 );
284 Listen( Key.A, state, handler, helpText, -Vector.UnitX, p1, p2 );
285 Listen( Key.D, state, handler, helpText, Vector.UnitX, p1, p2 );
286 }
287
300 public void ListenWSAD<T1, T2, T3>( ButtonState state, Action<Vector, T1, T2, T3> handler, String helpText, T1 p1, T2 p2, T3 p3 )
301 {
302 Listen( Key.W, state, handler, helpText, Vector.UnitY, p1, p2, p3 );
303 Listen( Key.S, state, handler, helpText, -Vector.UnitY, p1, p2, p3 );
304 Listen( Key.A, state, handler, helpText, -Vector.UnitX, p1, p2, p3 );
305 Listen( Key.D, state, handler, helpText, Vector.UnitX, p1, p2, p3 );
306 }
307 #endregion
308
309 #region ListenArrows
316 public void ListenArrows( ButtonState state, Action<Vector> handler, String helpText )
317 {
318 Listen( Key.Up, state, handler, helpText, Vector.UnitY );
319 Listen( Key.Down, state, handler, helpText, -Vector.UnitY );
320 Listen( Key.Left, state, handler, helpText, -Vector.UnitX );
321 Listen( Key.Right, state, handler, helpText, Vector.UnitX );
322 }
323
332 public void ListenArrows<T1>( ButtonState state, Action<Vector, T1> handler, String helpText, T1 p1 )
333 {
334 Listen( Key.Up, state, handler, helpText, Vector.UnitY, p1 );
335 Listen( Key.Down, state, handler, helpText, -Vector.UnitY, p1 );
336 Listen( Key.Left, state, handler, helpText, -Vector.UnitX, p1 );
337 Listen( Key.Right, state, handler, helpText, Vector.UnitX, p1 );
338 }
339
350 public void ListenArrows<T1, T2>( ButtonState state, Action<Vector, T1, T2> handler, String helpText, T1 p1, T2 p2 )
351 {
352 Listen( Key.Up, state, handler, helpText, Vector.UnitY, p1, p2 );
353 Listen( Key.Down, state, handler, helpText, -Vector.UnitY, p1, p2 );
354 Listen( Key.Left, state, handler, helpText, -Vector.UnitX, p1, p2 );
355 Listen( Key.Right, state, handler, helpText, Vector.UnitX, p1, p2 );
356 }
357
370 public void ListenArrows<T1, T2, T3>( ButtonState state, Action<Vector, T1, T2, T3> handler, String helpText, T1 p1, T2 p2, T3 p3 )
371 {
372 Listen( Key.Up, state, handler, helpText, Vector.UnitY, p1, p2, p3 );
373 Listen( Key.Down, state, handler, helpText, -Vector.UnitY, p1, p2, p3 );
374 Listen( Key.Left, state, handler, helpText, -Vector.UnitX, p1, p2, p3 );
375 Listen( Key.Right, state, handler, helpText, Vector.UnitX, p1, p2, p3 );
376 }
377 #endregion
378 }
379}
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:96
Näppäimistö.
Definition: Keyboard.cs:41
Listener Listen(Key k, ButtonState state, Action handler, string helpText)
Kuuntelee näppäinten painalluksia.
Definition: Keyboard.cs:161
Listener Listen< T1, T2, T3 >(Key k, ButtonState state, Action< T1, T2, T3 > handler, string helpText, T1 p1, T2 p2, T3 p3)
Kuuntelee näppäinten painalluksia.
Definition: Keyboard.cs:212
Listener Listen< T1, T2, T3, T4 >(Key k, ButtonState state, Action< T1, T2, T3, T4 > handler, string helpText, T1 p1, T2 p2, T3 p3, T4 p4)
Kuuntelee näppäinten painalluksia.
Definition: Keyboard.cs:233
ChangePredicate< KeyboardState > MakeTriggerRule(Key k, ButtonState state)
Definition: Keyboard.cs:62
Action< char > TextInput
Tapahtuu kun tekstiä syötetään näppäimistöltä.
Definition: Keyboard.cs:45
void ListenArrows(ButtonState state, Action< Vector > handler, String helpText)
Kuuntelee nuolinäppäimiä.
Definition: Keyboard.cs:316
void ListenWSAD(ButtonState state, Action< Vector > handler, String helpText)
Kuuntelee W, S, A ja D -näppäimiä.
Definition: Keyboard.cs:246
bool IsShiftDown()
Tarkistaa, onko kumpikaan shift-näppäimistä painettuna.
Definition: Keyboard.cs:127
override KeyboardState GetState()
Lukee ja palauttaa laitteen viimeisimmän tilan.
Definition: Keyboard.cs:57
void ListenWSAD< T1, T2 >(ButtonState state, Action< Vector, T1, T2 > handler, String helpText, T1 p1, T2 p2)
Kuuntelee W, S, A ja D -näppäimiä.
Definition: Keyboard.cs:280
bool IsAltDown()
Tarkistaa, onko kumpikaan alt-näppäimistä painettuna.
Definition: Keyboard.cs:149
void ListenArrows< T1 >(ButtonState state, Action< Vector, T1 > handler, String helpText, T1 p1)
Kuuntelee nuolinäppäimiä.
Definition: Keyboard.cs:332
Listener Listen< T1, T2 >(Key k, ButtonState state, Action< T1, T2 > handler, string helpText, T1 p1, T2 p2)
Kuuntelee näppäinten painalluksia.
Definition: Keyboard.cs:193
void ListenWSAD< T1, T2, T3 >(ButtonState state, Action< Vector, T1, T2, T3 > handler, String helpText, T1 p1, T2 p2, T3 p3)
Kuuntelee W, S, A ja D -näppäimiä.
Definition: Keyboard.cs:300
Listener Listen< T >(Key k, ButtonState state, Action< T > handler, string helpText, T p)
Kuuntelee näppäinten painalluksia.
Definition: Keyboard.cs:176
string GetKeyName(Key k)
Definition: Keyboard.cs:84
bool IsCtrlDown()
Tarkistaa, onko kumpikaan ctrl-näppäimistä painettuna.
Definition: Keyboard.cs:138
void ListenArrows< T1, T2, T3 >(ButtonState state, Action< Vector, T1, T2, T3 > handler, String helpText, T1 p1, T2 p2, T3 p3)
Kuuntelee nuolinäppäimiä.
Definition: Keyboard.cs:370
void ListenWSAD< T1 >(ButtonState state, Action< Vector, T1 > handler, String helpText, T1 p1)
Kuuntelee W, S, A ja D -näppäimiä.
Definition: Keyboard.cs:262
ButtonState GetKeyState(Key k)
Palauttaa annetun näppäimen tilan (ks. ButtonState).
Definition: Keyboard.cs:105
void ListenArrows< T1, T2 >(ButtonState state, Action< Vector, T1, T2 > handler, String helpText, T1 p1, T2 p2)
Kuuntelee nuolinäppäimiä.
Definition: Keyboard.cs:350
ControllerState CurrentState
Nykyinen tila.
Definition: Controller.cs:81
ControllerState PrevState
Viimeisin tila.
Definition: Controller.cs:76
Listener AddListener(ChangePredicate< ControllerState > rule, Control control, string controlName, string helpText, Delegate handler, params object[] args)
Definition: Controller.cs:102
static readonly ChangePredicate< ControllerState > AlwaysTrigger
Definition: Controller.cs:66
Ohjaintapahtumien kuuntelija.
Definition: Listener.cs:72
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
Key
Näppäimistön näppäin.
Definition: Key.cs:39
2D-vektori.
Definition: Vector.cs:67
static readonly Vector UnitY
Pystysuuntainen yksikkövektori (pituus 1, suunta ylös).
Definition: Vector.cs:86
static readonly Vector UnitX
Vaakasuuntainen yksikkövektori (pituus 1, suunta oikealle).
Definition: Vector.cs:81