diff options
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens')
6 files changed, 248 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/GameScreen.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/GameScreen.cs new file mode 100644 index 0000000..7752c02 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/GameScreen.cs @@ -0,0 +1,19 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGame.Extended.Screens +{ + public abstract class GameScreen : Screen + { + protected GameScreen(Game game) + { + Game = game; + } + + public Game Game { get; } + public ContentManager Content => Game.Content; + public GraphicsDevice GraphicsDevice => Game.GraphicsDevice; + public GameServiceContainer Services => Game.Services; + } +}
\ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Screen.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Screen.cs new file mode 100644 index 0000000..59137f4 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/Screen.cs @@ -0,0 +1,17 @@ +using System; +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Screens +{ + public abstract class Screen : IDisposable + { + public ScreenManager ScreenManager { get; internal set; } + + public virtual void Dispose() { } + public virtual void Initialize() { } + public virtual void LoadContent() { } + public virtual void UnloadContent() { } + public abstract void Update(GameTime gameTime); + public abstract void Draw(GameTime gameTime); + } +}
\ No newline at end of file diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/ScreenManager.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/ScreenManager.cs new file mode 100644 index 0000000..ad441c6 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended/Screens/ScreenManager.cs @@ -0,0 +1,78 @@ +using Microsoft.Xna.Framework; +using MonoGame.Extended.Screens.Transitions; + +namespace MonoGame.Extended.Screens +{ + public class ScreenManager : SimpleDrawableGameComponent + { + public ScreenManager() + { + } + + private Screen _activeScreen; + //private bool _isInitialized; + //private bool _isLoaded; + private Transition _activeTransition; + + public void LoadScreen(Screen screen, Transition transition) + { + if(_activeTransition != null) + return; + + _activeTransition = transition; + _activeTransition.StateChanged += (sender, args) => LoadScreen(screen); + _activeTransition.Completed += (sender, args) => + { + _activeTransition.Dispose(); + _activeTransition = null; + }; + } + + public void LoadScreen(Screen screen) + { + _activeScreen?.UnloadContent(); + _activeScreen?.Dispose(); + + screen.ScreenManager = this; + + screen.Initialize(); + + screen.LoadContent(); + + _activeScreen = screen; + } + + public override void Initialize() + { + base.Initialize(); + _activeScreen?.Initialize(); + //_isInitialized = true; + } + + protected override void LoadContent() + { + base.LoadContent(); + _activeScreen?.LoadContent(); + //_isLoaded = true; + } + + protected override void UnloadContent() + { + base.UnloadContent(); + _activeScreen?.UnloadContent(); + //_isLoaded = false; + } + + public override void Update(GameTime gameTime) + { + _activeScreen?.Update(gameTime); + _activeTransition?.Update(gameTime); + } + + public override void Draw(GameTime gameTime) + { + _activeScreen?.Draw(gameTime); + _activeTransition?.Draw(gameTime); + } + } +} 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 |