Skip to content

Commit

Permalink
Less is more (hopefully). Trusting the ModulePartVariant is doing its…
Browse files Browse the repository at this point in the history
… job **before** calling us.

Working in progress for issue #261
  • Loading branch information
Lisias committed Jan 24, 2023
1 parent d1688c6 commit 7732b7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 116 deletions.
120 changes: 4 additions & 116 deletions Source/PartDB14x/PartDB/VariantPartScaler.cs
Expand Up @@ -20,8 +20,6 @@
You should have received a copy of the GNU General Public License 2.0
along with TweakScale /L. If not, see <https://www.gnu.org/licenses/>.
*/
using System.Linq;
using UnityEngine;

namespace TweakScale.PartDB
{
Expand Down Expand Up @@ -65,123 +63,13 @@ protected override void OnEditorOut()

internal void OnEditorVariantApplied(Part part, PartVariant partVariant)
{
if (!this.ts.IsScaled) return;
if(!this.ts.IsScaled) return;
Log.dbg("VariantPartScaler.OnEditorVariantApplied {0} {1}", this.ts.InstanceID, partVariant.Name);
this.SetVariant(partVariant);

// When an variant is applied, all the attachment nodes are replaced by "vanilla" ones
// So we need to rescale them using absolute scales.
this.MoveAttachmentNodes(true, true);

// And since the super's Move code doesn't works right when you are "scaling back" a part, we need to
// "fix" them now.
// this.MoveParts();

this.OnChange();
}
#if false
// This whole section of code is now useless once KSP-Recall is in charge of preventing Editor from screwing up with us.
//
// As a matter of fact, this whole code was **never** really necessary - in the end, I was just playing
// a tug-of-war with Editor, redoing things unnecessarily
//
// I expect to have "fixed" a lot of problems by removing this krapload of code!!!

protected override AttachNode[] FindBaseNodesWithSameId(AttachNode node)
{
Log.dbg("VariantPartScaler.FindBaseNodesWithSameId {0}", node.id);

return this.FindBaseNodesWithSameId(node, this.currentVariant);
}

protected AttachNode[] FindBaseNodesWithSameId(AttachNode node, PartVariant variant)
{
Log.dbg("VariantPartScaler.FindBaseNodesWithSameId {0} {1}", node.id, variant.DisplayName);

AttachNode [] baseNodesWithSameId = this.prefab.variants.variantList[this.prefab.variants.GetVariantIndex(variant.Name)].AttachNodes
.Where(a => a.id == node.id)
.ToArray();

if (0 == baseNodesWithSameId.Length)
baseNodesWithSameId = this.prefab.attachNodes
.Where(a => a.id == node.id)
.ToArray();

return baseNodesWithSameId;
}

protected void MoveParts()
{
Log.dbg("VariantPartScaler.MoveParts");

int len = this.part.attachNodes.Count;
for (int i = 0; i < len; i++) {
AttachNode node = this.part.attachNodes[i];

if (null == node.attachedPart)
{
Log.dbg("{0}'s node {1} has not attached part.", this.InstanceID(), node.id);
continue;
}

if (0 != this.part.symmetryCounterparts.Count)
this.MovePartSymetry(node);
else
this.MovePart(node);
}
}

protected void MovePart(AttachNode node)
{
Log.dbg("VariantPartScaler.MovePart {0}", node.id);

AttachNode[] currentNodesWithSameId = this.FindNodesWithSameId(node); // The node was scaled correctly, we can use the node as is
AttachNode[] previousBaseNodesWithSameId = this.FindBaseNodesWithSameId(node, this.previousVariant); // This is where the part was
AttachNode[] currentBaseNodesWithSameId = this.FindBaseNodesWithSameId(node, this.currentVariant); // This is where the part should be
AttachNode[] attachedPartNode = this.FindAttachingNode(this.part, node.attachedPart);

if (currentNodesWithSameId.Length > 0 && previousBaseNodesWithSameId.Length > 0 && currentBaseNodesWithSameId.Length > 0)
{
Vector3 currentPosition = this.part.partTransform.InverseTransformPoint(node.attachedPart.partTransform.position); // Where we are
Vector3 desiredPosition = currentNodesWithSameId[0].position; // Where we should be
Vector3 deltaPos = desiredPosition - currentPosition;

if (attachedPartNode.Length > 0)
{
deltaPos -= attachedPartNode[0].position;
}

bool isAttachedParent = node.attachedPart == this.part.parent;
if (isAttachedParent) {
deltaPos = -deltaPos + this.part.attPos;
this.part.transform.Translate(deltaPos, this.part.parent.transform);
} else {
node.attachedPart.transform.Translate(deltaPos, node.attachedPart.transform);
}

Log.dbg("Moving {0}'s node {1} attached part {2}{3} from {4} to {5} by {6}."
, this.ts.InstanceID, node.id, InstanceID(node.attachedPart)
, isAttachedParent ? " those attachment is his parent" : ""
, currentPosition, desiredPosition, deltaPos);
} else
Log.error("Error moving part on Variant. Node {0} does not have counterpart in part variants {1} and/or {2}.", node.id, this.previousVariant.Name, this.currentVariant.Name);
}

protected void MovePartSymetry(AttachNode node)
{
Log.dbg("VariantPartScaler.MovePartSymetry {0}", node.id);
}

private AttachNode[] FindAttachingNode(Part part, Part attachedPart)
{
Log.dbg("VariantPartScaler.FindAttachingNode {0} {1}", InstanceID(part), InstanceID(attachedPart));

AttachNode [] attachingNodes = attachedPart.attachNodes
.Where(a => a.attachedPart == part)
.ToArray();

return attachingNodes;
// Rescale everything, as new variants may have different resources definitions, etc.
// I will trust (or hope) that any changes made by the Variant is already applied.
this.ts.Rescale();
}
#endif
}
}
7 changes: 7 additions & 0 deletions Source/Scale/Scale.cs
Expand Up @@ -1095,6 +1095,13 @@ public void SetState(bool active, bool available)
p.FindModuleImplementing<TweakScale>().SetStateInternal(active, available);
}

// Helper to a 3rd party be able to force a complete rescaling.
// It's up to the caller to prevent an infinit loop, do not call this inside a TweakScale message handler!
public void Rescale()
{
this.RestoreScaleIfNeededAndUpdate();
}

#endregion

internal void SetStateInternal(bool active, bool available)
Expand Down

0 comments on commit 7732b7c

Please sign in to comment.