Jypeli  9
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 
30 using System;
31 using Jypeli.Controls;
32 using Jypeli.Controls.GamePad;
33 using Microsoft.Xna.Framework;
34 using Microsoft.Xna.Framework.Input;
35 
36 using XnaGamePad = Microsoft.Xna.Framework.Input.GamePad;
37 
38 namespace 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 }
Jypeli.Controls.GamePad.Vibration.Execute
static void Execute(PlayerIndex p, IEnumerable< Vibration > vibrations)
Definition: Vibration.cs:42
Jypeli.GamePad.Vibrate
void Vibrate(double leftMotor, double rightMotor, double leftAcceleration, double rightAcceleration, double time)
Täristää peliohjainta.
Definition: GamePad.cs:216
Jypeli.AnalogControl
AnalogControl
Analoginen ohjain. Tämä voi olla joko painike, jota voi painaa eri voimakkuuksilla (padiohjaimen liip...
Definition: AnalogControl.cs:9
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.GamePad.GetButtonName
string GetButtonName(Button b)
Definition: GamePad.cs:198
Jypeli.Listener
Ohjaintapahtumien kuuntelija.
Definition: Listener.cs:50
Jypeli.GamePad.MakeTriggerRule
ChangePredicate< GamePadState > MakeTriggerRule(AnalogControl control, double moveTrigger)
Definition: GamePad.cs:162
Jypeli.GamePad.ListenAnalog< T1, T2, T3 >
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
Jypeli.GamePad.RightThumbDirection
Vector RightThumbDirection
Oikean tatin suuntavektori. Vaihtelee välillä (-1, -1) - (1, 1)
Definition: GamePad.cs:63
Jypeli
Definition: Automobile.cs:5
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.GamePad.LeftTriggerChange
double LeftTriggerChange
Vasemman liipaisimen tilan viimeisin muutos.
Definition: GamePad.cs:117
Jypeli.Controls.GamePad.Vibration
Definition: Vibration.cs:8
Jypeli.Controls.GamePad.GamePadAnalogState
Definition: GamePadAnalogState.cs:4
Jypeli.GamePad.vibrations
SynchronousList< Vibration > vibrations
Definition: GamePad.cs:43
Jypeli.SynchronousList.Add
void Add(T item)
Definition: SynchronousList.cs:197
Jypeli.GamePad.Listen< T1, T2, T3 >
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
Jypeli.GamePad.LeftTriggerState
double LeftTriggerState
Vasemman liipaisimen tila. Vaihtelee välillä 0 - 1.
Definition: GamePad.cs:76
Jypeli.GamePad.MakeTriggerRule
ChangePredicate< GamePadState > MakeTriggerRule(Button b, ButtonState state)
Definition: GamePad.cs:140
Jypeli.Controls.GamePad
Definition: GamePadAnalogState.cs:2
Jypeli.GamePad.GetAnalogName
string GetAnalogName(AnalogControl a)
Definition: GamePad.cs:203
Jypeli.GamePad.LeftThumbDirection
Vector LeftThumbDirection
Vasemman tatin suuntavektori. Vaihtelee välillä (-1, -1) - (1, 1)
Definition: GamePad.cs:50
Jypeli.GamePad.GenerateAnalogState
GamePadAnalogState GenerateAnalogState(AnalogControl control)
Definition: GamePad.cs:314
Jypeli.GamePad.playerIndex
PlayerIndex playerIndex
Definition: GamePad.cs:42
Jypeli.SynchronousList.Clear
void Clear()
Definition: SynchronousList.cs:207
Jypeli.GamePad
Definition: GamePad.cs:41
Jypeli.Time
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
Jypeli.GamePad.GetState
override GamePadState GetState()
Definition: GamePad.cs:135
XnaGamePad
Microsoft.Xna.Framework.Input.GamePad XnaGamePad
Definition: BackButton.cs:6
Jypeli.Controls
Definition: Controller.cs:34
Jypeli.GamePad.ListenAnalog< T >
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
Jypeli.GamePad.RightTriggerState
double RightTriggerState
Oikean liipaisimen tila. Vaihtelee välillä 0 - 1.
Definition: GamePad.cs:85
Jypeli.Button
Button
Definition: Button.cs:35
Jypeli.GamePad.Listen< T1, T2 >
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
Jypeli.Vector.Magnitude
double Magnitude
Vektorin pituus.
Definition: Vector.cs:319
Jypeli.ButtonState
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.SynchronousList< Vibration >
Jypeli.GamePad.ListenAnalog< T1, T2 >
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
Jypeli.GamePad.LeftThumbChange
Vector LeftThumbChange
Vasemman tatin suuntavektorin viimeisin muutos (liike).
Definition: GamePad.cs:93
Jypeli.GamePad.RightThumbChange
Vector RightThumbChange
Oikean tatin suuntavektorin viimeisin muutos (liike).
Definition: GamePad.cs:105
Jypeli.SynchronousList.Update
void Update(Time time)
Lisää ja poistaa jonossa olevat elementit sekä kutsuu niiden Update-metodia.
Definition: SynchronousList.cs:272
Jypeli.GamePad.RightTriggerChange
double RightTriggerChange
Oikean liipaisimen tilan viimeisin muutos.
Definition: GamePad.cs:125
Jypeli.GamePad.UpdateVibrations
void UpdateVibrations(Time time)
Definition: GamePad.cs:229
Jypeli.Controls.Controller
Ohjainlaite.
Definition: Controller.cs:39
Jypeli.GamePad.StopVibration
void StopVibration()
Lopettaa täristyksen.
Definition: GamePad.cs:224
Jypeli.GamePad.GamePad
GamePad(PlayerIndex index)
Definition: GamePad.cs:129
Jypeli.GamePad.Listen
Listener Listen(Button button, ButtonState state, Action handler, string helpText)
Kuuntelee peliohjaimen nappulan painalluksia.
Definition: GamePad.cs:242
Jypeli.GamePad.Listen< T >
Listener Listen< T >(Button button, ButtonState state, Action< T > handler, string helpText, T p)
Kuuntelee peliohjaimen nappulan painalluksia.
Definition: GamePad.cs:257
Jypeli.GamePad.ListenAnalog
Listener ListenAnalog(AnalogControl control, double trigger, Action< AnalogState > handler, string helpText)
Kuuntelee analogisen kontrollin (tatin tai liipaisimen) liikettä.
Definition: GamePad.cs:307