#include "StdAfx.h"
#include "RsAnimAttr.h"


HRESULT CRsAnimAttr::GetOrigValue(IRsNode* pAnimSpace, VARIANT* pValue)
{
	HRESULT hRes;

	if(m_spCon == NULL)
	{
		CComQIPtr<IRnEncapsulator>	spAnimSpaceEncaps = pAnimSpace;	
		spAnimSpaceEncaps->FindConByFullName(m_szAttrId, &m_spCon);

		if(m_spCon == NULL)
		{
			CComQIPtr<IRsBaseNode>	spBaseNode = pAnimSpace;
			hRes = spBaseNode->FindConByName(m_szAttrId, &m_spCon);
		}
	}

	if(m_spCon)
	{
		// check if it is end connector:
		RtBool	bEndCon;
		m_spCon->IsEndConnector(&bEndCon);
		if(!bEndCon)
			return E_FAIL;

		hRes = RetrieveValue(pValue);
	}
	return hRes;
}

HRESULT CRsAnimAttr::RetrieveValue(VARIANT* vVariant)
{
	CONID	conId;
	m_spCon->GetConId(&conId);

	CComPtr<IRsNode>	spNode;
	m_spCon->GetNode(&spNode);

	HRESULT hRes;
	if(FAILED(hRes = spNode->GetValue(conId, vVariant)))
	{
		ATLASSERT(!"Failed to get animation value");
	}

	return hRes;
}

HRESULT CRsAnimAttr::SetNewValue(const VARIANT* vVariant)
{
	HRESULT hRes = S_OK;

	if(m_spCon == NULL)
		return E_FAIL;
		

	CONID	conId;
	m_spCon->GetConId(&conId);

	CComPtr<IRsNode>	spNode;
	m_spCon->GetNode(&spNode);

	CComPtr<IRsConDescriptor>	spConDesc;
	m_spCon->GetDescriptor(&spConDesc);
	if(spConDesc)
	{
		VARTYPE varType;
		spConDesc->GetDataType(&varType);
		CComVariant vConvertedVar;
		if(FAILED(vConvertedVar.ChangeType(varType, vVariant)))
		{
			if(FAILED(hRes = spNode->SetValue(conId, *vVariant)))
			{
				ATLASSERT(!"Failed to set animation value");
			}
		}
		else
		{
			if(FAILED(hRes = spNode->SetValue(conId, vConvertedVar)))
			{
				ATLASSERT(!"Failed to set animation value");
			}

		}
	}
	return hRes;
}

