2 using Microsoft
.Xna
.Framework
;
3 using Microsoft
.Xna
.Framework
.Graphics
;
4 using Microsoft
.Xna
.Framework
.Input
;
5 using Microsoft
.Xna
.Framework
.Input
.Touch
;
11 private readonly Vector2 baseScreenSize
;
12 private Matrix globalTransformation
;
13 private readonly Texture2D texture
;
15 private float secondsSinceLastInput
;
16 private float opacity
;
18 public VirtualGamePad(Vector2 baseScreenSize
, Matrix globalTransformation
, Texture2D texture
)
20 this.baseScreenSize
= baseScreenSize
;
21 this.globalTransformation
= Matrix
.Invert(globalTransformation
);
22 this.texture
= texture
;
23 secondsSinceLastInput
= float.MaxValue
;
26 public void NotifyPlayerIsMoving()
28 secondsSinceLastInput
= 0;
31 public void Update(GameTime gameTime
)
33 var secondsElapsed
= (float)gameTime
.ElapsedGameTime
.TotalSeconds
;
34 secondsSinceLastInput
+= secondsElapsed
;
36 //If the player is moving, fade the controls out
37 // otherwise, if they haven't moved in 4 seconds, fade the controls back in
38 if (secondsSinceLastInput
< 4)
39 opacity
= Math
.Max(0, opacity
- secondsElapsed
* 4);
41 opacity
= Math
.Min(1, opacity
+ secondsElapsed
* 2);
44 public void Draw(SpriteBatch spriteBatch
)
46 var spriteCenter
= new Vector2(64, 64);
47 var color
= Color
.Multiply(Color
.White
, opacity
);
49 spriteBatch
.Draw(texture
, new Vector2(64, baseScreenSize
.Y
- 64), null, color
, -MathHelper
.PiOver2
, spriteCenter
, 1, SpriteEffects
.None
, 0);
50 spriteBatch
.Draw(texture
, new Vector2(192, baseScreenSize
.Y
- 64), null, color
, MathHelper
.PiOver2
, spriteCenter
, 1, SpriteEffects
.None
, 0);
51 spriteBatch
.Draw(texture
, new Vector2(baseScreenSize
.X
- 128, baseScreenSize
.Y
- 128), null, color
, 0, Vector2
.Zero
, 1, SpriteEffects
.None
, 0);
55 /// Generates a GamePadState based on the touch input provided (as applied to the on screen controls) and the gamepad state
57 public GamePadState
GetState(TouchCollection touchState
, GamePadState gpState
)
59 //Work out what buttons are pressed based on the touchState
60 Buttons buttonsPressed
= 0;
62 foreach (var touch
in touchState
)
64 if (touch
.State
== TouchLocationState
.Moved
|| touch
.State
== TouchLocationState
.Pressed
)
66 //Scale the touch position to be in _baseScreenSize coordinates
67 Vector2 pos
= touch
.Position
;
68 Vector2
.Transform(ref pos
, ref globalTransformation
, out pos
);
71 buttonsPressed
|= Buttons
.DPadLeft
;
73 buttonsPressed
|= Buttons
.DPadRight
;
74 else if (pos
.X
>= baseScreenSize
.X
- 128)
75 buttonsPressed
|= Buttons
.A
;
79 //Combine the buttons of the real gamepad
80 var gpButtons
= gpState
.Buttons
;
81 buttonsPressed
|= (gpButtons
.A
== ButtonState
.Pressed
? Buttons
.A
: 0);
82 buttonsPressed
|= (gpButtons
.B
== ButtonState
.Pressed
? Buttons
.B
: 0);
83 buttonsPressed
|= (gpButtons
.X
== ButtonState
.Pressed
? Buttons
.X
: 0);
84 buttonsPressed
|= (gpButtons
.Y
== ButtonState
.Pressed
? Buttons
.Y
: 0);
86 buttonsPressed
|= (gpButtons
.Start
== ButtonState
.Pressed
? Buttons
.Start
: 0);
87 buttonsPressed
|= (gpButtons
.Back
== ButtonState
.Pressed
? Buttons
.Back
: 0);
89 buttonsPressed
|= gpState
.IsButtonDown(Buttons
.DPadDown
) ? Buttons
.DPadDown
: 0;
90 buttonsPressed
|= gpState
.IsButtonDown(Buttons
.DPadLeft
) ? Buttons
.DPadLeft
: 0;
91 buttonsPressed
|= gpState
.IsButtonDown(Buttons
.DPadRight
) ? Buttons
.DPadRight
: 0;
92 buttonsPressed
|= gpState
.IsButtonDown(Buttons
.DPadUp
) ? Buttons
.DPadUp
: 0;
94 buttonsPressed
|= (gpButtons
.BigButton
== ButtonState
.Pressed
? Buttons
.BigButton
: 0);
95 buttonsPressed
|= (gpButtons
.LeftShoulder
== ButtonState
.Pressed
? Buttons
.LeftShoulder
: 0);
96 buttonsPressed
|= (gpButtons
.RightShoulder
== ButtonState
.Pressed
? Buttons
.RightShoulder
: 0);
98 buttonsPressed
|= (gpButtons
.LeftStick
== ButtonState
.Pressed
? Buttons
.LeftStick
: 0);
99 buttonsPressed
|= (gpButtons
.RightStick
== ButtonState
.Pressed
? Buttons
.RightStick
: 0);
101 var buttons
= new GamePadButtons(buttonsPressed
);
103 return new GamePadState(gpState
.ThumbSticks
, gpState
.Triggers
, buttons
, gpState
.DPad
);