Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1435 lines
45 KiB

  1. //====== Copyright � 1996-2006, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "dme_controls/BaseAnimationSetEditorController.h"
  7. #include "tier1/fmtstr.h"
  8. #include "tier1/KeyValues.h"
  9. #include "tier2/fileutils.h"
  10. #include "vgui_controls/FileOpenDialog.h"
  11. #include "vgui_controls/MessageBox.h"
  12. #include "vgui_controls/perforcefilelistframe.h"
  13. #include "dme_controls/BaseAnimationSetEditor.h"
  14. #include "dme_controls/BaseAnimSetAttributeSliderPanel.h"
  15. #include "dme_controls/BaseAnimSetPresetFaderPanel.h"
  16. #include "dme_controls/attributeslider.h"
  17. #include "movieobjects/dmechannel.h"
  18. #include "movieobjects/dmeanimationset.h"
  19. #include "movieobjects/dmeanimationlist.h"
  20. #include "movieobjects/dmeclip.h"
  21. #include "movieobjects/dmegamemodel.h"
  22. #include "movieobjects/dmetransformcontrol.h"
  23. #include "vgui/IInput.h"
  24. #include <windows.h>
  25. #undef PostMessage
  26. #undef MessageBox
  27. // memdbgon must be the last include file in a .cpp file!!!
  28. #include "tier0/memdbgon.h"
  29. using namespace vgui;
  30. //-----------------------------------------------------------------------------
  31. // Blends flex values in left-right space instead of balance/value space
  32. //-----------------------------------------------------------------------------
  33. void BlendValues( bool bTransform, AttributeValue_t *pResult, const AttributeValue_t &src, const AttributeValue_t &dest, float flBlend, float flBalanceFilter = 0.5f );
  34. DEFINE_FIXEDSIZE_ALLOCATOR( SelectionInfo_t, 256, CUtlMemoryPool::GROW_SLOW );
  35. // TODO - we really need to create a solid way of notifying panels when things they care about changes
  36. // right now, we sometimes send vgui messages, sometimes call methods, sometimes use NotifyDataChanged, OnPreviewChanged, FireXXXChangedListeners, etc.
  37. class CNotifyAnimationSetControlSelectionChangedScopeGuard
  38. {
  39. public:
  40. CNotifyAnimationSetControlSelectionChangedScopeGuard( CBaseAnimationSetControl *pControl )
  41. : m_pControl( pControl ), m_selectionMode( SELECTION_REMOVE )
  42. {
  43. }
  44. CNotifyAnimationSetControlSelectionChangedScopeGuard( CBaseAnimationSetControl *pControl, ESelectionMode selectionMode )
  45. : m_pControl( pControl ), m_selectionMode( selectionMode )
  46. {
  47. ++m_nDepth;
  48. }
  49. ~CNotifyAnimationSetControlSelectionChangedScopeGuard()
  50. {
  51. Assert( m_nDepth >= 0 );
  52. if ( m_nDepth > 0 )
  53. {
  54. Finish();
  55. }
  56. }
  57. void Start( ESelectionMode selectionMode )
  58. {
  59. m_selectionMode = selectionMode;
  60. ++m_nDepth;
  61. }
  62. void Finish()
  63. {
  64. Assert( m_nDepth > 0 );
  65. if ( --m_nDepth == 0 )
  66. {
  67. m_pControl->FireControlSelectionChangedListeners();
  68. }
  69. }
  70. private:
  71. CBaseAnimationSetControl *m_pControl;
  72. ESelectionMode m_selectionMode;
  73. static int m_nDepth;
  74. };
  75. int CNotifyAnimationSetControlSelectionChangedScopeGuard::m_nDepth = 0;
  76. CBaseAnimationSetControl::CBaseAnimationSetControl() :
  77. m_pEditor( NULL ),
  78. m_bShowHiddenControls( false ),
  79. m_PreviousPresetSlider( "" ),
  80. m_flPreviousPresetAmount( 0.0f ),
  81. m_bPresetPreviouslyDragged( false ),
  82. m_bPreviouslyHoldingPresetPreviewKey( false ),
  83. m_bPresetSliderChanged( false ),
  84. m_nDominantSlider( -1 )
  85. {
  86. }
  87. CBaseAnimationSetControl::~CBaseAnimationSetControl()
  88. {
  89. m_ControlSelectionChangedListeners.RemoveAll();
  90. ChangeAnimationSetClip( NULL );
  91. int nCount = m_crossfadePresetControlValues.Count();
  92. for ( int i = 0; i < nCount; ++i )
  93. {
  94. DestroyElement( m_crossfadePresetControlValues[ i ], TD_SHALLOW );
  95. }
  96. }
  97. void CBaseAnimationSetControl::ChangeAnimationSetClip( CDmeFilmClip *pFilmClip )
  98. {
  99. m_hFilmClip = pFilmClip;
  100. // Force recomputation
  101. m_nDominantSlider = -1;
  102. SetWorkCameraParent( NULL );
  103. ClearSelection();
  104. if ( CBaseAnimSetPresetFaderPanel *pPresetFader = m_pEditor->GetPresetFader() )
  105. {
  106. pPresetFader->PopulatePresetList( true );
  107. }
  108. }
  109. void CBaseAnimationSetControl::OnControlsAddedOrRemoved()
  110. {
  111. // Force recomputation
  112. m_nDominantSlider = -1;
  113. bool bSelectionChanged = false;
  114. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); )
  115. {
  116. SelectionInfo_t *psi = m_SelectionHistory[ i ];
  117. int iCurr = i;
  118. i = m_SelectionHistory.Next( i );
  119. CDmElement *pControl = psi->m_hControl.Get();
  120. if ( pControl && IsControlVisible( pControl ) )
  121. continue;
  122. psi->m_nComponentFlags = TRANSFORM_COMPONENT_NONE;
  123. m_SelectionHistory.Remove( iCurr );
  124. bSelectionChanged = true;
  125. }
  126. if ( bSelectionChanged )
  127. {
  128. CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this, SELECTION_REMOVE );
  129. }
  130. m_pEditor->GetPresetFader()->PopulatePresetList( false );
  131. }
  132. void CBaseAnimationSetControl::SetWorkCameraParent( CDmeDag *pParent )
  133. {
  134. m_hWorkCameraParent = pParent;
  135. }
  136. CDmeDag *CBaseAnimationSetControl::GetWorkCameraParent()
  137. {
  138. return m_hWorkCameraParent;
  139. }
  140. void CBaseAnimationSetControl::AddOverrideParentChangedListener( IOverrideParentChangedListener *pListener )
  141. {
  142. if ( m_OverrideParentChangedListeners.Find( pListener ) == m_OverrideParentChangedListeners.InvalidIndex() )
  143. {
  144. m_OverrideParentChangedListeners.AddToTail( pListener );
  145. }
  146. }
  147. void CBaseAnimationSetControl::RemoveOverrideParentChangedListener( IOverrideParentChangedListener *pListener )
  148. {
  149. m_OverrideParentChangedListeners.FindAndRemove( pListener );
  150. }
  151. void CBaseAnimationSetControl::FireOverrideParentChangedListeners( CDmeDag *pChildDag )
  152. {
  153. int nNumListeners = m_OverrideParentChangedListeners.Count();
  154. for ( int i = 0; i < nNumListeners; ++i )
  155. {
  156. m_OverrideParentChangedListeners[ i ]->OnOverrideParentChanged( pChildDag );
  157. }
  158. }
  159. void CBaseAnimationSetControl::AddControlSelectionChangedListener( IAnimationSetControlSelectionChangedListener *listener )
  160. {
  161. if ( m_ControlSelectionChangedListeners.Find( listener ) == m_ControlSelectionChangedListeners.InvalidIndex() )
  162. {
  163. m_ControlSelectionChangedListeners.AddToTail( listener );
  164. }
  165. }
  166. void CBaseAnimationSetControl::RemoveControlSelectionChangedListener( IAnimationSetControlSelectionChangedListener *listener )
  167. {
  168. m_ControlSelectionChangedListeners.FindAndRemove( listener );
  169. }
  170. void CBaseAnimationSetControl::FireControlSelectionChangedListeners()
  171. {
  172. for ( int i = 0; i < m_ControlSelectionChangedListeners.Count(); ++i )
  173. {
  174. m_ControlSelectionChangedListeners[ i ]->OnControlSelectionChanged();
  175. }
  176. }
  177. void CBaseAnimationSetControl::FireRebuildControlHierarchyListeners()
  178. {
  179. for ( int i = 0; i < m_ControlSelectionChangedListeners.Count(); ++i )
  180. {
  181. m_ControlSelectionChangedListeners[ i ]->OnRebuildControlHierarchy();
  182. }
  183. }
  184. CDmeFilmClip *CBaseAnimationSetControl::GetAnimationSetClip()
  185. {
  186. return m_hFilmClip;
  187. }
  188. bool CBaseAnimationSetControl::IsControlSelected( CDmElement *pControl ) const
  189. {
  190. return GetSelectionComponentFlags( pControl ) != 0;
  191. }
  192. void CBaseAnimationSetControl::ClearSelection()
  193. {
  194. CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this, SELECTION_REMOVE );
  195. for ( int i = m_SelectionHistory.Tail(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Previous( i ) )
  196. {
  197. m_SelectionHistory[ i ]->m_nComponentFlags = TRANSFORM_COMPONENT_NONE;
  198. }
  199. m_SelectionHistory.RemoveAll();
  200. }
  201. bool CBaseAnimationSetControl::SelectControl( const CDmElement *pControl, ESelectionMode selectionMode /*= SELECTION_ADD*/, TransformComponent_t nComponentFlags /*= TRANFORM_COMPONENT_ALL*/, bool bExpandTree /*= false*/ )
  202. {
  203. CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this, selectionMode );
  204. if ( !pControl )
  205. return false;
  206. // Check to see if the control is hidden, if it is hidden it may not be selected.
  207. if ( !IsControlVisible( pControl ) && ( selectionMode != SELECTION_REMOVE ) )
  208. return false;
  209. SelectionInfo_t *psi = FindSelectionInfoForControl( pControl );
  210. Assert( psi );
  211. if ( !psi )
  212. return false;
  213. if ( selectionMode == SELECTION_SET )
  214. {
  215. ClearSelection();
  216. psi->m_nComponentFlags = nComponentFlags;
  217. }
  218. else
  219. {
  220. if ( psi->m_nComponentFlags != 0 )
  221. {
  222. m_SelectionHistory.FindAndRemove( psi );
  223. }
  224. switch ( selectionMode )
  225. {
  226. case SELECTION_ADD: psi->m_nComponentFlags |= nComponentFlags; break;
  227. case SELECTION_REMOVE: psi->m_nComponentFlags &= ~nComponentFlags; break;
  228. case SELECTION_TOGGLE: psi->m_nComponentFlags ^= nComponentFlags; break;
  229. }
  230. }
  231. if ( psi->m_nComponentFlags != 0 )
  232. {
  233. m_SelectionHistory.AddToTail( psi );
  234. if ( bExpandTree )
  235. {
  236. for ( int i = 0; i < m_ControlSelectionChangedListeners.Count(); ++i )
  237. {
  238. m_ControlSelectionChangedListeners[ i ]->ExpandTreeToControl( pControl, nComponentFlags );
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. void CBaseAnimationSetControl::SaveSelection( CUtlVector< SelectionInfo_t > &selection ) const
  245. {
  246. int nSelectionCount = m_SelectionHistory.Count();
  247. selection.RemoveAll();
  248. selection.EnsureCapacity( nSelectionCount );
  249. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Next( i ) )
  250. {
  251. const SelectionInfo_t *pSelection = m_SelectionHistory[ i ];
  252. if ( pSelection )
  253. {
  254. selection.AddToTail( *pSelection );
  255. }
  256. }
  257. }
  258. void CBaseAnimationSetControl::RestoreSelection( const CUtlVector< SelectionInfo_t > &selection )
  259. {
  260. CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this, SELECTION_SET );
  261. ClearSelection();
  262. int nNumSelected = selection.Count();
  263. for ( int iSelected = 0; iSelected < nNumSelected; ++iSelected )
  264. {
  265. const SelectionInfo_t &selectionElement = selection[ iSelected ];
  266. CDmElement *pControl = g_pDataModel->GetElement( selectionElement.m_hControl );
  267. if ( pControl == NULL )
  268. continue;
  269. SelectionInfo_t *pSelectionInfo = FindSelectionInfoForControl( pControl );
  270. if ( pSelectionInfo == NULL )
  271. continue;
  272. pSelectionInfo->m_nComponentFlags = selectionElement.m_nComponentFlags;
  273. if ( pSelectionInfo->m_nComponentFlags != 0 )
  274. {
  275. m_SelectionHistory.AddToHead( pSelectionInfo );
  276. }
  277. }
  278. }
  279. void CBaseAnimationSetControl::DeselectHiddenControls()
  280. {
  281. CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this, SELECTION_REMOVE );
  282. // Find all of the selected controls which are hidden
  283. CUtlVector< const CDmElement* > hiddenControls( 0, m_SelectionHistory.Count() );
  284. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Next( i ) )
  285. {
  286. const SelectionInfo_t *pSelection = m_SelectionHistory[ i ];
  287. if ( pSelection )
  288. {
  289. const CDmElement *pControl = pSelection->m_hControl;
  290. if ( pControl == NULL )
  291. continue;
  292. if ( IsControlVisible( pControl ) == false )
  293. {
  294. hiddenControls.AddToTail( pSelection->m_hControl );
  295. }
  296. }
  297. }
  298. // Remove the hidden controls from the selection
  299. int nNumHiddenControls = hiddenControls.Count();
  300. for ( int iControl = 0; iControl < nNumHiddenControls; ++iControl )
  301. {
  302. SelectControl( hiddenControls[ iControl ], SELECTION_REMOVE );
  303. }
  304. }
  305. void CBaseAnimationSetControl::SelectControlGroup( CDmeControlGroup *pGroup, ESelectionMode selectionMode /*= SELECTION_ADD*/ )
  306. {
  307. CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this, selectionMode );
  308. if ( !pGroup )
  309. return;
  310. CUtlVector< CDmElement * > list( 0, 32 );
  311. pGroup->GetControlsInGroup( list, true );
  312. for ( int j = list.Count() - 1; j >= 0 ; --j )
  313. {
  314. // Only set selection on the first control which was actually selected,
  315. // otherwise we'll de-select everything but the last control
  316. if ( SelectControl( list[ j ], selectionMode ) && ( selectionMode == SELECTION_SET ) )
  317. {
  318. selectionMode = SELECTION_ADD;
  319. }
  320. }
  321. }
  322. void CBaseAnimationSetControl::SelectControlForDag( const CDmeDag *pDag, ESelectionMode selectionMode )
  323. {
  324. CDmeTransformControl *pTransformControl = pDag->FindTransformControl();
  325. if ( pTransformControl )
  326. {
  327. SelectControl( pTransformControl, selectionMode, TRANSFORM_COMPONENT_ALL, true );
  328. }
  329. }
  330. void CBaseAnimationSetControl::SetRangeSelectionState( bool bInRangeSelection )
  331. {
  332. static CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this );
  333. if ( bInRangeSelection )
  334. {
  335. sg.Start( SELECTION_SET );
  336. }
  337. else
  338. {
  339. sg.Finish();
  340. }
  341. }
  342. void CBaseAnimationSetControl::SelectAnimationSet( CDmeAnimationSet *pAnimSet, ESelectionMode selectionMode /*= SELECTION_ADD*/ )
  343. {
  344. CNotifyAnimationSetControlSelectionChangedScopeGuard sg( this, selectionMode );
  345. SelectControlGroup( pAnimSet->GetRootControlGroup(), selectionMode );
  346. // Find all of the root of the aniamtion set
  347. CUtlVector< CDmeDag* > rootDagNodes;
  348. pAnimSet->FindRootDagNodes( rootDagNodes );
  349. // Make the fist selected root the primary selection
  350. // by moving it to the end of the selection list
  351. int nNumRoots = rootDagNodes.Count();
  352. for ( int iRoot = 0; iRoot < nNumRoots; ++iRoot )
  353. {
  354. CDmeDag *pRootDag = rootDagNodes[ iRoot ];
  355. if ( pRootDag == NULL )
  356. continue;
  357. CDmeTransformControl *pControl = pRootDag->FindTransformControl();
  358. SelectionInfo_t *pSelectionInfo = FindSelectionInfoForControl( pControl );
  359. if ( ( pSelectionInfo ) && ( pSelectionInfo->m_nComponentFlags != 0 ) )
  360. {
  361. m_SelectionHistory.FindAndRemove( pSelectionInfo );
  362. m_SelectionHistory.AddToTail( pSelectionInfo );
  363. break;
  364. }
  365. }
  366. }
  367. SelectionState_t CBaseAnimationSetControl::GetSelectionState( CDmeAnimationSet *pAnimSet ) const
  368. {
  369. if ( !pAnimSet )
  370. return SEL_EMPTY;
  371. return GetSelectionState( pAnimSet->GetRootControlGroup() );
  372. }
  373. SelectionState_t CBaseAnimationSetControl::GetSelectionState( CDmeControlGroup *pControlGroup ) const
  374. {
  375. if ( !pControlGroup )
  376. return SEL_EMPTY;
  377. SelectionState_t selection = SEL_EMPTY;
  378. const CDmaElementArray< CDmeControlGroup > &children = pControlGroup->Children();
  379. int nChildren = children.Count();
  380. for ( int i = 0; i < nChildren; ++i )
  381. {
  382. selection += GetSelectionState( children[ i ] );
  383. if ( selection == SEL_SOME )
  384. return SEL_SOME; // once we get to SEL_SOME, there we stay
  385. }
  386. const CDmaElementArray< CDmElement > &controls = pControlGroup->Controls();
  387. int nControls = controls.Count();
  388. for ( int i = 0; i < nControls; ++i )
  389. {
  390. selection += GetSelectionState( controls[ i ] );
  391. if ( selection == SEL_SOME )
  392. return SEL_SOME; // once we get to SEL_SOME, there we stay
  393. }
  394. return selection;
  395. }
  396. SelectionState_t CBaseAnimationSetControl::GetSelectionState( CDmElement *pControl, TransformComponent_t componentFlags /*= TRANSFORM_COMPONENT_ALL*/ ) const
  397. {
  398. Assert( pControl );
  399. if ( ( pControl == NULL ) || !IsControlVisible( pControl ) )
  400. return SEL_EMPTY;
  401. TransformComponent_t nSelectionComponentFlags = GetSelectionComponentFlags( pControl );
  402. TransformComponent_t nCombinedComponentFlags = nSelectionComponentFlags & componentFlags;
  403. if ( nCombinedComponentFlags == 0 )
  404. return SEL_NONE;
  405. if ( nCombinedComponentFlags == componentFlags )
  406. return SEL_ALL;
  407. return SEL_SOME;
  408. }
  409. CDmElement *CBaseAnimationSetControl::GetMostRecentlySelectedControl()
  410. {
  411. int i = m_SelectionHistory.Tail();
  412. if ( i == m_SelectionHistory.InvalidIndex() )
  413. return NULL;
  414. return m_SelectionHistory[ i ]->m_hControl;
  415. }
  416. TransformComponent_t CBaseAnimationSetControl::GetSelectionComponentFlags( CDmElement *pControl ) const
  417. {
  418. if ( pControl == NULL )
  419. return TRANSFORM_COMPONENT_NONE;
  420. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Next( i ) )
  421. {
  422. const SelectionInfo_t *psi = m_SelectionHistory[ i ];
  423. if ( psi->m_hControl.Get() == pControl )
  424. return psi->m_nComponentFlags;
  425. }
  426. return TRANSFORM_COMPONENT_NONE;
  427. }
  428. void SetPresetFromControl( CDmePreset *pPreset, CDmElement *pControl )
  429. {
  430. CDmeTransformControl *pTransformControl = CastElement< CDmeTransformControl >( pControl );
  431. if ( pTransformControl )
  432. {
  433. CDmElement *pControlValue = pPreset->FindOrAddControlValue( pControl->GetName() );
  434. if ( pControlValue == NULL )
  435. return;
  436. CDmeChannel *pPosChannel = pTransformControl->GetPositionChannel();
  437. if ( pPosChannel )
  438. {
  439. pControlValue->SetValue< Vector >( "valuePosition", pTransformControl->GetPosition() );
  440. }
  441. CDmeChannel *pRotChannel = pTransformControl->GetOrientationChannel();
  442. if ( pRotChannel )
  443. {
  444. pControlValue->SetValue< Quaternion >( "valueOrientation", pTransformControl->GetOrientation() );
  445. }
  446. }
  447. else
  448. {
  449. // Stamp the control value
  450. CDmElement *pControlValue = pPreset->FindOrAddControlValue( pControl->GetName() );
  451. if ( IsStereoControl( pControl ) )
  452. {
  453. pControlValue->RemoveAttribute( "value" );
  454. pControlValue->SetValue< float >( "leftValue", pControl->GetValue< float >( "leftValue" ) );
  455. pControlValue->SetValue< float >( "rightValue", pControl->GetValue< float >( "rightValue" ) );
  456. }
  457. else
  458. {
  459. pControlValue->SetValue< float >( "value", pControl->GetValue< float >( "value" ) );
  460. pControlValue->RemoveAttribute( "leftValue" );
  461. pControlValue->RemoveAttribute( "rightValue" );
  462. }
  463. }
  464. }
  465. template < class T >
  466. void AddKeysToPreset( CDmePreset *pPreset, const char *pValuesAttrName, const char *pTimesAttrName, const CDmElement *pControl, const char *pChannelAttrName, DmeTime_t tHead, DmeTime_t tStart, DmeTime_t tEnd )
  467. {
  468. if ( !pPreset || !pControl )
  469. return;
  470. CDmeChannel *pChannel = pControl->GetValueElement< CDmeChannel >( pChannelAttrName );
  471. if ( !pChannel )
  472. return;
  473. CDmElement *pControlValue = pPreset->FindOrAddControlValue( pControl->GetName() );
  474. CDmrArray< T > values( pControlValue, pValuesAttrName, true );
  475. CDmrArray< DmeTime_t > times ( pControlValue, pTimesAttrName, true );
  476. CDmeTypedLog< T > *pLog = CastElement< CDmeTypedLog< T > >( pChannel->GetLog() );
  477. CDmeChannelsClip *pChannelsClip = FindReferringElement< CDmeChannelsClip >( pChannel, "channels" );
  478. if ( !pLog || pLog->IsEmpty() || !pChannelsClip )
  479. {
  480. times.AddToTail( DMETIME_ZERO );
  481. T v;
  482. pChannel->GetInputValue( v );
  483. values.AddToTail( v );
  484. return;
  485. }
  486. DmeTime_t tLocalStart = pChannelsClip->ToChildMediaTime( tStart, false );
  487. DmeTime_t tLocalEnd = pChannelsClip->ToChildMediaTime( tEnd, false );
  488. bool bFirst = true;
  489. int nKeys = pLog->GetKeyCount();
  490. for ( int i = 0; i < nKeys; ++i )
  491. {
  492. DmeTime_t t = pLog->GetKeyTime( i );
  493. if ( t < tLocalStart )
  494. continue;
  495. if ( bFirst )
  496. {
  497. bFirst = false;
  498. times.AddToTail( tStart - tHead );
  499. values.AddToTail( pLog->GetValue( tLocalStart ) );
  500. if ( t == tLocalStart )
  501. continue;
  502. }
  503. if ( t >= tLocalEnd )
  504. {
  505. int nTimes = times.Count();
  506. DmeTime_t tLast = nTimes > 0 ? times[ nTimes - 1 ] : tEnd;
  507. if ( tLast <= tLocalEnd )
  508. {
  509. times.AddToTail( tEnd - tHead );
  510. values.AddToTail( pLog->GetValue( tLocalEnd ) );
  511. }
  512. break;
  513. }
  514. t = pChannelsClip->FromChildMediaTime( t, false );
  515. times.AddToTail( t - tHead );
  516. values.AddToTail( pLog->GetKeyValue( i ) );
  517. }
  518. }
  519. void SetPresetFromControlChannels( CDmePreset *pPreset, const CDmElement *pControl, DmeTime_t tHead, DmeTime_t tStart, DmeTime_t tEnd )
  520. {
  521. if ( IsTransformControl( pControl ) )
  522. {
  523. AddKeysToPreset< Vector >( pPreset, AS_VALUES_POSITION_ATTR, AS_TIMES_POSITION_ATTR, pControl, "positionChannel", tHead, tStart, tEnd );
  524. AddKeysToPreset< Quaternion >( pPreset, AS_VALUES_ORIENTATION_ATTR, AS_TIMES_ORIENTATION_ATTR, pControl, "orientationChannel", tHead, tStart, tEnd );
  525. }
  526. if ( IsStereoControl( pControl ) )
  527. {
  528. AddKeysToPreset< float >( pPreset, AS_VALUES_LEFT_ATTR, AS_TIMES_LEFT_ATTR, pControl, "leftvaluechannel", tHead, tStart, tEnd );
  529. AddKeysToPreset< float >( pPreset, AS_VALUES_RIGHT_ATTR, AS_TIMES_RIGHT_ATTR, pControl, "rightvaluechannel", tHead, tStart, tEnd );
  530. }
  531. else
  532. {
  533. AddKeysToPreset< float >( pPreset, AS_VALUES_ATTR, AS_TIMES_ATTR, pControl, "channel", tHead, tStart, tEnd );
  534. }
  535. }
  536. //-----------------------------------------------------------------------------
  537. // Reads the current animation set control values, creates presets
  538. //-----------------------------------------------------------------------------
  539. void CBaseAnimationSetControl::SetPresetFromControls( const char *pPresetGroupName, const char *pPresetName )
  540. {
  541. if ( !m_hFilmClip.Get() )
  542. return;
  543. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Set Preset" );
  544. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Next( i ) )
  545. {
  546. SelectionInfo_t *psi = m_SelectionHistory[ i ];
  547. CDmeAnimationSet *pAnimSet = psi->m_hAnimSet;
  548. CDmElement *pControl = psi->m_hControl;
  549. if ( !pControl || !pAnimSet )
  550. continue;
  551. CDmePresetGroup *pPresetGroup = pAnimSet->FindPresetGroup( pPresetGroupName );
  552. if ( !pPresetGroup )
  553. continue;
  554. CDmePreset *pPreset = pPresetGroup->FindPreset( pPresetName );
  555. if ( !pPreset )
  556. continue;
  557. SetPresetFromControl( pPreset, pControl );
  558. }
  559. }
  560. void CBaseAnimationSetControl::AddPreset( const char *pPresetGroupName, const char *pPresetName, bool bAnimated )
  561. {
  562. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Add Preset" );
  563. CAnimSetGroupAnimSetTraversal traversal( m_hFilmClip );
  564. while ( CDmeAnimationSet *pAnimSet = traversal.Next() )
  565. {
  566. SelectionState_t selstate = GetSelectionState( pAnimSet );
  567. if ( selstate == SEL_EMPTY || selstate == SEL_NONE )
  568. continue;
  569. AddPreset( pAnimSet, pPresetGroupName, pPresetName, bAnimated );
  570. }
  571. }
  572. void CBaseAnimationSetControl::GetAnimatedPresetTimeParameters( DmeTime_t &tHead, DmeTime_t &tStart, DmeTime_t &tEnd )
  573. {
  574. tHead = DMETIME_ZERO;
  575. tStart = DMETIME_MINTIME / 2;
  576. tEnd = DMETIME_MAXTIME / 2;
  577. }
  578. void CBaseAnimationSetControl::AddPreset( CDmeAnimationSet *pAnimSet, const char *pPresetGroupName, const char *pPresetName, bool bAnimated )
  579. {
  580. CDmePresetGroup *pPresetGroup = pAnimSet->FindOrAddPresetGroup( pPresetGroupName );
  581. CDmePreset *pPreset = pPresetGroup->FindPreset( pPresetName );
  582. if ( pPreset )
  583. {
  584. Assert( 0 );
  585. return; // TODO - should this preset be skipped, deleted and re-added, or somehow merged? (merging gives undesirable results if the control selections differ)
  586. }
  587. pPreset = pPresetGroup->FindOrAddPreset( pPresetName );
  588. if ( bAnimated )
  589. {
  590. DmeTime_t tHead, tStart, tEnd;
  591. GetAnimatedPresetTimeParameters( tHead, tStart, tEnd );
  592. pPreset->SetValue( "animated", true );
  593. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Next( i ) )
  594. {
  595. SelectionInfo_t *psi = m_SelectionHistory[ i ];
  596. if ( psi->m_hAnimSet != pAnimSet )
  597. continue;
  598. // TODO - factor in componentFlags!!!
  599. SetPresetFromControlChannels( pPreset, psi->m_hControl, tHead, tStart, tEnd );
  600. }
  601. }
  602. else
  603. {
  604. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Next( i ) )
  605. {
  606. SelectionInfo_t *psi = m_SelectionHistory[ i ];
  607. if ( psi->m_hAnimSet != pAnimSet )
  608. continue;
  609. // TODO - factor in componentFlags!!!
  610. SetPresetFromControl( pPreset, psi->m_hControl );
  611. }
  612. }
  613. }
  614. //-----------------------------------------------------------------------------
  615. // Can the control be snapped to
  616. //-----------------------------------------------------------------------------
  617. bool CBaseAnimationSetControl::IsControlSnapTarget( const CDmElement *pControl ) const
  618. {
  619. CDmeControlGroup *pGroup = CDmeControlGroup::FindGroupContainingControl( pControl );
  620. if ( pGroup == NULL )
  621. return false;
  622. return pGroup->IsSnappable();
  623. }
  624. //-----------------------------------------------------------------------------
  625. // Is the control selectable in the viewport
  626. //-----------------------------------------------------------------------------
  627. bool CBaseAnimationSetControl::IsControlSelectable( const CDmElement *pControl ) const
  628. {
  629. CDmeControlGroup *pGroup = CDmeControlGroup::FindGroupContainingControl( pControl );
  630. if ( pGroup == NULL )
  631. return false;
  632. return ( pGroup->IsVisible() && pGroup->IsSelectable() );
  633. }
  634. //-----------------------------------------------------------------------------
  635. // Purpose: Determine if the specified control is hidden
  636. //-----------------------------------------------------------------------------
  637. bool CBaseAnimationSetControl::IsControlVisible( const CDmElement *pControl ) const
  638. {
  639. if ( IsShowingHiddenControls() )
  640. return true;
  641. CDmeControlGroup *pGroup = CDmeControlGroup::FindGroupContainingControl( pControl );
  642. if ( pGroup )
  643. {
  644. return pGroup->IsVisible();
  645. }
  646. return false;
  647. }
  648. //-----------------------------------------------------------------------------
  649. // Determine if the specified control group is visible
  650. //-----------------------------------------------------------------------------
  651. bool CBaseAnimationSetControl::IsControlGroupVisible( const CDmeControlGroup *pGroup ) const
  652. {
  653. if ( IsShowingHiddenControls() )
  654. return true;
  655. return pGroup->IsVisible();
  656. }
  657. void CBaseAnimationSetControl::UpdatePreviewSliderValues()
  658. {
  659. CBaseAnimSetAttributeSliderPanel *pAttributeSlider = m_pEditor->GetAttributeSlider();
  660. if ( !pAttributeSlider )
  661. return;
  662. float flBalanceSliderValue = pAttributeSlider->GetBalanceSliderValue();
  663. CBaseAnimSetPresetFaderPanel *presets = m_pEditor->GetPresetFader();
  664. if ( !presets )
  665. return;
  666. FaderPreview_t fader;
  667. presets->GetPreviewFader( fader );
  668. bool nameChanged = fader.name && ( m_PreviousPresetSlider.IsEmpty() || Q_stricmp( m_PreviousPresetSlider.Get(), fader.name ) );
  669. bool beingDraggedChanged = fader.isbeingdragged != m_bPresetPreviouslyDragged;
  670. bool previewKeyChanged = fader.holdingPreviewKey != m_bPreviouslyHoldingPresetPreviewKey;
  671. bool bFaderChanged = nameChanged || beingDraggedChanged || previewKeyChanged;
  672. bool faderAmountChanged = fader.amount != m_flPreviousPresetAmount;
  673. // Update values for procedural presets, but not if we are already actively dragging, otherwise the
  674. // target value of the head preset can change during the drag if the head is within the falloff region.
  675. if ( fader.holdingPreviewKey || ( fader.isbeingdragged && beingDraggedChanged ) || bFaderChanged )
  676. {
  677. presets->UpdateProceduralPresetSlider( fader.values );
  678. }
  679. // logic moved from CAnimSetAttributeSliderPanel for simplicity - another pass may remove a fair amount of it entirely
  680. bool previewKeyPressed = fader.holdingPreviewKey && previewKeyChanged;
  681. bool startedDrag = fader.isbeingdragged && beingDraggedChanged;
  682. bool newControl = nameChanged;
  683. m_bPresetSliderChanged = newControl || previewKeyPressed || startedDrag;
  684. m_PreviousPresetSlider = fader.name;
  685. m_flPreviousPresetAmount = fader.amount;
  686. m_bPresetPreviouslyDragged = fader.isbeingdragged;
  687. m_bPreviouslyHoldingPresetPreviewKey = fader.holdingPreviewKey;
  688. bool shiftDown = input()->IsKeyDown( KEY_LSHIFT ) || input()->IsKeyDown( KEY_RSHIFT );
  689. int c = pAttributeSlider->GetSliderCount();
  690. for ( int i = 0; i < c; ++i )
  691. {
  692. CAttributeSlider *slider = pAttributeSlider->GetSlider( i );
  693. if ( !slider->IsVisible() )
  694. continue;
  695. CDmElement *pControl = slider->GetControl();
  696. if ( !pControl )
  697. continue;
  698. bool bTransform = slider->IsTransform();
  699. if ( m_ActiveAttributeSlider.Get() == slider && !slider->IsDragging() && shiftDown )
  700. {
  701. // The preset stuff shouldn't be active when we're holding the preview key over the raw attribute sliders!!!
  702. Assert( !fader.isbeingdragged );
  703. AttributeValue_t dest;
  704. if ( !bTransform )
  705. {
  706. int x, y;
  707. input()->GetCursorPos( x, y );
  708. slider->ScreenToLocal( x, y );
  709. float flEstimatedValue = slider->EstimateValueAtPos( x, y );
  710. dest.m_pValue[ ANIM_CONTROL_VALUE ] = flEstimatedValue;
  711. dest.m_pValue[ ANIM_CONTROL_VALUE_LEFT ] = flEstimatedValue;
  712. dest.m_pValue[ ANIM_CONTROL_VALUE_RIGHT ] = flEstimatedValue;
  713. }
  714. else
  715. {
  716. slider->GetValue( ANIM_CONTROL_TXFORM_POSITION, dest.m_Vector );
  717. slider->GetValue( ANIM_CONTROL_TXFORM_ORIENTATION, dest.m_Quaternion );
  718. }
  719. // If we aren't over any of the preset sliders, then we need to be able to ramp down to the current value, too
  720. slider->SetPreview( dest, dest );
  721. continue;
  722. }
  723. if ( !fader.values )
  724. continue;
  725. bool simple = fader.isbeingdragged || !fader.holdingPreviewKey;
  726. if ( bFaderChanged || fader.isbeingdragged )
  727. {
  728. int idx = fader.values->Find( pControl->GetHandle() );
  729. const AttributeValue_t &previewin = idx == fader.values->InvalidIndex() ? slider->GetValue() : fader.values->Element( idx );
  730. const AttributeValue_t &current = slider->GetValue();
  731. AttributeValue_t preview;
  732. BlendValues( bTransform, &preview, current, previewin, 1.0f, flBalanceSliderValue );
  733. // If being dragged, slam to current value right away
  734. if ( simple )
  735. {
  736. slider->SetPreview( preview, preview );
  737. }
  738. else
  739. {
  740. // Apply the left-right balance to the target
  741. AttributeValue_t dest;
  742. BlendValues( bTransform, &dest, current, preview, fader.amount );
  743. slider->SetPreview( dest, preview );
  744. }
  745. }
  746. if ( faderAmountChanged || fader.isbeingdragged || fader.holdingPreviewKey )
  747. {
  748. slider->UpdateFaderAmount( fader.amount );
  749. }
  750. }
  751. }
  752. void CBaseAnimationSetControl::UpdatePreviewSliderTimes()
  753. {
  754. CBaseAnimSetAttributeSliderPanel *pAttributeSlider = m_pEditor->GetAttributeSlider();
  755. if ( !pAttributeSlider )
  756. return;
  757. bool ctrlDown = input()->IsKeyDown( KEY_LCONTROL ) || input()->IsKeyDown( KEY_RCONTROL );
  758. bool shiftDown = input()->IsKeyDown( KEY_LSHIFT ) || input()->IsKeyDown( KEY_RSHIFT );
  759. if ( ctrlDown )
  760. {
  761. int mx, my;
  762. input()->GetCursorPos( mx, my );
  763. bool bInside = pAttributeSlider->IsWithin( mx, my );
  764. if ( !bInside )
  765. {
  766. shiftDown = false;
  767. ctrlDown = false;
  768. }
  769. VPANEL topMost = input()->GetMouseOver();
  770. if ( topMost && !ipanel()->HasParent( topMost, pAttributeSlider->GetVPanel() ) )
  771. {
  772. shiftDown = false;
  773. ctrlDown = false;
  774. }
  775. }
  776. bool previewing = ( m_bPreviouslyHoldingPresetPreviewKey ) || ( m_ActiveAttributeSlider.Get() && shiftDown );
  777. bool changingvalues = ( m_bPresetPreviouslyDragged ) || ( m_ActiveAttributeSlider.Get() && m_ActiveAttributeSlider->IsDragging() );
  778. // If control is being pressed or this slider is the currently active dominant slider set
  779. // this slider to be the new dominant slider. This will cause the dominant slider to be
  780. // selected when you press control but not to be lost if the control key is released.
  781. int newDominantSlider = -1;
  782. if ( m_ActiveAttributeSlider.Get() && m_ActiveAttributeSlider->IsDragging() )
  783. {
  784. if ( ctrlDown || ( m_nDominantSlider >= 0 && pAttributeSlider->GetSlider( m_nDominantSlider ) == m_ActiveAttributeSlider.Get() ) )
  785. {
  786. newDominantSlider = pAttributeSlider->FindSliderIndexForControl( m_ActiveAttributeSlider->GetControl() );
  787. }
  788. }
  789. // If the dominant slider has changed update starting values that are used to determine what
  790. // values the other sliders should fade from as the value of the dominant slider increases.
  791. if ( newDominantSlider != m_nDominantSlider )
  792. {
  793. UpdateDominantSliderStartValues( newDominantSlider < 0 );
  794. m_nDominantSlider = newDominantSlider;
  795. }
  796. CAttributeSlider *dragSlider = m_ActiveAttributeSlider && m_ActiveAttributeSlider->IsDragging() ? m_ActiveAttributeSlider.Get() : NULL;
  797. pAttributeSlider->UpdateControlSetMode( changingvalues, previewing, dragSlider );
  798. }
  799. bool CBaseAnimationSetControl::IsPresetFaderBeingDragged() const
  800. {
  801. return m_bPresetPreviouslyDragged;
  802. }
  803. float CBaseAnimationSetControl::GetDominantSliderStartValue( int nSliderIndex, AnimationControlType_t type )
  804. {
  805. return m_DominantSliderStartValues[ nSliderIndex ].m_pValue[ type ];
  806. }
  807. //-----------------------------------------------------------------------------
  808. // Purpose: Save the current values of the visible sliders to the dominant
  809. // slider start value or restore slider values to the value saved before if
  810. // the restoreSliderValues flag is true.
  811. //-----------------------------------------------------------------------------
  812. void CBaseAnimationSetControl::UpdateDominantSliderStartValues( bool restoreSliderValues )
  813. {
  814. CBaseAnimSetAttributeSliderPanel *pAttributeSlider = m_pEditor->GetAttributeSlider();
  815. if ( !pAttributeSlider )
  816. return;
  817. int nSliders = pAttributeSlider->GetSliderCount();
  818. // If the start values have not been saved yet the array will need to be allocated.
  819. if ( m_DominantSliderStartValues.Count() != nSliders )
  820. {
  821. Assert( m_DominantSliderStartValues.Count() == 0 );
  822. m_DominantSliderStartValues.SetCount( nSliders );
  823. // Can't restore the values of the sliders to the
  824. // start values if the start values have not been set.
  825. if ( restoreSliderValues )
  826. {
  827. Assert( restoreSliderValues == false );
  828. return;
  829. }
  830. }
  831. // Loop through all of the sliders and update the values of the visible sliders.
  832. for ( int i = 0; i < nSliders; ++i )
  833. {
  834. CAttributeSlider *pSlider = pAttributeSlider->GetSlider( i );
  835. if ( pSlider == NULL )
  836. continue;
  837. if ( !pSlider->IsVisible() )
  838. continue;
  839. if ( restoreSliderValues )
  840. {
  841. if ( pSlider->IsDragging() == false )
  842. {
  843. pSlider->SetValue( m_DominantSliderStartValues[ i ] );
  844. }
  845. }
  846. else
  847. {
  848. m_DominantSliderStartValues[ i ] = pSlider->GetValue();
  849. }
  850. }
  851. }
  852. //-----------------------------------------------------------------------------
  853. // Purpose: Get the start and current values for the dominant slider based on
  854. // the first active control.
  855. //-----------------------------------------------------------------------------
  856. void CBaseAnimationSetControl::GetDominantSliderValues( float &flDomStart, float &flDomValue )
  857. {
  858. flDomStart = 0.0f;
  859. flDomValue = 0.0f;
  860. if ( m_nDominantSlider < 0 )
  861. return;
  862. CBaseAnimSetAttributeSliderPanel *pAttributeSlider = m_pEditor->GetAttributeSlider();
  863. if ( !pAttributeSlider )
  864. return;
  865. CAttributeSlider *pDominantSlider = pAttributeSlider->GetSlider( m_nDominantSlider );
  866. if ( pDominantSlider == NULL )
  867. return;
  868. if ( pDominantSlider->IsTransform() )
  869. return;
  870. AnimationControlType_t type = pDominantSlider->IsStereo() ? ANIM_CONTROL_VALUE_RIGHT : ANIM_CONTROL_VALUE;
  871. flDomStart = max( 0.0f, min( 1.0f, GetDominantSliderStartValue( m_nDominantSlider, type ) ) );
  872. flDomValue = max( 0.0f, min( 1.0f, pDominantSlider->GetValue( type ) ) );
  873. }
  874. void CBaseAnimationSetControl::ApplyPreset( float flScale, AttributeDict_t& values )
  875. {
  876. CBaseAnimSetAttributeSliderPanel *pAttributeSlider = m_pEditor->GetAttributeSlider();
  877. if ( !pAttributeSlider )
  878. return;
  879. bool bChanged = false;
  880. int c = pAttributeSlider->GetSliderCount();
  881. for ( int i = 0; i < c; ++i )
  882. {
  883. CAttributeSlider *slider = pAttributeSlider->GetSlider( i );
  884. if ( !slider || !slider->IsVisible() )
  885. continue;
  886. int idx = values.Find( slider->GetControl()->GetHandle() );
  887. const AttributeValue_t &target = idx == values.InvalidIndex() ? slider->GetValue() : values[ idx ];
  888. const AttributeValue_t &current = slider->GetValue();
  889. // Apply the left-right balance to the target
  890. AttributeValue_t blend;
  891. BlendValues( slider->IsTransform(), &blend, current, target, flScale, pAttributeSlider->GetBalanceSliderValue() );
  892. slider->SetValue( blend );
  893. bChanged = true;
  894. }
  895. if ( bChanged )
  896. {
  897. pAttributeSlider->UpdatePreview( "ApplyPreset\n" );
  898. }
  899. }
  900. template< class T >
  901. void CBaseAnimationSetControl::ApplyTransformSliderValue( CAttributeSlider *pSlider, CDmeTransformControl *pTranformControl, bool bUsePreviewValue, bool bForce, bool &valuesChanged, AnimationControlType_t type )
  902. {
  903. CDmAttribute *pAttr = NULL;
  904. CDmeChannel *pChannel = NULL;
  905. if ( type == ANIM_CONTROL_TXFORM_POSITION )
  906. {
  907. pAttr = pTranformControl->GetPositionAttr();
  908. pChannel = pTranformControl->GetPositionChannel();
  909. }
  910. else if ( type == ANIM_CONTROL_TXFORM_ORIENTATION )
  911. {
  912. pAttr = pTranformControl->GetOrientationAttr();
  913. pChannel = pTranformControl->GetOrientationChannel();
  914. }
  915. Assert( pAttr );
  916. if ( !pAttr )
  917. return;
  918. Assert( pChannel );
  919. // Figure out what to do based on the channel's mode
  920. ChannelMode_t mode = pChannel ? pChannel->GetMode() : CM_PASS;
  921. bool bPushSlidersIntoScene = ( mode == CM_PASS || mode == CM_RECORD );
  922. bool bPullSlidersFromScene = ( mode == CM_PLAY );
  923. if ( bPullSlidersFromScene )
  924. {
  925. T value = pAttr->GetValue< T >();
  926. pChannel->GetCurrentPlaybackValue< T >( value );
  927. pSlider->SetValue( type, value );
  928. pAttr->SetValue( value );
  929. }
  930. else if ( bPushSlidersIntoScene )
  931. {
  932. T value;
  933. if ( bUsePreviewValue )
  934. {
  935. pSlider->GetPreview( type, value );
  936. }
  937. else
  938. {
  939. pSlider->GetValue( type, value );
  940. }
  941. if ( pAttr->GetValue< T >() != value || bForce )
  942. {
  943. // The txform manipulator drives the UpdatePreview call, so don't do it twice unless
  944. // we are just dialing in a preset (bForce == true)
  945. valuesChanged = bForce;
  946. T currentValue = pAttr->GetValue< T >();
  947. T maskedValue = MaskValue( value, currentValue, pSlider->VisibleComponents() );
  948. pAttr->SetValue( maskedValue );
  949. }
  950. }
  951. }
  952. void CBaseAnimationSetControl::ApplySliderValueWithDominance( CAttributeSlider *pSlider, int si, float flDomStart, float flDomValue, CDmElement *pControl, bool bUsePreviewValue, bool bForce, bool &valuesChanged, AnimationControlType_t type, const char *pChannelAttrName, const char *pValueAttrName )
  953. {
  954. CDmAttribute *pAttr = pControl->GetAttribute( pValueAttrName, AT_FLOAT );
  955. Assert( pAttr );
  956. if ( !pAttr )
  957. return;
  958. CDmeChannel *pChannel = pControl->GetValueElement< CDmeChannel >( pChannelAttrName );
  959. Assert( pChannel );
  960. // Figure out what to do based on the channel's mode
  961. ChannelMode_t mode = pChannel ? pChannel->GetMode() : CM_PASS;
  962. bool bPushSlidersIntoScene = ( mode == CM_PASS || mode == CM_RECORD );
  963. bool bPullSlidersFromScene = ( mode == CM_PLAY );
  964. if ( bPullSlidersFromScene )
  965. {
  966. // If it's actively being manipulated, the UI will be up to date
  967. if ( pSlider->IsDragging() )
  968. return;
  969. // Drive value setting based on the output data
  970. float flValue = pControl->GetValue< float >( DEFAULT_FLOAT_ATTR );
  971. if ( pChannel->GetCurrentPlaybackValue< float >( flValue ) )
  972. {
  973. pAttr->SetValue( flValue );
  974. }
  975. else
  976. {
  977. flValue = pAttr->GetValue< float >();
  978. }
  979. pSlider->SetValue( type, flValue );
  980. }
  981. else if ( bPushSlidersIntoScene )
  982. {
  983. float flValue = bUsePreviewValue ? pSlider->GetPreview( type ) : pSlider->GetValue( type );
  984. // If there is an active dominant slider then all other visible
  985. // sliders, should scale down based on the value of the dominant slider.
  986. if ( ( m_nDominantSlider >= 0 ) && pSlider->IsVisible() && !bUsePreviewValue )
  987. {
  988. if ( m_nDominantSlider != si )
  989. {
  990. float flSliderStart = GetDominantSliderStartValue( si, type );
  991. float flRange = 1.0f - flDomStart;
  992. float flScale = ( flRange > 0 ) ? ( max( 0.0f, flDomValue - flDomStart ) / flRange ) : 0.0f;
  993. float flTargetValue = pControl->GetValue< float >( DEFAULT_FLOAT_ATTR );
  994. flValue = flSliderStart * ( 1.0f - flScale ) + flTargetValue * flScale;
  995. pSlider->SetValue( type, flValue );
  996. }
  997. }
  998. if ( pAttr->GetValue< float >() != flValue || bForce )
  999. {
  1000. valuesChanged = true;
  1001. pAttr->SetValue( flValue );
  1002. }
  1003. }
  1004. }
  1005. bool CBaseAnimationSetControl::ApplySliderValues( bool bForce )
  1006. {
  1007. CBaseAnimSetAttributeSliderPanel *pAttributeSlider = m_pEditor->GetAttributeSlider();
  1008. if ( !pAttributeSlider )
  1009. return false;
  1010. if ( !bForce )
  1011. {
  1012. bForce = m_bPresetPreviouslyDragged;
  1013. }
  1014. CDisableUndoScopeGuard guard;
  1015. float flDomStart = 0;
  1016. float flDomValue = 0;
  1017. GetDominantSliderValues( flDomStart, flDomValue );
  1018. bool valuesChanged = false;
  1019. int nSliders = pAttributeSlider->GetSliderCount();
  1020. for ( int si = 0; si < nSliders; ++si )
  1021. {
  1022. CAttributeSlider *pSlider = pAttributeSlider->GetSlider( si );
  1023. Assert( pSlider );
  1024. if ( !pSlider || !pSlider->IsVisible() )
  1025. continue;
  1026. CDmElement *pControl = pSlider->GetControl();
  1027. if ( !pControl )
  1028. continue;
  1029. bool shiftDown = input()->IsKeyDown( KEY_LSHIFT ) || input()->IsKeyDown( KEY_RSHIFT );
  1030. bool bPreviewingAttributeSlider = m_ActiveAttributeSlider.Get() == pSlider && !pSlider->IsDragging() && shiftDown;
  1031. bool bMouseOverPresetSlider = m_pEditor->GetPresetFader()->GetActivePresetSlider() != NULL;
  1032. bool bUsePreviewValue = m_bPreviouslyHoldingPresetPreviewKey || bPreviewingAttributeSlider || ( bForce && bMouseOverPresetSlider );
  1033. CDmeTransformControl *pTransformControl = CastElement< CDmeTransformControl >( pControl );
  1034. if ( pTransformControl )
  1035. {
  1036. if ( pSlider->IsOrientation() )
  1037. {
  1038. ApplyTransformSliderValue< Quaternion >( pSlider, pTransformControl, bUsePreviewValue, bForce, valuesChanged, ANIM_CONTROL_TXFORM_ORIENTATION );
  1039. }
  1040. else
  1041. {
  1042. ApplyTransformSliderValue< Vector >( pSlider, pTransformControl, bUsePreviewValue, bForce, valuesChanged, ANIM_CONTROL_TXFORM_POSITION );
  1043. }
  1044. }
  1045. else if ( IsStereoControl( pControl ) )
  1046. {
  1047. ApplySliderValueWithDominance( pSlider, si, flDomStart, flDomValue, pControl, bUsePreviewValue, bForce, valuesChanged, ANIM_CONTROL_VALUE_LEFT, "leftvaluechannel", "leftValue" );
  1048. ApplySliderValueWithDominance( pSlider, si, flDomStart, flDomValue, pControl, bUsePreviewValue, bForce, valuesChanged, ANIM_CONTROL_VALUE_RIGHT, "rightvaluechannel", "rightValue" );
  1049. }
  1050. else
  1051. {
  1052. ApplySliderValueWithDominance( pSlider, si, flDomStart, flDomValue, pControl, bUsePreviewValue, bForce, valuesChanged, ANIM_CONTROL_VALUE, "channel", "value" );
  1053. }
  1054. }
  1055. guard.Release();
  1056. return valuesChanged;
  1057. }
  1058. void CBaseAnimationSetControl::SetActiveAttributeSlider( CAttributeSlider *pSlider )
  1059. {
  1060. m_ActiveAttributeSlider = pSlider;
  1061. }
  1062. void CBaseAnimationSetControl::EnsureCrossfadePresetControlValues( int nCount )
  1063. {
  1064. m_crossfadePresetControlValues.RemoveAll();
  1065. int nOldCount = m_crossfadePresetControlValues.Count();
  1066. for ( int i = nOldCount; i < nCount; ++i )
  1067. {
  1068. m_crossfadePresetControlValues.AddToTail( CreateElement< CDmElement >( "procedural preset control value", DMFILEID_INVALID ) );
  1069. }
  1070. }
  1071. void CBaseAnimationSetControl::ProceduralPreset_UpdateCrossfade( AttributeDict_t *pPresetValuesLookup, int nPresetType )
  1072. {
  1073. switch ( nPresetType )
  1074. {
  1075. case PROCEDURAL_PRESET_HEAD_CROSSFADE:
  1076. case PROCEDURAL_PRESET_IN_CROSSFADE:
  1077. case PROCEDURAL_PRESET_OUT_CROSSFADE:
  1078. return; // can only update these in the sfm (or an app that knows about current time (head) and time selection (in/out)
  1079. }
  1080. CDisableUndoScopeGuard guard;
  1081. bool bIsDefaultPreset = nPresetType == PROCEDURAL_PRESET_DEFAULT_CROSSFADE;
  1082. bool bSinglePreset = !bIsDefaultPreset; // the only other presets this code deals with are zero, half, one
  1083. EnsureCrossfadePresetControlValues( bSinglePreset ? 1 : m_SelectionHistory.Count() );
  1084. if ( bSinglePreset )
  1085. {
  1086. float flValue = 0.0f;
  1087. switch ( nPresetType )
  1088. {
  1089. case PROCEDURAL_PRESET_ZERO_CROSSFADE:
  1090. flValue = 0.0f;
  1091. break;
  1092. case PROCEDURAL_PRESET_HALF_CROSSFADE:
  1093. flValue = 0.5f;
  1094. break;
  1095. case PROCEDRUAL_PRESET_ONE_CROSSFADE:
  1096. flValue = 1.0f;
  1097. break;
  1098. }
  1099. CDmElement *pPresetControlValue = m_crossfadePresetControlValues[ 0 ];
  1100. pPresetControlValue->SetValue( "valuePosition", vec3_origin );
  1101. pPresetControlValue->SetValue( "valueOrientation", quat_identity );
  1102. pPresetControlValue->SetValue( "leftValue", flValue );
  1103. pPresetControlValue->SetValue( "rightValue", flValue );
  1104. pPresetControlValue->SetValue( "value", flValue );
  1105. }
  1106. pPresetValuesLookup->RemoveAll();
  1107. int pcvi = -1;
  1108. for ( int i = m_SelectionHistory.Head(); i != m_SelectionHistory.InvalidIndex(); i = m_SelectionHistory.Next( i ) )
  1109. {
  1110. SelectionInfo_t *psi = m_SelectionHistory[ i ];
  1111. CDmElement *pControl = psi->m_hControl;
  1112. if ( !pControl )
  1113. continue;
  1114. CDmElement *pPresetControlValue = m_crossfadePresetControlValues[ bSinglePreset ? 0 : ++pcvi ];
  1115. DmElementHandle_t handle;
  1116. handle = pControl->GetHandle();
  1117. int idx = pPresetValuesLookup->Find( handle );
  1118. if ( idx == pPresetValuesLookup->InvalidIndex() )
  1119. {
  1120. idx = pPresetValuesLookup->Insert( handle );
  1121. }
  1122. AnimationControlAttributes_t &val = pPresetValuesLookup->Element( idx );
  1123. val.Clear();
  1124. CDmeTransformControl *pTransformControl = CastElement< CDmeTransformControl >( pControl );
  1125. if ( pTransformControl )
  1126. {
  1127. if ( psi->AreAnyPositionComponentsSelected() )
  1128. {
  1129. CDmAttribute *pValueAttribute = pPresetControlValue->AddAttribute( "valuePosition", AT_VECTOR3 );
  1130. Assert( pValueAttribute );
  1131. if ( !pValueAttribute )
  1132. continue;
  1133. if ( !bSinglePreset )
  1134. {
  1135. pValueAttribute->SetValue( pTransformControl->GetDefaultPosition() );
  1136. }
  1137. val.m_pValueAttribute[ ANIM_CONTROL_TXFORM_POSITION ] = pValueAttribute;
  1138. val.m_Vector = pValueAttribute->GetValue< Vector >();
  1139. }
  1140. if ( psi->AreAnyOrientationComponentsSelected() )
  1141. {
  1142. CDmAttribute *pValueAttribute = pPresetControlValue->AddAttribute( "valueOrientation", AT_QUATERNION );
  1143. Assert( pValueAttribute );
  1144. if ( !pValueAttribute )
  1145. continue;
  1146. if ( !bSinglePreset )
  1147. {
  1148. pValueAttribute->SetValue( pTransformControl->GetDefaultOrientation() );
  1149. }
  1150. val.m_pValueAttribute[ ANIM_CONTROL_TXFORM_ORIENTATION ] = pValueAttribute;
  1151. val.m_Quaternion = pValueAttribute->GetValue< Quaternion >();
  1152. }
  1153. }
  1154. else if ( IsStereoControl( pControl ) )
  1155. {
  1156. CDmAttribute *pLeftValueAttribute = pPresetControlValue->AddAttribute( "leftValue", AT_FLOAT );
  1157. CDmAttribute *pRightValueAttribute = pPresetControlValue->AddAttribute( "rightValue", AT_FLOAT );
  1158. Assert( pLeftValueAttribute && pRightValueAttribute );
  1159. if ( !pLeftValueAttribute || !pRightValueAttribute )
  1160. continue;
  1161. if ( !bSinglePreset )
  1162. {
  1163. float flDefaultValue = pControl->GetValue< float >( DEFAULT_FLOAT_ATTR );
  1164. pLeftValueAttribute ->SetValue( flDefaultValue );
  1165. pRightValueAttribute->SetValue( flDefaultValue );
  1166. }
  1167. val.m_pValueAttribute[ ANIM_CONTROL_VALUE_LEFT ] = pLeftValueAttribute;
  1168. val.m_pValue [ ANIM_CONTROL_VALUE_LEFT ] = pLeftValueAttribute->GetValue< float >();
  1169. val.m_pValueAttribute[ ANIM_CONTROL_VALUE_RIGHT ] = pRightValueAttribute;
  1170. val.m_pValue [ ANIM_CONTROL_VALUE_RIGHT ] = pRightValueAttribute->GetValue< float >();
  1171. }
  1172. else
  1173. {
  1174. CDmAttribute *pValueAttribute = pPresetControlValue->AddAttribute( "value", AT_FLOAT );
  1175. Assert( pValueAttribute );
  1176. if ( !pValueAttribute )
  1177. continue;
  1178. if ( !bSinglePreset )
  1179. {
  1180. pValueAttribute->SetValue( pControl->GetValue< float >( DEFAULT_FLOAT_ATTR ) );
  1181. }
  1182. val.m_pValueAttribute[ ANIM_CONTROL_VALUE ] = pValueAttribute;
  1183. val.m_pValue [ ANIM_CONTROL_VALUE ] = pValueAttribute->GetValue< float >();
  1184. }
  1185. }
  1186. }