MultiTouch.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 : Microsoft.Xna.Framework.Game
  13. {
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. public Game1()
  17. {
  18. graphics = new GraphicsDeviceManager(this);
  19. Content.RootDirectory = "Content";
  20. }
  21. /// <summary>
  22. /// Allows the game to perform any initialization it needs to before starting to run.
  23. /// This is where it can query for any required services and load any non-graphic
  24. /// related content. Calling base.Initialize will enumerate through any components
  25. /// and initialize them as well.
  26. /// </summary>
  27. protected override void Initialize()
  28. {
  29. // TODO: Add your initialization logic here
  30. acellerometer.Start();
  31. base.Initialize();
  32. }
  33. /// <summary>
  34. /// LoadContent will be called once per game and is the place to load
  35. /// all of your content.
  36. /// </summary>
  37. Texture2D Brush;
  38. TouchCollection touchStateCollection;
  39. bool Cls = true;
  40. List<Color> drawColors = new List<Color>();
  41. Dictionary<int, Color> LineColors = new Dictionary<int, Color>();
  42. int ShakeTime = 0;
  43. Accelerometer acellerometer = new Accelerometer();
  44. float LastAccelX = 0f;
  45. protected override void LoadContent()
  46. {
  47. // Create a new SpriteBatch, which can be used to draw textures.
  48. spriteBatch = new SpriteBatch(GraphicsDevice);
  49. // Load in a single pixel to use as the brush
  50. Brush = Content.Load<Texture2D>("sqbrush");
  51. // Set the random colors for multi touch painting
  52. drawColors.Add(Color.Orange);
  53. drawColors.Add(Color.Yellow);
  54. drawColors.Add(Color.Green);
  55. drawColors.Add(Color.Cyan);
  56. drawColors.Add(Color.HotPink);
  57. }
  58. /// <summary>
  59. /// Allows the game to run logic such as updating the world,
  60. /// checking for collisions, gathering input, and playing audio.
  61. /// </summary>
  62. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  63. protected override void Update(GameTime gameTime)
  64. {
  65. // Allows the game to exit
  66. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  67. this.Exit();
  68. // Check if the x force on the Zune has changed by more than 0.4g in the past half second - if so, it's shaking
  69. ShakeTime += gameTime.TotalGameTime.Milliseconds;
  70. if (ShakeTime >= 500)
  71. {
  72. Vector3 acceleration = acellerometer.CurrentValue.Acceleration;
  73. if (Math.Abs(acceleration.X - LastAccelX) > 0.4)
  74. {
  75. Cls = true;
  76. }
  77. LastAccelX = acceleration.X;
  78. ShakeTime = 0;
  79. }
  80. // Update touch panel state
  81. touchStateCollection = TouchPanel.GetState();
  82. base.Update(gameTime);
  83. }
  84. /// <summary>
  85. /// This is called when the game should draw itself.
  86. /// </summary>
  87. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  88. protected override void Draw(GameTime gameTime)
  89. {
  90. if (Cls)
  91. {
  92. Cls = false;
  93. graphics.GraphicsDevice.Clear(Color.Black);
  94. }
  95. spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.AlphaBlend);
  96. foreach (TouchLocation t in touchStateCollection)
  97. {
  98. TouchLocation PrevLocation = new TouchLocation();
  99. if (t.TryGetPreviousLocation(out PrevLocation))
  100. {
  101. if (!LineColors.ContainsKey(t.Id))
  102. {
  103. if (touchStateCollection.Count > 1)
  104. {
  105. Random randomizer = new Random();
  106. LineColors[t.Id] = drawColors[randomizer.Next(0, 4)];
  107. }
  108. else
  109. {
  110. LineColors[t.Id] = Color.White;
  111. }
  112. }
  113. spriteBatch.Draw(Brush, PrevLocation.Position, null,
  114. LineColors[t.Id], (float)Math.Atan2((double)(t.Position.Y - PrevLocation.Position.Y), (double)(t.Position.X - PrevLocation.Position.X)), Vector2.Zero,
  115. new Vector2(Vector2.Distance(PrevLocation.Position, t.Position), 1f), SpriteEffects.None, 0f);
  116. }
  117. }
  118. spriteBatch.End();
  119. base.Draw(gameTime);
  120. }
  121. }
  122. }