#include "ordeviceinput_device.h"
#define ORDEVICEINPUT__CLASS ORDEVICEINPUT__CLASSNAME
#define ORDEVICEINPUT__NAME ORDEVICEINPUT__CLASSSTR
#define ORDEVICEINPUT__LABEL "OR - Input Device"
#define ORDEVICEINPUT__DESC "OR - Moving Clock Hands"
#define ORDEVICEINPUT__PREFIX "ClockHands"
ORDEVICEINPUT__CLASS,
ORDEVICEINPUT__LABEL,
ORDEVICEINPUT__DESC,
bool ORDeviceInput::FBCreate()
{
ModelTemplate.Children.Add(mRootTemplate);
mHierarchyIsDefined = false;
return true;
}
void ORDeviceInput::FBDestroy()
{
}
bool ORDeviceInput::DeviceOperation( kDeviceOperations pOperation )
{
switch (pOperation)
{
case kOpInit: return Init();
case kOpStart: return Start();
case kOpStop: return Stop();
case kOpReset: return Reset();
case kOpDone: return Done();
}
return FBDevice::DeviceOperation( pOperation );
}
bool ORDeviceInput::Init()
{
return true;
}
bool ORDeviceInput::Done()
{
BeginChannelSetDefinition ();
EndChannelSetDefinition ();
mHierarchyIsDefined = false;
return false;
}
bool ORDeviceInput::Stop()
{
FBProgress lProgress;
lProgress.Caption = "Shutting down device";
lProgress.Text = "Stopping data stream";
Information = "Stopping data stream";
if(! mHardware.StopDataStream() )
{
Information = "Could not stop data stream.";
}
lProgress.Text = "Closing device communication";
Information = "Closing device communication";
if(! mHardware.Close() )
{
Information = "Could not close device";
}
return false;
}
bool ORDeviceInput::Start()
{
FBProgress Progress;
Progress.Caption = "Setting up device";
if(! mHardware.Open() )
{
Information = "Could not open device";
return false;
}
Progress.Text = "Device found, scanning for channel information...";
Information = "Retrieving channel information";
if(!mHardware.GetSetupInfo())
{
Information = "Could not get channel information from device.";
return false;
}
BeginChannelSetDefinition();
for(
int i=0;
i<GetChannelCount();
i++)
{
UseChannel(GetChannelName(
i));
}
EndChannelSetDefinition();
if(! mHardware.StartDataStream() )
{
Information = "Could not start data stream.";
return false;
}
return true;
}
bool ORDeviceInput::Reset()
{
Stop();
return Start();
}
void ORDeviceInput::DefineHierarchy()
{
if( !mHierarchyIsDefined && mChannels.GetCount() > 0 )
{
mChannels[PARENT_A]->mModelTemplate->Children.Add(mChannels[CHILD_A]->mModelTemplate);
mChannels[PARENT_B]->mModelTemplate->Children.Add(mChannels[CHILD_B]->mModelTemplate);
mRootTemplate->Children.Add(mChannels[PARENT_A]->mModelTemplate);
mRootTemplate->Children.Add(mChannels[PARENT_B]->mModelTemplate);
mHierarchyIsDefined = true;
}
}
void ORDeviceInput::BeginChannelSetDefinition()
{
for(i=0; i< mChannels.GetCount(); i++)
{
mChannels[
i]->mIsUsed =
false;
}
}
void ORDeviceInput::EndChannelSetDefinition()
{
for( i = 0; i < mChannels.GetCount(); i++ )
{
if( mChannels[i]->mIsUsed )
{
if( !mChannels[i]->mTAnimNode )
{
FBString lName( mChannels[i]->mName, " T" );
}
if( !mChannels[i]->mRAnimNode )
{
FBString lName( mChannels[i]->mName, " R" );
}
if(!mChannels[i]->mModelTemplate)
{
mChannels[
i]->mModelTemplate->Bindings.Add(mChannels[i]->mTAnimNode);
mChannels[
i]->mModelTemplate->Bindings.Add(mChannels[i]->mRAnimNode);
}
}
else
{
if( mChannels[i]->mTAnimNode )
{
if( mChannels[i]->mModelTemplate )
{
mChannels[
i]->mModelTemplate->Bindings.Remove(mChannels[i]->mTAnimNode);
}
}
if( mChannels[i]->mRAnimNode )
{
if( mChannels[i]->mModelTemplate )
{
mChannels[
i]->mModelTemplate->Bindings.Remove(mChannels[i]->mRAnimNode);
}
}
if( mRootTemplate->Children.Find(mChannels[i]->mModelTemplate) >= 0 )
{
mRootTemplate->Children.Remove(mChannels[i]->mModelTemplate);
}
if( mChannels[i]->mModelTemplate )
{
for(
int j = 0; j < mChannels[
i]->mModelTemplate->Children.GetCount(); j++ )
{
mChannels[
i]->mModelTemplate->Children.RemoveAt(0);
}
}
delete mChannels[
i]->mModelTemplate;
mChannels[
i]->mModelTemplate =
NULL;
if( mChannels[i]->mTAnimNode )
{
AnimationNodeDestroy(mChannels[i]->mTAnimNode);
}
if( mChannels[i]->mRAnimNode )
{
AnimationNodeDestroy(mChannels[i]->mRAnimNode);
}
mChannels[
i]->mTAnimNode =
NULL;
mChannels[
i]->mRAnimNode =
NULL;
}
}
for( i = mChannels.GetCount() - 1; i >= 0; i-- )
{
if( !mChannels[i]->mIsUsed )
{
mChannels.SetAt(i,
NULL);
mChannels.RemoveAt(i);
}
}
for( i = 0; i < mChannels.GetCount(); i++ )
{
if( mChannels[i]->mTAnimNode )
{
mChannels[
i]->mTAnimNode->Reference =
i;
}
}
DefineHierarchy();
}
int ORDeviceInput::UseChannel(char *pName)
{
char lName[256];
strcpy(lName,pName);
for(unsigned int j = 0; j < strlen(lName); j++)
{
if (lName[j]==' ') lName[j] = '_';
}
for(int i=0;i<mChannels.GetCount();i++)
{
if(mChannels[i]->mName == lName)
{
mChannels[
i]->mIsUsed =
true;
}
}
DataChannel *newChannel = new DataChannel;
newChannel->mName = lName;
newChannel->mIsUsed = true;
return mChannels.Add(newChannel);
}
bool ORDeviceInput::AnimationNodeNotify(FBAnimationNode* pAnimationNode ,FBEvaluateInfo* pEvaluateInfo)
{
double Pos[3];
double Rot[3];
int Index = pAnimationNode->Reference;
Pos[0] = GetDataTX(Index);
Pos[1] = GetDataTY(Index);
Pos[2] = GetDataTZ(Index);
Rot[0] = GetDataRX(Index);
Rot[1] = GetDataRY(Index);
Rot[2] = GetDataRZ(Index);
if(mChannels[Index]->mTAnimNode && mChannels[Index]->mRAnimNode)
{
mChannels[
Index]->mRAnimNode->WriteData( Rot, pEvaluateInfo );
mChannels[
Index]->mTAnimNode->WriteData( Pos, pEvaluateInfo );
}
return true;
}
void ORDeviceInput::DeviceIONotify( kDeviceIOs pAction,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBTime lEvalTime;
switch (pAction)
{
case kIOPlayModeWrite:
case kIOStopModeWrite:
{
}
break;
case kIOStopModeRead:
case kIOPlayModeRead:
{
while(mHardware.FetchDataPacket(lEvalTime))
{
DeviceRecordFrame (lEvalTime,pDeviceNotifyInfo);
AckOneSampleReceived();
}
}
break;
}
}
void ORDeviceInput::DeviceRecordFrame(FBTime &pTime,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBAnimationNode* Data;
pTime = mSystem.LocalTime;
double Pos[3];
double Rot[3];
for (i=0; i<mChannels.GetCount(); i++)
{
if(mChannels[i]->mIsUsed)
{
if (mChannels[i]->mTAnimNode)
{
Data = mChannels[
i]->mTAnimNode->GetAnimationToRecord();
if (Data)
{
{
Pos[0] = GetDataTX(i);
Pos[1] = GetDataTY(i);
Pos[2] = GetDataTZ(i);
switch( SamplingMode )
{
Data->KeyAdd(pTime, Pos);
break;
Data->KeyAdd(Pos);
break;
}
}
}
}
if (mChannels[i]->mRAnimNode)
{
Data = mChannels[
i]->mRAnimNode->GetAnimationToRecord();
if (Data)
{
{
Rot[0] = GetDataRX(i);
Rot[1] = GetDataRY(i);
Rot[2] = GetDataRZ(i);
switch( SamplingMode )
{
Data->KeyAdd(pTime, Rot);
break;
Data->KeyAdd(Rot);
break;
}
}
}
}
}
}
}
#define FBX_CHANNEL_TAG "Channels"
#define FBX_VERSION_TAG "Version"
bool ORDeviceInput::FbxStore(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
{
pFbxObject->FieldWriteI(FBX_VERSION_TAG,100);
pFbxObject->FieldWriteBegin(FBX_CHANNEL_TAG);
for (int i=0;i<mChannels.GetCount();i++)
{
if(mChannels[i]->mIsUsed)
{
pFbxObject->FieldWriteC(mChannels[i]->mName);
}
}
pFbxObject->FieldWriteEnd();
}
return true;
}
bool ORDeviceInput::FbxRetrieve(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
int Version;
if( pStoreWhat & kAttributes )
{
BeginChannelSetDefinition();
Version = pFbxObject->FieldReadI(FBX_VERSION_TAG);
if( pFbxObject->FieldReadBegin(FBX_CHANNEL_TAG) )
{
int channelCount = pFbxObject->FieldReadGetCount();
for( int i = 0; i < channelCount; i++ )
{
name = pFbxObject->FieldReadC();
UseChannel(name);
}
pFbxObject->FieldReadEnd();
}
EndChannelSetDefinition();
}
return true;
}