private BoosterGuidanceCore CheckCore(Vessel vessel)
 {
     if (core == null)
     {
         core = BoosterGuidanceCore.GetBoosterGuidanceCore(vessel);
         if (core != null)
         {
             UpdateFromCore();
             Targets.SetVisibility(showTargets, showTargets && core.Enabled() && (FlightGlobals.ActiveVessel == core.vessel));
         }
         else
         {
             return(core);
         }
     }
     else
     {
         if (core.vessel != vessel)
         {
             Debug.Log("[BoosterGuidance] core.vessel!=vessel vessel=" + vessel + " map=" + MapView.MapIsEnabled);
             // Get new BoosterGuidanceCore as vessel changed
             core = BoosterGuidanceCore.GetBoosterGuidanceCore(vessel);
             UpdateFromCore();
         }
     }
     if (core == null)
     {
         Debug.Log("[BoosterGuidance] Vessel " + vessel.name + " has no BoosterGuidanceCore");
     }
     return(core);
 }
 void OnUpdate()
 {
     // Set visibility of targets
     Targets.InitTargets(); // ensure updated with map switch
     Targets.SetVisibility(showTargets, core.Enabled() && showTargets);
     if (pickingPositionTarget)
     {
         OnPickingPositionTarget();
     }
 }
        bool MainTab(int windowID)
        {
            bool targetChanged        = false;
            BoosterGuidanceCore core  = CheckCore(FlightGlobals.ActiveVessel);
            BLControllerPhase   phase = core.Phase();

            // Target:

            // Draw any Controls inside the window here
            GUILayout.Label(Localizer.Format("#BoosterGuidance_Target"));//Target coordinates:

            GUILayout.BeginHorizontal();
            double step = 1.0 / (60 * 60); // move by 1 arc second

            tgtLatitude.DrawEditGUI(EditableAngle.Direction.NS);
            if (GUILayout.Button("▲"))
            {
                tgtLatitude  += step;
                targetChanged = true;
            }
            if (GUILayout.Button("▼"))
            {
                tgtLatitude  -= step;
                targetChanged = true;
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            tgtLongitude.DrawEditGUI(EditableAngle.Direction.EW);
            if (GUILayout.Button("◄"))
            {
                tgtLongitude -= step;
                targetChanged = true;
            }
            if (GUILayout.Button("►"))
            {
                tgtLongitude += step;
                targetChanged = true;
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button(Localizer.Format("#BoosterGuidance_PickTarget")))
            {
                PickTarget();
            }
            if (GUILayout.Button("Set Here"))
            {
                SetTargetHere();
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            showTargets = GUILayout.Toggle(showTargets, Localizer.Format("#BoosterGuidance_ShowTargets"));

            bool prevLogging = core.logging;
            // TODO
            string filename = FlightGlobals.ActiveVessel.name;

            filename         = filename.Replace(" ", "_");
            filename         = filename.Replace("(", "");
            filename         = filename.Replace(")", "");
            core.logFilename = filename;
            core.logging     = GUILayout.Toggle(core.logging, Localizer.Format("#BoosterGuidance_Logging"));
            if (core.Enabled())
            {
                if ((!prevLogging) && (core.logging)) // logging switched on
                {
                    core.StartLogging();
                }
                if ((prevLogging) && (!core.logging)) // logging switched off
                {
                    core.StopLogging();
                }
            }
            GUILayout.EndHorizontal();

            // Info box
            GUILayout.BeginHorizontal();
            GUILayout.Label(core.Info());
            GUILayout.EndHorizontal();

            // Boostback
            SetEnabledColors((phase == BLControllerPhase.BoostBack) || (phase == BLControllerPhase.Unset));
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(new GUIContent(Localizer.Format("#BoosterGuidance_Boostback"), "Enable thrust towards target when out of atmosphere")))
            {
                EnableGuidance(BLControllerPhase.BoostBack);
            }
            GUILayout.EndHorizontal();

            // Coasting
            SetEnabledColors((phase == BLControllerPhase.Coasting) || (phase == BLControllerPhase.Unset));
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(new GUIContent(Localizer.Format("#BoosterGuidance_Coasting"), "Turn to retrograde attitude and wait for Aero Descent phase")))
            {
                EnableGuidance(BLControllerPhase.Coasting);
            }
            GUILayout.EndHorizontal();

            // Re-Entry Burn
            SetEnabledColors((phase == BLControllerPhase.ReentryBurn) || (phase == BLControllerPhase.Unset));
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(new GUIContent(Localizer.Format("#BoosterGuidance_ReentryBurn"), "Ignite engine on re-entry to reduce overheating")))
            {
                EnableGuidance(BLControllerPhase.ReentryBurn);
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GuiUtils.SimpleTextBox(Localizer.Format("#BoosterGuidance_EnableAltitude"), reentryBurnAlt, "m", 65);
            core.reentryBurnAlt = reentryBurnAlt;
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GuiUtils.SimpleTextBox(Localizer.Format("#BoosterGuidance_TargetSpeed"), reentryBurnTargetSpeed, "m/s", 40);
            core.reentryBurnTargetSpeed = reentryBurnTargetSpeed;
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Steer", GUILayout.Width(40));
            core.reentryBurnSteerKp = Mathf.Clamp(core.reentryBurnSteerKp, 0, maxReentryGain);
            core.reentryBurnSteerKp = GUILayout.HorizontalSlider(core.reentryBurnSteerKp, 0, maxReentryGain);
            GUILayout.Label(((int)(core.reentryBurnMaxAoA)).ToString() + "°(max)", GUILayout.Width(60));
            GUILayout.EndHorizontal();

            // Aero Descent
            SetEnabledColors((phase == BLControllerPhase.AeroDescent) || (phase == BLControllerPhase.Unset));
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(new GUIContent(Localizer.Format("#BoosterGuidance_AeroDescent"), "No thrust aerodynamic descent, steering with gridfins within atmosphere")))
            {
                EnableGuidance(BLControllerPhase.AeroDescent);
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Steer", GUILayout.Width(40));
            core.aeroDescentSteerKp = Mathf.Clamp(core.aeroDescentSteerKp, 0, maxAeroDescentGain);
            core.aeroDescentSteerKp = GUILayout.HorizontalSlider(core.aeroDescentSteerKp, 0, maxAeroDescentGain); // max turn 2 degrees for 100m error
            GUILayout.Label(((int)core.aeroDescentMaxAoA).ToString() + "°(max)", GUILayout.Width(60));
            GUILayout.EndHorizontal();

            // Landing Burn
            SetEnabledColors((phase == BLControllerPhase.LandingBurn) || (phase == BLControllerPhase.Unset));
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(Localizer.Format("#BoosterGuidance_LandingBurn")))
            {
                EnableGuidance(BLControllerPhase.LandingBurn);
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label(Localizer.Format("#BoosterGuidance_EnableAltitude"));
            String text = "n/a";

            if (core.Enabled())
            {
                if (core.LandingBurnHeight() > 0)
                {
                    text = ((int)(core.LandingBurnHeight() + tgtAlt)).ToString() + "m";
                }
                else
                {
                    if (core.LandingBurnHeight() < 0)
                    {
                        text = Localizer.Format("#BoosterGuidance_TooHeavy");
                    }
                }
            }
            GUILayout.Label(text, GUILayout.Width(60));
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label(Localizer.Format("#BoosterGuidance_Engines"));
            if (numLandingBurnEngines == Localizer.Format("#BoosterGuidance_Current"))
            {
                GUILayout.Label(numLandingBurnEngines);
            }
            else
            {
                GUILayout.Label(numLandingBurnEngines);
            }
            if (numLandingBurnEngines == "current")                             // Save active engines
            {
                if (GUILayout.Button(Localizer.Format("#BoosterGuidance_Set"))) // Set to currently active engines
                {
                    numLandingBurnEngines = core.SetLandingBurnEngines();
                }
            }
            else
            {
                if (GUILayout.Button(Localizer.Format("#BoosterGuidance_Unset"))) // Set to currently active engines
                {
                    numLandingBurnEngines = core.UnsetLandingBurnEngines();
                }
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Steer", GUILayout.Width(40));
            core.landingBurnSteerKp = Mathf.Clamp(core.landingBurnSteerKp, 0, maxLandingBurnGain);
            core.landingBurnSteerKp = GUILayout.HorizontalSlider(core.landingBurnSteerKp, 0, maxLandingBurnGain);
            string max = Localizer.Format("#BoosterGuidance_Max");

            GUILayout.Label(((int)(core.landingBurnMaxAoA)).ToString() + "°(" + max + ")", GUILayout.Width(60));
            GUILayout.EndHorizontal();

            // Activate guidance
            SetEnabledColors(true); // back to normal
            GUILayout.BeginHorizontal();
            if (!core.Enabled())
            {
                if (GUILayout.Button(Localizer.Format("#BoosterGuidance_EnableGuidance")))
                {
                    core.EnableGuidance();
                }
            }
            else
            {
                if (GUILayout.Button(Localizer.Format("#BoosterGuidance_DisableGuidance")))
                {
                    core.DisableGuidance();
                }
            }
            GUILayout.EndHorizontal();

            GUI.DragWindow();
            return((GUI.changed) || targetChanged);
        }