Skip to content

Commit ec6c181

Browse files
committed
1.2.0
- Added plane mode - Fixed a bug where the SAS would keep going toward the fly by wire direction after switching back to the stock stability assist mode. - Put all icons in a single texture atlas
1 parent a8163a2 commit ec6c181

16 files changed

Lines changed: 178 additions & 44 deletions

FlyByWireSASMode.cs

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,48 @@ namespace FlyByWireSASMode
1616
[DefaultExecutionOrder(1)] // our LateUpdate() need to run after VesselAutopilotUI.LateUpdate()
1717
internal class FlyByWireSASMode : MonoBehaviour
1818
{
19+
private const string TEX_PATH = "FlyByWireSASMode/textures";
20+
// singleton accessor
1921
internal static FlyByWireSASMode instance;
2022

23+
// internal state
2124
private static bool init;
25+
26+
// sprites
2227
private static Sprite flyByWireSprite;
28+
private static Sprite flyByWirePlaneSprite;
2329
private static Sprite parallelPosSprite;
2430
private static Sprite parallelNegSprite;
31+
32+
// config
2533
private static AutopilotMode requiredSASMode = AutopilotMode.Maneuver;
26-
private static float inputSensitivity = 1f;
34+
internal static float inputSensitivity = 1f;
2735

36+
// internal state
2837
private bool started;
2938

39+
// SAS UI buttons references
3040
private UIStateToggleButton stabilityAssistButton;
3141
private UIStateToggleButton flyByWireButton;
42+
private UIStateToggleButton flyByWirePlaneButton;
3243
private UIStateToggleButton parallelPosButton;
3344
private UIStateToggleButton parallelNegButton;
3445

46+
// stock navball reference
3547
internal NavBall navBall;
48+
49+
// custom navball markers
3650
private GameObject navBallMarker;
3751
private GameObject navBallArrow;
3852

53+
// known active vessel (for polling purposes)
3954
private Vessel activeVessel;
55+
56+
// per vessel direction and state tracking
4057
private VesselState activeVesselState;
4158
private List<VesselState> nonActiveVesselStates = new List<VesselState>();
59+
60+
// known state for polling based state change detection
4261
private bool isModeAvailableOnActiveVessel;
4362

4463
private void Awake()
@@ -68,21 +87,22 @@ private void Awake()
6887
}
6988
}
7089

