TitleMenu.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // TitleMenu.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. #endregion
  17. namespace XnaGraphicsDemo
  18. {
  19. /// <summary>
  20. /// The main menu screen allows users to choose between the various demo screens.
  21. /// </summary>
  22. class TitleMenu : MenuComponent
  23. {
  24. // Constants.
  25. const float XnaSpawnRate = 1.5f;
  26. const float XnaLifespan = 7;
  27. // Fields.
  28. int attractCycle;
  29. float time;
  30. Random random = new Random();
  31. // We display a set of floating "xna" text labels in the background of the menu.
  32. class FloatingXna
  33. {
  34. public Vector2 Position;
  35. public float Age;
  36. public float Size;
  37. }
  38. List<FloatingXna> floatingXnas = new List<FloatingXna>();
  39. /// <summary>
  40. /// Constructor.
  41. /// </summary>
  42. public TitleMenu(DemoGame game)
  43. : base(game)
  44. {
  45. Entries.Add(new MenuEntry { Text = "basic effect", Clicked = delegate { Game.SetActiveMenu(1); } });
  46. Entries.Add(new MenuEntry { Text = "dual texture effect", Clicked = delegate { Game.SetActiveMenu(2); } });
  47. Entries.Add(new MenuEntry { Text = "alpha test effect", Clicked = delegate { Game.SetActiveMenu(3); } });
  48. Entries.Add(new MenuEntry { Text = "skinned effect", Clicked = delegate { Game.SetActiveMenu(4); } });
  49. Entries.Add(new MenuEntry { Text = "environment map effect", Clicked = delegate { Game.SetActiveMenu(5); } });
  50. Entries.Add(new MenuEntry { Text = "particles", Clicked = delegate { Game.SetActiveMenu(6); } });
  51. Entries.Add(new MenuEntry { Text = "quit", Clicked = delegate { game.Exit(); } });
  52. }
  53. /// <summary>
  54. /// Resets the menu state.
  55. /// </summary>
  56. public override void Reset()
  57. {
  58. floatingXnas.Clear();
  59. time = 0;
  60. base.Reset();
  61. }
  62. /// <summary>
  63. /// The main menu wants a shorter attract delay than the other screens.
  64. /// </summary>
  65. override protected TimeSpan AttractDelay { get { return TimeSpan.FromSeconds(3); } }
  66. /// <summary>
  67. /// When the attract mode timeout is reached, we cycle through each other screen in turn.
  68. /// </summary>
  69. override protected void OnAttract()
  70. {
  71. Entries[attractCycle].OnClicked();
  72. if (++attractCycle >= Entries.Count - 1)
  73. attractCycle = 0;
  74. }
  75. /// <summary>
  76. /// Updates the floating "xna" background labels.
  77. /// </summary>
  78. public override void Update(GameTime gameTime)
  79. {
  80. time += (float)gameTime.ElapsedGameTime.TotalSeconds;
  81. // Spawn a new label?
  82. if (time > XnaSpawnRate)
  83. {
  84. FloatingXna xna = new FloatingXna();
  85. xna.Size = (float)random.NextDouble() * 2 + 0.5f;
  86. xna.Position.X = (float)random.NextDouble() * 320 + 80;
  87. xna.Position.Y = (float)random.NextDouble() * 700 + 50;
  88. floatingXnas.Add(xna);
  89. time -= XnaSpawnRate;
  90. }
  91. // Animate the existing labels.
  92. int i = 0;
  93. while (i < floatingXnas.Count)
  94. {
  95. FloatingXna xna = floatingXnas[i];
  96. xna.Age += (float)gameTime.ElapsedGameTime.TotalSeconds;
  97. // Different size labels move at different speeds.
  98. float speed = 1.5f - xna.Size;
  99. if (Math.Abs(speed) > 0.01f)
  100. xna.Position.Y -= xna.Age * xna.Age / speed / 10;
  101. // Remove old labels.
  102. if (xna.Age >= XnaLifespan)
  103. floatingXnas.RemoveAt(i);
  104. else
  105. i++;
  106. }
  107. base.Update(gameTime);
  108. }
  109. /// <summary>
  110. /// Draws the main menu.
  111. /// </summary>
  112. public override void Draw(GameTime gameTime)
  113. {
  114. DrawTitle("xna demo", Color.CornflowerBlue, Color.Lerp(Color.Blue, Color.CornflowerBlue, 0.85f));
  115. // Draw the background "xna" labels.
  116. SpriteBatch.Begin();
  117. foreach (FloatingXna blob in floatingXnas)
  118. {
  119. float alpha = Math.Min(blob.Age, 1) * Math.Min((XnaLifespan - blob.Age) / (XnaLifespan - 2), 1);
  120. alpha *= alpha;
  121. alpha /= 8;
  122. SpriteBatch.DrawString(BigFont, "xna", blob.Position, Color.Blue * alpha, MathHelper.PiOver2, Vector2.Zero, blob.Size, 0, 0);
  123. }
  124. SpriteBatch.End();
  125. // This will draw the various menu items.
  126. base.Draw(gameTime);
  127. }
  128. }
  129. }