<commit_before>#region Using

using System;
using System.Collections.Generic;
using System.Reflection;
using FlatRedBall.Graphics;
using FlatRedBall.ManagedSpriteGroups;
using FlatRedBall.Math;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Gui;
using FlatRedBall.Utilities;
using FlatRedBall.IO;
using System.Linq;
#if !SILVERLIGHT
#endif

#if WINDOWS_PHONE
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;
#endif

#endregion

namespace FlatRedBall.Screens
{

    public static partial class ScreenManager
    {
        #region Fields

        static bool? mWasFixedTimeStep = null;
        static double? mLastTimeFactor = null;

        private static Screen mCurrentScreen;

        private static bool mSuppressStatePush = false;

        private static bool mWarnIfNotEmptyBetweenScreens = true;

        private static int mNumberOfFramesSinceLastScreenLoad = 0;

        private static Layer mNextScreenLayer;

        // The ScreenManager can be told to ignore certain objects which
        // we recognize will persist from screen to screen.  This should
        // NOT be used as a solution to get around the ScreenManager's check.
        private static PositionedObjectList<Camera> mPersistentCameras = new PositionedObjectList<Camera>();

        private static PositionedObjectList<SpriteFrame> mPersistentSpriteFrames = new PositionedObjectList<SpriteFrame>();

        private static PositionedObjectList<Text> mPersistentTexts = new PositionedObjectList<Text>();

        private static List<IDrawableBatch> mPersistentDrawableBatches = new List<IDrawableBatch>();

        private static Action<Screen> nextCallback;

        #endregion

        #region Properties

        public static Assembly MainAssembly { get; private set; }

        public static Screen CurrentScreen
        {
            get { return mCurrentScreen; }
        }

        public static Layer NextScreenLayer
        {
            get { return mNextScreenLayer; }
        }

        public static PositionedObjectList<Camera> PersistentCameras
        {
            get { return mPersistentCameras; }
        }

        public static PositionedObjectList<SpriteFrame> PersistentSpriteFrames
        {
            get { return mPersistentSpriteFrames; }
        }

        /// <summary>
        /// A list of IDrawableBatch instances which persist inbetween screens. Items in this list
        /// do not result in exceptions if they are not cleaned up inbetween screens.
        /// </summary>
        public static List<IDrawableBatch> PersistentDrawableBatches
        {
            get
            {
                return mPersistentDrawableBatches;
            }
        }

        public static PositionedObjectList<Text> PersistentTexts
        {
            get { return mPersistentTexts; }
        }

        public static List<IWindow> PersistentWindows
        {
            get; private set;
        } = new List<IWindow>();

        public static bool WarnIfNotEmptyBetweenScreens
        {
            get { return mWarnIfNotEmptyBetweenScreens; }
            set { mWarnIfNotEmptyBetweenScreens = value; }
        }
		
		public static bool ShouldActivateScreen
        {
            get;
            set;
        }
		
		public static Action<string> RehydrateAction
		{
			get;
			set;
		}
		
        #endregion

        #region Methods

        #region Public Methods

