Skip to content

Commit

Permalink
Merge pull request Crzyrndm#7 from Rafterman82/CarnationRED-master
Browse files Browse the repository at this point in the history
Carnation red master
  • Loading branch information
Rafterman82 authored May 13, 2021
2 parents a55506f + 0eb178e commit bd37d7d
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 20 deletions.
27 changes: 13 additions & 14 deletions B9 PWings Fork/B9_Aerospace_WingStuff.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>B9_Aerospace_WingStuff</RootNamespace>
<AssemblyName>B9_Aerospace_WingStuff</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>Q:\Steam\steamapps\common\Kerbal Space Program190DEV\GameData\B9_Aerospace_ProceduralWings\Plugins\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand All @@ -26,7 +26,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>Q:\Steam\steamapps\common\Kerbal Space Program190DEV\GameData\B9_Aerospace_ProceduralWings\Plugins\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand All @@ -41,6 +41,7 @@
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\ref\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -50,38 +51,40 @@
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>..\..\ref\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.AssetBundleModule">
<HintPath>..\..\ref\UnityEngine.AssetBundleModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\..\ref\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.IMGUIModule">
<HintPath>..\..\ref\UnityEngine.IMGUIModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.InputLegacyModule">
<HintPath>..\..\ref\UnityEngine.InputLegacyModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.InputModule">
<HintPath>..\..\ref\UnityEngine.InputModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.PhysicsModule">
<HintPath>..\..\ref\UnityEngine.PhysicsModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.TextRenderingModule">
<HintPath>..\..\ref\UnityEngine.TextRenderingModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="EditorHandle.cs" />
<Compile Include="MathD.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="StaticWingGlobals.cs" />
<Compile Include="UIUtility.cs" />
<Compile Include="UtilityTexture.cs" />
Expand All @@ -91,12 +94,8 @@
<Compile Include="WingTankResource.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="shaders\SpecularLayered.shader" />
</ItemGroup>
Expand Down
140 changes: 140 additions & 0 deletions B9 PWings Fork/CtrlSrfWingSynchronizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using ferram4;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace WingProcedural
{
/// <summary>
/// This is to make connected control surface wings deflects like one big wing
/// </summary>
[DefaultExecutionOrder(25565)]
public class CtrlSrfWingSynchronizer : MonoBehaviour
{
public List<WingProcedural> connectedCtrlSrfWings;
public List<ModuleControlSurface> ctrlSrfs;
public List<Quaternion> ctrlSrfsNeutral;
public List<Transform> ctrlSrfTrans;

public float deflectionLastFrame;

private static readonly FieldInfo deflection;
private static readonly FieldInfo neutral;
private static readonly FieldInfo ctrlSurface;
static CtrlSrfWingSynchronizer()
{
deflection = typeof(ModuleControlSurface).GetField("deflection", (BindingFlags)0b1111111111111111111111111);
neutral = typeof(ModuleControlSurface).GetField("neutral", (BindingFlags)0b1111111111111111111111111);
ctrlSurface = typeof(ModuleControlSurface).GetField("ctrlSurface", (BindingFlags)0b1111111111111111111111111);
}
public void Start()
{
ctrlSrfs = connectedCtrlSrfWings.Select(wp => wp.part.FindModuleImplementing<ModuleControlSurface>()).ToList();
ctrlSrfsNeutral = ctrlSrfs.Select(cs => (Quaternion)neutral.GetValue(cs)).ToList();
ctrlSrfTrans = ctrlSrfs.Select(cs => (Transform)ctrlSurface.GetValue(cs)).ToList();
}

public void FixedUpdate()
{
var deflectionAngle = 0f;
for (int i = 0; i < connectedCtrlSrfWings.Count; i++)
{
var c = connectedCtrlSrfWings[i];
if (!c || !c.part)
{
connectedCtrlSrfWings.RemoveAt(i);
ctrlSrfs.RemoveAt(i);
ctrlSrfsNeutral.RemoveAt(i);
ctrlSrfTrans.RemoveAt(i--);
if (connectedCtrlSrfWings.Count == 1)
Destroy(this);
continue;
}

//Get max deflection
var cs = ctrlSrfs[i];
var d = (float)deflection.GetValue(cs);
if (Mathf.Abs(d) > deflectionAngle)
deflectionAngle = d;
}

//Fix a bug when the wing is near vessel's centerline
var delta = deflectionAngle - deflectionLastFrame;
if (Mathf.Abs(delta) > 1f)
deflectionAngle = deflectionLastFrame + Mathf.Sign(delta);
deflectionLastFrame = deflectionAngle;

//apply deflection
for (int i = 0; i < ctrlSrfTrans.Count; i++)
ctrlSrfTrans[i].localRotation = Quaternion.AngleAxis(deflectionAngle, Vector3.right) * ctrlSrfsNeutral[i];
}

public static void AddSynchronizer(Part p, List<WingProcedural> connectedCtrlSrfWings)
{
p.gameObject.AddComponent<CtrlSrfWingSynchronizer>().connectedCtrlSrfWings = connectedCtrlSrfWings;
}
public static void FARAddSynchronizer(Part p, List<WingProcedural> connectedCtrlSrfWings)
{
p.gameObject.AddComponent<FARCtrlSrfWingSynchronizer>().connectedCtrlSrfWings = connectedCtrlSrfWings;
}
}
/// <summary>
/// This is to make connected control surface wings deflects like one big wing
/// </summary>
[DefaultExecutionOrder(25565)]
public class FARCtrlSrfWingSynchronizer : MonoBehaviour
{
public List<WingProcedural> connectedCtrlSrfWings;
public List<FARControllableSurface> farCtrlSrfs;
public double deflectionLastFrame;

private static readonly FieldInfo AoAoffset;
private static readonly MethodInfo DeflectionAnimation;
static FARCtrlSrfWingSynchronizer()
{
AoAoffset = typeof(FARControllableSurface).GetField("AoAoffset", (BindingFlags)0b1111111111111111111111111);
DeflectionAnimation = typeof(FARControllableSurface).GetMethod("DeflectionAnimation", (BindingFlags)0b1111111111111111111111111);
}
public void Start() => farCtrlSrfs = connectedCtrlSrfWings.Select(wp => wp.part.FindModuleImplementing<FARControllableSurface>()).ToList();

public void FixedUpdate()
{
var deflectionAngle = 0d;
for (int i = 0; i < connectedCtrlSrfWings.Count; i++)
{
var c = connectedCtrlSrfWings[i];
if (!c || !c.part)
{
connectedCtrlSrfWings.RemoveAt(i);
farCtrlSrfs.RemoveAt(i--);
if (connectedCtrlSrfWings.Count == 1)
Destroy(this);
continue;
}

//Get max deflection
var cs = farCtrlSrfs[i];
var d = (double)AoAoffset.GetValue(cs);
if (Math.Abs(d) > deflectionAngle)
deflectionAngle = d;
}

//Fix a bug when the wing is near vessel's centerline
float delta = (float)(deflectionAngle - deflectionLastFrame);
if (Mathf.Abs(delta) > 1f)
deflectionAngle = deflectionLastFrame + Mathf.Sign(delta);
deflectionLastFrame = deflectionAngle;

//apply deflection
foreach (var farcs in farCtrlSrfs)
{
AoAoffset.SetValue(farcs, deflectionAngle);
DeflectionAnimation.Invoke(farcs, null);
}
}
}
}
15 changes: 13 additions & 2 deletions B9 PWings Fork/UIUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ public static float FieldSlider(float value, float increment, float incrementLar
Rect rectButtonR = new Rect(rectLast.xMin + rectLast.width - buttonWidth, rectLast.yMin, buttonWidth, rectLast.height);
Rect rectLabelValue = new Rect(rectSlider.xMin + rectSlider.width * 0.75f, rectSlider.yMin, rectSlider.width * 0.25f, rectSlider.height);

bool buttonAdjust = false;

if (GUI.Button(rectButtonL, string.Empty, UIUtility.uiStyleButton))
{
buttonAdjust = true;
if (Input.GetMouseButtonUp(0) || !allowFine)
{
value01 -= incrementLarge / range;
Expand All @@ -163,9 +166,11 @@ public static float FieldSlider(float value, float increment, float incrementLar
{
value01 -= increment01;
}
else buttonAdjust = false;
}
if (GUI.Button(rectButtonR, string.Empty, UIUtility.uiStyleButton))
{
buttonAdjust = true;
if (Input.GetMouseButtonUp(0) || !allowFine)
{
value01 += incrementLarge / range;
Expand All @@ -174,6 +179,7 @@ public static float FieldSlider(float value, float increment, float incrementLar
{
value01 += increment01;
}
else buttonAdjust = false;
}

if (!numericInput)
Expand Down Expand Up @@ -217,8 +223,13 @@ public static float FieldSlider(float value, float increment, float incrementLar
changed = false;
if (float.TryParse(GUI.TextField(rectLabelValue, value.ToString("F3"), UIUtility.uiStyleInputField), out var temp)) // Add optional numeric input
{
value = temp;
value01 = (value - limits.x) / range;
if (!buttonAdjust)
{
value = temp;
value01 = (value - limits.x) / range;
}
else
value = Mathf.Clamp((float)(value01 * range + limits.x), Mathf.Min((float)(limits.x * 0.5), limits.x), limits.y); // lower limit is halved so the fine control can reduce it further but the normal tweak still snaps. Min makes -ve values work
changed = valueOld != value01;
}
value = Mathf.Clamp(value, limits.x, limits.y);
Expand Down
Loading

0 comments on commit bd37d7d

Please sign in to comment.