TextMenuComponent.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections.Specialized;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using System.Collections.Generic;
  6. using System;
  7. namespace RockRain.Core
  8. {
  9. /// <summary>
  10. /// This is a game component that implements a menu with text elements.
  11. /// </summary>
  12. public class TextMenuComponent : DrawableGameComponent
  13. {
  14. // Spritebatch
  15. protected SpriteBatch spriteBatch = null;
  16. // Fonts
  17. protected readonly SpriteFont regularFont, selectedFont;
  18. // Colors
  19. protected Color regularColor = Color.White, selectedColor = Color.Red;
  20. // Menu Position
  21. protected Vector2 position = new Vector2();
  22. // Items
  23. protected int selectedIndex = 0;
  24. private readonly List<string> menuItems;
  25. // Size of menu in pixels
  26. protected int width, height;
  27. // Position of menu itens
  28. protected List<Vector2> menuItenSize;
  29. public bool Selected {get;set;}
  30. /// <summary>
  31. /// Default constructor
  32. /// </summary>
  33. /// <param name="game">the main game object</param>
  34. /// <param name="normalFont">Font to regular items</param>
  35. /// <param name="selectedFont">Font to selected item</param>
  36. public TextMenuComponent(Game game, SpriteFont normalFont,
  37. SpriteFont selectedFont) : base(game)
  38. {
  39. regularFont = normalFont;
  40. this.selectedFont = selectedFont;
  41. menuItems = new List<string>();
  42. Selected = false;
  43. // Get the current spritebatch
  44. spriteBatch = (SpriteBatch)
  45. Game.Services.GetService(typeof (SpriteBatch));
  46. }
  47. /// <summary>
  48. /// Set the Menu Options
  49. /// </summary>
  50. /// <param name="items"></param>
  51. public void SetMenuItems(string[] items)
  52. {
  53. menuItems.Clear();
  54. menuItems.AddRange(items);
  55. CalculateBounds();
  56. }
  57. /// <summary>
  58. /// Width of menu in pixels
  59. /// </summary>
  60. public int Width
  61. {
  62. get { return width; }
  63. }
  64. /// <summary>
  65. /// Height of menu in pixels
  66. /// </summary>
  67. public int Height
  68. {
  69. get { return height; }
  70. }
  71. /// <summary>
  72. /// Selected menu item index
  73. /// </summary>
  74. public int SelectedIndex
  75. {
  76. get { return selectedIndex; }
  77. set { selectedIndex = value; }
  78. }
  79. /// <summary>
  80. /// Regular item color
  81. /// </summary>
  82. public Color RegularColor
  83. {
  84. get { return regularColor; }
  85. set { regularColor = value; }
  86. }
  87. /// <summary>
  88. /// Selected item color
  89. /// </summary>
  90. public Color SelectedColor
  91. {
  92. get { return selectedColor; }
  93. set { selectedColor = value; }
  94. }
  95. /// <summary>
  96. /// Position of component in screen
  97. /// </summary>
  98. public Vector2 Position
  99. {
  100. get { return position; }
  101. set { position = value; }
  102. }
  103. /// <summary>
  104. /// Get the menu bounds
  105. /// </summary>
  106. protected void CalculateBounds()
  107. {
  108. width = 0;
  109. height = 0;
  110. menuItenSize = new List<Vector2>();
  111. foreach (string item in menuItems)
  112. {
  113. Vector2 size = selectedFont.MeasureString(item);
  114. menuItenSize.Add(size);
  115. if (size.X > width)
  116. {
  117. width = (int) size.X;
  118. }
  119. height += selectedFont.LineSpacing;
  120. }
  121. }
  122. /// <summary>
  123. /// Allows the game component to update itself.
  124. /// </summary>
  125. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  126. public override void Update(GameTime gameTime)
  127. {
  128. float y = position.Y;
  129. selectedIndex = 0;
  130. foreach (Vector2 item in menuItenSize)
  131. {
  132. Rectangle rect = new Rectangle((int)position.X, (int)y,(int)item.X,(int)item.Y);
  133. if (rect.Contains(Mouse.GetState().X,Mouse.GetState().Y))
  134. {
  135. Selected = true;
  136. break;
  137. }
  138. y += selectedFont.LineSpacing;
  139. selectedIndex ++;
  140. }
  141. if (!Selected)
  142. {
  143. selectedIndex = 0;
  144. }
  145. base.Update(gameTime);
  146. }
  147. /// <summary>
  148. /// Allows the game component to draw itself.
  149. /// </summary>
  150. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  151. public override void Draw(GameTime gameTime)
  152. {
  153. float y = position.Y;
  154. for (int i = 0; i < menuItems.Count; i++)
  155. {
  156. SpriteFont font;
  157. Color theColor;
  158. if (i == SelectedIndex)
  159. {
  160. font = selectedFont;
  161. theColor = selectedColor;
  162. }
  163. else
  164. {
  165. font = regularFont;
  166. theColor = regularColor;
  167. }
  168. // Draw the text shadow
  169. spriteBatch.DrawString(font, menuItems[i],
  170. new Vector2(position.X + 1, y + 1), Color.Black);
  171. // Draw the text item
  172. spriteBatch.DrawString(font, menuItems[i],
  173. new Vector2(position.X, y), theColor);
  174. y += font.LineSpacing;
  175. }
  176. base.Draw(gameTime);
  177. }
  178. }
  179. }