InputManager.cs 3.7 KB

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