Jypeli 10
The simple game programming library
GamePad.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2009 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;
33using Microsoft.Xna.Framework;
34using Microsoft.Xna.Framework.Input;
35
36using XnaGamePad = Microsoft.Xna.Framework.Input.GamePad;
37
38namespace Jypeli
39{
40 public class GamePad : Controller<GamePadState, Enum>
41 {
42 private PlayerIndex playerIndex;
44
50 {
51 get
52 {
53 Vector2 v = CurrentState.ThumbSticks.Left;
54 return new Vector( v.X, v.Y );
55 }
56 }
57
63 {
64 get
65 {
66 Vector2 v = CurrentState.ThumbSticks.Right;
67 return new Vector( v.X, v.Y );
68 }
69 }
70
75 public double LeftTriggerState
76 {
77 get { return CurrentState.Triggers.Left; }
78 }
79
84 public double RightTriggerState
85 {
86 get { return CurrentState.Triggers.Right; }
87 }
88
93 {
94 get
95 {
96 Vector2 v = CurrentState.ThumbSticks.Left - PrevState.ThumbSticks.Left;
97 return new Vector( v.X, v.Y );
98 }
99 }
100
105 {
106 get
107 {
108 Vector2 v = CurrentState.ThumbSticks.Right - PrevState.ThumbSticks.Right;
109 return new Vector( v.X, v.Y );
110 }
111 }
112
116 public double LeftTriggerChange
117 {
118 get { return CurrentState.Triggers.Left - PrevState.Triggers.Left; }
119 }
120
124 public double RightTriggerChange
125 {
126 get { return CurrentState.Triggers.Right - PrevState.Triggers.Left; }
127 }
128
129 internal GamePad( PlayerIndex index )
130 {
131 playerIndex = index;
133 }
134
135 internal override GamePadState GetState()
136 {
137 return XnaGamePad.GetState( playerIndex );
138 }
139
140 private ChangePredicate<GamePadState> MakeTriggerRule( Button b, ButtonState state )
141 {
142 Buttons buttons = (Buttons)b;
143
144 switch ( state )
145 {
146 case ButtonState.Up:
147 return delegate( GamePadState prev, GamePadState curr ) { return ( curr.IsButtonUp( buttons ) ); };
148
149 case ButtonState.Down:
150 return delegate( GamePadState prev, GamePadState curr ) { return ( curr.IsButtonDown( buttons ) ); };
151
152 case ButtonState.Pressed:
153 return delegate( GamePadState prev, GamePadState curr ) { return ( prev.IsButtonUp( buttons ) && curr.IsButtonDown( buttons ) ); };
154
155 case ButtonState.Released:
156 return delegate( GamePadState prev, GamePadState curr ) { return ( prev.IsButtonDown( buttons ) && curr.IsButtonUp( buttons ) ); };
157 }
158
159 return AlwaysTrigger;
160 }
161
162 private ChangePredicate<GamePadState> MakeTriggerRule( AnalogControl control, double moveTrigger )
163 {
164 switch ( control )
165 {
166 case AnalogControl.LeftStick:
167 return delegate( GamePadState prev, GamePadState curr )
168 {
169 double xdist = curr.ThumbSticks.Left.X - prev.ThumbSticks.Left.X;
170 double ydist = curr.ThumbSticks.Left.Y - prev.ThumbSticks.Left.Y;
171 return xdist * xdist + ydist * ydist > moveTrigger * moveTrigger;
172 };
173
174 case AnalogControl.RightStick:
175 return delegate( GamePadState prev, GamePadState curr )
176 {
177 double xdist = curr.ThumbSticks.Right.X - prev.ThumbSticks.Right.X;
178 double ydist = curr.ThumbSticks.Right.Y - prev.ThumbSticks.Right.Y;
179 return xdist * xdist + ydist * ydist > moveTrigger * moveTrigger;
180 };
181
182 case AnalogControl.LeftTrigger:
183 return delegate( GamePadState prev, GamePadState curr )
184 {
185 return Math.Abs( curr.Triggers.Left - prev.Triggers.Left ) > moveTrigger;
186 };
187
188 case AnalogControl.RightTrigger:
189 return delegate( GamePadState prev, GamePadState curr )
190 {
191 return Math.Abs( curr.Triggers.Right - prev.Triggers.Right ) > moveTrigger;
192 };
193 }
194
195 throw new ArgumentException( control.ToString() + " is not a valid analog control for a GamePad" );
196 }
197
198 private string GetButtonName( Button b )
199 {
200 return String.Format( "GamePad{0} {1}", playerIndex.ToString(), b.ToString() );
201 }
202
203 private string GetAnalogName( AnalogControl a )
204 {
205 return String.Format( "GamePad{0} {1}", playerIndex.ToString(), a.ToString() );
206 }
207
216 public void Vibrate( double leftMotor, double rightMotor, double leftAcceleration, double rightAcceleration, double time )
217 {
218 vibrations.Add( new Vibration( leftMotor, rightMotor, leftAcceleration, rightAcceleration, time ) );
219 }
220
224 public void StopVibration()
225 {
226 vibrations.Clear();
227 }
228
229 internal void UpdateVibrations( Time time )
230 {
231 vibrations.Update( time );
233 }
234
242 public Listener Listen( Button button, ButtonState state, Action handler, string helpText )
243 {
244 ChangePredicate<GamePadState> rule = MakeTriggerRule( button, state );
245 return AddListener( rule, button, GetButtonName( button ), helpText, handler );
246 }
247
257 public Listener Listen<T>( Button button, ButtonState state, Action<T> handler, string helpText, T p )
258 {
259 ChangePredicate<GamePadState> rule = MakeTriggerRule( button, state );
260 return AddListener( rule, button, GetButtonName( button ), helpText, handler, p );
261 }
262
274 public Listener Listen<T1, T2>( Button button, ButtonState state, Action<T1, T2> handler, string helpText, T1 p1, T2 p2 )
275 {
276 ChangePredicate<GamePadState> rule = MakeTriggerRule( button, state );
277 return AddListener( rule, button, GetButtonName( button ), helpText, handler, p1, p2 );
278 }
279
293 public Listener Listen<T1, T2, T3>( Button button, ButtonState state, Action<T1, T2, T3> handler, string helpText, T1 p1, T2 p2, T3 p3 )
294 {
295 ChangePredicate<GamePadState> rule = MakeTriggerRule( button, state );
296 return AddListener( rule, button, GetButtonName( button ), helpText, handler, p1, p2, p3 );
297 }
298
307 public Listener ListenAnalog( AnalogControl control, double trigger, Action<AnalogState> handler, string helpText )
308 {
309 ChangePredicate<GamePadState> rule = MakeTriggerRule( control, trigger );
310 Action analogHandler = delegate { handler(GenerateAnalogState(control)); };
311 return AddListener( rule, control, GetAnalogName( control ), helpText, analogHandler );
312 }
313
315 {
316 switch (control)
317 {
318 case AnalogControl.LeftStick:
320 case AnalogControl.RightStick:
322 default:
323 throw new NotImplementedException("Unsupported Controller / GamePad control for ListenAnalog: " + control.ToString());
324 }
325 }
326
337 public Listener ListenAnalog<T>( AnalogControl control, double trigger, Action<AnalogState, T> handler, string helpText, T p )
338 {
339 ChangePredicate<GamePadState> rule = MakeTriggerRule( control, trigger );
340 Action analogHandler = delegate { handler(GenerateAnalogState(control), p); };
341 return AddListener( rule, control, GetAnalogName( control ), helpText, analogHandler);
342 }
343
356 public Listener ListenAnalog<T1, T2>( AnalogControl control, double trigger, Action<AnalogState, T1, T2> handler, string helpText, T1 p1, T2 p2 )
357 {
358 ChangePredicate<GamePadState> rule = MakeTriggerRule( control, trigger );
359 Action analogHandler = delegate { handler(GenerateAnalogState(control), p1, p2); };
360 return AddListener( rule, control, GetAnalogName( control ), helpText, analogHandler);
361 }
362
377 public Listener ListenAnalog<T1, T2, T3>( AnalogControl control, double trigger, Action<AnalogState, T1, T2, T3> handler, string helpText, T1 p1, T2 p2, T3 p3 )
378 {
379 ChangePredicate<GamePadState> rule = MakeTriggerRule( control, trigger );
380 Action analogHandler = delegate { handler(GenerateAnalogState(control), p1, p2, p3); };
381 return AddListener( rule, control, GetAnalogName( control ), helpText, analogHandler);
382 }
383 }
384}
Microsoft.Xna.Framework.Input.GamePad XnaGamePad
Definition: BackButton.cs:6
System.Numerics.Vector2 Vector2
static void Execute(PlayerIndex p, IEnumerable< Vibration > vibrations)
Definition: Vibration.cs:42
Listener ListenAnalog< T >(AnalogControl control, double trigger, Action< AnalogState, T > handler, string helpText, T p)
Kuuntelee analogisen kontrollin (tatin tai liipaisimen) liikettä.
Definition: GamePad.cs:337
double LeftTriggerState
Vasemman liipaisimen tila. Vaihtelee välillä 0 - 1.
Definition: GamePad.cs:76
string GetAnalogName(AnalogControl a)
Definition: GamePad.cs:203
Listener Listen< T1, T2, T3 >(Button button, ButtonState state, Action< T1, T2, T3 > handler, string helpText, T1 p1, T2 p2, T3 p3)
Kuuntelee peliohjaimen nappulan painalluksia.
Definition: GamePad.cs:293
GamePadAnalogState GenerateAnalogState(AnalogControl control)
Definition: GamePad.cs:314
double LeftTriggerChange
Vasemman liipaisimen tilan viimeisin muutos.
Definition: GamePad.cs:117
Vector LeftThumbDirection
Vasemman tatin suuntavektori. Vaihtelee välillä (-1, -1) - (1, 1)
Definition: GamePad.cs:50
SynchronousList< Vibration > vibrations
Definition: GamePad.cs:43
Vector RightThumbChange
Oikean tatin suuntavektorin viimeisin muutos (liike).
Definition: GamePad.cs:105
Listener ListenAnalog< T1, T2, T3 >(AnalogControl control, double trigger, Action< AnalogState, T1, T2, T3 > handler, string helpText, T1 p1, T2 p2, T3 p3)
Kuuntelee analogisen kontrollin (tatin tai liipaisimen) liikettä.
Definition: GamePad.cs:377
void UpdateVibrations(Time time)
Definition: GamePad.cs:229
string GetButtonName(Button b)
Definition: GamePad.cs:198
void StopVibration()
Lopettaa täristyksen.
Definition: GamePad.cs:224
Vector LeftThumbChange
Vasemman tatin suuntavektorin viimeisin muutos (liike).
Definition: GamePad.cs:93
override GamePadState GetState()
Lukee ja palauttaa laitteen viimeisimmän tilan.
Definition: GamePad.cs:135
Listener Listen(Button button, ButtonState state, Action handler, string helpText)
Kuuntelee peliohjaimen nappulan painalluksia.
Definition: GamePad.cs:242
double RightTriggerState
Oikean liipaisimen tila. Vaihtelee välillä 0 - 1.
Definition: GamePad.cs:85
Listener ListenAnalog(AnalogControl control, double trigger, Action< AnalogState > handler, string helpText)
Kuuntelee analogisen kontrollin (tatin tai liipaisimen) liikettä.
Definition: GamePad.cs:307
Listener Listen< T >(Button button, ButtonState state, Action< T > handler, string helpText, T p)
Kuuntelee peliohjaimen nappulan painalluksia.
Definition: GamePad.cs:257
Listener ListenAnalog< T1, T2 >(AnalogControl control, double trigger, Action< AnalogState, T1, T2 > handler, string helpText, T1 p1, T2 p2)
Kuuntelee analogisen kontrollin (tatin tai liipaisimen) liikettä.
Definition: GamePad.cs:356
void Vibrate(double leftMotor, double rightMotor, double leftAcceleration, double rightAcceleration, double time)
Täristää peliohjainta.
Definition: GamePad.cs:216
Vector RightThumbDirection
Oikean tatin suuntavektori. Vaihtelee välillä (-1, -1) - (1, 1)
Definition: GamePad.cs:63
GamePad(PlayerIndex index)
Definition: GamePad.cs:129
Listener Listen< T1, T2 >(Button button, ButtonState state, Action< T1, T2 > handler, string helpText, T1 p1, T2 p2)
Kuuntelee peliohjaimen nappulan painalluksia.
Definition: GamePad.cs:274
double RightTriggerChange
Oikean liipaisimen tilan viimeisin muutos.
Definition: GamePad.cs:125
PlayerIndex playerIndex
Definition: GamePad.cs:42
ChangePredicate< GamePadState > MakeTriggerRule(Button b, ButtonState state)
Definition: GamePad.cs:140
ChangePredicate< GamePadState > MakeTriggerRule(AnalogControl control, double moveTrigger)
Definition: GamePad.cs:162
Synkroninen lista, eli lista joka päivittyy vasta kun sen Update-metodia kutsutaan....
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
Button
Definition: Button.cs:36
AnalogControl
Analoginen ohjain. Tämä voi olla joko painike, jota voi painaa eri voimakkuuksilla (padiohjaimen liip...
Definition: AnalogControl.cs:9
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
2D-vektori.
Definition: Vector.cs:67
double Magnitude
Vektorin pituus.
Definition: Vector.cs:345