VirtualButton.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input.Touch;
  5. namespace FarseerPhysics.SamplesFramework
  6. {
  7. public sealed class VirtualButton
  8. {
  9. private Texture2D _sprite;
  10. private Vector2 _origin;
  11. private Rectangle _normal;
  12. private Rectangle _pressed;
  13. private Vector2 _position;
  14. public bool Pressed;
  15. public VirtualButton(Texture2D sprite, Vector2 position, Rectangle normal, Rectangle pressed)
  16. {
  17. _sprite = sprite;
  18. _origin = new Vector2(normal.Width / 2f, normal.Height / 2f);
  19. _normal = normal;
  20. _pressed = pressed;
  21. Pressed = false;
  22. _position = position;
  23. }
  24. public void Update(TouchLocation touchLocation)
  25. {
  26. if (touchLocation.State == TouchLocationState.Pressed ||
  27. touchLocation.State == TouchLocationState.Moved)
  28. {
  29. Vector2 delta = touchLocation.Position - _position;
  30. if (delta.LengthSquared() <= 400f)
  31. {
  32. Pressed = true;
  33. }
  34. }
  35. }
  36. public void Draw(SpriteBatch batch)
  37. {
  38. batch.Draw(_sprite, _position, Pressed ? _pressed : _normal, Color.White, 0f, _origin, 1f, SpriteEffects.None, 0f);
  39. }
  40. }
  41. }