MenuButton.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace FarseerPhysics.SamplesFramework
  5. {
  6. /// <summary>
  7. /// Helper class represents a single entry in a MenuScreen. By default this
  8. /// just draws the entry text string, but it can be customized to display menu
  9. /// entries in different ways. This also provides an event that will be raised
  10. /// when the menu entry is selected.
  11. /// </summary>
  12. public sealed class MenuButton
  13. {
  14. private Vector2 _baseOrigin;
  15. private bool _flip;
  16. private bool _hover;
  17. /// <summary>
  18. /// The position at which the entry is drawn. This is set by the MenuScreen
  19. /// each frame in Update.
  20. /// </summary>
  21. private Vector2 _position;
  22. private float _scale;
  23. private GameScreen _screen;
  24. /// <summary>
  25. /// Tracks a fading selection effect on the entry.
  26. /// </summary>
  27. /// <remarks>
  28. /// The entries transition out of the selection effect when they are deselected.
  29. /// </remarks>
  30. private float _selectionFade;
  31. private Texture2D _sprite;
  32. /// <summary>
  33. /// Constructs a new menu entry with the specified text.
  34. /// </summary>
  35. public MenuButton(Texture2D sprite, bool flip, Vector2 position, GameScreen screen)
  36. {
  37. _screen = screen;
  38. _scale = 1f;
  39. _sprite = sprite;
  40. _baseOrigin = new Vector2(_sprite.Width / 2f, _sprite.Height / 2f);
  41. _hover = false;
  42. _flip = flip;
  43. Position = position;
  44. }
  45. /// <summary>
  46. /// Gets or sets the position at which to draw this menu entry.
  47. /// </summary>
  48. public Vector2 Position
  49. {
  50. get { return _position; }
  51. set { _position = value; }
  52. }
  53. public bool Hover
  54. {
  55. get { return _hover; }
  56. set { _hover = value; }
  57. }
  58. public GameScreen Screen
  59. {
  60. get { return _screen; }
  61. }
  62. /// <summary>
  63. /// Updates the menu entry.
  64. /// </summary>
  65. public void Update(GameTime gameTime)
  66. {
  67. float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
  68. if (_hover)
  69. {
  70. _selectionFade = Math.Min(_selectionFade + fadeSpeed, 1f);
  71. }
  72. else
  73. {
  74. _selectionFade = Math.Max(_selectionFade - fadeSpeed, 0f);
  75. }
  76. _scale = 1f + 0.1f * _selectionFade;
  77. }
  78. public void Collide(Vector2 position)
  79. {
  80. Rectangle collisonBox = new Rectangle((int)(Position.X - _sprite.Width / 2f),
  81. (int)(Position.Y - _sprite.Height / 2f),
  82. (_sprite.Width),
  83. (_sprite.Height));
  84. if (collisonBox.Contains((int)position.X, (int)position.Y))
  85. {
  86. _hover = true;
  87. }
  88. else
  89. {
  90. _hover = false;
  91. }
  92. }
  93. /// <summary>
  94. /// Draws the menu entry. This can be overridden to customize the appearance.
  95. /// </summary>
  96. public void Draw()
  97. {
  98. SpriteBatch batch = _screen.ScreenManager.SpriteBatch;
  99. Color color = Color.Lerp(Color.White, new Color(255, 210, 0), _selectionFade);
  100. batch.Draw(_sprite, _position - _baseOrigin * _scale, null, color, 0f, Vector2.Zero,
  101. _scale, _flip ? SpriteEffects.FlipVertically : SpriteEffects.None, 0f);
  102. }
  103. }
  104. }