MenuEntry.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //-----------------------------------------------------------------------------
  2. // MenuEntry.cs
  3. //
  4. // XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10. namespace RolePlaying
  11. {
  12. /// <summary>
  13. /// Helper class represents a single entry in a MenuScreen. By default this
  14. /// just draws the entry text string, but it can be customized to display menu
  15. /// entries in different ways. This also provides an event that will be raised
  16. /// when the menu entry is selected.
  17. /// </summary>
  18. /// <remarks>
  19. /// Similar to a class found in the Game State Management sample on the
  20. /// XNA Creators Club Online website (http://creators.xna.com).
  21. /// </remarks>
  22. class MenuEntry
  23. {
  24. /// <summary>
  25. /// The text rendered for this entry.
  26. /// </summary>
  27. string text;
  28. /// <summary>
  29. /// The font used for this menu item.
  30. /// </summary>
  31. SpriteFont spriteFont;
  32. /// <summary>
  33. /// The position of this menu item on the screen.
  34. /// </summary>
  35. Vector2 position;
  36. /// <summary>
  37. /// The offset from the positionof this menu item.
  38. /// </summary>
  39. Vector2 textOffset = new Vector2(0, 0);
  40. /// <summary>
  41. /// The angle of this menu item on the screen.
  42. /// </summary>
  43. float angle = 0f;
  44. /// <summary>
  45. /// A description of the function of the button.
  46. /// </summary>
  47. private string description;
  48. /// <summary>
  49. /// An optional texture drawn with the text.
  50. /// </summary>
  51. /// <remarks>If present, the text will be centered on the texture.</remarks>
  52. private Texture2D texture;
  53. /// <summary>
  54. /// Gets or sets the text of this menu entry.
  55. /// </summary>
  56. public string Text
  57. {
  58. get { return text; }
  59. set { text = value; }
  60. }
  61. /// <summary>
  62. /// Gets or sets the font used to draw this menu entry.
  63. /// </summary>
  64. public SpriteFont Font
  65. {
  66. get { return spriteFont; }
  67. set { spriteFont = value; }
  68. }
  69. /// <summary>
  70. /// Gets or sets the position of this menu entry.
  71. /// </summary>
  72. public Vector2 Position
  73. {
  74. get { return position; }
  75. set { position = value; }
  76. }
  77. /// <summary>
  78. /// Gets or sets the angle of this menu entry.
  79. /// </summary>
  80. public float Angle
  81. {
  82. get { return angle; }
  83. set { angle = value; }
  84. }
  85. /// <summary>
  86. /// Gets or sets the position of this menu entry.
  87. /// </summary>
  88. public Vector2 TextOffset
  89. {
  90. get { return textOffset; }
  91. set { textOffset = value; }
  92. }
  93. /// <summary>
  94. /// A description of the function of the button.
  95. /// </summary>
  96. public string Description
  97. {
  98. get { return description; }
  99. set { description = value; }
  100. }
  101. /// <summary>
  102. /// An optional texture drawn with the text.
  103. /// </summary>
  104. /// <remarks>If present, the text will be centered on the texture.</remarks>
  105. public Texture2D Texture
  106. {
  107. get { return texture; }
  108. set { texture = value; }
  109. }
  110. /// <summary>
  111. /// Event raised when the menu entry is selected.
  112. /// </summary>
  113. public event EventHandler<EventArgs> Selected;
  114. /// <summary>
  115. /// Method for raising the Selected event.
  116. /// </summary>
  117. protected internal virtual void OnSelectEntry()
  118. {
  119. if (Selected != null)
  120. Selected(this, EventArgs.Empty);
  121. }
  122. /// <summary>
  123. /// Constructs a new menu entry with the specified text.
  124. /// </summary>
  125. public MenuEntry(string text)
  126. {
  127. this.text = text;
  128. }
  129. /// <summary>
  130. /// Updates the menu entry.
  131. /// </summary>
  132. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  133. { }
  134. /// <summary>
  135. /// Draws the menu entry. This can be overridden to customize the appearance.
  136. /// </summary>
  137. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  138. {
  139. // Draw the selected entry in yellow, otherwise white.
  140. Color color = isSelected ? Fonts.MenuSelectedColor : Fonts.TitleColor;
  141. // Draw text, centered on the middle of each line.
  142. ScreenManager screenManager = screen.ScreenManager;
  143. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  144. if (texture != null)
  145. {
  146. spriteBatch.Draw(texture, position, Color.White);
  147. if ((spriteFont != null) && !String.IsNullOrEmpty(text))
  148. {
  149. Vector2 textSize = spriteFont.MeasureString(text);
  150. Vector2 textPosition = position + new Vector2(
  151. (float)Math.Floor((texture.Width - textSize.X + textOffset.X) / 2),
  152. (float)Math.Floor((texture.Height - textSize.Y + textOffset.Y) / 2));
  153. spriteBatch.DrawString(spriteFont, text, textPosition, color, MathHelper.ToRadians(angle), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  154. }
  155. }
  156. else if ((spriteFont != null) && !String.IsNullOrEmpty(text))
  157. {
  158. spriteBatch.DrawString(spriteFont, text, position, color, MathHelper.ToRadians(angle), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  159. }
  160. }
  161. /// <summary>
  162. /// Queries how much space this menu entry requires.
  163. /// </summary>
  164. public virtual int GetHeight(MenuScreen screen)
  165. {
  166. return Font.LineSpacing;
  167. }
  168. }
  169. }