Game1.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. KeyboardState currentKeyboardState;
  21. GamePadState currentGamePadState;
  22. TouchCollection currentTouchState;
  23. public Game1 ()
  24. {
  25. graphics = new GraphicsDeviceManager (this);
  26. Content.RootDirectory = "Content";
  27. graphics.PreferMultiSampling = true;
  28. graphics.IsFullScreen = true;
  29. graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight
  30. | DisplayOrientation.Portrait
  31. ;
  32. }
  33. /// <summary>
  34. /// Allows the game to perform any initialization it needs to before starting to run.
  35. /// This is where it can query for any required services and load any non-graphic
  36. /// related content. Calling base.Initialize will enumerate through any components
  37. /// and initialize them as well.
  38. /// </summary>
  39. protected override void Initialize ()
  40. {
  41. // TODO: Add your initialization logic here
  42. base.Initialize ();
  43. }
  44. /// <summary>
  45. /// LoadContent will be called once per game and is the place to load
  46. /// all of your content.
  47. /// </summary>
  48. protected override void LoadContent ()
  49. {
  50. // Create a new SpriteBatch, which can be used to draw textures.
  51. spriteBatch = new SpriteBatch (GraphicsDevice);
  52. // TODO: use this.Content to load your game content here
  53. font = Content.Load<SpriteFont> ("spriteFont1");
  54. }
  55. /// <summary>
  56. /// Allows the game to run logic such as updating the world,
  57. /// checking for collisions, gathering input, and playing audio.
  58. /// </summary>
  59. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  60. protected override void Update (GameTime gameTime)
  61. {
  62. currentKeyboardState = Keyboard.GetState ();
  63. currentGamePadState = GamePad.GetState (PlayerIndex.One);
  64. currentTouchState = TouchPanel.GetState();
  65. base.Update (gameTime);
  66. }
  67. /// <summary>
  68. /// This is called when the game should draw itself.
  69. /// </summary>
  70. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  71. protected override void Draw (GameTime gameTime)
  72. {
  73. graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
  74. // Won't be visible until we hide the movie
  75. spriteBatch.Begin();
  76. Vector2 center = new Vector2(GraphicsDevice.Viewport.Width /2, GraphicsDevice.Viewport.Height /2);
  77. Vector2 s1 = font.MeasureString("Touch the Screen");
  78. center.X -= (s1.X /2);
  79. spriteBatch.DrawString(font, "Touch the Screen", center, Color.Red);
  80. center.Y += 20;
  81. spriteBatch.DrawString(font, GraphicsDevice.Viewport.Width.ToString() + " x " + GraphicsDevice.Viewport.Height.ToString(), center, Color.Red);
  82. center.Y += 20;
  83. spriteBatch.DrawString(font, GraphicsDevice.PresentationParameters.DisplayOrientation.ToString(), center, Color.Red);
  84. spriteBatch.DrawString(font, "0,0", new Vector2(0,0), Color.Red);
  85. string s = GraphicsDevice.Viewport.Width.ToString() + ",0";
  86. Vector2 v = font.MeasureString(s);
  87. spriteBatch.DrawString(font, s, new Vector2(GraphicsDevice.Viewport.Width-v.X, 0), Color.Red);
  88. s = "0,"+GraphicsDevice.Viewport.Height.ToString();
  89. v = font.MeasureString(s);
  90. spriteBatch.DrawString(font, s , new Vector2(0, GraphicsDevice.Viewport.Height-v.Y), Color.Red);
  91. string wandh = GraphicsDevice.Viewport.Width.ToString() + ", " + GraphicsDevice.Viewport.Height.ToString();
  92. Vector2 wh = font.MeasureString(wandh);
  93. spriteBatch.DrawString(font, wandh, new Vector2(GraphicsDevice.Viewport.Width-wh.X, GraphicsDevice.Viewport.Height-wh.Y), Color.Red);
  94. if (currentTouchState != null && currentTouchState.Count > 0)
  95. {
  96. for (int i = 0 ; i < currentTouchState.Count; i++)
  97. {
  98. center.Y += s1.Y;
  99. Vector2 p = currentTouchState[i].Position;
  100. spriteBatch.DrawString(font, "+", p, Color.Red);
  101. spriteBatch.DrawString(font, p.ToString(), center, Color.Red);
  102. }
  103. }
  104. spriteBatch.End();
  105. }
  106. }
  107. }