Game1.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.Xna.Framework.Input.Touch;
  8. namespace MonoGame.Samples.Input
  9. {
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14. SpriteFont font;
  15. Color clearColor = Color.CornflowerBlue;
  16. KeyboardState currentKeyboardState;
  17. GamePadState currentGamePadState;
  18. TouchCollection currentTouchState;
  19. MouseState currentMouseState;
  20. MouseState previousMouseState;
  21. TimeSpan lastClickTime;
  22. public Game1()
  23. {
  24. graphics = new GraphicsDeviceManager(this);
  25. Content.RootDirectory = "Content";
  26. graphics.PreferMultiSampling = true;
  27. // Only use fullscreen on mobile platforms
  28. #if MOBILE
  29. graphics.IsFullScreen = true;
  30. #endif
  31. graphics.PreferredBackBufferHeight = 640;
  32. graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight
  33. | DisplayOrientation.Portrait;
  34. IsMouseVisible = true;
  35. }
  36. /// <summary>
  37. /// Allows the game to perform any initialization it needs to before starting to run.
  38. /// This is where it can query for any required services and load any non-graphic
  39. /// related content. Calling base.Initialize will enumerate through any components
  40. /// and initialize them as well.
  41. /// </summary>
  42. protected override void Initialize()
  43. {
  44. // TODO: Add your initialization logic here
  45. TouchPanel.EnabledGestures = GestureType.Tap | GestureType.DoubleTap;
  46. base.Initialize();
  47. }
  48. /// <summary>
  49. /// LoadContent will be called once per game and is the place to load
  50. /// all of your content.
  51. /// </summary>
  52. protected override void LoadContent()
  53. {
  54. // Create a new SpriteBatch, which can be used to draw textures.
  55. spriteBatch = new SpriteBatch(GraphicsDevice);
  56. // TODO: use this.Content to load your game content here
  57. font = Content.Load<SpriteFont>("spriteFont1");
  58. }
  59. /// <summary>
  60. /// Allows the game to run logic such as updating the world,
  61. /// checking for collisions, gathering input, and playing audio.
  62. /// </summary>
  63. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  64. protected override void Update(GameTime gameTime)
  65. {
  66. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  67. currentKeyboardState = Keyboard.GetState();
  68. currentMouseState = Mouse.GetState();
  69. currentTouchState = TouchPanel.GetState();
  70. // Handle ESC key to exit (for desktop platforms)
  71. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  72. // Handle gamepad back button (for mobile/console platforms)
  73. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  74. {
  75. Exit();
  76. }
  77. // Replicate TouchPanel tap/double-tap with mouse for non-touch platforms
  78. // Detect left mouse button click (tap)
  79. if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
  80. {
  81. clearColor = clearColor == Color.MonoGameOrange ? Color.Black : Color.MonoGameOrange;
  82. }
  83. // Detect double-click (double-tap)
  84. if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
  85. {
  86. // Simple double-click detection: check if click happened within 300ms
  87. if ((gameTime.TotalGameTime - lastClickTime).TotalMilliseconds < 300)
  88. {
  89. clearColor = clearColor == Color.MonoGameOrange ? Color.Red : Color.MonoGameOrange;
  90. }
  91. lastClickTime = gameTime.TotalGameTime;
  92. }
  93. while (TouchPanel.IsGestureAvailable)
  94. {
  95. var gesture = TouchPanel.ReadGesture();
  96. switch (gesture.GestureType)
  97. {
  98. case GestureType.DoubleTap:
  99. clearColor = clearColor == Color.MonoGameOrange ? Color.Red : Color.MonoGameOrange;
  100. break;
  101. case GestureType.Tap:
  102. clearColor = clearColor == Color.MonoGameOrange ? Color.Black : Color.MonoGameOrange;
  103. break;
  104. }
  105. }
  106. previousMouseState = currentMouseState;
  107. base.Update(gameTime);
  108. }
  109. /// <summary>
  110. /// This is called when the game should draw itself.
  111. /// </summary>
  112. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  113. protected override void Draw(GameTime gameTime)
  114. {
  115. graphics.GraphicsDevice.Clear(clearColor);
  116. // Won't be visible until we hide the movie
  117. spriteBatch.Begin();
  118. Vector2 center = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
  119. Vector2 s1 = font.MeasureString("Touch the Screen");
  120. center.X -= (s1.X / 2);
  121. spriteBatch.DrawString(font, "Touch the Screen", center, Color.Red);
  122. center.Y += 20;
  123. spriteBatch.DrawString(font, GraphicsDevice.Viewport.Width.ToString() + " x " + GraphicsDevice.Viewport.Height.ToString(), center, Color.Red);
  124. center.Y += 20;
  125. spriteBatch.DrawString(font, GraphicsDevice.PresentationParameters.DisplayOrientation.ToString(), center, Color.Red);
  126. spriteBatch.DrawString(font, "0,0", new Vector2(0, 0), Color.Red);
  127. string s = GraphicsDevice.Viewport.Width.ToString() + ",0";
  128. Vector2 v = font.MeasureString(s);
  129. spriteBatch.DrawString(font, s, new Vector2(GraphicsDevice.Viewport.Width - v.X, 0), Color.Red);
  130. s = "0," + GraphicsDevice.Viewport.Height.ToString();
  131. v = font.MeasureString(s);
  132. spriteBatch.DrawString(font, s, new Vector2(0, GraphicsDevice.Viewport.Height - v.Y), Color.Red);
  133. string wandh = GraphicsDevice.Viewport.Width.ToString() + ", " + GraphicsDevice.Viewport.Height.ToString();
  134. Vector2 wh = font.MeasureString(wandh);
  135. spriteBatch.DrawString(font, wandh, new Vector2(GraphicsDevice.Viewport.Width - wh.X, GraphicsDevice.Viewport.Height - wh.Y), Color.Red);
  136. if (currentTouchState.Count > 0)
  137. {
  138. for (int i = 0; i < currentTouchState.Count; i++)
  139. {
  140. center.Y += s1.Y;
  141. Vector2 p = currentTouchState[i].Position;
  142. spriteBatch.DrawString(font, "+", p, Color.Red);
  143. spriteBatch.DrawString(font, p.ToString(), center, Color.Red);
  144. }
  145. }
  146. spriteBatch.End();
  147. }
  148. }
  149. }