示例#1
0
        /// <summary>
        /// Update a Flight Vessel axis control (including throttle) by name
        /// </summary>
        /// <param name="Mapping">The mapping for the axis to update</param>
        /// <param name="value">The value to set, if override</param>
        /// <param name="factor">The factor for the computed value</param>
        public static void UpdateAxis(AltMapping Mapping, float value, float factor)
        {
            FieldInfo field = typeof(FlightCtrlState).GetField(Mapping.Action);
            if (field == null)
            {
                print("AltInput: '" + Mapping.Action + "' is not a valid Axis name");
                return;
            }
            Boolean isThrottle = Mapping.Action.EndsWith("Throttle");
            if (Mapping.Type == MappingType.Delta)
                value += (float)field.GetValue(UpdatedState);
            else if (isThrottle)
                value = (value + 1.0f) / 2.0f;

            value *= factor;
            value = Mathf.Clamp(value, isThrottle?0.0f:-1.0f, +1.0f);
            field.SetValue(UpdatedState, value);
        }
示例#2
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;
            }
        }
示例#3
0
 private void ParseMapping(String Section, String Name, AltMapping[] Mapping, int mode)
 {
     // Try to read a mapping from the common/.Flight section first
     var ConfigData = ini.IniReadValue(Section, Name);
     if ((mode != 0) && (ConfigData == ""))
         ConfigData = ini.IniReadValue(Section + "." + GameState.ModeName[0], Name);
     // Then check for an override
     var Override = ini.IniReadValue(Section + "." + GameState.ModeName[mode], Name);
     if (Override != "")
         ConfigData = Override;
     try
     {
         String[] MappingData = ConfigData.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
         if (MappingData[0].EndsWith(".Delta"))
         {
             Mapping[mode].Type = MappingType.Delta;
             Mapping[mode].Action = MappingData[0].Remove(MappingData[0].IndexOf(".Delta"));
             float.TryParse(MappingData[1], out Mapping[mode].Value);
         }
         else if (MappingData.Length == 1)
         {
             Mapping[mode].Type = MappingType.Range;
             Mapping[mode].Action = MappingData[0];
         }
         else
         {
             Mapping[mode].Type = MappingType.Absolute;
             Mapping[mode].Action = MappingData[0];
             float.TryParse(MappingData[1], out Mapping[mode].Value);
         }
     } catch (Exception) { }
 }