Game1.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  65. {
  66. Exit();
  67. }
  68. currentKeyboardState = Keyboard.GetState ();
  69. currentGamePadState = GamePad.GetState (PlayerIndex.One);
  70. currentTouchState = TouchPanel.GetState();
  71. while (TouchPanel.IsGestureAvailable)
  72. {
  73. var gesture = TouchPanel.ReadGesture();
  74. switch (gesture.GestureType) {
  75. case GestureType.DoubleTap:
  76. clearColor = clearColor == Color.CornflowerBlue ? Color.Red : Color.CornflowerBlue;
  77. break;
  78. case GestureType.Tap:
  79. clearColor = clearColor == Color.CornflowerBlue ? Color.Black : Color.CornflowerBlue;
  80. break;
  81. }
  82. }
  83. base.Update (gameTime);
  84. }
  85. /// <summary>
  86. /// This is called when the game should draw itself.
  87. /// </summary>
  88. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  89. protected override void Draw (GameTime gameTime)
  90. {
  91. graphics.GraphicsDevice.Clear (clearColor);
  92. // Won't be visible until we hide the movie
  93. spriteBatch.Begin();
  94. Vector2 center = new Vector2(GraphicsDevice.Viewport.Width /2, GraphicsDevice.Viewport.Height /2);
  95. Vector2 s1 = font.MeasureString("Touch the Screen");
  96. center.X -= (s1.X /2);
  97. spriteBatch.DrawString(font, "Touch the Screen", center, Color.Red);
  98. center.Y += 20;
  99. spriteBatch.DrawString(font, GraphicsDevice.Viewport.Width.ToString() + " x " + GraphicsDevice.Viewport.Height.ToString(), center, Color.Red);
  100. center.Y += 20;
  101. spriteBatch.DrawString(font, GraphicsDevice.PresentationParameters.DisplayOrientation.ToString(), center, Color.Red);
  102. spriteBatch.DrawString(font, "0,0", new Vector2(0,0), Color.Red);
  103. string s = GraphicsDevice.Viewport.Width.ToString() + ",0";
  104. Vector2 v = font.MeasureString(s);
  105. spriteBatch.DrawString(font, s, new Vector2(GraphicsDevice.Viewport.Width-v.X, 0), Color.Red);
  106. s = "0,"+GraphicsDevice.Viewport.Height.ToString();
  107. v = font.MeasureString(s);
  108. spriteBatch.DrawString(font, s , new Vector2(0, GraphicsDevice.Viewport.Height-v.Y), Color.Red);
  109. string wandh = GraphicsDevice.Viewport.Width.ToString() + ", " + GraphicsDevice.Viewport.Height.ToString();
  110. Vector2 wh = font.MeasureString(wandh);
  111. spriteBatch.DrawString(font, wandh, new Vector2(GraphicsDevice.Viewport.Width-wh.X, GraphicsDevice.Viewport.Height-wh.Y), Color.Red);
  112. if (currentTouchState.Count > 0)
  113. {
  114. for (int i = 0 ; i < currentTouchState.Count; i++)
  115. {
  116. center.Y += s1.Y;
  117. Vector2 p = currentTouchState[i].Position;
  118. spriteBatch.DrawString(font, "+", p, Color.Red);
  119. spriteBatch.DrawString(font, p.ToString(), center, Color.Red);
  120. }
  121. }
  122. spriteBatch.End();
  123. }
  124. }
  125. }