Skip to content

Commit

Permalink
getting autoscale to work
Browse files Browse the repository at this point in the history
rewrote GetRelativeScaling based on the nodes of the prefab part
because the old way did not work with a %-based free scaletype
  • Loading branch information
pellinor0 committed Feb 16, 2015
1 parent 6741604 commit 2cac39a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 53 deletions.
83 changes: 61 additions & 22 deletions GameData/TweakScale/DefaultScales.cfg
Expand Up @@ -5,6 +5,23 @@
minScale = 0.3
maxScale = 10
defaultScale = 1.25
suffix = m
// scaleFactors = 0.625, 1.25, 2.5, 3.75, 5.0, 6.25, 7.5
// scaleNames = 62.5cm, 1.25m, 2.5m, 3.75m, 5m, 6.25m, 7.5m

// ATTACHNODES
// {
// base = 1
// top = Stack:1
// top01 = Stack:1
// top02 = Stack:1
// top03 = Stack:1
// bottom = Stack:1
// bottom01 = Stack:1
// bottom02 = Stack:1
// bottom03 = Stack:1
// bottom04 = Stack:1
// }
}
SCALETYPE
{
Expand All @@ -23,40 +40,62 @@ SCALETYPE
maxScale = 400
defaultScale = 100
suffix = %

// ATTACHNODES
// {
// base = 1
// top = Stack:1
// top01 = Stack:1
// top02 = Stack:1
// top03 = Stack:1
// bottom = Stack:1
// bottom01 = Stack:1
// bottom02 = Stack:1
// bottom03 = Stack:1
// bottom04 = Stack:1
// }
}

SCALETYPE
{
name = adapter_2_1
freeScale = false
scaleFactors = 1.25, 2.5, 5.0, 7.5
scaleNames = 1.25m to 62.5cm, 2.5m to 1.25m, 5m to 2.5m, 7.5m to 3.75m
freeScale = true
minScale = 0.3
maxScale = 10
suffix = m
defaultScale = 1.25
ATTACHNODES
{
base = 2
top = Stack:1
bottom = Stack:2
bottom01 = Stack:2
bottom02 = Stack:2
}
// scaleFactors = 1.25, 2.5, 5.0, 7.5
// scaleNames = 1.25m to 62.5cm, 2.5m to 1.25m, 5m to 2.5m, 7.5m to 3.75m
// ATTACHNODES
// {
// base = 2
// top = Stack:1
// bottom = Stack:2
// bottom01 = Stack:2
// bottom02 = Stack:2
// }
}

SCALETYPE
{
name = adapter_1_2
freeScale = false
scaleFactors = 1.25, 2.5, 5.0, 7.5
scaleNames = 62.5cm to 1.25m, 1.25m to 2.5m, 2.5m to 5m, 3.75m to 7.5m
freeScale = true
minScale = 0.3
maxScale = 10
suffix = m
defaultScale = 1.25
ATTACHNODES
{
base = 2
top = Stack:2
bottom = Stack:1
bottom01 = Stack:1
bottom02 = Stack:1
}
// scaleFactors = 1.25, 2.5, 5.0, 7.5
// scaleNames = 62.5cm to 1.25m, 1.25m to 2.5m, 2.5m to 5m, 3.75m to 7.5m
// ATTACHNODES
// {
// base = 2
// top = Stack:2
// bottom = Stack:1
// bottom01 = Stack:1
// bottom02 = Stack:1
// bottom03 = Stack:1
// bottom04 = Stack:1
// }
}

SCALETYPE
Expand Down
Binary file modified GameData/TweakScale/plugins/Scale.dll
Binary file not shown.
53 changes: 29 additions & 24 deletions Scale.cs
Expand Up @@ -479,13 +479,13 @@ private bool HasResources

return Tuple.Create(nodeA, nodeB);
}

