From c5adca14c8a658f76c8036bc74115fc4a6ab334f Mon Sep 17 00:00:00 2001
From: Shon Ferguson <shonferg@users.sourceforge.net>
Date: Mon, 3 Jul 2006 02:55:07 +0000
Subject: [PATCH] Made the MergeNifTree function set the start time, stop time,
 and frequency of the controller it attaches an interpolator to. Made the
 MergeNifTree function create a controller of the right type and attach it to
 the specified node if one does not already exist.

---
 niflib.cpp | 41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)

diff --git a/niflib.cpp b/niflib.cpp
index b491043e..9e953658 100644
--- a/niflib.cpp
+++ b/niflib.cpp
@@ -803,23 +803,44 @@ void MergeNifTrees( const Ref<NiNode> & target, const Ref<NiControllerSequence>
 				//attach it to the specific type of controller that's
 				//connected to the named node
 				NiNodeRef node = name_map[node_name];
+				cout << "Attaching interpolator to " << node << endl;
 				list<NiTimeControllerRef> ctlrs = node->GetControllers();
+				NiSingleInterpolatorControllerRef ctlr;
 				for ( list<NiTimeControllerRef>::iterator it = ctlrs.begin(); it != ctlrs.end(); ++it ) {
 					if ( *it != NULL && (*it)->GetType().GetTypeName() == ctlr_type ) {
-						NiSingleInterpolatorControllerRef ctlr = DynamicCast<NiSingleInterpolatorController>(*it);
+						ctlr = DynamicCast<NiSingleInterpolatorController>(*it);
 						if ( ctlr != NULL ) {
-							//Clone the interpolator and attached data and
-							//add it to controller of matching type that was
-							//found
-							NiObjectRef clone = CloneNifTree( StaticCast<NiObject>(data[i].interpolator), version, user_version );
-							NiInterpolatorRef interp = DynamicCast<NiInterpolator>(clone);
-							if ( interp != NULL ) {
-								ctlr->SetInterpolator( interp );
-								break;
-							}
+							break;
 						}
 					}
 				}
+
+				//If the controller wasn't found, create one of the right type and attach it
+				if ( ctlr == NULL ) {
+					NiObjectRef new_ctlr = CreateBlock( ctlr_type );
+					ctlr = DynamicCast<NiSingleInterpolatorController>( new_ctlr );
+					if ( ctlr == NULL ) {
+						throw runtime_error ("Non-NiSingleInterpolatorController controller found in KF file.");
+					}
+					node->AddController( StaticCast<NiTimeController>(ctlr) );
+				}
+
+				cout << "Controller is " << ctlr << endl;
+
+				//Clone the interpolator and attached data and
+				//add it to controller of matching type that was
+				//found
+				NiObjectRef clone = CloneNifTree( StaticCast<NiObject>(data[i].interpolator), version, user_version );
+				NiInterpolatorRef interp = DynamicCast<NiInterpolator>(clone);
+				if ( interp != NULL ) {
+					ctlr->SetInterpolator( interp );
+
+					//Set the start/stop time and frequency of this controller
+					ctlr->SetStartTime( right->GetStartTime() );
+					ctlr->SetStopTime( right->GetStopTime() );
+					ctlr->SetFrequency( right->GetFrequency() );
+					ctlr->SetPhase( 1.0f ); //TODO:  Is phase somewhere in NiControllerSequence?
+				}
 			}
 		}
 	}
-- 
GitLab