        #region XML Docs
        /// <summary>
        /// Calls activity on the current screen and checks to see if screen
        /// activity is finished.  If activity is finished, the current Screen's
        /// NextScreen is loaded.
        /// </summary>
        #endregion
        public static void Activity()
        {
            if (mCurrentScreen == null) return;

            mCurrentScreen.Activity(mCurrentScreen.ActivityCallCount == 0);

            mCurrentScreen.ActivityCallCount++;

            if (mCurrentScreen.ActivityCallCount == 1 && mWasFixedTimeStep.HasValue)
            {
                FlatRedBallServices.Game.IsFixedTimeStep = mWasFixedTimeStep.Value;
                TimeManager.TimeFactor = mLastTimeFactor.Value;
            }

            if (mCurrentScreen.IsActivityFinished)
            {
                string type = mCurrentScreen.NextScreen;

                var isFullyQualified = type.Contains(".");
                if(!isFullyQualified)
                {
                    // try to prepend the current type to make the next screen fully qualified:
                    var prepend = mCurrentScreen.GetType().Namespace;
                    type = prepend + "." + type;
                }

                Screen asyncLoadedScreen = mCurrentScreen.mNextScreenToLoadAsync;



                mWasFixedTimeStep = FlatRedBallServices.Game.IsFixedTimeStep;
                mLastTimeFactor = TimeManager.TimeFactor;

                FlatRedBallServices.Game.IsFixedTimeStep = false;
                TimeManager.TimeFactor = 0;

                GuiManager.Cursor.IgnoreInputThisFrame = true;

                mCurrentScreen.Destroy();

                // check to see if there is any leftover data
                CheckAndWarnIfNotEmpty(mCurrentScreen);

                // Let's perform a GC here.  
                GC.Collect();

				// Not sure why this started to freeze on Android in the automated test project
				// on April 22, 2015. I'm commenting it out because I don't think we need to wait
				// for finalizers, and we can just continue on. Maybe try to bring the code back<commit_msg>Reorganized destroy so that the current screen Destroy is called before "last" values are set on the screen, enabling the changing of time factor in Destroy.<commit_after>#region Using

using System;
using System.Collections.Generic;
using System.Reflection;
using FlatRedBall.Graphics;
using FlatRedBall.ManagedSpriteGroups;
using FlatRedBall.Math;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Gui;
using FlatRedBall.Utilities;
using FlatRedBall.IO;
using System.Linq;
#if !SILVERLIGHT
#endif

#if WINDOWS_PHONE
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;
#endif

#endregion

namespace FlatRedBall.Screens
{

    public static partial class ScreenManager
    {
        #region Fields

        static bool? mWasFixedTimeStep = null;
        static double? mLastTimeFactor = null;

        private static Screen mCurrentScreen;

        private static bool mSuppressStatePush = false;

        private static bool mWarnIfNotEmptyBetweenScreens = true;

        private static int mNumberOfFramesSinceLastScreenLoad = 0;

        private static Layer mNextScreenLayer;

        // The ScreenManager can be told to ignore certain objects which
        // we recognize will persist from screen to screen.  This should
        // NOT be used as a solution to get around the ScreenManager's check.
        private static PositionedObjectList<Camera> mPersistentCameras = new PositionedObjectList<Camera>();

        private static PositionedObjectList<SpriteFrame> mPersistentSpriteFrames = new PositionedObjectList<SpriteFrame>();

        private static PositionedObjectList<Text> mPersistentTexts = new PositionedObjectList<Text>();

        private static List<IDrawableBatch> mPersistentDrawableBatches = new List<IDrawableBatch>();

        private static Action<Screen> nextCallback;

        #endregion

        #region Properties

        public static Assembly MainAssembly { get; private set; }

        public static Screen CurrentScreen
        {
            get { return mCurrentScreen; }
        }

        public static Layer NextScreenLayer
        {
            get { return mNextScreenLayer; }
        }

        public static PositionedObjectList<Camera> PersistentCameras
        {
            get { return mPersistentCameras; }
        }

        public static PositionedObjectList<SpriteFrame> PersistentSpriteFrames
        {
            get { return mPersistentSpriteFrames; }
        }

        /// <summary>
        /// A list of IDrawableBatch instances which persist inbetween screens. Items in this list
        /// do not result in exceptions if they are not cleaned up inbetween screens.
        /// </summary>
        public static List<IDrawableBatch> PersistentDrawableBatches
        {
            get
            {
                return mPersistentDrawableBatches;
            }
        }

        public static PositionedObjectList<Text> PersistentTexts
        {
            get { return mPersistentTexts; }
        }

        public static List<IWindow> PersistentWindows
        {
            get; private set;
        } = new List<IWindow>();

        public static bool WarnIfNotEmptyBetweenScreens
        {
            get { return mWarnIfNotEmptyBetweenScreens; }
            set { mWarnIfNotEmptyBetweenScreens = value; }
        }
		
		public static bool ShouldActivateScreen
        {
            get;
            set;
        }
		
		public static Action<string> RehydrateAction
		{
			get;
			set;
		}
		
        #endregion

        #region Methods

        #region Public Methods

        #region XML Docs
        /// <summary>
        /// Calls activity on the current screen and checks to see if screen
        /// activity is finished.  If activity is finished, the current Screen's
        /// NextScreen is loaded.
        /// </summary>
        #endregion
        public static void Activity()
        {
            if (mCurrentScreen == null) return;

            mCurrentScreen.Activity(mCurrentScreen.ActivityCallCount == 0);

            mCurrentScreen.ActivityCallCount++;

            if (mCurrentScreen.ActivityCallCount == 1 && mWasFixedTimeStep.HasValue)
            {
                FlatRedBallServices.Game.IsFixedTimeStep = mWasFixedTimeStep.Value;
                TimeManager.TimeFactor = mLastTimeFactor.Value;
            }

            if (mCurrentScreen.IsActivityFinished)
            {
                string type = mCurrentScreen.NextScreen;

                var isFullyQualified = type.Contains(".");
                if(!isFullyQualified)
                {
                    // try to prepend the current type to make the next screen fully qualified:
                    var prepend = mCurrentScreen.GetType().Namespace;
                    type = prepend + "." + type;
                }

                Screen asyncLoadedScreen = mCurrentScreen.mNextScreenToLoadAsync;



                // October 10, 2020
                // We used to set the time and values before calling
                // destroy. Now we want to call Destroy on the screen first
                // in case Destroy happens to change the time factor:
                mCurrentScreen.Destroy();

                mWasFixedTimeStep = FlatRedBallServices.Game.IsFixedTimeStep;
                mLastTimeFactor = TimeManager.TimeFactor;
                FlatRedBallServices.Game.IsFixedTimeStep = false;
                TimeManager.TimeFactor = 0;
                GuiManager.Cursor.IgnoreInputThisFrame = true;

                //mCurrentScreen.Destroy();

                // check to see if there is any leftover data
                CheckAndWarnIfNotEmpty(mCurrentScreen);

                // Let's perform a GC here.  
                GC.Collect();

				// Not sure why this started to freeze on Android in the automated test project
				// on April 22, 2015. I'm commenting it out because I don't think we need to wait
				// for finalizers, and we can just continue on. Maybe try to bring the code back
{'fraction_non_alphanumeric': 0.06230448383733055, 'fraction_numerical': 0.0022592978797358358, 'mean_word_length': 2.072343833422317, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4151468', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2764, 'n_words': 868}