diff --git a/AFBW/ControllerPreset.cs b/AFBW/ControllerPreset.cs index 4c8d6e3..dd1b7ae 100644 --- a/AFBW/ControllerPreset.cs +++ b/AFBW/ControllerPreset.cs @@ -136,7 +136,11 @@ public enum ContinuousAction WheelThrottleTrim, CameraX, CameraY, - CameraZoom + CameraZoom, + Custom1, + Custom2, + Custom3, + Custom4, } public class DiscreteActionEntry diff --git a/AFBW/FlightManager.cs b/AFBW/FlightManager.cs index b118f50..20dadbb 100644 --- a/AFBW/FlightManager.cs +++ b/AFBW/FlightManager.cs @@ -27,6 +27,13 @@ class FlightManager public FlightProperty m_CameraPitch = new FlightProperty(-1.0f, 1.0f); public FlightProperty m_CameraHeading = new FlightProperty(-1.0f, 1.0f); public FlightProperty m_CameraZoom = new FlightProperty(-1.0f, 1.0f); + public FlightProperty[] m_CustomAxes = new[] { + new FlightProperty(-1.0f, 1.0f), + new FlightProperty(-1.0f, 1.0f), + new FlightProperty(-1.0f, 1.0f), + new FlightProperty(-1.0f, 1.0f), + }; + // VesselAutopilot.VesselSAS public static float controlDetectionThreshold = 0.05f; @@ -91,6 +98,8 @@ public void OnFlyByWire(FlightCtrlState state) state.roll = Utility.Clamp(state.roll + state.rollTrim, -1.0f, 1.0f); } } + + UpdateAxisGroups(FlightGlobals.ActiveVessel, state); } private void UpdateAxes(ControllerConfiguration config, FlightCtrlState state) @@ -184,6 +193,11 @@ private void UpdateFlightProperties(FlightCtrlState state) } state.wheelSteer = Utility.Clamp(state.wheelSteer + m_WheelSteer.Update(), -1.0f, 1.0f); + + for (int i = 0; i < Math.Min(m_CustomAxes.Length, state.custom_axes.Length); ++i) + { + state.custom_axes[i] = Utility.Clamp(state.custom_axes[i] + m_CustomAxes[i].Update(), -1.0f, 1.0f); + } } private void ZeroOutFlightProperties() @@ -249,6 +263,44 @@ private void ZeroOutFlightProperties() { m_CameraZoom.SetValue(0.0f); } + + for (int i = 0; i < m_CustomAxes.Length; ++i) + { + if (!m_CustomAxes[i].HasIncrement()) + { + m_CustomAxes[i].SetValue(0.0f); + } + } + } + + // Mirror the flight state into the vessel's axis group modules + // + // Without this, the axes will be updated for physics processing but not axis control bindings. + // Lack of this used to result in the main throttle updating but not affecting axis control groups. + // + // Optimization may be possible by caching the module fetch step for each vessel. + private void UpdateAxisGroups(Vessel vessel, FlightCtrlState state) + { + for (int m = 0; m < vessel.vesselModules.Count; ++m) + { + if (vessel.vesselModules[m] is AxisGroupsModule) + { + var agModule = vessel.vesselModules[m] as AxisGroupsModule; + agModule.UpdateAxisGroup(KSPAxisGroup.Pitch, state.pitch); + agModule.UpdateAxisGroup(KSPAxisGroup.Yaw, state.yaw); + agModule.UpdateAxisGroup(KSPAxisGroup.Roll, state.roll); + agModule.UpdateAxisGroup(KSPAxisGroup.TranslateX, state.X); + agModule.UpdateAxisGroup(KSPAxisGroup.TranslateY, state.Y); + agModule.UpdateAxisGroup(KSPAxisGroup.TranslateZ, state.Z); + agModule.UpdateAxisGroup(KSPAxisGroup.MainThrottle, state.mainThrottle); + agModule.UpdateAxisGroup(KSPAxisGroup.WheelSteer, state.wheelSteer); + agModule.UpdateAxisGroup(KSPAxisGroup.WheelThrottle, state.wheelThrottle); + agModule.UpdateAxisGroup(KSPAxisGroup.Custom01, state.custom_axes[0]); + agModule.UpdateAxisGroup(KSPAxisGroup.Custom02, state.custom_axes[1]); + agModule.UpdateAxisGroup(KSPAxisGroup.Custom03, state.custom_axes[2]); + agModule.UpdateAxisGroup(KSPAxisGroup.Custom04, state.custom_axes[3]); + } + } } public void EvaluateDiscreteAction(ControllerConfiguration controller, DiscreteAction action, FlightCtrlState state) @@ -718,6 +770,18 @@ public void EvaluateContinuousAction(ControllerConfiguration controller, Continu case ContinuousAction.CameraZoom: m_CameraZoom.Increment(value); return; + case ContinuousAction.Custom1: + m_CustomAxes[0].SetValue(value); + return; + case ContinuousAction.Custom2: + m_CustomAxes[1].SetValue(value); + return; + case ContinuousAction.Custom3: + m_CustomAxes[2].SetValue(value); + return; + case ContinuousAction.Custom4: + m_CustomAxes[3].SetValue(value); + return; } } diff --git a/AFBW/Stringify.cs b/AFBW/Stringify.cs index a8cc8f9..3c497f9 100644 --- a/AFBW/Stringify.cs +++ b/AFBW/Stringify.cs @@ -225,6 +225,14 @@ public static string ContinuousActionToString(ContinuousAction action) return "Camera Y"; case ContinuousAction.CameraZoom: return "Camera Zoom"; + case ContinuousAction.Custom1: + return "Custom Axis 1"; + case ContinuousAction.Custom2: + return "Custom Axis 2"; + case ContinuousAction.Custom3: + return "Custom Axis 3"; + case ContinuousAction.Custom4: + return "Custom Axis 4"; default: return "Unknown Action"; } diff --git a/controller_test_tool/ControllerTestTool/ControllerPreset.cs b/controller_test_tool/ControllerTestTool/ControllerPreset.cs index e5451f9..2a97b16 100644 --- a/controller_test_tool/ControllerTestTool/ControllerPreset.cs +++ b/controller_test_tool/ControllerTestTool/ControllerPreset.cs @@ -115,7 +115,11 @@ public enum ContinuousAction WheelThrottleTrim, CameraX, CameraY, - CameraZoom + CameraZoom, + Custom1, + Custom2, + Custom3, + Custom4, } public class ControllerPreset diff --git a/controller_test_tool/ControllerTestTool/Stringify.cs b/controller_test_tool/ControllerTestTool/Stringify.cs index b4cc5fe..2481b28 100644 --- a/controller_test_tool/ControllerTestTool/Stringify.cs +++ b/controller_test_tool/ControllerTestTool/Stringify.cs @@ -183,6 +183,14 @@ public static string ContinuousActionToString(ContinuousAction action) return "Camera Y"; case ContinuousAction.CameraZoom: return "Camera zoom"; + case ContinuousAction.Custom1: + return "Custom Axis 1"; + case ContinuousAction.Custom2: + return "Custom Axis 2"; + case ContinuousAction.Custom3: + return "Custom Axis 3"; + case ContinuousAction.Custom4: + return "Custom Axis 4"; default: return "Unknown action"; }