/// <summary>
/// Calculate the correct scale to use for scaling a part relative to another.
/// </summary>
/// <param name="a">Source part, from which we get the desired scale.</param>
/// <param name="b">Target part, which will potentially be scaled.</param>
/// <returns>The difference in scale between <paramref name="a"/> and <paramref name="b"/>, or null if the parts are incompatible.</returns>
/// <returns>The difference in scale between the (scaled) attachment nodes connecting <paramref name="a"/> and <paramref name="b"/>, or null if somethinng went wrong.</returns>
private static float? GetRelativeScaling(TweakScale a, TweakScale b)
{
if (a == null || b == null)
Expand All @@ -495,23 +495,26 @@ private bool HasResources

if (!nodes.HasValue)
return null;

var nodeA = nodes.Value.Item1;
var nodeB = nodes.Value.Item2;

if (!a.ScaleType.AttachNodes.ContainsKey(nodeA.id) ||
!b.ScaleType.AttachNodes.ContainsKey(nodeB.id))
return null;

var scaleA = a.ScaleType.AttachNodes[nodeA.id];
var scaleB = b.ScaleType.AttachNodes[nodeB.id];
var baseA = a.ScaleType.BaseScale;
var baseB = b.ScaleType.BaseScale;

if (scaleA.Family != scaleB.Family)
var aIdx = a._prefabPart.attachNodes.FindIndex( t => t.id == nodeA.id);
var bIdx = b._prefabPart.attachNodes.FindIndex( t => t.id == nodeB.id);
if (aIdx < 0 || bIdx < 0
|| aIdx >= a._prefabPart.attachNodes.Count
|| aIdx >= a._prefabPart.attachNodes.Count)
return null;

return (scaleA.Scale*baseB)/(scaleB.Scale*baseA);

var sizeA = (float)a._prefabPart.attachNodes[aIdx].size;
var sizeB = (float)b._prefabPart.attachNodes[bIdx].size;

if (sizeA == 0)
sizeA = 0.5f;
if (sizeB == 0)
sizeB = 0.5f;

return (sizeA * a.tweakScale/a.defaultScale)/(sizeB * b.tweakScale/b.defaultScale);
}

/// <summary>
Expand All @@ -528,8 +531,8 @@ private static void AutoScale(TweakScale a, TweakScale b)
if (!factor.HasValue)
return;

b.tweakScale = a.tweakScale * factor.Value;
if (a.ScaleFactors.Length > 0)
b.tweakScale = b.tweakScale * factor.Value;
if (b.ScaleFactors.Length > 0)
{
b.tweakName = Tools.ClosestIndex(b.tweakScale, b.ScaleFactors);
}
Expand All @@ -544,14 +547,16 @@ private void ChainScale()
foreach (var child in part.children)
{
var ts = child.GetComponent<TweakScale>();
var factor = GetRelativeScaling(this, ts);
if (!factor.HasValue)
continue;

if (factor.Value*currentScale == ts.tweakScale)
{
// var factor = GetRelativeScaling(this, ts);
// if (!factor.HasValue)
// continue;

// no idea what this condition does
// but it will not work with the changed GetRelativeScaling method
// if (factor.Value*currentScale == ts.tweakScale)
// {
AutoScale(this, ts);
}
// }
}
}

Expand Down
24 changes: 17 additions & 7 deletions Scale.csproj
Expand Up @@ -11,6 +11,8 @@
<AssemblyName>Scale</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProductVersion>12.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -34,7 +36,6 @@
<Compile Include="Enums.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="Hotkey.cs" />
<Compile Include="Hotkeyable.cs" />
<Compile Include="HotkeyManager.cs" />
<Compile Include="MemberUpdater.cs" />
<Compile Include="OSD.cs" />
Expand All @@ -47,22 +48,21 @@
<Compile Include="ScaleType.cs" />
<Compile Include="Tuple.cs" />
<Compile Include="Updater.cs" />
<Compile Include="HotkeyAble.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>lib\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
<HintPath>..\ksp_090\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="KSPAPIExtensions">
<HintPath>lib\KSPAPIExtensions.dll</HintPath>
<HintPath>GameData\TweakScale\plugins\KSPAPIExtensions.dll</HintPath>
</Reference>
<Reference Include="Scale_Redist">
<HintPath>lib\Scale_Redist.dll</HintPath>
<HintPath>GameData\TweakScale\plugins\Scale_Redist.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="UnityEngine">
<HintPath>lib\UnityEngine.dll</HintPath>
<Private>False</Private>
<HintPath>..\ksp_090\KSP_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand All @@ -76,4 +76,14 @@
<Target Name="AfterBuild">
</Target>
-->
<ProjectExtensions>
<MonoDevelop>
<Properties>
<Policies>
<TextStylePolicy IndentWidth="8" NoTabsAfterNonTabs="True" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
<CSharpFormattingPolicy inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" />
</Policies>
</Properties>
</MonoDevelop>
</ProjectExtensions>
</Project>

0 comments on commit 2cac39a

Please sign in to comment.