ExpandTransition.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. namespace MonoGame.Extended.Screens.Transitions
  4. {
  5. public class ExpandTransition : Transition
  6. {
  7. private readonly GraphicsDevice _graphicsDevice;
  8. private readonly SpriteBatch _spriteBatch;
  9. public ExpandTransition(GraphicsDevice graphicsDevice, Color color, float duration = 1.0f)
  10. : base(duration)
  11. {
  12. Color = color;
  13. _graphicsDevice = graphicsDevice;
  14. _spriteBatch = new SpriteBatch(graphicsDevice);
  15. }
  16. public override void Dispose()
  17. {
  18. _spriteBatch.Dispose();
  19. }
  20. public Color Color { get; }
  21. public override void Draw(GameTime gameTime)
  22. {
  23. var halfWidth = _graphicsDevice.Viewport.Width / 2f;
  24. var halfHeight = _graphicsDevice.Viewport.Height / 2f;
  25. var x = halfWidth * (1.0f - Value);
  26. var y = halfHeight * (1.0f - Value);
  27. var width = _graphicsDevice.Viewport.Width * Value;
  28. var height = _graphicsDevice.Viewport.Height * Value;
  29. var rectangle = new RectangleF(x, y, width, height);
  30. _spriteBatch.Begin(SpriteSortMode.Deferred, null, samplerState: SamplerState.PointClamp, null, null);
  31. _spriteBatch.FillRectangle(rectangle, Color);
  32. _spriteBatch.End();
  33. }
  34. }
  35. }