Game1.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. #if ANDROID
  4. using Android.App;
  5. #endif
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Media;
  12. using Microsoft.Xna.Framework.Storage;
  13. namespace MonoGame.Samples.Input
  14. {
  15. public class Game1 : Microsoft.Xna.Framework.Game
  16. {
  17. GraphicsDeviceManager graphics;
  18. SpriteBatch spriteBatch;
  19. SpriteFont font;
  20. Color clearColor = Color.CornflowerBlue;
  21. KeyboardState currentKeyboardState;
  22. GamePadState currentGamePadState;
  23. TouchCollection currentTouchState;
  24. public Game1 ()
  25. {
  26. graphics = new GraphicsDeviceManager (this);
  27. Content.RootDirectory = "Content";
  28. graphics.PreferMultiSampling = true;
  29. graphics.IsFullScreen = true;
  30. graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight
  31. | DisplayOrientation.Portrait
  32. ;
  33. }
  34. /// <summary>
  35. /// Allows the game to perform any initialization it needs to before starting to run.
  36. /// This is where it can query for any required services and load any non-graphic
  37. /// related content. Calling base.Initialize will enumerate through any components
  38. /// and initialize them as well.
  39. /// </summary>
  40. protected override void Initialize ()
  41. {
  42. // TODO: Add your initialization logic here
  43. TouchPanel.EnabledGestures = GestureType.Tap | GestureType.DoubleTap;
  44. base.Initialize ();
  45. }
  46. /// <summary>
  47. /// LoadContent will be called once per game and is the place to load
  48. /// all of your content.
  49. /// </summary>
  50. protected override void LoadContent ()
  51. {
  52. // Create a new SpriteBatch, which can be used to draw textures.
  53. spriteBatch = new SpriteBatch (GraphicsDevice);
  54. // TODO: use this.Content to load your game content here
  55. font = Content.Load<SpriteFont> ("spriteFont1");
  56. }
  57. /// <summary>
  58. /// Allows the game to run logic such as updating the world,
  59. /// checking for collisions, gathering input, and playing audio.
  60. /// </summary>
  61. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  62. protected override void Update (GameTime gameTime)
  63. {
  64. currentKeyboardState = Keyboard.GetState ();
  65. currentGamePadState = GamePad.GetState (PlayerIndex.One);
  66. currentTouchState = TouchPanel.GetState();
  67. while (TouchPanel.IsGestureAvailable)
  68. {
  69. var gesture = TouchPanel.ReadGesture();
  70. switch (gesture.GestureType) {
  71. case GestureType.DoubleTap:
  72. clearColor = clearColor == Color.CornflowerBlue ? Color.Red : Color.CornflowerBlue;
  73. break;
  74. case GestureType.Tap:
  75. clearColor = clearColor == Color.CornflowerBlue ? Color.Black : Color.CornflowerBlue;
  76. break;
  77. }
  78. }
  79. base.Update (gameTime);
  80. }
  81. /// <summary>
  82. /// This is called when the game should draw itself.
  83. /// </summary>
  84. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  85. protected override void Draw (GameTime gameTime)
  86. {
  87. graphics.GraphicsDevice.Clear (clearColor);
  88. // Won't be visible until we hide the movie
  89. spriteBatch.Begin();
  90. Vector2 center = new Vector2(GraphicsDevice.Viewport.Width /2, GraphicsDevice.Viewport.Height /2);
  91. Vector2 s1 = font.MeasureString("Touch the Screen");
  92. center.X -= (s1.X /2);
  93. spriteBatch.DrawString(font, "Touch the Screen", center, Color.Red);
  94. center.Y += 20;
  95. spriteBatch.DrawString(font, GraphicsDevice.Viewport.Width.ToString() + " x " + GraphicsDevice.Viewport.Height.ToString(), center, Color.Red);
  96. center.Y += 20;
  97. spriteBatch.DrawString(font, GraphicsDevice.PresentationParameters.DisplayOrientation.ToString(), center, Color.Red);
  98. spriteBatch.DrawString(font, "0,0", new Vector2(0,0), Color.Red);
  99. string s = GraphicsDevice.Viewport.Width.ToString() + ",0";
  100. Vector2 v = font.MeasureString(s);
  101. spriteBatch.DrawString(font, s, new Vector2(GraphicsDevice.Viewport.Width-v.X, 0), Color.Red);
  102. s = "0,"+GraphicsDevice.Viewport.Height.ToString();
  103. v = font.MeasureString(s);
  104. spriteBatch.DrawString(font, s , new Vector2(0, GraphicsDevice.Viewport.Height-v.Y), Color.Red);
  105. string wandh = GraphicsDevice.Viewport.Width.ToString() + ", " + GraphicsDevice.Viewport.Height.ToString();
  106. Vector2 wh = font.MeasureString(wandh);
  107. spriteBatch.DrawString(font, wandh, new Vector2(GraphicsDevice.Viewport.Width-wh.X, GraphicsDevice.Viewport.Height-wh.Y), Color.Red);
  108. if (currentTouchState != null && currentTouchState.Count > 0)
  109. {
  110. for (int i = 0 ; i < currentTouchState.Count; i++)
  111. {
  112. center.Y += s1.Y;
  113. Vector2 p = currentTouchState[i].Position;
  114. spriteBatch.DrawString(font, "+", p, Color.Red);
  115. spriteBatch.DrawString(font, p.ToString(), center, Color.Red);
  116. }
  117. }
  118. spriteBatch.End();
  119. }
  120. }
  121. }