diff options
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions')
3 files changed, 134 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/ExpandTransition.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/ExpandTransition.cs new file mode 100644 index 0000000..32a8cca --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/ExpandTransition.cs @@ -0,0 +1,42 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGame.Extended.Screens.Transitions +{ + public class ExpandTransition : Transition + { + private readonly GraphicsDevice _graphicsDevice; + private readonly SpriteBatch _spriteBatch; + + public ExpandTransition(GraphicsDevice graphicsDevice, Color color, float duration = 1.0f) + : base(duration) + { + Color = color; + + _graphicsDevice = graphicsDevice; + _spriteBatch = new SpriteBatch(graphicsDevice); + } + + public override void Dispose() + { + _spriteBatch.Dispose(); + } + + public Color Color { get; } + + public override void Draw(GameTime gameTime) + { + var halfWidth = _graphicsDevice.Viewport.Width / 2f; + var halfHeight = _graphicsDevice.Viewport.Height / 2f; + var x = halfWidth * (1.0f - Value); + var y = halfHeight * (1.0f - Value); + var width = _graphicsDevice.Viewport.Width * Value; + var height = _graphicsDevice.Viewport.Height * Value; + var rectangle = new RectangleF(x, y, width, height); + + _spriteBatch.Begin(samplerState: SamplerState.PointClamp); + _spriteBatch.FillRectangle(rectangle, Color); + _spriteBatch.End(); + } + } +}
\ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/FadeTransition.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/FadeTransition.cs new file mode 100644 index 0000000..49fab4a --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/FadeTransition.cs @@ -0,0 +1,34 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGame.Extended.Screens.Transitions +{ + public class FadeTransition : Transition + { + private readonly GraphicsDevice _graphicsDevice; + private readonly SpriteBatch _spriteBatch; + + public FadeTransition(GraphicsDevice graphicsDevice, Color color, float duration = 1.0f) + : base(duration) + { + Color = color; + + _graphicsDevice = graphicsDevice; + _spriteBatch = new SpriteBatch(graphicsDevice); + } + + public override void Dispose() + { + _spriteBatch.Dispose(); + } + + public Color Color { get; } + + public override void Draw(GameTime gameTime) + { + _spriteBatch.Begin(samplerState: SamplerState.PointClamp); + _spriteBatch.FillRectangle(0, 0, _graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height, Color * Value); + _spriteBatch.End(); + } + } +} diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/Transition.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/Transition.cs new file mode 100644 index 0000000..8336a04 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Transitions/Transition.cs @@ -0,0 +1,58 @@ +using System; +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Screens.Transitions +{ + public enum TransitionState { In, Out } + + public abstract class Transition : IDisposable + { + private readonly float _halfDuration; + private float _currentSeconds; + + protected Transition(float duration) + { + Duration = duration; + _halfDuration = Duration / 2f; + } + + public abstract void Dispose(); + + public TransitionState State { get; private set; } = TransitionState.Out; + public float Duration { get; } + public float Value => MathHelper.Clamp(_currentSeconds / _halfDuration, 0f, 1f); + + public event EventHandler StateChanged; + public event EventHandler Completed; + + public void Update(GameTime gameTime) + { + var elapsedSeconds = gameTime.GetElapsedSeconds(); + + switch (State) + { + case TransitionState.Out: + _currentSeconds += elapsedSeconds; + + if (_currentSeconds >= _halfDuration) + { + State = TransitionState.In; + StateChanged?.Invoke(this, EventArgs.Empty); + } + break; + case TransitionState.In: + _currentSeconds -= elapsedSeconds; + + if (_currentSeconds <= 0.0f) + { + Completed?.Invoke(this, EventArgs.Empty); + } + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + + public abstract void Draw(GameTime gameTime); + } +}
\ No newline at end of file |