InputManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace BackgroundThreadTester
  5. {
  6. public class InputManager
  7. {
  8. private Game1 cG;
  9. private MouseState mousestatus;
  10. private MsState mssButtonLeft;
  11. public TimeSpan tsTimeSinceLastClick;
  12. private int nClicksForDoubleClick;
  13. public InputManager(Game game)
  14. {
  15. cG = (Game1)game;
  16. mssButtonLeft = MsState.ButtonWasReleased;
  17. tsTimeSinceLastClick = TimeSpan.Zero;
  18. nClicksForDoubleClick = 0;
  19. }//InputManger
  20. public void InputHandler(MouseState mst, GameTime gameTime)
  21. {
  22. if (!cG.IsActive) return;
  23. mousestatus = mst;
  24. HandleMouseLeftButton(gameTime);
  25. if (mssButtonLeft == MsState.ButtonWasPressed)
  26. {
  27. if (mousestatus.X >= 38 && mousestatus.X <= 702)
  28. {
  29. if (mousestatus.Y >= 200 && mousestatus.Y <= 296)
  30. {
  31. cG.CreateBackgroundThread();
  32. }//if
  33. }//if
  34. }//if
  35. }//InputHandler
  36. private void HandleMouseLeftButton(GameTime gTime)
  37. {
  38. tsTimeSinceLastClick += gTime.ElapsedGameTime;
  39. if (tsTimeSinceLastClick >= TimeSpan.FromMilliseconds(250))
  40. {
  41. nClicksForDoubleClick = 0;
  42. }//if
  43. if (mousestatus.LeftButton == ButtonState.Pressed)
  44. {
  45. if (mssButtonLeft == MsState.ButtonWasReleased)
  46. {
  47. if (GetMouseX() >= 0 && GetMouseX() <= cG.GetBackBufferWidth())
  48. {
  49. if (GetMouseY() >= 0 && GetMouseY() <= cG.GetBackBufferHeight())
  50. {
  51. mssButtonLeft = MsState.ButtonWasPressed;
  52. nClicksForDoubleClick++;
  53. if (nClicksForDoubleClick == 1)
  54. {
  55. tsTimeSinceLastClick = TimeSpan.Zero;
  56. }//if
  57. if (nClicksForDoubleClick == 2)
  58. {
  59. if (tsTimeSinceLastClick < TimeSpan.FromMilliseconds(250))
  60. {
  61. nClicksForDoubleClick = 0;
  62. mssButtonLeft = MsState.ButtonWasDoublePressed;
  63. }//if
  64. }//if
  65. if (nClicksForDoubleClick == 3) nClicksForDoubleClick = 0;
  66. }//if
  67. }//if
  68. }//if
  69. else
  70. {
  71. if (mssButtonLeft == MsState.ButtonWasPressed || mssButtonLeft == MsState.ButtonWasDoublePressed)
  72. {
  73. mssButtonLeft = MsState.ButtonStillPressed;
  74. }//if
  75. }//else
  76. }//if
  77. if (mousestatus.LeftButton == ButtonState.Released)
  78. {
  79. mssButtonLeft = MsState.ButtonWasReleased;
  80. }//if
  81. }//HandleMouseLeftButton
  82. public float GetMouseX()
  83. {
  84. return mousestatus.X;
  85. }//GetMouseX
  86. public float GetMouseY()
  87. {
  88. return mousestatus.Y;
  89. }//GetMouseY
  90. }
  91. }