/// <summary> /// Ask the current state if the state needs to be changed /// and change the state accordingly /// </summary> private void CheckForNewState() { //Holds the instance of the state that is to //be changed to. And if it should be a popUp //or not var newState = _currentState.ChangeState; var popUp = _currentState.PopUpState; //If the newState is null, there will not be any state change, //and therefore we will return if (newState == null) return; if (popUp && !_pausedStates.Contains(newState)) { //Remember the current state _pausedStates.Push(_currentState); //Pause the current state _currentState.Pause(); //Change the state to the new state _currentState = newState; //Show the new state _currentState.Show(); } else { //Check if the newState is in the pause stack and hide the //states that is between the current and the new state State pausedState = null; while (_pausedStates.Count > 0) { pausedState = _pausedStates.Pop(); if (pausedState == newState) break; pausedState.Hide(); pausedState = null; } //Hide the current state _currentState.Hide(); //set the new state to the current state _currentState = newState; //Resume the current state if it was found in the paused states //else start it from the begining through show() //If pausedState != null then the newState exists in the _pausedStates if (pausedState != null) _currentState.Resume(); else _currentState.Show(); } }