InputHelper.cs 3.3 KB

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