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.

594 lines
16 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "dme_controls/BaseAnimSetAttributeSliderPanel.h"
  7. #include "movieobjects/dmechannel.h"
  8. #include "movieobjects/dmeanimationset.h"
  9. #include "vgui_controls/TextImage.h"
  10. #include "vgui_controls/Button.h"
  11. #include "vgui_controls/Slider.h"
  12. #include "vgui_controls/PanelListPanel.h"
  13. #include "dme_controls/BaseAnimSetPresetFaderPanel.h"
  14. #include "dme_controls/BaseAnimationSetEditor.h"
  15. #include "dme_controls/attributeslider.h"
  16. #include "dme_controls/dmecontrols_utils.h"
  17. #include "vgui/ISurface.h"
  18. #include "vgui/ISystem.h"
  19. #include "vgui/IInput.h"
  20. #include "vgui/IVgui.h"
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. using namespace vgui;
  24. #define ANIMATION_SET_EDITOR_ATTRIBUTESLIDERS_BUTTONTRAY_HEIGHT 32
  25. const int FREE_SLIDER_LIST = 1500;
  26. //-----------------------------------------------------------------------------
  27. //
  28. // CPresetSideFilterSlider class begins
  29. //
  30. //-----------------------------------------------------------------------------
  31. class CPresetSideFilterSlider : public Slider
  32. {
  33. DECLARE_CLASS_SIMPLE( CPresetSideFilterSlider, Slider );
  34. public:
  35. CPresetSideFilterSlider( CBaseAnimSetAttributeSliderPanel *pParent, const char *panelName );
  36. virtual ~CPresetSideFilterSlider();
  37. float GetPos();
  38. void SetPos( float frac );
  39. protected:
  40. virtual void Paint();
  41. virtual void PaintBackground();
  42. virtual void ApplySchemeSettings( IScheme *scheme );
  43. virtual void GetTrackRect( int &x, int &y, int &w, int &h );
  44. virtual void OnMousePressed(MouseCode code);
  45. virtual void OnMouseDoublePressed(MouseCode code);
  46. private:
  47. CBaseAnimSetAttributeSliderPanel *m_pParent;
  48. Color m_ZeroColor;
  49. Color m_TextColor;
  50. Color m_TextColorFocus;
  51. TextImage *m_pName;
  52. };
  53. //-----------------------------------------------------------------------------
  54. // Constructor, destructor
  55. //-----------------------------------------------------------------------------
  56. CPresetSideFilterSlider::CPresetSideFilterSlider( CBaseAnimSetAttributeSliderPanel *parent, const char *panelName ) :
  57. BaseClass( (Panel *)parent, panelName ), m_pParent( parent )
  58. {
  59. SetRange( 0, 1000 );
  60. SetDragOnRepositionNob( true );
  61. SetPos( 0.5f );
  62. SetPaintBackgroundEnabled( true );
  63. m_pName = new TextImage( "Preset Side Filter" );
  64. SetBgColor( Color( 128, 128, 128, 128 ) );
  65. m_ZeroColor = Color( 33, 33, 33, 255 );
  66. m_TextColor = Color( 200, 200, 200, 255 );
  67. m_TextColorFocus = Color( 208, 143, 40, 255 );
  68. }
  69. CPresetSideFilterSlider::~CPresetSideFilterSlider()
  70. {
  71. delete m_pName;
  72. }
  73. void CPresetSideFilterSlider::OnMousePressed(MouseCode code)
  74. {
  75. if ( code == MOUSE_RIGHT )
  76. {
  77. SetPos( 0.5f );
  78. return;
  79. }
  80. BaseClass::OnMousePressed( code );
  81. }
  82. void CPresetSideFilterSlider::OnMouseDoublePressed(MouseCode code)
  83. {
  84. if ( code == MOUSE_LEFT )
  85. {
  86. SetPos( 0.5f );
  87. return;
  88. }
  89. BaseClass::OnMouseDoublePressed( code );
  90. }
  91. float CPresetSideFilterSlider::GetPos()
  92. {
  93. return GetValue() * 0.001f;
  94. }
  95. void CPresetSideFilterSlider::SetPos( float frac )
  96. {
  97. SetValue( (int)( frac * 1000.0f + 0.5f ), false );
  98. }
  99. void CPresetSideFilterSlider::ApplySchemeSettings( IScheme *scheme )
  100. {
  101. BaseClass::ApplySchemeSettings( scheme );
  102. m_pName->SetFont( scheme->GetFont( "DefaultBold" ) );
  103. m_pName->SetColor( m_TextColor );
  104. m_pName->ResizeImageToContent();
  105. SetFgColor( Color( 194, 120, 0, 255 ) );
  106. SetThumbWidth( 3 );
  107. }
  108. void CPresetSideFilterSlider::GetTrackRect( int &x, int &y, int &w, int &h )
  109. {
  110. GetSize( w, h );
  111. x = 0;
  112. y = 2;
  113. h -= 4;
  114. }
  115. void CPresetSideFilterSlider::Paint()
  116. {
  117. // horizontal nob
  118. int x, y;
  119. int wide,tall;
  120. GetTrackRect( x, y, wide, tall );
  121. Color col = GetFgColor();
  122. surface()->DrawSetColor( col );
  123. surface()->DrawFilledRect( _nobPos[0], 1, _nobPos[1], GetTall() - 1 );
  124. surface()->DrawSetColor( m_ZeroColor );
  125. surface()->DrawFilledRect( _nobPos[0] - 1, y + 1, _nobPos[0], y + tall - 1 );
  126. }
  127. void CPresetSideFilterSlider::PaintBackground()
  128. {
  129. int w, h;
  130. GetSize( w, h );
  131. int tx, ty, tw, th;
  132. GetTrackRect( tx, ty, tw, th );
  133. surface()->DrawSetColor( m_ZeroColor );
  134. surface()->DrawFilledRect( tx, ty, tx + tw, ty + th );
  135. int cw, ch;
  136. m_pName->SetColor( _dragging ? m_TextColorFocus : m_TextColor );
  137. m_pName->GetContentSize( cw, ch );
  138. m_pName->SetPos( ( w - cw ) * 0.5f, ( h - ch ) * 0.5f );
  139. m_pName->Paint();
  140. }
  141. //-----------------------------------------------------------------------------
  142. //
  143. // CBaseAnimSetAttributeSliderPanel begins
  144. //
  145. //-----------------------------------------------------------------------------
  146. CBaseAnimSetAttributeSliderPanel::CBaseAnimSetAttributeSliderPanel( vgui::Panel *parent, const char *className, CBaseAnimationSetEditor *editor ) :
  147. BaseClass( parent, className ),
  148. m_pController( NULL )
  149. {
  150. m_hEditor = editor;
  151. m_pLeftRightBoth[ 0 ] = new Button( this, "AttributeSliderLeftOnly", "", this, "OnLeftOnly" );
  152. m_pLeftRightBoth[ 1 ] = new Button( this, "AttributeSliderRightOnly", "", this, "OnRightOnly" );
  153. m_pPresetSideFilter = new CPresetSideFilterSlider( this, "PresetSideFilter" );
  154. m_Sliders = new PanelListPanel( this, "AttributeSliders" );
  155. m_Sliders->SetFirstColumnWidth( 0 );
  156. m_Sliders->SetAutoResize
  157. (
  158. Panel::PIN_TOPLEFT,
  159. Panel::AUTORESIZE_DOWNANDRIGHT,
  160. 0, ANIMATION_SET_EDITOR_ATTRIBUTESLIDERS_BUTTONTRAY_HEIGHT,
  161. 0, 0
  162. );
  163. m_Sliders->SetVerticalBufferPixels( 0 );
  164. m_pController = editor->GetController();
  165. m_pController->AddControlSelectionChangedListener( this );
  166. ivgui()->AddTickSignal( GetVPanel(), 0 );
  167. InitFreeSliderList( FREE_SLIDER_LIST );
  168. }
  169. void CBaseAnimSetAttributeSliderPanel::OnCommand( const char *pCommand )
  170. {
  171. if ( !Q_stricmp( pCommand, "OnLeftOnly" ) )
  172. {
  173. m_pPresetSideFilter->SetPos( 0.0f );
  174. return;
  175. }
  176. if ( !Q_stricmp( pCommand, "OnRightOnly" ) )
  177. {
  178. m_pPresetSideFilter->SetPos( 1.0f );
  179. return;
  180. }
  181. BaseClass::OnCommand( pCommand );
  182. }
  183. void CBaseAnimSetAttributeSliderPanel::ApplySchemeSettings( IScheme *scheme )
  184. {
  185. BaseClass::ApplySchemeSettings( scheme );
  186. m_Sliders->SetBgColor( Color( 42, 42, 42, 255 ) );
  187. }
  188. void CBaseAnimSetAttributeSliderPanel::PerformLayout()
  189. {
  190. BaseClass::PerformLayout();
  191. int w, h;
  192. GetSize( w, h );
  193. int availH = ANIMATION_SET_EDITOR_ATTRIBUTESLIDERS_BUTTONTRAY_HEIGHT;
  194. int btnSize = 9;
  195. m_pLeftRightBoth[ 0 ]->SetBounds( 15, ( availH - btnSize ) / 2, btnSize, btnSize );
  196. m_pLeftRightBoth[ 1 ]->SetBounds( w - 15, ( availH - btnSize ) / 2, btnSize, btnSize );
  197. m_pPresetSideFilter->SetBounds( 23 + btnSize, 4, w - 38 - 2 * btnSize, availH - 8 );
  198. }
  199. void CBaseAnimSetAttributeSliderPanel::OnTick()
  200. {
  201. BaseClass::OnTick();
  202. bool bVisible = IsVisible();
  203. if ( bVisible )
  204. {
  205. // chain up and see if any parent panel is hiding us
  206. VPANEL p = GetVParent();
  207. while ( p )
  208. {
  209. if ( !ipanel()->IsVisible(p) )
  210. {
  211. bVisible = false;
  212. break;
  213. }
  214. p = ipanel()->GetParent(p);
  215. }
  216. }
  217. if ( !bVisible )
  218. {
  219. OnThink();
  220. }
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Purpose: Determines:
  224. // a) are we holding the ctrl key still, if so
  225. // figures out the crossfade amount of each preset slider with non-zero influence
  226. // b) not holding control, then just see if we are previewing whichever preset the mouse is over
  227. // Input : -
  228. //-----------------------------------------------------------------------------
  229. void CBaseAnimSetAttributeSliderPanel::OnThink()
  230. {
  231. BaseClass::OnThink();
  232. m_pController->UpdatePreviewSliderValues();
  233. m_pController->UpdatePreviewSliderTimes();
  234. ApplySliderValues( false );
  235. UpdateSliderDependencyFlags();
  236. }
  237. void CBaseAnimSetAttributeSliderPanel::ChangeAnimationSetClip( CDmeFilmClip *pFilmClip )
  238. {
  239. RebuildSliderLists();
  240. }
  241. void CBaseAnimSetAttributeSliderPanel::RebuildSliderLists()
  242. {
  243. int c = m_SliderList.Count();
  244. for ( int i = 0 ; i < c; ++i )
  245. {
  246. FreeSlider( m_SliderList[ i ] );
  247. }
  248. m_SliderList.RemoveAll();
  249. m_Sliders->RemoveAll();
  250. CUtlVector< CDmElement * > controlList;
  251. CAnimSetGroupAnimSetTraversal traversal( m_pController->GetAnimationSetClip() );
  252. while ( CDmeAnimationSet *pAnimSet = traversal.Next() )
  253. {
  254. const CDmaElementArray< CDmElement > &controls = pAnimSet->GetControls();
  255. // Now create sliders for all known controls, nothing visible by default
  256. int controlCount = controls.Count();
  257. for ( int ci = 0 ; ci < controlCount; ++ci )
  258. {
  259. CDmElement *control = controls[ ci ];
  260. if ( !control )
  261. continue;
  262. controlList.AddToTail( control );
  263. }
  264. }
  265. for ( int i = 0; i < controlList.Count(); ++i )
  266. {
  267. CDmElement *control = controlList[ i ];
  268. CAttributeSlider *slider = AllocateSlider();
  269. slider->Init( control, false );
  270. slider->SetVisible( false );
  271. m_SliderList.AddToTail( slider );
  272. // Transform controls get two separate sliders, one for position and one for rotation.
  273. // This is something of an artifact of having separate controls for position and rotation,
  274. // but it is useful for value type in to have a separate slider for each.
  275. CDmeTransformControl *pTransformControl = CastElement< CDmeTransformControl >( control );
  276. if ( pTransformControl )
  277. {
  278. CAttributeSlider *pOrientationSlider = AllocateSlider();
  279. pOrientationSlider->Init( control, true );
  280. pOrientationSlider->SetVisible( false );
  281. m_SliderList.AddToTail( pOrientationSlider );
  282. }
  283. }
  284. }
  285. void CBaseAnimSetAttributeSliderPanel::OnControlsAddedOrRemoved()
  286. {
  287. bool changed = false;
  288. int nSliderIndex = 0;
  289. CAnimSetGroupAnimSetTraversal traversal( m_pController->GetAnimationSetClip() );
  290. while ( CDmeAnimationSet *pAnimSet = traversal.Next() )
  291. {
  292. // See if every slider is the same as before
  293. const CDmaElementArray< CDmElement > &controls = pAnimSet->GetControls();
  294. int controlCount = controls.Count();
  295. for ( int i = 0 ; i < controlCount; ++i )
  296. {
  297. CDmElement *control = controls[ i ];
  298. if ( !control )
  299. continue;
  300. if ( nSliderIndex >= m_SliderList.Count() )
  301. {
  302. changed = true;
  303. break;
  304. }
  305. CAttributeSlider *pSlider = m_SliderList[ nSliderIndex++ ];
  306. if ( pSlider->GetControl() != control )
  307. {
  308. changed = true;
  309. break;
  310. }
  311. if ( IsTransformControl( control ) )
  312. {
  313. // Transform controls add two sliders so make sure the
  314. // next slider refers to the transform control as well.
  315. pSlider = m_SliderList[ nSliderIndex++ ];
  316. if ( pSlider->GetControl() != control )
  317. {
  318. changed = true;
  319. break;
  320. }
  321. continue;
  322. }
  323. if ( pSlider->IsStereo() != IsStereoControl( control ) )
  324. {
  325. changed = true;
  326. break;
  327. }
  328. }
  329. }
  330. changed = changed || nSliderIndex != m_SliderList.Count();
  331. if ( !changed )
  332. return;
  333. RebuildSliderLists();
  334. }
  335. void CBaseAnimSetAttributeSliderPanel::OnControlSelectionChanged()
  336. {
  337. bool visibleSlidersChanged = false;
  338. // Walk through all sliders and show only those in the symbol table
  339. int c = m_SliderList.Count();
  340. for ( int i = 0; i < c; ++i )
  341. {
  342. CAttributeSlider *pSlider = m_SliderList[ i ];
  343. CDmElement* pSliderControl = pSlider->GetControl();
  344. TransformComponent_t nComponentFlags = m_pController->GetSelectionComponentFlags( pSliderControl );
  345. // If the slider is transform control slider determine if it is the
  346. // slider for the position or rotation and mask the flags accordingly.
  347. LogComponents_t nLogComponentFlags = SelectionInfo_t::ConvertTransformFlagsToLogFlags( nComponentFlags, pSlider->IsOrientation() );
  348. bool bShowSlider = nComponentFlags != 0;
  349. if ( pSlider->IsVisible() != bShowSlider )
  350. {
  351. pSlider->SetVisible( bShowSlider );
  352. visibleSlidersChanged = true;
  353. }
  354. if ( pSlider->VisibleComponents() != nLogComponentFlags )
  355. {
  356. pSlider->SetVisibleComponents( nLogComponentFlags );
  357. }
  358. }
  359. // If nothing changed then nothing else needs to be done
  360. if ( !visibleSlidersChanged )
  361. return;
  362. // If the visibility of entire sliders changed then
  363. // the slider visibility list must be updated.
  364. m_Sliders->RemoveAll();
  365. for ( int i = 0; i < c; ++i )
  366. {
  367. CAttributeSlider *slider = m_SliderList[ i ];
  368. if ( slider->IsVisible() )
  369. {
  370. m_Sliders->AddItem( NULL, slider );
  371. }
  372. }
  373. }
  374. void CBaseAnimSetAttributeSliderPanel::GetTypeInValueForControl( CDmElement *pControl, bool bOrientation, AttributeValue_t &controlValue, const AttributeValue_t &sliderValue )
  375. {
  376. CDmeTransformControl *pTransformControl = CastElement< CDmeTransformControl >( pControl );
  377. if ( pTransformControl && bOrientation )
  378. {
  379. const Quaternion &q = sliderValue.m_Quaternion;
  380. QAngle ang;
  381. QuaternionAngles( q, ang );
  382. controlValue.m_Vector.x = ang.x;
  383. controlValue.m_Vector.y = ang.y;
  384. controlValue.m_Vector.z = ang.z;
  385. }
  386. else
  387. {
  388. controlValue = sliderValue;
  389. }
  390. }
  391. void CBaseAnimSetAttributeSliderPanel::UpdatePreview( char const *pchFormat, ... )
  392. {
  393. }
  394. bool CBaseAnimSetAttributeSliderPanel::ApplySliderValues( bool bForce )
  395. {
  396. bool bValuesChanged = m_pController->ApplySliderValues( bForce );
  397. if ( bValuesChanged )
  398. {
  399. UpdatePreview( "ApplySliderValues\n" );
  400. }
  401. return bValuesChanged;
  402. }
  403. //-----------------------------------------------------------------------------
  404. // Purpose: Update the slider flags specifying if a slider is dependency of the
  405. // currently active slider.
  406. //-----------------------------------------------------------------------------
  407. void CBaseAnimSetAttributeSliderPanel::UpdateSliderDependencyFlags() const
  408. {
  409. bool ctrlDown = input()->IsKeyDown( KEY_LCONTROL ) || input()->IsKeyDown( KEY_RCONTROL );
  410. CAttributeSlider *pPrimarySlider = ctrlDown ? m_pController->GetActiveAttributeSlider() : NULL;
  411. int nSliders = m_SliderList.Count();
  412. for ( int iSlider = 0; iSlider < nSliders; ++iSlider )
  413. {
  414. CAttributeSlider *pSlider = m_SliderList[ iSlider ];
  415. if ( !pSlider )
  416. continue;
  417. pSlider->SetDependent( pPrimarySlider ? pPrimarySlider->IsDependent( pSlider ) : false );
  418. }
  419. }
  420. void CBaseAnimSetAttributeSliderPanel::SetupForPreset( FaderPreview_t &fader )
  421. {
  422. // Nothing special here
  423. }
  424. float CBaseAnimSetAttributeSliderPanel::GetBalanceSliderValue()
  425. {
  426. return m_pPresetSideFilter->GetPos();
  427. }
  428. int CBaseAnimSetAttributeSliderPanel::FindSliderIndexForControl( const CDmElement *control )
  429. {
  430. int c = m_SliderList.Count();
  431. for ( int i = 0; i < c; ++i )
  432. {
  433. if ( m_SliderList[ i ]->GetControl() == control )
  434. return i;
  435. }
  436. return -1;
  437. }
  438. CAttributeSlider *CBaseAnimSetAttributeSliderPanel::FindSliderForControl( const CDmElement *control )
  439. {
  440. int i = FindSliderIndexForControl( control );
  441. if ( i < 0 )
  442. return NULL;
  443. return m_SliderList[ i ];
  444. }
  445. bool CBaseAnimSetAttributeSliderPanel::GetSliderValues( AttributeValue_t *pValue, int nIndex )
  446. {
  447. Assert( pValue );
  448. Assert( nIndex >= 0 && nIndex < m_SliderList.Count() );
  449. CAttributeSlider *pSlider = m_SliderList[ nIndex ];
  450. CAttributeSlider *pAttrSlider = m_pController->GetActiveAttributeSlider();
  451. bool shiftDown = input()->IsKeyDown( KEY_LSHIFT ) || input()->IsKeyDown( KEY_RSHIFT );
  452. bool bPreviewingAttrSlider = pAttrSlider == pSlider && shiftDown;
  453. bool bGetPreview = bPreviewingAttrSlider || m_pController->WasPreviouslyHoldingPresetPreviewKey() || m_pController->IsPresetFaderBeingDragged();
  454. *pValue = bGetPreview ? pSlider->GetPreview() : pSlider->GetValue();
  455. return pSlider->IsVisible();
  456. }
  457. void CBaseAnimSetAttributeSliderPanel::DispatchCurve( int nCurveType )
  458. {
  459. // Nothing, handled by SFM
  460. }
  461. void CBaseAnimSetAttributeSliderPanel::InitFreeSliderList( int nCount )
  462. {
  463. for ( int i = 0; i < nCount; ++i )
  464. {
  465. CAttributeSlider *slider = new CAttributeSlider( this );
  466. slider->SetVisible( false );
  467. m_FreeSliderList.AddToTail( slider );
  468. }
  469. }
  470. CAttributeSlider *CBaseAnimSetAttributeSliderPanel::AllocateSlider()
  471. {
  472. int c = m_FreeSliderList.Count();
  473. if ( c > 0 )
  474. {
  475. CAttributeSlider *slider = m_FreeSliderList[ c - 1 ];
  476. m_FreeSliderList.Remove( c - 1 );
  477. slider->SetVisible( true );
  478. return slider;
  479. }
  480. // Add a new one
  481. CAttributeSlider *slider = new CAttributeSlider( this );
  482. slider->SetVisible( true );
  483. return slider;
  484. }
  485. void CBaseAnimSetAttributeSliderPanel::FreeSlider( CAttributeSlider *slider )
  486. {
  487. slider->SetVisible( false );
  488. m_FreeSliderList.AddToTail( slider );
  489. }