Game1.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using Microsoft.Xna.Framework.Input.Touch;
  7. namespace Microsoft.Xna.Samples.MultiTouch
  8. {
  9. /// <summary>
  10. /// This is the main type for your game
  11. /// </summary>
  12. public class Game1 : Game
  13. {
  14. private GraphicsDeviceManager graphics;
  15. private SpriteBatch spriteBatch;
  16. public Game1()
  17. {
  18. graphics = new GraphicsDeviceManager(this);
  19. IsMouseVisible = true;
  20. Content.RootDirectory = "Content";
  21. }
  22. /// <summary>
  23. /// Allows the game to perform any initialization it needs to before starting to run.
  24. /// This is where it can query for any required services and load any non-graphic
  25. /// related content. Calling base.Initialize will enumerate through any components
  26. /// and initialize them as well.
  27. /// </summary>
  28. protected override void Initialize()
  29. {
  30. // Enable multi-touch
  31. TouchPanel.EnabledGestures = GestureType.None;
  32. base.Initialize();
  33. }
  34. /// <summary>
  35. /// LoadContent will be called once per game and is the place to load
  36. /// all of your content.
  37. /// </summary>
  38. private Texture2D Brush;
  39. private TouchCollection touchStateCollection;
  40. private bool Cls = true;
  41. private List<Color> drawColors = new List<Color>();
  42. private Dictionary<int, Color> LineColors = new Dictionary<int, Color>();
  43. private int ShakeTime = 0;
  44. protected override void LoadContent()
  45. {
  46. // Create a new SpriteBatch, which can be used to draw textures.
  47. spriteBatch = new SpriteBatch(GraphicsDevice);
  48. // Load in a single pixel to use as the brush
  49. Brush = Content.Load<Texture2D>("sqbrush");
  50. // Set the random colors for multi touch painting
  51. drawColors.Add(Color.Orange);
  52. drawColors.Add(Color.Yellow);
  53. drawColors.Add(Color.Green);
  54. drawColors.Add(Color.Cyan);
  55. drawColors.Add(Color.HotPink);
  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. private Vector2? prevMousePos = null;
  63. private bool prevMouseDown = false;
  64. protected override void Update(GameTime gameTime)
  65. {
  66. // Allows the game to exit
  67. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
  68. || Keyboard.GetState().IsKeyDown(Keys.Escape))
  69. this.Exit();
  70. // Simple shake detection using gamepad or keyboard
  71. ShakeTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
  72. if (ShakeTime >= 500)
  73. {
  74. // Clear screen on Space key press or gamepad button press
  75. if (Keyboard.GetState().IsKeyDown(Keys.Space) ||
  76. GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
  77. {
  78. Cls = true;
  79. }
  80. ShakeTime = 0;
  81. }
  82. // Update touch panel state
  83. touchStateCollection = TouchPanel.GetState();
  84. // Mouse input tracking
  85. var mouseState = Mouse.GetState();
  86. bool mouseDown = mouseState.LeftButton == ButtonState.Pressed;
  87. if (!mouseDown)
  88. {
  89. prevMousePos = null;
  90. }
  91. prevMouseDown = mouseDown;
  92. base.Update(gameTime);
  93. }
  94. /// <summary>
  95. /// This is called when the game should draw itself.
  96. /// </summary>
  97. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  98. protected override void Draw(GameTime gameTime)
  99. {
  100. if (Cls)
  101. {
  102. Cls = false;
  103. graphics.GraphicsDevice.Clear(Color.Black);
  104. }
  105. spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.AlphaBlend);
  106. // Touch drawing
  107. foreach (TouchLocation t in touchStateCollection)
  108. {
  109. TouchLocation PrevLocation = new TouchLocation();
  110. if (t.TryGetPreviousLocation(out PrevLocation))
  111. {
  112. if (!LineColors.ContainsKey(t.Id))
  113. {
  114. if (touchStateCollection.Count > 1)
  115. {
  116. Random randomizer = new Random();
  117. LineColors[t.Id] = drawColors[randomizer.Next(0, 4)];
  118. }
  119. else
  120. {
  121. LineColors[t.Id] = Color.White;
  122. }
  123. }
  124. spriteBatch.Draw(Brush, PrevLocation.Position, null,
  125. LineColors[t.Id], (float)Math.Atan2((double)(t.Position.Y - PrevLocation.Position.Y), (double)(t.Position.X - PrevLocation.Position.X)), Vector2.Zero,
  126. new Vector2(Vector2.Distance(PrevLocation.Position, t.Position), 1f), SpriteEffects.None, 0f);
  127. }
  128. }
  129. // Mouse drawing (parity with touch)
  130. var mouseState = Mouse.GetState();
  131. bool mouseDown = mouseState.LeftButton == ButtonState.Pressed;
  132. Vector2 mousePos = new Vector2(mouseState.X, mouseState.Y);
  133. if (mouseDown)
  134. {
  135. if (prevMousePos.HasValue)
  136. {
  137. // Use same color logic as touch
  138. Color mouseColor = Color.White;
  139. if (touchStateCollection.Count > 1)
  140. {
  141. Random randomizer = new Random();
  142. mouseColor = drawColors[randomizer.Next(0, 4)];
  143. }
  144. spriteBatch.Draw(Brush, prevMousePos.Value, null,
  145. mouseColor, (float)Math.Atan2(mousePos.Y - prevMousePos.Value.Y, mousePos.X - prevMousePos.Value.X), Vector2.Zero,
  146. new Vector2(Vector2.Distance(prevMousePos.Value, mousePos), 1f), SpriteEffects.None, 0f);
  147. }
  148. prevMousePos = mousePos;
  149. }
  150. else
  151. {
  152. prevMousePos = null;
  153. }
  154. spriteBatch.End();
  155. base.Draw(gameTime);
  156. }
  157. }
  158. }