71-
flyByWireSprite = LoadSprite("FlyByWireSASMode/FlyByWireMarker");
72-
parallelPosSprite = LoadSprite("FlyByWireSASMode/ParallelPosMarker");
73-
parallelNegSprite = LoadSprite("FlyByWireSASMode/ParallelNegMarker");
74-
}
75-
76-
private static Sprite LoadSprite(string path)
77-
{
78-
Texture2D tex = GameDatabase.Instance.GetTexture(path, false);
90+
Texture2D tex = GameDatabase.Instance.GetTexture(TEX_PATH, false);
7991
if (tex == null)
8092
{
81-
Debug.LogError($"[FlyByWireSASMode] Unable to get 'GameData/{path}.png' texture, make sure the plugin is installed in the right folder.");
82-
return null;
93+
Debug.LogError($"[FlyByWireSASMode] Unable to get 'GameData/{TEX_PATH}.png' texture, make sure the plugin is installed in the right folder.");
94+
return;
8395
}
8496

85-
return Sprite.Create(tex, new Rect(0f, 0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
97+
CreateSprite(tex, 0f, 0f, 64f, 64f, out flyByWireSprite);
98+
CreateSprite(tex, 0f, 64f, 64f, 64f, out flyByWirePlaneSprite);
99+
CreateSprite(tex, 0f, 128f, 34f, 34f, out parallelNegSprite);
100+
CreateSprite(tex, 34f, 128f, 34f, 34f, out parallelPosSprite);
101+
}
102+
103+
private static void CreateSprite(Texture2D tex, float xOffset, float yOffset, float xSize, float ySize, out Sprite sprite)
104+
{
105+
sprite = Sprite.Create(tex, new Rect(xOffset, tex.height - yOffset - ySize, xSize, ySize), new Vector2(0.5f, 0.5f));
86106
}
87107

88108
private IEnumerator Start()
@@ -99,6 +119,7 @@ private IEnumerator Start()
99119
button.onClick.AddListener(DeactivateFromUI);
100120

101121
flyByWireButton = CopySASButton(autopilotUI, AutopilotMode.Maneuver, "FlyByWire", "Fly by wire", 7f, 25f, flyByWireSprite, ActivateFlyByWireFromUI);
122+
flyByWirePlaneButton = CopySASButton(autopilotUI, AutopilotMode.StabilityAssist, "FlyByWirePlane", "Fly by wire\n(plane mode)", 7f, 25f, flyByWirePlaneSprite, ActivateFlyByWirePlaneFromUI);
102123
parallelPosButton = CopySASButton(autopilotUI, AutopilotMode.Target, "Parallel", "Parallel", 5f, -25f, parallelPosSprite, ActivateParallelPosFromUI);
103124
parallelNegButton = CopySASButton(autopilotUI, AutopilotMode.AntiTarget, "AntiParallel", "AntiParallel", 5f, -25f, parallelNegSprite, ActivateParallelNegFromUI);
104125

@@ -168,6 +189,21 @@ private void ActivateFlyByWireFromUI()
168189
SetUIMode(CustomSASMode.FlyByWire);
169190
}
170191

192+
private void ActivateFlyByWirePlaneFromUI()
193+
{
194+
if (activeVesselState != null)
195+
{
196+
activeVesselState.sasMode = CustomSASMode.FlyByWirePlaneMode;
197+
activeVesselState.ResetDirection();
198+
}
199+
else
200+
{
201+
activeVesselState = new VesselState(FlightGlobals.ActiveVessel, CustomSASMode.FlyByWirePlaneMode);
202+
}
203+
204+
SetUIMode(CustomSASMode.FlyByWirePlaneMode);
205+
}
206+
171207
private void ActivateParallelPosFromUI()
172208
{
173209
if (activeVesselState != null)
@@ -204,12 +240,14 @@ private void SetUIMode(CustomSASMode mode)
204240
if (mode == CustomSASMode.Stock)
205241
{
206242
flyByWireButton.SetState(false);
243+
flyByWirePlaneButton.SetState(false);
207244
parallelPosButton.SetState(false);
208245
parallelNegButton.SetState(false);
209246
}
210247
else
211248
{
212249
flyByWireButton.SetState(mode == CustomSASMode.FlyByWire);
250+
flyByWirePlaneButton.SetState(mode == CustomSASMode.FlyByWirePlaneMode);
213251
parallelPosButton.SetState(mode == CustomSASMode.ParallelPos);
214252
parallelNegButton.SetState(mode == CustomSASMode.ParallelNeg);
215253

@@ -265,6 +303,7 @@ private void LateUpdate()
265303
{
266304
isModeAvailableOnActiveVessel = isModeAvailable;
267305
flyByWireButton.gameObject.SetActive(isModeAvailable);
306+
flyByWirePlaneButton.gameObject.SetActive(isModeAvailable);
268307
parallelPosButton.gameObject.SetActive(isModeAvailable);
269308
parallelNegButton.gameObject.SetActive(isModeAvailable);
270309
}
@@ -305,7 +344,7 @@ private void LateUpdate()
305344
// continously disable the stability assist button
306345
stabilityAssistButton.SetState(false);
307346

308-
if (activeVesselState.sasMode == CustomSASMode.FlyByWire)
347+
if (activeVesselState.sasMode == CustomSASMode.FlyByWire || activeVesselState.sasMode == CustomSASMode.FlyByWirePlaneMode)
309348
{
310349
// set marker position on navball
311350
Vector3 markerLocalPos = navBall.attitudeGymbal * (activeVesselState.direction * navBall.VectorUnitScale);

FlyByWireSASMode.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
</ItemGroup>
8686
<ItemGroup>
8787
<Compile Include="FlyByWireSASMode.cs" />
88+
<Compile Include="Lib.cs" />
8889
<Compile Include="Properties\AssemblyInfo.cs" />
8990
<Compile Include="VesselState.cs" />
9091
</ItemGroup>
-1.17 KB
Binary file not shown.

GameData/FlyByWireSASMode/FlyByWireSASMode.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"NAME": "FlyByWireSASMode",
33
"URL": "https://raw.githubusercontent.com/gotmachine/FlyByWireSASMode/master/GameData/FlyByWireSASMode/FlyByWireSASMode.version",
44
"DOWNLOAD": "https://github.com/gotmachine/FlyByWireSASMode/releases",
5-
"VERSION": {"MAJOR": 1, "MINOR": 1, "PATCH": 0, "BUILD": 0},
5+
"VERSION": {"MAJOR": 1, "MINOR": 2, "PATCH": 0, "BUILD": 0},
66
"KSP_VERSION": {"MAJOR": 1, "MINOR": 12, "PATCH": 5},
77
"KSP_VERSION_MIN": {"MAJOR": 1, "MINOR": 12, "PATCH": 3},
88
"KSP_VERSION_MAX": {"MAJOR": 1, "MINOR": 12, "PATCH": 5}
-584 Bytes
Binary file not shown.
-600 Bytes
Binary file not shown.
15.2 KB
Loading

Lib.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
namespace FlyByWireSASMode
9+
{
10+
public static class Lib
11+
{
12+
/// <summary>
13+
/// Return a quaternion describing a rotation from a normalized direction vector to another.
14+
/// </summary>
15+
/// <param name="fromV">The normalized starting vector</param>
16+
/// <param name="toV">The normalized end vector</param>
17+
public static QuaternionD QuaternionDFromToRotation(Vector3d fromV, Vector3d toV)
18+
{
19+
double dot = Vector3d.Dot(fromV, toV);
20+
21+
if (dot >= 1.0)
22+
return QuaternionD.identity;
23+
24+
if (dot <= -1.0)
25+
{
26+
Vector3d axis = Vector3d.Cross(Vector3d.right, fromV);
27+
if (axis.sqrMagnitude == 0.0)
28+
axis = Vector3d.Cross(Vector3d.up, fromV);
29+
30+
return new QuaternionD(axis.x, axis.y, axis.z, 0.0);
31+
}
32+
33+
double s = Math.Sqrt((1.0 + dot) * 2.0);
34+
double rs = 1.0 / s;
35+
Vector3d cross = Vector3d.Cross(fromV, toV);
36+
return new QuaternionD(cross.x * rs, cross.y * rs, cross.z * rs, s * 0.5);
37+
}
38+
39+
private const double halfDegToRad = Math.PI / 180.0 * 0.5;
40+
41+
/// <summary>
42+
/// Returns a rotation that rotates z degrees around the z axis,
43+
/// x degrees around the x axis, and y degrees around the y axis,
44+
/// applied in that order.
45+
/// </summary>
46+
public static QuaternionD QuaternionDFromEuler(double x, double y, double z)
47+
{
48+
double sz, cz, sx, cx, sy, cy;
49+
50+
double halfX = x * halfDegToRad;
51+
sx = Math.Sin(halfX);
52+
cx = Math.Cos(halfX);
53+
54+
double halfY = y * halfDegToRad;
55+
sy = Math.Sin(halfY);
56+
cy = Math.Cos(halfY);
57+
58+
double halfZ = z * halfDegToRad;
59+
sz = Math.Sin(halfZ);
60+
cz = Math.Cos(halfZ);
61+
62+
return new QuaternionD(
63+
cy * sx * cz + sy * cx * sz,
64+
sy * cx * cz - cy * sx * sz,
65+
cy * cx * sz - sy * sx * cz,
66+
cy * cx * cz + sy * sx * sz);
67+
}
68+
}
69+
}

NavballScreenshot.png

-114 KB
Binary file not shown.

Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
3535
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.1.0.0")]
36+
[assembly: AssemblyFileVersion("1.2.0.0")]

0 commit comments

Comments
 (0)