public void Undo()
        {
            try {
                RenderingManager.RemoveFromPostDrawQueue(0, Draw);

                if (mBackup == null)
                {
                    return;
                }

                ScreenSafeUISlideTab tab = mBackup.TimeQuadrant.timeQuadrantTab;

                if (tab.collider != null)
                {
                    ((BoxCollider)tab.collider).center = mBackup.Center;
                }

                List <Transform> children = new List <Transform>();

                foreach (Transform child in tab.transform)
                {
                    children.Add(child);
                    child.parent = tab.transform.parent;
                }

                tab.transform.localScale = mBackup.Scale;
                tab.expandedPos          = mBackup.ExpandedPosition;
                tab.collapsedPos         = mBackup.CollapsedPosition;
                foreach (Transform child in children)
                {
                    child.localPosition += new Vector3(0, -0.013f, 0);
                }
                tab.Collapse();
                tab.renderer.material.mainTexture = mBackup.Texture;

                foreach (Transform child in children)
                {
                    child.parent = tab.transform;
                }

                mBackup = null;
            } catch (Exception) { }
        }
        public void Patch(TimeWarp timeQuadrant)
        {
            if (mBackup != null)
            {
                throw new InvalidOperationException("Patcher is already in use.");
            }
            ScreenSafeUISlideTab tab = timeQuadrant.timeQuadrantTab;

            mBackup = new Backup()
            {
                TimeQuadrant      = timeQuadrant,
                Texture           = tab.renderer.material.mainTexture,
                Scale             = tab.transform.localScale,
                Center            = ((BoxCollider)tab.collider).center,
                ExpandedPosition  = tab.expandedPos,
                CollapsedPosition = tab.collapsedPos,
            };

            List <Transform> children = new List <Transform>();

            foreach (Transform child in tab.transform)
            {
                children.Add(child);
            }

            foreach (Transform child in children)
            {
                child.parent = tab.transform.parent;
            }

            // Set the new texture
            Texture2D newTexture;

            RTUtil.LoadImage(out newTexture, "texTimeQuadrant.png");
            tab.renderer.material.mainTexture = newTexture;

            // Apply new scale, positions
            tab.transform.localScale = new Vector3(tab.transform.localScale.x,
                                                   tab.transform.localScale.y,
                                                   tab.transform.localScale.z *
                                                   1.3970588235294117647058823529412f);
            tab.collapsedPos += new Vector3(0, -0.013f, 0);
            tab.expandedPos  += new Vector3(0, -0.013f, 0);
            foreach (Transform child in children)
            {
                child.localPosition += new Vector3(0, 0.013f, 0);
            }
            tab.Expand();

            foreach (Transform child in children)
            {
                child.parent = tab.transform;
            }

            ((BoxCollider)tab.collider).center += new Vector3(0, 0, -0.37f);

            ScreenSafeGUIText text =
                tab.transform.FindChild("MET timer").GetComponent <ScreenSafeGUIText>();

            mTextStyle          = new GUIStyle(text.textStyle);
            mTextStyle.fontSize = (int)(text.textSize * ScreenSafeUI.PixelRatio);

            RenderingManager.AddToPostDrawQueue(0, Draw);
        }
