VirtualGamePad.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using Microsoft.Xna.Framework.Input.Touch;
  6. namespace Platformer2D
  7. {
  8. class VirtualGamePad
  9. {
  10. private readonly Vector2 baseScreenSize;
  11. private Matrix globalTransformation;
  12. private readonly Texture2D texture;
  13. private float secondsSinceLastInput;
  14. private float opacity;
  15. public VirtualGamePad(Vector2 baseScreenSize, Matrix globalTransformation, Texture2D texture)
  16. {
  17. this.baseScreenSize = baseScreenSize;
  18. this.globalTransformation = Matrix.Invert(globalTransformation);
  19. this.texture = texture;
  20. secondsSinceLastInput = float.MaxValue;
  21. }
  22. public void NotifyPlayerIsMoving()
  23. {
  24. secondsSinceLastInput = 0;
  25. }
  26. public void Update(GameTime gameTime)
  27. {
  28. var secondsElapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  29. secondsSinceLastInput += secondsElapsed;
  30. //If the player is moving, fade the controls out
  31. // otherwise, if they haven't moved in 4 seconds, fade the controls back in
  32. if (secondsSinceLastInput < 4)
  33. opacity = Math.Max(0, opacity - secondsElapsed * 4);
  34. else
  35. opacity = Math.Min(1, opacity + secondsElapsed * 2);
  36. }
  37. public void Draw(SpriteBatch spriteBatch)
  38. {
  39. var spriteCenter = new Vector2(64, 64);
  40. var color = Color.Multiply(Color.White, opacity);
  41. spriteBatch.Draw(texture, new Vector2(64, baseScreenSize.Y - 64), null, color, -MathHelper.PiOver2, spriteCenter, 1, SpriteEffects.None, 0);
  42. spriteBatch.Draw(texture, new Vector2(192, baseScreenSize.Y - 64), null, color, MathHelper.PiOver2, spriteCenter, 1, SpriteEffects.None, 0);
  43. spriteBatch.Draw(texture, new Vector2(baseScreenSize.X - 128, baseScreenSize.Y - 128), null, color, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
  44. }
  45. /// <summary>
  46. /// Generates a GamePadState based on the touch input provided (as applied to the on screen controls) and the gamepad state
  47. /// </summary>
  48. public GamePadState GetState(TouchCollection touchState, GamePadState gpState)
  49. {
  50. //Work out what buttons are pressed based on the touchState
  51. Buttons buttonsPressed = 0;
  52. foreach (var touch in touchState)
  53. {
  54. if (touch.State == TouchLocationState.Moved || touch.State == TouchLocationState.Pressed)
  55. {
  56. //Scale the touch position to be in _baseScreenSize coordinates
  57. Vector2 pos = touch.Position;
  58. Vector2.Transform(ref pos, ref globalTransformation, out pos);
  59. if (pos.X < 128)
  60. buttonsPressed |= Buttons.DPadLeft;
  61. else if (pos.X < 256)
  62. buttonsPressed |= Buttons.DPadRight;
  63. else if (pos.X >= baseScreenSize.X - 128)
  64. buttonsPressed |= Buttons.A;
  65. }
  66. }
  67. //Combine the buttons of the real gamepad
  68. var gpButtons = gpState.Buttons;
  69. buttonsPressed |= (gpButtons.A == ButtonState.Pressed ? Buttons.A : 0);
  70. buttonsPressed |= (gpButtons.B == ButtonState.Pressed ? Buttons.B : 0);
  71. buttonsPressed |= (gpButtons.X == ButtonState.Pressed ? Buttons.X : 0);
  72. buttonsPressed |= (gpButtons.Y == ButtonState.Pressed ? Buttons.Y : 0);
  73. buttonsPressed |= (gpButtons.Start == ButtonState.Pressed ? Buttons.Start : 0);
  74. buttonsPressed |= (gpButtons.Back == ButtonState.Pressed ? Buttons.Back : 0);
  75. buttonsPressed |= gpState.IsButtonDown(Buttons.DPadDown) ? Buttons.DPadDown : 0;
  76. buttonsPressed |= gpState.IsButtonDown(Buttons.DPadLeft) ? Buttons.DPadLeft : 0;
  77. buttonsPressed |= gpState.IsButtonDown(Buttons.DPadRight) ? Buttons.DPadRight : 0;
  78. buttonsPressed |= gpState.IsButtonDown(Buttons.DPadUp) ? Buttons.DPadUp : 0;
  79. buttonsPressed |= (gpButtons.BigButton == ButtonState.Pressed ? Buttons.BigButton : 0);
  80. buttonsPressed |= (gpButtons.LeftShoulder == ButtonState.Pressed ? Buttons.LeftShoulder : 0);
  81. buttonsPressed |= (gpButtons.RightShoulder == ButtonState.Pressed ? Buttons.RightShoulder : 0);
  82. buttonsPressed |= (gpButtons.LeftStick == ButtonState.Pressed ? Buttons.LeftStick : 0);
  83. buttonsPressed |= (gpButtons.RightStick == ButtonState.Pressed ? Buttons.RightStick : 0);
  84. var buttons = new GamePadButtons(buttonsPressed);
  85. return new GamePadState(gpState.ThumbSticks, gpState.Triggers, buttons, gpState.DPad);
  86. }
  87. }
  88. }