ScreenManager.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Microsoft.Xna.Framework;
  2. using MonoGame.Extended.Screens.Transitions;
  3. namespace MonoGame.Extended.Screens
  4. {
  5. public class ScreenManager : SimpleDrawableGameComponent
  6. {
  7. public ScreenManager()
  8. {
  9. }
  10. private Screen _activeScreen;
  11. //private bool _isInitialized;
  12. //private bool _isLoaded;
  13. private Transition _activeTransition;
  14. public void LoadScreen(Screen screen, Transition transition)
  15. {
  16. if(_activeTransition != null)
  17. return;
  18. _activeTransition = transition;
  19. _activeTransition.StateChanged += (sender, args) => LoadScreen(screen);
  20. _activeTransition.Completed += (sender, args) =>
  21. {
  22. _activeTransition.Dispose();
  23. _activeTransition = null;
  24. };
  25. }
  26. public void LoadScreen(Screen screen)
  27. {
  28. _activeScreen?.UnloadContent();
  29. _activeScreen?.Dispose();
  30. screen.ScreenManager = this;
  31. screen.Initialize();
  32. screen.LoadContent();
  33. _activeScreen = screen;
  34. }
  35. public override void Initialize()
  36. {
  37. base.Initialize();
  38. _activeScreen?.Initialize();
  39. //_isInitialized = true;
  40. }
  41. protected override void LoadContent()
  42. {
  43. base.LoadContent();
  44. _activeScreen?.LoadContent();
  45. //_isLoaded = true;
  46. }
  47. protected override void UnloadContent()
  48. {
  49. base.UnloadContent();
  50. _activeScreen?.UnloadContent();
  51. //_isLoaded = false;
  52. }
  53. public override void Update(GameTime gameTime)
  54. {
  55. _activeScreen?.Update(gameTime);
  56. _activeTransition?.Update(gameTime);
  57. }
  58. public override void Draw(GameTime gameTime)
  59. {
  60. _activeScreen?.Draw(gameTime);
  61. _activeTransition?.Draw(gameTime);
  62. }
  63. }
  64. }