Skip to content

Commit

Permalink
Merge pull request #39 from Dessix/dessix/groundbreaking
Browse files Browse the repository at this point in the history
Add support for Custom Axis Groups
  • Loading branch information
linuxgurugamer committed Dec 26, 2022
2 parents b5d0afb + c5c7570 commit 5827278
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
6 changes: 5 additions & 1 deletion AFBW/ControllerPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public enum ContinuousAction
WheelThrottleTrim,
CameraX,
CameraY,
CameraZoom
CameraZoom,
Custom1,
Custom2,
Custom3,
Custom4,
}

public class DiscreteActionEntry
Expand Down
64 changes: 64 additions & 0 deletions AFBW/FlightManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}

Expand Down
8 changes: 8 additions & 0 deletions AFBW/Stringify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
6 changes: 5 additions & 1 deletion controller_test_tool/ControllerTestTool/ControllerPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public enum ContinuousAction
WheelThrottleTrim,
CameraX,
CameraY,
CameraZoom
CameraZoom,
Custom1,
Custom2,
Custom3,
Custom4,
}

public class ControllerPreset
Expand Down
8 changes: 8 additions & 0 deletions controller_test_tool/ControllerTestTool/Stringify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

0 comments on commit 5827278

Please sign in to comment.