InputHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //-----------------------------------------------------------------------------
  2. // InputHelper.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. using GameStateManagement;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. namespace Blackjack
  14. {
  15. /// <summary>
  16. /// Used to simulate a cursor on the Xbox.
  17. /// </summary>
  18. public class InputHelper : DrawableGameComponent
  19. {
  20. //public static Game Game;
  21. //static InputHelper instance;
  22. public bool IsEscape;
  23. public bool IsPressed;
  24. Vector2 drawPosition;
  25. private ScreenManager screenManager;
  26. Texture2D texture;
  27. SpriteBatch spriteBatch;
  28. float maxVelocity;
  29. public InputHelper(ScreenManager screenManager)
  30. : base(screenManager.Game)
  31. {
  32. this.screenManager = screenManager;
  33. texture = screenManager.Game.Content.Load<Texture2D>(Path.Combine("Images", "GamePadCursor"));
  34. spriteBatch = screenManager.SpriteBatch;
  35. maxVelocity = (float)(screenManager.Game.GraphicsDevice.Viewport.Width +
  36. screenManager.Game.GraphicsDevice.Viewport.Height) / 3000f;
  37. drawPosition = new Vector2(screenManager.Game.GraphicsDevice.Viewport.Width / 2,
  38. screenManager.Game.GraphicsDevice.Viewport.Height / 2);
  39. }
  40. //public static InputHelper Instance
  41. //{
  42. // get
  43. // {
  44. // if (instance == null)
  45. // instance = new InputHelper();
  46. // return instance;
  47. // }
  48. //}
  49. public Vector2 PointPosition
  50. {
  51. get
  52. {
  53. return drawPosition + new Vector2(texture.Width / 2f,
  54. texture.Height / 2f);
  55. }
  56. }
  57. /// <summary>
  58. /// Updates itself.
  59. /// </summary>
  60. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  61. public override void Update(GameTime gameTime)
  62. {
  63. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  64. IsPressed = gamePadState.Buttons.A == ButtonState.Pressed;
  65. IsEscape = gamePadState.Buttons.Back == ButtonState.Pressed;
  66. drawPosition += gamePadState.ThumbSticks.Left
  67. * new Vector2(1, -1)
  68. * gameTime.ElapsedGameTime.Milliseconds
  69. * maxVelocity;
  70. drawPosition = Vector2.Clamp(drawPosition, Vector2.Zero,
  71. new Vector2(Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height)
  72. - new Vector2(texture.Bounds.Width, texture.Bounds.Height));
  73. }
  74. /// <summary>
  75. /// Draws cursor.
  76. /// </summary>
  77. public override void Draw(GameTime gameTime)
  78. {
  79. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screenManager.GlobalTransformation);
  80. spriteBatch.Draw(texture, drawPosition, null, Color.White, 0, Vector2.Zero, 1,
  81. SpriteEffects.None, 0);
  82. spriteBatch.End();
  83. }
  84. }
  85. }