2
0

TextMenuComponent.cs 5.8 KB

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