Beispiel #3
0
        /// <summary>
        /// Update a Flight Vessel button control (including KSPActionGroups) by name
        /// </summary>
        /// <param name="Name">The name of the axis to update</param>
        /// <param name="value">The value to set</param>
        public static void UpdateButton(AltMapping Mapping, float value)
        {
            int i;

            if (Mapping.Action == null)
            {
                return;
            }

            // If we are in a time warp, drop all actions besides the ones we authorise below
            if (TimeWarp.CurrentRate != 1)
            {
                switch (Mapping.Action)
                {
                case "increaseWarp":
                case "decreaseWarp":
                case "switchMode":
                case "switchView":
                case "toggleMapView":
                    break;

                default:
                    return;
                }
            }

            // Check if our mapping is a FlightCtrlState axis
            if (AxisFields.Where(item => item.Name == Mapping.Action).Any())
            {
                UpdateAxis(Mapping, value * Mapping.Value, 1.0f);
                return;
            }

            // Most actions only need to occur when the button state is pressed.
            // We handle the few that don't here
            if (value < 0.5)
            {
                switch (Mapping.Action)
                {
                case "overrideRCS":
                    FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.RCS, false);
                    break;

                case "activateBrakes":
                    FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, false);
                    break;

                case "overrideSAS":
                    FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
                    break;

                case "activateCustom01":
                case "activateCustom02":
                case "activateCustom03":
                case "activateCustom04":
                case "activateCustom05":
                case "activateCustom06":
                case "activateCustom07":
                case "activateCustom08":
                case "activateCustom09":
                case "activateCustom10":
                    int.TryParse(Mapping.Action.Substring("activateCustom##".Length), out i);
                    if (i > 0)
                    {
                        FlightGlobals.ActiveVessel.ActionGroups.SetGroup((KSPActionGroup)(128 << i), true);
                    }
                    break;
                }
                return;
            }

            switch (Mapping.Action)
            {
            case "increaseWarp":
                // Do a bunch of checks to see if we can increase the warp rate:
                if (TimeWarp.CurrentRateIndex + 1 == TimeWarp.fetch.warpRates.Length)
                {
                    break;     // Already at max warp
                }
                if (!FlightGlobals.ActiveVessel.LandedOrSplashed)
                {
                    CelestialBody mainBody           = FlightGlobals.getMainBody();
                    double        instantAltitudeASL = (FlightGlobals.ActiveVessel.CoM - mainBody.position).magnitude - mainBody.Radius;
                    if (TimeWarp.fetch.GetAltitudeLimit(TimeWarp.CurrentRateIndex + 1, mainBody) > instantAltitudeASL)
                    {
                        break;     // Altitude too low to increase warp
                    }
                }
                if (TimeWarp.fetch.warpRates[TimeWarp.CurrentRateIndex] != TimeWarp.CurrentRate)
                {
                    break;     // Most recent warp change is not yet complete
                }
                TimeWarp.SetRate(TimeWarp.CurrentRateIndex + 1, false);
                break;

            case "decreaseWarp":
                if (TimeWarp.CurrentRateIndex == 0)
                {
                    break;     // Already at minimum warp
                }
                TimeWarp.SetRate(TimeWarp.CurrentRateIndex - 1, false);
                break;

            case "switchMode":
                Mode NextMode = GetNextMode();
                if (NextMode != CurrentMode)
                {
                    // Ensure that we reset our controls and buttons before switching
                    // TODO: Drop ResetDevice() and check that no buttons/POVs are active besides mode switch instead
                    CurrentDevice.ResetDevice();
                    CurrentMode = NextMode;
                    ScreenMessages.PostScreenMessage("Input Mode: " + ModeName[(int)CurrentMode],
                                                     1f, ScreenMessageStyle.UPPER_CENTER);
                }
                break;

            case "switchView":
                FlightCamera fc = FlightCamera.fetch;
                fc.SetNextMode();
                break;

            case "toggleMapView":
                if (MapView.MapIsEnabled)
                {
                    MapView.ExitMapView();
                }
                else
                {
                    MapView.EnterMapView();
                }
                break;

            case "toggleNavBall":
                // MapView.MapCollapse_navBall is not currently exposed by the API
                // so we need to use reflection
                ScreenSafeUISlideTab navball = (ScreenSafeUISlideTab)typeof(MapView).
                                               GetField("MapCollapse_navBall").GetValue(MapView.fetch);
                if (navball.expanded)
                {
                    navball.Collapse();
                }
                else
                {
                    navball.Expand();
                }
                break;

            case "activateStaging":
                Staging.ActivateNextStage();
                break;

            case "toggleGears":
                FlightGlobals.ActiveVessel.ActionGroups.ToggleGroup(KSPActionGroup.Gear);
                break;

            case "toggleLights":
                FlightGlobals.ActiveVessel.ActionGroups.ToggleGroup(KSPActionGroup.Light);
                break;

            case "overrideRCS":
                FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.RCS, true);
                break;

            case "toggleRCS":
                FlightGlobals.ActiveVessel.ActionGroups.ToggleGroup(KSPActionGroup.RCS);
                break;

            case "overrideSAS":
                FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.SAS, true);
                break;

            case "toggleSAS":
                FlightGlobals.ActiveVessel.ActionGroups.ToggleGroup(KSPActionGroup.SAS);
                break;

            case "toggleAbort":
                FlightGlobals.ActiveVessel.ActionGroups.ToggleGroup(KSPActionGroup.Abort);
                break;

            case "activateBrakes":
                FlightGlobals.ActiveVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, true);
                break;

            case "toggleBrakes":
                FlightGlobals.ActiveVessel.ActionGroups.ToggleGroup(KSPActionGroup.Brakes);
                break;

            case "killThrottle":
                UpdatedState.mainThrottle = 0.0f;
                break;

            case "fullThrottle":
                UpdatedState.mainThrottle = 1.0f;
                break;

            case "toggleCustom01":
            case "toggleCustom02":
            case "toggleCustom03":
            case "toggleCustom04":
            case "toggleCustom05":
            case "toggleCustom06":
            case "toggleCustom07":
            case "toggleCustom08":
            case "toggleCustom09":
            case "toggleCustom10":
                int.TryParse(Mapping.Action.Substring("toggleCustom##".Length), out i);
                if (i > 0)
                {
                    FlightGlobals.ActiveVessel.ActionGroups.ToggleGroup((KSPActionGroup)(128 << i));
                }
                break;

            case "activateCustom01":
            case "activateCustom02":
            case "activateCustom03":
            case "activateCustom04":
            case "activateCustom05":
            case "activateCustom06":
            case "activateCustom07":
            case "activateCustom08":
            case "activateCustom09":
            case "activateCustom10":
                int.TryParse(Mapping.Action.Substring("activateCustom##".Length), out i);
                if (i > 0)
                {
                    FlightGlobals.ActiveVessel.ActionGroups.SetGroup((KSPActionGroup)(128 << i), true);
                }
                break;

            default:
                print("AltInput: Unhandled action '" + Mapping.Action + "'");
                break;
            }
        }