/// <summary>
 /// Seamlessly changes between two screens
 /// </summary>
 /// <param name="focusedScreen">Currently focused screen</param>
 /// <param name="exchangedFor">New screen</param>
 private static void SeamlesslyChangeScreen(MyGuiScreenBase focusedScreen, MyGuiScreenBase exchangedFor)
 {
     focusedScreen.SkipTransition = true;
     focusedScreen.CloseScreen();
     exchangedFor.SkipTransition = true;
     MyScreenManager.AddScreenNow(exchangedFor);
 }
        protected override void OnActionCompleted(IAsyncResult asyncResult, MySectorServiceClient client)
        {
            try
            {
                byte[] result        = client.EndLoadSectorObjects(asyncResult);
                var    sectorBuilder = MyMwcObjectBuilder_Base.FromBytes <MyMwcObjectBuilder_Sector>(result);

                m_parentScreen.CloseScreen();
                MyGuiManager.AddScreen(new MyGuiScreenEditorCopyTool(sectorBuilder));
            }
            catch (FaultException <MyCustomFault> faultException)
            {
                if (faultException.Detail.ErrorCode == MyCustomFaultCode.UserNotFound)
                {
                    MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, MyTextsWrapperEnum.PlayerNotFound, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                }
                else if (faultException.Detail.ErrorCode == MyCustomFaultCode.SectorNotFound)
                {
                    MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, MyTextsWrapperEnum.SectorNotFound, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                }
                else
                {
                    throw faultException;
                }
            }

            CloseScreen();
        }
        void HandleRegister()
        {
            if (m_parentScreen != null)
            {
                m_parentScreen.CloseScreen();
            }

            if (m_openAfterSuccesfullRegistration != null)
            {
                if (m_openAfterSuccesfullRegistration is MyGuiScreenLogin)
                {
                    MyGuiScreenLogin loginScreen = (MyGuiScreenLogin)m_openAfterSuccesfullRegistration;
                    loginScreen.SetUsernameAndPassword(m_playerName, m_password);
                    loginScreen.OnOkClick(null);
                }
            }
        }
        private void AddGroupBox(String text, Type screenType, List <MyGuiControlBase> controlGroup)
        {
            MyGuiControlCheckbox checkBox = AddCheckBox(text, true, null, controlGroup: controlGroup);

            checkBox.IsChecked = s_activeScreen != null && s_activeScreen.GetType() == screenType;
            checkBox.UserData  = screenType;

            s_groupList.Add(checkBox);

            checkBox.IsCheckedChanged += delegate(MyGuiControlCheckbox sender)
            {
                var senderScreenType = sender.UserData as Type;
                if (sender.IsChecked)
                {
                    foreach (MyGuiControlCheckbox chb in s_groupList)
                    {
                        if (chb != sender)
                        {
                            chb.IsChecked = false;
                        }
                    }
                    var newScreen = (MyGuiScreenBase)Activator.CreateInstance(senderScreenType);
                    newScreen.Closed += (source) =>
                    {
                        if (source == s_activeScreen)
                        {
                            s_activeScreen = null;
                        }
                    };
                    MyGuiSandbox.AddScreen(newScreen);
                    s_activeScreen = newScreen;
                }
                else if (s_activeScreen != null && s_activeScreen.GetType() == senderScreenType)
                {
                    s_activeScreen.CloseScreen();
                }
            };
        }
        public override bool Update(bool hasFocus)
        {
            if (base.Update(hasFocus) == false)
            {
                return(false);
            }

            //  Only continue if this screen is really open (not closing or closed)
            if (GetState() != MyGuiScreenState.OPENED)
            {
                return(false);
            }

            if (LoginResponse.HasValue)
            {
                MyMwcLog.WriteLine("Login Response: " + LoginResponse.Value.ToString());

                if (LoginResponse.Value == MyMwcLoginResponseResultEnum.OK)
                {
                    //  Login successful!
                    MyConfig.LastLoginWasSuccessful = true;
                    MyConfig.Save();

                    if (m_closeAfterSuccesfulLogin != null)
                    {
                        m_closeAfterSuccesfulLogin.CloseScreen();
                    }

                    if (CloseScreenBeforeCallingHandler)
                    {
                        CloseScreenNow();
                    }

                    if (m_openAfterSuccessfulLogin != null)
                    {
                        MyGuiManager.AddScreen(m_openAfterSuccessfulLogin);
                    }
                    else if (m_callAfterSuccessfulLogin != null)
                    {
                        m_callAfterSuccessfulLogin();
                    }

                    if (!CloseScreenBeforeCallingHandler)
                    {
                        CloseScreen();
                    }
                }
                else
                {
                    if (LoginResponse.Value == MyMwcLoginResponseResultEnum.USER_ALREADY_LOGGED_IN)
                    {
                        MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, MyTextsWrapperEnum.CantLoginPlayerAlreadyLoggedIn, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                    }
                    else if (LoginResponse.Value == MyMwcLoginResponseResultEnum.WRONG_USERNAME_OR_PASSWORD)
                    {
                        MyTextsWrapperEnum wrongMessage = MyTextsWrapperEnum.CantLoginWrongPlayerNameOrPassword;
                        if (MyMwcFinalBuildConstants.IS_PUBLIC)
                        {
                            wrongMessage = MyTextsWrapperEnum.CantLoginWrongPlayerNameOrPasswordPublic;
                        }
                        MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, wrongMessage, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                    }
                    else if (LoginResponse.Value == MyMwcLoginResponseResultEnum.USERNAME_OR_PASSWORD_EMPTY)
                    {
                        MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, MyTextsWrapperEnum.CantLoginEmptyUsernameOrPassword, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                    }
                    else if (LoginResponse.Value == MyMwcLoginResponseResultEnum.ACCESS_RESTRICTED && !MyMwcFinalBuildConstants.IS_PUBLIC)
                    {
                        MyTextsWrapperEnum restrictedMessage = MyTextsWrapperEnum.CantLoginAccessRestrictedDevelop;
                        if (MyMwcFinalBuildConstants.IS_TEST)
                        {
                            restrictedMessage = MyTextsWrapperEnum.CantLoginAccessRestrictedTest;
                        }
                        MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, restrictedMessage, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                    }
                    else if (LoginResponse.Value == MyMwcLoginResponseResultEnum.WRONG_CLIENT_VERSION)
                    {
                        MyTextsWrapperEnum message = MyTextsWrapperEnum.CantLoginClientVersionIsWrong;
                        MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, message, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                    }
                    else if (LoginResponse.Value == MyMwcLoginResponseResultEnum.INVALID_TIME)
                    {
                        MyTextsWrapperEnum message = MyTextsWrapperEnum.InvalidTime;
                        MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, message, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null));
                    }
                    else
                    {
                        MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, MyTextsWrapperEnum.PleaseTryAgain, MyTextsWrapperEnum.MessageBoxNetworkErrorCaption, MyTextsWrapperEnum.Ok, null));
                    }

                    // This is used for quick launch - login progress screen is not created from login screen
                    if (LoginResponse.Value != MyMwcLoginResponseResultEnum.OK && !MyGuiManager.IsLoginScreenOpened())
                    {
                        AddLoginScreen();
                    }

                    CloseScreen();
                }

                MySectorServiceClient.SafeClose();
                return(false);
            }

            return(true);
        }