MenuEntry.cs 5.7 KB

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