MenuEntry.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MenuEntry.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using Microsoft.Xna.Framework.Input;
  17. #endregion
  18. namespace XnaGraphicsDemo
  19. {
  20. /// <summary>
  21. /// Base class for each entry in a MenuComponent.
  22. /// </summary>
  23. class MenuEntry
  24. {
  25. // Constants.
  26. public const int Height = 64;
  27. public const int Border = 32;
  28. // Properties.
  29. public virtual string Text { get; set; }
  30. public Vector2 Position { get; set; }
  31. public bool IsFocused { get; set; }
  32. public bool IsDraggable { get; set; }
  33. public Action Clicked { get; set; }
  34. public Color Color { get { return IsFocused ? Color.Blue : Color.White; } }
  35. Vector2 positionOffset;
  36. /// <summary>
  37. /// Draws the menu entry.
  38. /// </summary>
  39. public virtual void Draw(SpriteBatch spriteBatch, SpriteFont font, Texture2D blankTexture)
  40. {
  41. positionOffset = new Vector2(0, (Height - font.LineSpacing) / 2);
  42. spriteBatch.DrawString(font, Text, Position + positionOffset, Color);
  43. }
  44. /// <summary>
  45. /// Handles clicks on this menu entry.
  46. /// </summary>
  47. public virtual void OnClicked()
  48. {
  49. // If we have a click delegate, call that now.
  50. if (Clicked != null)
  51. Clicked();
  52. // If we are not draggable, spawn a visual feedback effect.
  53. if (!IsDraggable)
  54. DemoGame.SpawnZoomyText(Text, Position + positionOffset);
  55. }
  56. /// <summary>
  57. /// Handles dragging this menu entry from left to right.
  58. /// </summary>
  59. public virtual void OnDragged(float delta)
  60. {
  61. }
  62. }
  63. /// <summary>
  64. /// Menu entry subclass for boolean toggle values.
  65. /// </summary>
  66. class BoolMenuEntry : MenuEntry
  67. {
  68. // Properties.
  69. public bool Value { get; set; }
  70. public string Label { get; set; }
  71. /// <summary>
  72. /// Constructor.
  73. /// </summary>
  74. public BoolMenuEntry(string label)
  75. {
  76. Label = label;
  77. }
  78. /// <summary>
  79. /// Click handler toggles the boolean value.
  80. /// </summary>
  81. public override void OnClicked()
  82. {
  83. Value = !Value;
  84. base.OnClicked();
  85. }
  86. /// <summary>
  87. /// Customize our text string.
  88. /// </summary>
  89. public override string Text
  90. {
  91. get { return Label + " " + (Value ? "on" : "off"); }
  92. set { }
  93. }
  94. }
  95. /// <summary>
  96. /// Menu entry subclass for floating point slider values.
  97. /// </summary>
  98. class FloatMenuEntry : MenuEntry
  99. {
  100. // Properties.
  101. public float Value { get; set; }
  102. /// <summary>
  103. /// Constructor.
  104. /// </summary>
  105. public FloatMenuEntry()
  106. {
  107. IsDraggable = true;
  108. }
  109. /// <summary>
  110. /// Drag handler changes the slider position.
  111. /// </summary>
  112. public override void OnDragged(float delta)
  113. {
  114. const float speed = 1f / 300;
  115. Value = MathHelper.Clamp(Value + delta * speed, 0, 1);
  116. }
  117. /// <summary>
  118. /// Custom draw function displays a slider bar in addition to the item text.
  119. /// </summary>
  120. public override void Draw(SpriteBatch spriteBatch, SpriteFont font, Texture2D blankTexture)
  121. {
  122. base.Draw(spriteBatch, font, blankTexture);
  123. Vector2 size = font.MeasureString(Text);
  124. size.Y /= 2;
  125. Vector2 pos = Position + size;
  126. pos.X += 8;
  127. pos.Y += (Height - font.LineSpacing) / 2;
  128. float w = 480 - Border - pos.X;
  129. spriteBatch.Draw(blankTexture, new Rectangle((int)pos.X, (int)pos.Y - 3, (int)(w * Value), 6), Color);
  130. }
  131. }
  132. }