MenuEntry.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 angle of this menu item on the screen.
  38. /// </summary>
  39. float angle = 0f;
  40. /// <summary>
  41. /// A description of the function of the button.
  42. /// </summary>
  43. private string description;
  44. /// <summary>
  45. /// An optional texture drawn with the text.
  46. /// </summary>
  47. /// <remarks>If present, the text will be centered on the texture.</remarks>
  48. private Texture2D texture;
  49. /// <summary>
  50. /// Gets or sets the text of this menu entry.
  51. /// </summary>
  52. public string Text
  53. {
  54. get { return text; }
  55. set { text = value; }
  56. }
  57. /// <summary>
  58. /// Gets or sets the font used to draw this menu entry.
  59. /// </summary>
  60. public SpriteFont Font
  61. {
  62. get { return spriteFont; }
  63. set { spriteFont = value; }
  64. }
  65. /// <summary>
  66. /// Gets or sets the position of this menu entry.
  67. /// </summary>
  68. public Vector2 Position
  69. {
  70. get { return position; }
  71. set { position = value; }
  72. }
  73. /// <summary>
  74. /// Gets or sets the angle of this menu entry.
  75. /// </summary>
  76. public float Angle
  77. {
  78. get { return angle; }
  79. set { angle = value; }
  80. }
  81. /// <summary>
  82. /// A description of the function of the button.
  83. /// </summary>
  84. public string Description
  85. {
  86. get { return description; }
  87. set { description = value; }
  88. }
  89. /// <summary>
  90. /// An optional texture drawn with the text.
  91. /// </summary>
  92. /// <remarks>If present, the text will be centered on the texture.</remarks>
  93. public Texture2D Texture
  94. {
  95. get { return texture; }
  96. set { texture = value; }
  97. }
  98. /// <summary>
  99. /// Event raised when the menu entry is selected.
  100. /// </summary>
  101. public event EventHandler<EventArgs> Selected;
  102. /// <summary>
  103. /// Method for raising the Selected event.
  104. /// </summary>
  105. protected internal virtual void OnSelectEntry()
  106. {
  107. if (Selected != null)
  108. Selected(this, EventArgs.Empty);
  109. }
  110. /// <summary>
  111. /// Constructs a new menu entry with the specified text.
  112. /// </summary>
  113. public MenuEntry(string text)
  114. {
  115. this.text = text;
  116. }
  117. /// <summary>
  118. /// Updates the menu entry.
  119. /// </summary>
  120. public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
  121. { }
  122. /// <summary>
  123. /// Draws the menu entry. This can be overridden to customize the appearance.
  124. /// </summary>
  125. public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
  126. {
  127. // Draw the selected entry in yellow, otherwise white.
  128. Color color = isSelected ? Fonts.MenuSelectedColor : Fonts.TitleColor;
  129. // Draw text, centered on the middle of each line.
  130. ScreenManager screenManager = screen.ScreenManager;
  131. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  132. if (texture != null)
  133. {
  134. spriteBatch.Draw(texture, position, Color.White);
  135. if ((spriteFont != null) && !String.IsNullOrEmpty(text))
  136. {
  137. Vector2 textSize = spriteFont.MeasureString(text);
  138. Vector2 textPosition = position + new Vector2(
  139. (float)Math.Floor((texture.Width - textSize.X) / 2),
  140. (float)Math.Floor((texture.Height - textSize.Y) / 2));
  141. spriteBatch.DrawString(spriteFont, text, textPosition, color, MathHelper.ToRadians(angle), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  142. }
  143. }
  144. else if ((spriteFont != null) && !String.IsNullOrEmpty(text))
  145. {
  146. spriteBatch.DrawString(spriteFont, text, position, color, MathHelper.ToRadians(angle), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  147. }
  148. }
  149. /// <summary>
  150. /// Queries how much space this menu entry requires.
  151. /// </summary>
  152. public virtual int GetHeight(MenuScreen screen)
  153. {
  154. return Font.LineSpacing;
  155. }
  156. }
  157. }