MenuEntry.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //-----------------------------------------------------------------------------
  2. // MenuEntry.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input;
  14. namespace XnaGraphicsDemo
  15. {
  16. /// <summary>
  17. /// Base class for each entry in a MenuComponent.
  18. /// </summary>
  19. class MenuEntry
  20. {
  21. // Constants.
  22. public const int Height = 64;
  23. public const int Border = 32;
  24. // Properties.
  25. /// <summary>
  26. /// Gets or sets the text displayed for this menu entry.
  27. /// </summary>
  28. public virtual string Text { get; set; }
  29. /// <returns>The text displayed for this menu entry.</returns>
  30. /// <summary>
  31. /// Gets or sets the position of this menu entry on the screen.
  32. /// </summary>
  33. public Vector2 Position { get; set; }
  34. /// <returns>The position of this menu entry on the screen.</returns>
  35. /// <summary>
  36. /// Gets or sets whether this menu entry is currently focused.
  37. /// </summary>
  38. public bool IsFocused { get; set; }
  39. /// <returns>True if the menu entry is focused; otherwise, false.</returns>
  40. /// <summary>
  41. /// Gets or sets whether this menu entry can be dragged.
  42. /// </summary>
  43. public bool IsDraggable { get; set; }
  44. /// <returns>True if the menu entry can be dragged; otherwise, false.</returns>
  45. /// <summary>
  46. /// Gets or sets the action to invoke when this menu entry is clicked.
  47. /// </summary>
  48. public Action Clicked { get; set; }
  49. /// <returns>The action to invoke when this menu entry is clicked.</returns>
  50. /// <summary>
  51. /// Gets the color of the menu entry, blue if focused, white otherwise.
  52. /// </summary>
  53. public Color Color { get { return IsFocused ? Color.Blue : Color.White; } }
  54. /// <returns>The color of the menu entry.</returns>
  55. Vector2 positionOffset;
  56. /// <summary>
  57. /// Draws the menu entry.
  58. /// </summary>
  59. /// <param name="spriteBatch">The SpriteBatch used for drawing.</param>
  60. /// <param name="font">The font used to draw the text.</param>
  61. /// <param name="blankTexture">A blank texture for drawing backgrounds or highlights.</param>
  62. public virtual void Draw(SpriteBatch spriteBatch, SpriteFont font, Texture2D blankTexture)
  63. {
  64. positionOffset = new Vector2(0, (Height - font.LineSpacing) / 2);
  65. spriteBatch.DrawString(font, Text, Position + positionOffset, Color);
  66. }
  67. /// <summary>
  68. /// Handles clicks on this menu entry, invoking the Clicked action and spawning feedback if not draggable.
  69. /// </summary>
  70. public virtual void OnClicked()
  71. {
  72. // If we have a click delegate, call that now.
  73. if (Clicked != null)
  74. Clicked();
  75. // If we are not draggable, spawn a visual feedback effect.
  76. if (!IsDraggable)
  77. DemoGame.SpawnZoomyText(Text, Position + positionOffset);
  78. }
  79. /// <summary>
  80. /// Handles dragging this menu entry from left to right.
  81. /// </summary>
  82. /// <param name="delta">The amount the pointer has moved since the last drag event.</param>
  83. public virtual void OnDragged(float delta)
  84. {
  85. }
  86. }
  87. /// <summary>
  88. /// Menu entry subclass for boolean toggle values.
  89. /// </summary>
  90. class BoolMenuEntry : MenuEntry
  91. {
  92. // Properties.
  93. public bool Value { get; set; }
  94. public string Label { get; set; }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="BoolMenuEntry"/> class with the specified label.
  97. /// </summary>
  98. /// <param name="label">The label for the toggle entry.</param>
  99. public BoolMenuEntry(string label)
  100. {
  101. Label = label;
  102. }
  103. /// <summary>
  104. /// Click handler toggles the boolean value and invokes the base click logic.
  105. /// </summary>
  106. public override void OnClicked()
  107. {
  108. Value = !Value;
  109. base.OnClicked();
  110. }
  111. /// <summary>
  112. /// Gets the display text for the toggle entry, showing the label and current value.
  113. /// </summary>
  114. public override string Text
  115. {
  116. get { return Label + " " + (Value ? "on" : "off"); }
  117. set { }
  118. }
  119. }
  120. /// <summary>
  121. /// Menu entry subclass for floating point slider values.
  122. /// </summary>
  123. class FloatMenuEntry : MenuEntry
  124. {
  125. // Properties.
  126. public float Value { get; set; }
  127. /// <summary>
  128. /// Initializes a new instance of the <see cref="FloatMenuEntry"/> class and marks it as draggable.
  129. /// </summary>
  130. public FloatMenuEntry()
  131. {
  132. IsDraggable = true;
  133. }
  134. /// <summary>
  135. /// Drag handler changes the slider position.
  136. /// </summary>
  137. /// <param name="delta">The amount the pointer has moved since the last drag event.</param>
  138. public override void OnDragged(float delta)
  139. {
  140. const float speed = 1f / 300;
  141. Value = MathHelper.Clamp(Value + delta * speed, 0, 1);
  142. }
  143. /// <summary>
  144. /// Custom draw function displays a slider bar in addition to the item text.
  145. /// </summary>
  146. /// <param name="spriteBatch">The SpriteBatch used for drawing.</param>
  147. /// <param name="font">The font used to draw the text.</param>
  148. /// <param name="blankTexture">A blank texture for drawing the slider bar.</param>
  149. public override void Draw(SpriteBatch spriteBatch, SpriteFont font, Texture2D blankTexture)
  150. {
  151. base.Draw(spriteBatch, font, blankTexture);
  152. Vector2 size = font.MeasureString(Text);
  153. size.Y /= 2;
  154. Vector2 pos = Position + size;
  155. pos.X += 8;
  156. pos.Y += (Height - font.LineSpacing) / 2;
  157. float w = 480 - Border - pos.X;
  158. spriteBatch.Draw(blankTexture, new Rectangle((int)pos.X, (int)pos.Y - 3, (int)(w * Value), 6), Color);
  159. }
  160. }
  161. }