Team Fortress 2 Source Code as on 22/4/2020
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.

1504 lines
40 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "dme_controls/BaseAnimSetPresetFaderPanel.h"
  7. #include "dme_controls/DmePresetGroupEditorPanel.h"
  8. #include "vgui_controls/InputDialog.h"
  9. #include "vgui_controls/Button.h"
  10. #include "vgui_controls/Slider.h"
  11. #include "vgui_controls/ComboBox.h"
  12. #include "vgui_controls/TextImage.h"
  13. #include "vgui_controls/TextEntry.h"
  14. #include "vgui_controls/MessageBox.h"
  15. #include "vgui_controls/Menu.h"
  16. #include "vgui_controls/PanelListPanel.h"
  17. #include "movieobjects/dmeanimationset.h"
  18. #include "tier1/KeyValues.h"
  19. #include "dme_controls/dmecontrols_utils.h"
  20. #include "vstdlib/random.h"
  21. #include "vgui/IInput.h"
  22. #include "vgui/ISurface.h"
  23. #include "dme_controls/BaseAnimSetAttributeSliderPanel.h"
  24. #include "dme_controls/BaseAnimationSetEditor.h"
  25. // memdbgon must be the last include file in a .cpp file!!!
  26. #include "tier0/memdbgon.h"
  27. using namespace vgui;
  28. float ifm_fader_timescale = 5.0f;
  29. class CPresetSlider;
  30. //-----------------------------------------------------------------------------
  31. // Purpose: Utility dialog, used to let user type in some text
  32. //-----------------------------------------------------------------------------
  33. class CAddPresetDialog : public vgui::BaseInputDialog
  34. {
  35. DECLARE_CLASS_SIMPLE( CAddPresetDialog, vgui::BaseInputDialog );
  36. public:
  37. CAddPresetDialog( vgui::Panel *parent );
  38. void DoModal( CDmeAnimationSet *pAnimationSet, KeyValues *pContextKeyValues = NULL );
  39. protected:
  40. // command buttons
  41. virtual void OnCommand(const char *command);
  42. private:
  43. vgui::TextEntry *m_pInput;
  44. vgui::ComboBox *m_pPresetGroup;
  45. };
  46. //-----------------------------------------------------------------------------
  47. // Constructor
  48. //-----------------------------------------------------------------------------
  49. CAddPresetDialog::CAddPresetDialog( vgui::Panel *parent ) : BaseClass( parent, "Enter Preset Name" )
  50. {
  51. m_pInput = new TextEntry( this, "PresetName" );
  52. m_pPresetGroup = new vgui::ComboBox( this, "PresetGroup", 8, true );
  53. SetDeleteSelfOnClose( false );
  54. LoadControlSettings( "resource/addpresetdialog.res" );
  55. }
  56. void CAddPresetDialog::DoModal( CDmeAnimationSet *pAnimationSet, KeyValues *pContextKeyValues )
  57. {
  58. int nTextLength = m_pInput->GetTextLength() + 1;
  59. char* pCurrentGroupName = (char*)_alloca( nTextLength * sizeof(char) );
  60. m_pInput->GetText( pCurrentGroupName, nTextLength );
  61. m_pPresetGroup->DeleteAllItems();
  62. // Populate the combo box with preset group names
  63. CDmrElementArray< CDmePresetGroup > presetGroupList = pAnimationSet->GetPresetGroups();
  64. int nCount = presetGroupList.Count();
  65. for ( int i = 0; i < nCount; ++i )
  66. {
  67. CDmePresetGroup *pPresetGroup = presetGroupList[i];
  68. if ( pPresetGroup->m_bIsReadOnly )
  69. continue;
  70. KeyValues *kv = new KeyValues( "entry" );
  71. SetElementKeyValue( kv, "presetGroup", pPresetGroup );
  72. int nItemID = m_pPresetGroup->AddItem( pPresetGroup->GetName(), kv );
  73. if ( pCurrentGroupName && !Q_stricmp( pPresetGroup->GetName(), pCurrentGroupName ) )
  74. {
  75. m_pPresetGroup->ActivateItem( nItemID );
  76. }
  77. }
  78. BaseClass::DoModal( pContextKeyValues );
  79. m_pInput->SetText( "" );
  80. m_pInput->RequestFocus();
  81. PlaceUnderCursor( );
  82. }
  83. //-----------------------------------------------------------------------------
  84. // command handler
  85. //-----------------------------------------------------------------------------
  86. void CAddPresetDialog::OnCommand( const char *command )
  87. {
  88. if ( !Q_stricmp( command, "OK" ) )
  89. {
  90. int nTextLength = m_pInput->GetTextLength() + 1;
  91. char* txt = (char*)_alloca( nTextLength * sizeof(char) );
  92. m_pInput->GetText( txt, nTextLength );
  93. nTextLength = m_pPresetGroup->GetTextLength() + 1;
  94. char* pPresetGroupName = (char*)_alloca( nTextLength * sizeof(char) );
  95. m_pPresetGroup->GetText( pPresetGroupName, nTextLength );
  96. KeyValues *pCurrentGroup = m_pPresetGroup->GetActiveItemUserData();
  97. CDmePresetGroup *pPresetGroup = pCurrentGroup ? GetElementKeyValue<CDmePresetGroup>( pCurrentGroup, "presetGroup" ) : NULL;
  98. if ( pPresetGroup && Q_stricmp( pPresetGroup->GetName(), pPresetGroupName ) )
  99. {
  100. pPresetGroup = NULL;
  101. }
  102. KeyValues *kv = new KeyValues( "PresetNameSelected", "text", txt );
  103. kv->SetString( "presetGroupName", pPresetGroupName );
  104. SetElementKeyValue( kv, "presetGroup", pPresetGroup );
  105. if ( m_pContextKeyValues )
  106. {
  107. kv->AddSubKey( m_pContextKeyValues );
  108. m_pContextKeyValues = NULL;
  109. }
  110. PostActionSignal( kv );
  111. CloseModal();
  112. return;
  113. }
  114. if ( !Q_stricmp( command, "Cancel") )
  115. {
  116. CloseModal();
  117. return;
  118. }
  119. BaseClass::OnCommand( command );
  120. }
  121. //-----------------------------------------------------------------------------
  122. //
  123. // CPresetSliderEdgeButton: The buttons that lie on either side of the PresetSlider
  124. //
  125. //-----------------------------------------------------------------------------
  126. class CPresetSliderEdgeButton : public Button
  127. {
  128. DECLARE_CLASS_SIMPLE( CPresetSliderEdgeButton, Button );
  129. public:
  130. CPresetSliderEdgeButton( CPresetSlider *parent, const char *panelName, const char *text );
  131. private:
  132. virtual void OnCursorMoved(int x, int y);
  133. virtual void OnMousePressed( vgui::MouseCode code );
  134. virtual void OnMouseReleased( vgui::MouseCode code );
  135. virtual void OnMouseDoublePressed( vgui::MouseCode code );
  136. CPresetSlider *m_pSlider;
  137. };
  138. //-----------------------------------------------------------------------------
  139. //
  140. // CPresetSlider: The actual preset slider itself!
  141. //
  142. //-----------------------------------------------------------------------------
  143. class CPresetSlider : public Slider
  144. {
  145. DECLARE_CLASS_SIMPLE( CPresetSlider, Slider );
  146. public:
  147. friend class CPresetSliderEdgeButton;
  148. CPresetSlider( CBaseAnimSetPresetFaderPanel *parent, const char *panelName, CDmePreset *pPreset );
  149. ~CPresetSlider();
  150. void SetControlValues( );
  151. void SetGradientColor( const Color& clr );
  152. float GetCurrent();
  153. void SetPos( float frac );
  154. AttributeDict_t *GetAttributeDict();
  155. bool IsPreviewSlider();
  156. bool IsDragging();
  157. void UpdateProceduralValues();
  158. CDmePreset *GetPreset();
  159. protected:
  160. virtual void Paint();
  161. virtual void PaintBackground();
  162. virtual void ApplySchemeSettings( IScheme *scheme );
  163. virtual void GetTrackRect( int &x, int &y, int &w, int &h );
  164. virtual void PerformLayout();
  165. virtual void OnMousePressed(MouseCode code);
  166. virtual void OnMouseDoublePressed(MouseCode code);
  167. virtual void OnMouseReleased(MouseCode code);
  168. virtual void OnKeyCodeTyped( KeyCode code );
  169. virtual void OnCursorMoved(int x, int y);
  170. MESSAGE_FUNC( OnShowContextMenu, "OnShowContextMenu" );
  171. MESSAGE_FUNC( OnRename, "OnRename" );
  172. MESSAGE_FUNC( OnDelete, "OnDelete" );
  173. MESSAGE_FUNC( OnOverwrite, "OnOverwrite" );
  174. MESSAGE_FUNC_PARAMS( OnInputCompleted, "InputCompleted", params );
  175. MESSAGE_FUNC( OnDeleteConfirmed, "OnDeleteConfirmed" );
  176. MESSAGE_FUNC( OnOverwriteConfirmed, "OnOverwriteConfirmed" );
  177. private:
  178. void OnRenameCompleted( const char *pText, KeyValues *pContextKeyValues );
  179. void OnDragCompleted( float flValue );
  180. void UpdateTickPos( int x, int y );
  181. CBaseAnimSetPresetFaderPanel *m_pParent;
  182. Color m_GradientColor;
  183. Color m_ZeroColor;
  184. Color m_TextColor;
  185. Color m_TextColorFocus;
  186. TextImage *m_pName;
  187. float m_flCurrent;
  188. bool m_bSuppressCompletion;
  189. CPresetSliderEdgeButton *m_pEdgeButtons[ 2 ];
  190. vgui::DHANDLE< vgui::Menu > m_hContextMenu;
  191. vgui::DHANDLE< vgui::InputDialog > m_hInputDialog;
  192. AttributeDict_t m_AttributeLookup;
  193. vgui::DHANDLE< MessageBox > m_hConfirm;
  194. CDmeHandle< CDmePreset > m_hSelf;
  195. static bool s_bResetMousePosOnMouseUp;
  196. static int s_nMousePosX;
  197. static int s_nMousePosY;
  198. };
  199. //-----------------------------------------------------------------------------
  200. //
  201. // CPresetSliderEdgeButton: The buttons that lie on either side of the PresetSlider
  202. //
  203. //-----------------------------------------------------------------------------
  204. CPresetSliderEdgeButton::CPresetSliderEdgeButton( CPresetSlider *parent, const char *panelName, const char *text ) :
  205. BaseClass( (Panel *)parent, panelName, text ), m_pSlider( parent )
  206. {
  207. SetPaintBorderEnabled( false );
  208. }
  209. void CPresetSliderEdgeButton::OnCursorMoved(int x, int y)
  210. {
  211. LocalToScreen( x, y );
  212. m_pSlider->ScreenToLocal( x, y );
  213. m_pSlider->OnCursorMoved( x, y );
  214. }
  215. void CPresetSliderEdgeButton::OnMousePressed( vgui::MouseCode code )
  216. {
  217. BaseClass::OnMousePressed( code );
  218. PostMessage( m_pSlider->GetVPanel(), new KeyValues( "MousePressed", "code", code ) );
  219. PostMessage( m_pSlider->GetVPanel(), new KeyValues( "MouseReleased", "code", code ), 0.001f );
  220. }
  221. void CPresetSliderEdgeButton::OnMouseReleased( vgui::MouseCode code )
  222. {
  223. BaseClass::OnMouseReleased( code );
  224. PostMessage( m_pSlider->GetVPanel(), new KeyValues( "MouseReleased", "code", code ) );
  225. }
  226. void CPresetSliderEdgeButton::OnMouseDoublePressed( vgui::MouseCode code )
  227. {
  228. BaseClass::OnMouseDoublePressed( code );
  229. PostMessage( m_pSlider->GetVPanel(), new KeyValues( "MouseDoublePressed", "code", code ) );
  230. PostMessage( m_pSlider->GetVPanel(), new KeyValues( "MouseReleased", "code", code ), 0.001f );
  231. }
  232. //-----------------------------------------------------------------------------
  233. //
  234. // CPresetSlider: The actual preset slider itself!
  235. //
  236. //-----------------------------------------------------------------------------
  237. //-----------------------------------------------------------------------------
  238. // Static members
  239. //-----------------------------------------------------------------------------
  240. bool CPresetSlider::s_bResetMousePosOnMouseUp = false;
  241. int CPresetSlider::s_nMousePosX;
  242. int CPresetSlider::s_nMousePosY;
  243. //-----------------------------------------------------------------------------
  244. // Constructor, destructor
  245. //-----------------------------------------------------------------------------
  246. CPresetSlider::CPresetSlider( CBaseAnimSetPresetFaderPanel *parent, const char *panelName, CDmePreset *preset ) :
  247. BaseClass( (Panel *)parent, panelName ), m_pParent( parent ), m_bSuppressCompletion( false )
  248. {
  249. Assert( preset );
  250. m_hSelf = preset;
  251. SetRange( 0, 1000 );
  252. SetDragOnRepositionNob( true );
  253. SetPaintBackgroundEnabled( true );
  254. m_pName = new TextImage( panelName );
  255. m_pEdgeButtons[ 0 ] = new CPresetSliderEdgeButton( this, "PresetSliderLeftEdge", "" );
  256. m_pEdgeButtons[ 1 ] = new CPresetSliderEdgeButton( this, "PresetSliderRightEdge", "" );
  257. SetBgColor( Color( 128, 128, 128, 128 ) );
  258. m_ZeroColor = Color( 69, 69, 69, 255 );
  259. m_GradientColor = Color( 194, 120, 0, 255 );
  260. m_TextColor = Color( 200, 200, 200, 255 );
  261. m_TextColorFocus = Color( 208, 143, 40, 255 );
  262. }
  263. CPresetSlider::~CPresetSlider()
  264. {
  265. delete m_pName;
  266. }
  267. CDmePreset *CPresetSlider::GetPreset()
  268. {
  269. return m_hSelf;
  270. }
  271. // #define PRORCEDURAL_PRESET_TIMING
  272. void CPresetSlider::UpdateProceduralValues()
  273. {
  274. if ( !m_hSelf->IsProcedural() )
  275. return;
  276. #if defined( PRORCEDURAL_PRESET_TIMING )
  277. double st = Plat_FloatTime();
  278. #endif
  279. // Figure out what we need to do
  280. int nPresetType = m_hSelf->GetProceduralPresetType();
  281. switch ( nPresetType )
  282. {
  283. default:
  284. Assert( 0 );
  285. break;
  286. case PROCEDURAL_PRESET_REVEAL:
  287. case PROCEDURAL_PRESET_PASTE:
  288. case PROCEDURAL_PRESET_JITTER:
  289. case PROCEDURAL_PRESET_SMOOTH:
  290. case PROCEDURAL_PRESET_SHARPEN:
  291. case PROCEDURAL_PRESET_SOFTEN:
  292. case PROCEDURAL_PRESET_STAGGER:
  293. // These are handled elsewhere right now... at some point we'll copy in values at head position in order to do ctrl-key preview mode
  294. break;
  295. case PROCEDURAL_PRESET_IN_CROSSFADE:
  296. {
  297. m_pParent->ProceduralPreset_UpdateCrossfade( m_hSelf, true );
  298. }
  299. break;
  300. case PROCEDURAL_PRESET_OUT_CROSSFADE:
  301. {
  302. m_pParent->ProceduralPreset_UpdateCrossfade( m_hSelf, false );
  303. }
  304. break;
  305. }
  306. #if defined( PRORCEDURAL_PRESET_TIMING )
  307. double ed = Plat_FloatTime();
  308. Msg( "Update %.3f msec\n", 1000.0 * ( ed - st ) );
  309. #endif
  310. }
  311. //-----------------------------------------------------------------------------
  312. // Reads the sliders, sets control values into the attribute dictionary
  313. //-----------------------------------------------------------------------------
  314. void CPresetSlider::SetControlValues( )
  315. {
  316. m_AttributeLookup.Purge();
  317. if ( !m_hSelf.Get() )
  318. return;
  319. CDmrElementArray< CDmElement > values = m_hSelf->GetControlValues();
  320. int nControlValueCount = values.Count();
  321. for ( int i = 0; i < nControlValueCount; ++i )
  322. {
  323. CDmElement *v = values[ i ];
  324. AnimationControlAttributes_t val;
  325. val.m_pAttribute[ANIM_CONTROL_VALUE] = v->GetAttribute( "value" );
  326. val.m_pAttribute[ANIM_CONTROL_BALANCE] = v->GetAttribute( "balance" );
  327. val.m_pAttribute[ANIM_CONTROL_MULTILEVEL] = v->GetAttribute( "multilevel" );
  328. val.m_pValue[ANIM_CONTROL_VALUE] = v->GetValue< float >( "value" );
  329. val.m_pValue[ANIM_CONTROL_BALANCE] = v->GetValue< float >( "balance" );
  330. val.m_pValue[ANIM_CONTROL_MULTILEVEL] = v->GetValue< float >( "multilevel" );
  331. m_AttributeLookup.Insert( v->GetName(), val );
  332. }
  333. }
  334. AttributeDict_t *CPresetSlider::GetAttributeDict()
  335. {
  336. return &m_AttributeLookup;
  337. }
  338. void CPresetSlider::OnMouseDoublePressed(MouseCode code)
  339. {
  340. if ( code != MOUSE_LEFT )
  341. {
  342. BaseClass::OnMouseDoublePressed( code );
  343. return;
  344. }
  345. }
  346. void CPresetSlider::OnMousePressed(MouseCode code)
  347. {
  348. if ( code == MOUSE_RIGHT )
  349. return;
  350. BaseClass::OnMousePressed( code );
  351. if ( !_dragging )
  352. return;
  353. SetCursor( dc_blank );
  354. int mx, my;
  355. input()->GetCursorPos( mx, my );
  356. int tx, ty, tw, th;
  357. GetTrackRect( tx, ty, tw, th );
  358. ScreenToLocal( mx, my );
  359. // Off right?
  360. bool offright = mx >= ( tx + tw ) ? true : false;
  361. bool ctrldown = input()->IsKeyDown( KEY_LCONTROL ) || input()->IsKeyDown( KEY_RCONTROL );
  362. if ( !ctrldown && !offright )
  363. {
  364. if ( mx >= tx )
  365. {
  366. Assert( !s_bResetMousePosOnMouseUp );
  367. s_bResetMousePosOnMouseUp = true;
  368. s_nMousePosX = mx;
  369. s_nMousePosY = my;
  370. LocalToScreen( s_nMousePosX, s_nMousePosY );
  371. int offset = mx - tx;
  372. mx -= offset;
  373. LocalToScreen( mx, my );
  374. input()->SetCursorPos( mx, my );
  375. }
  376. SetPos( 0 );
  377. }
  378. }
  379. void CPresetSlider::OnMouseReleased(MouseCode code)
  380. {
  381. if ( code == MOUSE_RIGHT )
  382. {
  383. OnShowContextMenu();
  384. return;
  385. }
  386. float flLastValue = GetCurrent();
  387. bool bWasDragging = _dragging;
  388. BaseClass::OnMouseReleased( code );
  389. if ( bWasDragging )
  390. {
  391. OnDragCompleted( flLastValue );
  392. SetCursor( dc_arrow );
  393. }
  394. if( s_bResetMousePosOnMouseUp )
  395. {
  396. s_bResetMousePosOnMouseUp = false;
  397. input()->SetCursorPos( s_nMousePosX, s_nMousePosY );
  398. }
  399. }
  400. void CPresetSlider::OnKeyCodeTyped( KeyCode code )
  401. {
  402. if ( code != KEY_ESCAPE || !_dragging )
  403. {
  404. BaseClass::OnKeyCodeTyped( code );
  405. return;
  406. }
  407. m_bSuppressCompletion = true;
  408. OnMouseReleased( MOUSE_LEFT );
  409. m_bSuppressCompletion = false;
  410. SetCursor( dc_arrow );
  411. }
  412. void CPresetSlider::OnDragCompleted( float flValue )
  413. {
  414. if ( m_bSuppressCompletion )
  415. return;
  416. char sz[ 128 ];
  417. m_pName->GetText( sz, sizeof( sz ) );
  418. // Msg( "CPresetSlider slider drag completed %s [%.3f ]\n", sz, flValue );
  419. // Apply settings to attribute sliders
  420. m_pParent->ApplyPreset( flValue, m_AttributeLookup );
  421. }
  422. void CPresetSlider::OnRename()
  423. {
  424. if ( m_hInputDialog.Get() )
  425. {
  426. delete m_hInputDialog.Get();
  427. }
  428. m_hInputDialog = new InputDialog( this, "Rename Preset", "Name:", GetName() );
  429. if ( m_hInputDialog.Get() )
  430. {
  431. KeyValues *pContextKeyValues = new KeyValues( "RenamePreset" );
  432. m_hInputDialog->SetSmallCaption( true );
  433. m_hInputDialog->SetMultiline( false );
  434. m_hInputDialog->DoModal( pContextKeyValues );
  435. }
  436. else
  437. {
  438. Assert( 0 );
  439. }
  440. }
  441. void CPresetSlider::OnRenameCompleted( const char *pText, KeyValues *pContextKeyValues )
  442. {
  443. if ( !pText || !*pText )
  444. {
  445. Warning( "Can't rename preset for %s to an empty name\n", GetName() );
  446. return;
  447. }
  448. // No change( case sensitive)
  449. if ( !Q_strcmp( GetName(), pText ) )
  450. return;
  451. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Rename Preset" );
  452. SetName( pText );
  453. m_pName->SetText( pText );
  454. m_pName->ResizeImageToContent();
  455. //
  456. Assert( m_hSelf.Get() );
  457. if ( m_hSelf.Get() )
  458. {
  459. m_hSelf->SetName( pText );
  460. }
  461. }
  462. void CPresetSlider::OnInputCompleted( KeyValues *pParams )
  463. {
  464. const char *pText = pParams->GetString( "text", NULL );
  465. KeyValues *pContextKeyValues = pParams->FindKey( "RenamePreset" );
  466. if ( pContextKeyValues )
  467. {
  468. OnRenameCompleted( pText, pContextKeyValues );
  469. return;
  470. }
  471. Assert( 0 );
  472. }
  473. void CPresetSlider::OnDelete()
  474. {
  475. if ( m_hConfirm.Get() )
  476. delete m_hConfirm.Get();
  477. char sz[ 256 ];
  478. Q_snprintf( sz, sizeof( sz ), "Delete '%s'?", GetName() );
  479. m_hConfirm = new MessageBox( "Delete Preset", sz, this );
  480. Assert( m_hConfirm.Get() );
  481. if ( m_hConfirm )
  482. {
  483. m_hConfirm->SetCancelButtonVisible( true );
  484. m_hConfirm->SetCancelButtonText( "#VGui_Cancel" );
  485. m_hConfirm->SetCommand( new KeyValues( "OnDeleteConfirmed" ) );
  486. m_hConfirm->AddActionSignalTarget( this );
  487. m_hConfirm->DoModal();
  488. }
  489. }
  490. void CPresetSlider::OnDeleteConfirmed()
  491. {
  492. m_pParent->OnDeletePreset( m_hSelf.Get() );
  493. }
  494. void CPresetSlider::OnOverwrite()
  495. {
  496. if ( m_hConfirm.Get() )
  497. delete m_hConfirm.Get();
  498. char sz[ 256 ];
  499. Q_snprintf( sz, sizeof( sz ), "Overwrite '%s'?", GetName() );
  500. m_hConfirm = new MessageBox( "Overwrite Preset", sz, this );
  501. Assert( m_hConfirm.Get() );
  502. if ( m_hConfirm )
  503. {
  504. m_hConfirm->ShowMessageBoxOverCursor( true );
  505. m_hConfirm->SetCancelButtonVisible( true );
  506. m_hConfirm->SetCancelButtonText( "#VGui_Cancel" );
  507. m_hConfirm->SetCommand( new KeyValues( "OnOverwriteConfirmed" ) );
  508. m_hConfirm->AddActionSignalTarget( this );
  509. m_hConfirm->DoModal();
  510. }
  511. }
  512. void CPresetSlider::OnOverwriteConfirmed()
  513. {
  514. m_pParent->OnOverwritePreset( m_hSelf.Get() );
  515. }
  516. void CPresetSlider::OnShowContextMenu()
  517. {
  518. if ( m_hContextMenu.Get() )
  519. {
  520. delete m_hContextMenu.Get();
  521. m_hContextMenu = NULL;
  522. }
  523. m_hContextMenu = new Menu( this, "ActionMenu" );
  524. bool bIsReadOnly = m_hSelf.Get() ? m_hSelf->IsReadOnly() : false;
  525. bool bCanOverwrite = !bIsReadOnly;
  526. if ( m_hSelf->IsProcedural() )
  527. {
  528. switch ( m_hSelf->GetProceduralPresetType() )
  529. {
  530. default:
  531. break;
  532. case PROCEDURAL_PRESET_REVEAL:
  533. bCanOverwrite = true;
  534. break;
  535. }
  536. }
  537. if ( bCanOverwrite )
  538. {
  539. m_hContextMenu->AddMenuItem( "Overwrite", new KeyValues( "OnOverwrite" ), this );
  540. m_hContextMenu->AddSeparator();
  541. }
  542. if ( !bIsReadOnly )
  543. {
  544. m_hContextMenu->AddMenuItem( "Rename...", new KeyValues( "OnRename" ), this );
  545. if ( Q_stricmp( GetName(), "Default" ) )
  546. {
  547. m_hContextMenu->AddMenuItem( "Delete...", new KeyValues( "OnDelete" ), this );
  548. }
  549. m_hContextMenu->AddSeparator();
  550. }
  551. m_hContextMenu->AddMenuItem( "Add...", new KeyValues( "AddPreset" ), m_pParent );
  552. m_hContextMenu->AddMenuItem( "Change Crossfade Speed...", new KeyValues( "SetPresetCrossfadeSpeed" ), m_pParent );
  553. m_hContextMenu->AddSeparator();
  554. m_hContextMenu->AddMenuItem( "Manage...", new KeyValues( "ManagePresets" ), m_pParent );
  555. Menu::PlaceContextMenu( this, m_hContextMenu.Get() );
  556. }
  557. void CPresetSlider::UpdateTickPos( int x, int y )
  558. {
  559. int tx, ty, tw, th;
  560. GetTrackRect( tx, ty, tw, th );
  561. bool bIsCtrlKeyDown = vgui::input()->IsKeyDown( KEY_LCONTROL ) || vgui::input()->IsKeyDown( KEY_RCONTROL );
  562. bool bIsAltKeyDown = vgui::input()->IsKeyDown( KEY_LALT ) || vgui::input()->IsKeyDown( KEY_RALT );
  563. if ( bIsCtrlKeyDown && bIsAltKeyDown && !_dragging )
  564. {
  565. x = tx + tw;
  566. }
  567. float previewValue = 0.0f;
  568. if ( x > tx )
  569. {
  570. if ( x >= ( tx + tw ) || tw <= 0 )
  571. {
  572. previewValue = 1.0f;
  573. }
  574. else
  575. {
  576. previewValue = (float)( x - tx ) / (float)tw;
  577. }
  578. }
  579. SetPos( previewValue );
  580. }
  581. void CPresetSlider::OnCursorMoved(int x, int y)
  582. {
  583. UpdateTickPos( x, y );
  584. }
  585. bool CPresetSlider::IsDragging()
  586. {
  587. return _dragging;
  588. }
  589. bool CPresetSlider::IsPreviewSlider()
  590. {
  591. VPANEL capturePanel = input()->GetMouseCapture();
  592. if ( capturePanel )
  593. {
  594. if ( capturePanel != GetVPanel() )
  595. return false;
  596. return true;
  597. }
  598. VPANEL appModal = input()->GetAppModalSurface();
  599. if ( appModal )
  600. return false;
  601. int mx, my;
  602. input()->GetCursorPos( mx, my );
  603. /*
  604. VPANEL topMost = IsWithinTraverse( mx, my, true );
  605. if ( topMost && topMost != GetVPanel() )
  606. {
  607. const char *name = ipanel()->GetName( topMost );
  608. return false;
  609. }
  610. */
  611. return IsWithin( mx, my );
  612. }
  613. float CPresetSlider::GetCurrent()
  614. {
  615. return GetValue() * 0.001f;
  616. }
  617. void CPresetSlider::SetPos( float frac )
  618. {
  619. SetValue( (int)( frac * 1000.0f + 0.5f ), false );
  620. }
  621. void CPresetSlider::ApplySchemeSettings( IScheme *scheme )
  622. {
  623. BaseClass::ApplySchemeSettings( scheme );
  624. m_pName->SetFont( scheme->GetFont( "DefaultBold" ) );
  625. m_pName->SetColor( m_TextColor );
  626. m_pName->ResizeImageToContent();
  627. SetFgColor( Color( 194, 120, 0, 255 ) );
  628. SetThumbWidth( 3 );
  629. Color fullColor1( Color( 118, 71, 41, 255 ) );
  630. Color fullColor2( Color( 194, 120, 0, 255 ) );
  631. m_pEdgeButtons[ 0 ]->SetDefaultColor( m_ZeroColor, m_ZeroColor );
  632. m_pEdgeButtons[ 1 ]->SetDefaultColor( fullColor1, fullColor1 );
  633. m_pEdgeButtons[ 0 ]->SetDepressedColor( m_ZeroColor, m_ZeroColor );
  634. m_pEdgeButtons[ 1 ]->SetDepressedColor( fullColor2, fullColor2 );
  635. m_pEdgeButtons[ 0 ]->SetArmedColor( m_ZeroColor, m_ZeroColor );
  636. m_pEdgeButtons[ 1 ]->SetArmedColor( fullColor1, fullColor1 );
  637. m_pEdgeButtons[ 0 ]->SetButtonActivationType( Button::ACTIVATE_ONPRESSED );
  638. m_pEdgeButtons[ 1 ]->SetButtonActivationType( Button::ACTIVATE_ONPRESSED );
  639. }
  640. void CPresetSlider::PerformLayout()
  641. {
  642. BaseClass::PerformLayout();
  643. int w, h;
  644. GetSize( w, h );
  645. int btnSize = 9;
  646. m_pEdgeButtons[ 0 ]->SetBounds( 3, ( h - btnSize ) / 2, btnSize, btnSize );
  647. m_pEdgeButtons[ 1 ]->SetBounds( w - 12, ( h - btnSize ) / 2, btnSize, btnSize );
  648. }
  649. void CPresetSlider::GetTrackRect( int &x, int &y, int &w, int &h )
  650. {
  651. GetSize( w, h );
  652. x = 15;
  653. y = 2;
  654. w -= 30;
  655. h -= 4;
  656. }
  657. void CPresetSlider::SetGradientColor( const Color& clr )
  658. {
  659. m_GradientColor = clr;
  660. }
  661. void CPresetSlider::Paint()
  662. {
  663. if ( !IsPreviewSlider() )
  664. return;
  665. bool bIsCtrlKeyDown = vgui::input()->IsKeyDown( KEY_LCONTROL ) || vgui::input()->IsKeyDown( KEY_RCONTROL );
  666. if ( !IsDragging() && !bIsCtrlKeyDown )
  667. return;
  668. int mx, my;
  669. input()->GetCursorPos( mx, my );
  670. ScreenToLocal( mx, my );
  671. UpdateTickPos( mx, my );
  672. // horizontal nob
  673. int x, y;
  674. int wide,tall;
  675. GetTrackRect( x, y, wide, tall );
  676. Color col = GetFgColor();
  677. surface()->DrawSetColor( col );
  678. surface()->DrawFilledRect( _nobPos[0], 1, _nobPos[1], GetTall() - 1 );
  679. surface()->DrawSetColor( m_ZeroColor );
  680. surface()->DrawFilledRect( _nobPos[0] - 1, y + 1, _nobPos[0], y + tall - 1 );
  681. }
  682. void CPresetSlider::PaintBackground()
  683. {
  684. int w, h;
  685. GetSize( w, h );
  686. bool hasFocus = IsPreviewSlider();
  687. bool bIsCtrlKeyDown = vgui::input()->IsKeyDown( KEY_LCONTROL ) || vgui::input()->IsKeyDown( KEY_RCONTROL );
  688. bool bIsAltKeyDown = vgui::input()->IsKeyDown( KEY_LALT ) || vgui::input()->IsKeyDown( KEY_RALT );
  689. if ( hasFocus && ( IsDragging() || bIsCtrlKeyDown ) )
  690. {
  691. int tx, ty, tw, th;
  692. GetTrackRect( tx, ty, tw, th );
  693. surface()->DrawSetColor( Color( 0, 0, 0, 255 ) );
  694. surface()->DrawOutlinedRect( tx, ty, tx + tw, ty + th );
  695. surface()->DrawSetColor( m_GradientColor );
  696. ++tx;
  697. ++ty;
  698. tw -= 2;
  699. th -= 2;
  700. // Gradient fill rectangle
  701. int fillw = (int)( (float)tw * GetCurrent() + 0.5f );
  702. int minAlpha = 15;
  703. float alphaTarget = 255.0f;
  704. int curAlpha = max( (int)(GetCurrent() * alphaTarget), minAlpha );
  705. if ( _dragging )
  706. {
  707. surface()->DrawFilledRectFade( tx, ty, tx + fillw, ty + th, minAlpha, curAlpha, true );
  708. surface()->DrawSetColor( m_ZeroColor );
  709. surface()->DrawFilledRect( tx + fillw + 1, ty, tx + tw, ty + th );
  710. }
  711. else
  712. {
  713. surface()->DrawSetColor( bIsAltKeyDown ? m_GradientColor : m_ZeroColor );
  714. surface()->DrawFilledRect( tx, ty, tx + tw, ty + th );
  715. }
  716. }
  717. int cw, ch;
  718. m_pName->SetColor( hasFocus ? m_TextColorFocus : m_TextColor );
  719. m_pName->GetContentSize( cw, ch );
  720. m_pName->SetPos( ( w - cw ) * 0.5f, ( h - ch ) * 0.5f );
  721. m_pName->Paint();
  722. }
  723. //-----------------------------------------------------------------------------
  724. // Slider list panel
  725. //-----------------------------------------------------------------------------
  726. class CSliderListPanel : public PanelListPanel
  727. {
  728. DECLARE_CLASS_SIMPLE( CSliderListPanel, vgui::PanelListPanel );
  729. public:
  730. CSliderListPanel( CBaseAnimSetPresetFaderPanel *parent, vgui::Panel *pParent, const char *panelName );
  731. virtual void OnMouseReleased( vgui::MouseCode code );
  732. virtual void OnMousePressed( vgui::MouseCode code );
  733. private:
  734. MESSAGE_FUNC( OnShowContextMenu, "OnShowContextMenu" );
  735. vgui::DHANDLE< vgui::Menu > m_hContextMenu;
  736. CBaseAnimSetPresetFaderPanel *m_pParent;
  737. };
  738. //-----------------------------------------------------------------------------
  739. // Constructor
  740. //-----------------------------------------------------------------------------
  741. CSliderListPanel::CSliderListPanel( CBaseAnimSetPresetFaderPanel *pFader, vgui::Panel *pParent, const char *panelName ) :
  742. BaseClass( pParent, panelName )
  743. {
  744. m_pParent = pFader;
  745. }
  746. //-----------------------------------------------------------------------------
  747. // Context menu!
  748. //-----------------------------------------------------------------------------
  749. void CSliderListPanel::OnMousePressed( MouseCode code )
  750. {
  751. if ( code == MOUSE_RIGHT )
  752. return;
  753. BaseClass::OnMousePressed( code );
  754. }
  755. void CSliderListPanel::OnMouseReleased( MouseCode code )
  756. {
  757. if ( code == MOUSE_RIGHT )
  758. {
  759. OnShowContextMenu();
  760. return;
  761. }
  762. BaseClass::OnMouseReleased( code );
  763. }
  764. //-----------------------------------------------------------------------------
  765. // Shows the slider context menu
  766. //-----------------------------------------------------------------------------
  767. void CSliderListPanel::OnShowContextMenu()
  768. {
  769. if ( m_hContextMenu.Get() )
  770. {
  771. delete m_hContextMenu.Get();
  772. m_hContextMenu = NULL;
  773. }
  774. m_hContextMenu = new Menu( this, "ActionMenu" );
  775. m_hContextMenu->AddMenuItem( "Add...", new KeyValues( "AddPreset" ), m_pParent );
  776. m_hContextMenu->AddMenuItem( "Change Crossfade Speed...", new KeyValues( "SetPresetCrossfadeSpeed" ), m_pParent );
  777. m_hContextMenu->AddSeparator();
  778. m_hContextMenu->AddMenuItem( "Manage...", new KeyValues( "ManagePresets" ), m_pParent );
  779. Menu::PlaceContextMenu( this, m_hContextMenu.Get() );
  780. }
  781. //-----------------------------------------------------------------------------
  782. // Constructor
  783. //-----------------------------------------------------------------------------
  784. CBaseAnimSetPresetFaderPanel::CBaseAnimSetPresetFaderPanel( vgui::Panel *parent, const char *className, CBaseAnimationSetEditor *editor ) :
  785. BaseClass( parent, className ),
  786. m_flLastFrameTime( 0.0f ),
  787. m_pSliders( NULL ),
  788. m_pWorkspace( NULL )
  789. {
  790. m_hEditor = editor;
  791. m_pWorkspace = new EditablePanel( this, "PresetWorkspace" );
  792. m_pWorkspace->SetPaintBackgroundEnabled( false );
  793. m_pWorkspace->SetPaintEnabled( false );
  794. m_pWorkspace->SetPaintBorderEnabled( false );
  795. m_pSliders = new CSliderListPanel( this, m_pWorkspace, "PresetSliders" );
  796. m_pSliders->SetFirstColumnWidth( 0 );
  797. m_pSliders->SetAutoResize
  798. (
  799. Panel::PIN_TOPLEFT,
  800. Panel::AUTORESIZE_DOWNANDRIGHT,
  801. 0, 24,
  802. 0, 0
  803. );
  804. m_pSliders->SetPos( 0, 24 );
  805. m_pSliders->SetVerticalBufferPixels( 0 );
  806. m_pFilter = new TextEntry( m_pWorkspace, "PresetFilter" );
  807. m_pFilter->AddActionSignalTarget( this );
  808. m_pFilter->SetAutoResize
  809. (
  810. Panel::PIN_TOPLEFT,
  811. Panel::AUTORESIZE_RIGHT,
  812. 2, 2,
  813. 2, 22
  814. );
  815. m_pFilter->SetPos( 0, 0 );
  816. m_pWorkspace->SetAutoResize
  817. (
  818. Panel::PIN_TOPLEFT,
  819. Panel::AUTORESIZE_DOWNANDRIGHT,
  820. 0, 0,
  821. 0, 0
  822. );
  823. }
  824. //-----------------------------------------------------------------------------
  825. // Purpose: refreshes dialog on text changing
  826. //-----------------------------------------------------------------------------
  827. void CBaseAnimSetPresetFaderPanel::OnTextChanged( )
  828. {
  829. int nLength = m_pFilter->GetTextLength();
  830. m_Filter.SetLength( nLength );
  831. if ( nLength > 0 )
  832. {
  833. m_pFilter->GetText( m_Filter.GetForModify(), nLength+1 );
  834. }
  835. PopulateList( true );
  836. }
  837. //-----------------------------------------------------------------------------
  838. // Called from the input dialogs used by this class
  839. //-----------------------------------------------------------------------------
  840. void CBaseAnimSetPresetFaderPanel::OnInputCompleted( KeyValues *pParams )
  841. {
  842. const char *pText = pParams->GetString( "text", NULL );
  843. KeyValues *pContextKeyValues = pParams->FindKey( "CrossfadeSpeed" );
  844. if ( pContextKeyValues )
  845. {
  846. float f = Q_atof( pText );
  847. if ( f > 0.0f )
  848. {
  849. ifm_fader_timescale = f;
  850. }
  851. else
  852. {
  853. Warning( "Crossfade [%f] invalid, must be a postive number\n", f );
  854. }
  855. return;
  856. }
  857. Assert( 0 );
  858. }
  859. //-----------------------------------------------------------------------------
  860. // Called from the add preset name dialogs used by this class
  861. //-----------------------------------------------------------------------------
  862. void CBaseAnimSetPresetFaderPanel::OnPresetNameSelected( KeyValues *pParams )
  863. {
  864. const char *pText = pParams->GetString( "text", NULL );
  865. if ( !pText || !*pText )
  866. {
  867. vgui::MessageBox *pError = new MessageBox( "Add Preset Error", "Can't add preset with an empty name\n", this );
  868. pError->SetDeleteSelfOnClose( true );
  869. pError->DoModal();
  870. return;
  871. }
  872. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Add Preset" );
  873. CDmePresetGroup *pPresetGroup = GetElementKeyValue<CDmePresetGroup>( pParams, "presetGroup" );
  874. if ( !pPresetGroup )
  875. {
  876. const char *pGroupName = pParams->GetString( "presetGroupName" );
  877. if ( !pGroupName )
  878. return;
  879. pPresetGroup = m_AnimSet->FindOrAddPresetGroup( pGroupName );
  880. }
  881. if ( pPresetGroup->m_bIsReadOnly )
  882. {
  883. vgui::MessageBox *pError = new MessageBox( "Add Preset Error", "Can't add preset to a read-only preset group!\n", this );
  884. pError->SetDeleteSelfOnClose( true );
  885. pError->DoModal();
  886. return;
  887. }
  888. if ( pPresetGroup->FindPreset( pText ) )
  889. {
  890. vgui::MessageBox *pError = new MessageBox( "Add Preset Error", "A preset with that name already exists!\n", this );
  891. pError->SetDeleteSelfOnClose( true );
  892. pError->DoModal();
  893. return;
  894. }
  895. CDmePreset *pPreset = pPresetGroup->FindOrAddPreset( pText );
  896. AddNewPreset( pPreset );
  897. guard.Release();
  898. ChangeAnimationSet( m_AnimSet );
  899. }
  900. //-----------------------------------------------------------------------------
  901. // The 'set crossfade speed' context menu option
  902. //-----------------------------------------------------------------------------
  903. void CBaseAnimSetPresetFaderPanel::OnSetCrossfadeSpeed()
  904. {
  905. if ( m_hInputDialog.Get() )
  906. {
  907. delete m_hInputDialog.Get();
  908. }
  909. char sz[32 ];
  910. Q_snprintf( sz, sizeof( sz ), "%f", ifm_fader_timescale );
  911. m_hInputDialog = new InputDialog( this, "Crossfade Speed", "Fader Crossfade Rate:", sz );
  912. if ( m_hInputDialog.Get() )
  913. {
  914. KeyValues *pContextKeyValues = new KeyValues( "CrossfadeSpeed" );
  915. m_hInputDialog->SetSmallCaption( true );
  916. m_hInputDialog->SetMultiline( false );
  917. m_hInputDialog->DoModal( pContextKeyValues );
  918. }
  919. }
  920. //-----------------------------------------------------------------------------
  921. // The 'add preset' context menu option
  922. //-----------------------------------------------------------------------------
  923. void CBaseAnimSetPresetFaderPanel::OnAddPreset()
  924. {
  925. if ( !m_AnimSet.Get() )
  926. return;
  927. if ( !m_hAddPresetDialog.Get() )
  928. {
  929. m_hAddPresetDialog = new CAddPresetDialog( this );
  930. m_hAddPresetDialog->AddActionSignalTarget( this );
  931. }
  932. m_hAddPresetDialog->DoModal( m_AnimSet, NULL );
  933. }
  934. //-----------------------------------------------------------------------------
  935. // Called by the preset group editor panel when it changes presets
  936. //-----------------------------------------------------------------------------
  937. void CBaseAnimSetPresetFaderPanel::OnPresetsChanged()
  938. {
  939. ChangeAnimationSet( m_AnimSet );
  940. }
  941. //-----------------------------------------------------------------------------
  942. // Brings up the preset manager
  943. //-----------------------------------------------------------------------------
  944. void CBaseAnimSetPresetFaderPanel::OnManagePresets()
  945. {
  946. if ( !m_hPresetEditor.Get() )
  947. {
  948. m_hPresetEditor = new CDmePresetGroupEditorFrame( this, "Manage Presets" );
  949. m_hPresetEditor->AddActionSignalTarget( this );
  950. m_hPresetEditor->SetVisible( false );
  951. m_hPresetEditor->SetDeleteSelfOnClose( false );
  952. m_hPresetEditor->MoveToCenterOfScreen();
  953. }
  954. m_hPresetEditor->SetAnimationSet( m_AnimSet );
  955. m_hPresetEditor->DoModal( );
  956. }
  957. void CBaseAnimSetPresetFaderPanel::ApplySchemeSettings( IScheme *scheme )
  958. {
  959. BaseClass::ApplySchemeSettings( scheme );
  960. m_pSliders->SetBgColor( Color( 42, 42, 42, 255 ) );
  961. }
  962. void CBaseAnimSetPresetFaderPanel::GetPreviewFader( FaderPreview_t& fader )
  963. {
  964. Q_memset( &fader, 0, sizeof( fader ) );
  965. fader.isbeingdragged = false;
  966. fader.holdingctrl = input()->IsKeyDown( KEY_LCONTROL ) || input()->IsKeyDown( KEY_RCONTROL );
  967. int mx, my;
  968. input()->GetCursorPos( mx, my );
  969. if ( !IsWithin( mx, my ) )
  970. {
  971. fader.holdingctrl = false;
  972. }
  973. // Walk through sliders and figure out which is under the mouse
  974. CPresetSlider *mouseOver = NULL;
  975. for ( int i = m_pSliders->FirstItem(); i != m_pSliders->InvalidItemID(); i = m_pSliders->NextItem(i) )
  976. {
  977. CPresetSlider *slider = static_cast< CPresetSlider * >( m_pSliders->GetItemPanel( i ) );
  978. if ( !slider || !slider->IsPreviewSlider() )
  979. continue;
  980. mouseOver = slider;
  981. fader.isbeingdragged = slider->IsDragging();
  982. break;
  983. }
  984. if ( mouseOver )
  985. {
  986. // Deal with procedural presets here
  987. if ( fader.holdingctrl ||
  988. fader.isbeingdragged )
  989. {
  990. mouseOver->UpdateProceduralValues();
  991. }
  992. fader.name = mouseOver->GetName();
  993. fader.amount = mouseOver->GetCurrent();
  994. fader.values = mouseOver->GetAttributeDict();
  995. fader.preset = mouseOver->GetPreset();
  996. }
  997. }
  998. void CBaseAnimSetPresetFaderPanel::PopulateList( bool bChanged )
  999. {
  1000. if ( !m_AnimSet.Get() )
  1001. {
  1002. m_CurrentPresetList.RemoveAll();
  1003. m_pSliders->DeleteAllItems();
  1004. return;
  1005. }
  1006. CDmrElementArray< CDmePresetGroup > presetGroups = m_AnimSet->GetPresetGroups();
  1007. Assert( presetGroups.IsValid() );
  1008. int c = presetGroups.Count();
  1009. bool bNeedRebuild = false;
  1010. int slot = 0;
  1011. for ( int i = 0 ; i < c; ++i )
  1012. {
  1013. CDmePresetGroup *pPresetGroup = presetGroups[ i ];
  1014. Assert( pPresetGroup );
  1015. if ( !pPresetGroup || !pPresetGroup->m_bIsVisible )
  1016. continue;
  1017. CDmrElementArray< CDmePreset > presets = pPresetGroup->GetPresets();
  1018. int cp = presets.Count();
  1019. for ( int j = 0; j < cp; ++j )
  1020. {
  1021. CDmePreset *pPreset = presets[ j ];
  1022. Assert( pPreset );
  1023. const char *pElementName = pPreset->GetName();
  1024. if ( Q_stricmp( pElementName, "Default" ) )
  1025. {
  1026. if ( m_Filter.Length() && !Q_stristr( pElementName, m_Filter.Get() ) )
  1027. continue;
  1028. }
  1029. if ( slot >= m_CurrentPresetList.Count() )
  1030. {
  1031. bNeedRebuild = true;
  1032. break;
  1033. }
  1034. if ( pPreset != m_CurrentPresetList[ slot ] )
  1035. {
  1036. bNeedRebuild = true;
  1037. break;
  1038. }
  1039. ++slot;
  1040. }
  1041. }
  1042. if ( slot != m_CurrentPresetList.Count() )
  1043. {
  1044. bNeedRebuild = true;
  1045. }
  1046. if ( bNeedRebuild )
  1047. {
  1048. m_CurrentPresetList.RemoveAll();
  1049. m_pSliders->DeleteAllItems();
  1050. for ( int i = 0 ; i < c; ++i )
  1051. {
  1052. CDmePresetGroup *pPresetGroup = presetGroups[ i ];
  1053. Assert( pPresetGroup );
  1054. if ( !pPresetGroup || !pPresetGroup->m_bIsVisible )
  1055. continue;
  1056. CDmrElementArray< CDmePreset > presets = pPresetGroup->GetPresets();
  1057. int cp = presets.Count();
  1058. for ( int j = 0; j < cp; ++j )
  1059. {
  1060. CDmePreset *pPreset = presets[ j ];
  1061. Assert( pPreset );
  1062. const char *pElementName = pPreset->GetName();
  1063. if ( Q_stricmp( pElementName, "Default" ) )
  1064. {
  1065. if ( m_Filter.Length() && !Q_stristr( pElementName, m_Filter.Get() ) )
  1066. continue;
  1067. }
  1068. CPresetSlider *pSlider = new CPresetSlider( this, pPreset->GetName(), pPreset );
  1069. pSlider->SetPos( 0 );
  1070. pSlider->SetSize( 100, 20 );
  1071. pSlider->SetGradientColor( Color( 194, 120, 0, 255 ) );
  1072. pSlider->SetControlValues( );
  1073. m_pSliders->AddItem( NULL, pSlider );
  1074. m_CurrentPresetList.AddToTail( pPreset->GetHandle() );
  1075. }
  1076. }
  1077. }
  1078. else
  1079. {
  1080. UpdateControlValues();
  1081. }
  1082. }
  1083. void CBaseAnimSetPresetFaderPanel::ChangeAnimationSet( CDmeAnimationSet *newAnimSet )
  1084. {
  1085. bool bChanged = m_AnimSet != newAnimSet;
  1086. m_AnimSet = newAnimSet;
  1087. PopulateList( bChanged );
  1088. }
  1089. void CBaseAnimSetPresetFaderPanel::UpdateControlValues()
  1090. {
  1091. for ( int i = m_pSliders->FirstItem(); i != m_pSliders->InvalidItemID(); i = m_pSliders->NextItem(i) )
  1092. {
  1093. CPresetSlider *pSlider = static_cast< CPresetSlider * >( m_pSliders->GetItemPanel( i ) );
  1094. if ( pSlider )
  1095. {
  1096. pSlider->SetControlValues( );
  1097. }
  1098. }
  1099. }
  1100. void CBaseAnimSetPresetFaderPanel::ApplyPreset( float flScale, AttributeDict_t& dict )
  1101. {
  1102. CBaseAnimSetAttributeSliderPanel *sliderPanel = m_hEditor->GetAttributeSlider();
  1103. if ( sliderPanel )
  1104. {
  1105. sliderPanel->ApplyPreset( flScale, dict );
  1106. }
  1107. }
  1108. //-----------------------------------------------------------------------------
  1109. // Reads the current animation set control values, creates presets
  1110. //-----------------------------------------------------------------------------
  1111. void CBaseAnimSetPresetFaderPanel::SetPresetFromSliders( CDmePreset *pPreset )
  1112. {
  1113. if ( !m_AnimSet.Get() )
  1114. return;
  1115. CBaseAnimSetAttributeSliderPanel *pSliderPanel = m_hEditor->GetAttributeSlider();
  1116. if ( !pSliderPanel )
  1117. return;
  1118. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Set Preset" );
  1119. const CDmrElementArray< CDmElement > controlList = m_AnimSet->GetControls();
  1120. // Now set values for each known control
  1121. int numControls = controlList.Count();
  1122. for ( int i = 0; i < numControls; ++i )
  1123. {
  1124. CDmElement *pControl = controlList[ i ];
  1125. if ( pControl->GetValue<bool>( "transform" ) )
  1126. continue;
  1127. // Facial sliders below
  1128. AttributeValue_t value;
  1129. bool bIsDefault = !pSliderPanel->GetAttributeSliderValue( &value, pControl->GetName() );
  1130. if ( !bIsDefault )
  1131. {
  1132. float flDefaultValue = pControl->GetValue< float >( "defaultValue" );
  1133. float flDefaultBalance = pControl->GetValue< float >( "defaultBalance" );
  1134. float flDefaultMultilevel = pControl->GetValue< float >( "defaultMultilevel" );
  1135. bIsDefault = ( value.m_pValue[ANIM_CONTROL_VALUE] == flDefaultValue ) &&
  1136. ( value.m_pValue[ANIM_CONTROL_BALANCE] == flDefaultBalance ) &&
  1137. ( value.m_pValue[ANIM_CONTROL_MULTILEVEL] == flDefaultMultilevel );
  1138. }
  1139. // Blow away preset values for controls that contain the default values
  1140. if ( bIsDefault )
  1141. {
  1142. pPreset->RemoveControlValue( pControl->GetName() );
  1143. continue;
  1144. }
  1145. bool bIsCombo = pControl->GetValue< bool >( "combo" );
  1146. bool bIsMulti = pControl->GetValue< bool >( "multi" );
  1147. // Stamp the control value
  1148. CDmElement *pControlValue = pPreset->FindOrAddControlValue( pControl->GetName() );
  1149. pControlValue->SetValue< float >( "value", value.m_pValue[ANIM_CONTROL_VALUE] );
  1150. if ( bIsCombo )
  1151. {
  1152. pControlValue->SetValue< float >( "balance", value.m_pValue[ANIM_CONTROL_BALANCE] );
  1153. }
  1154. else
  1155. {
  1156. pControlValue->RemoveAttribute( "balance" );
  1157. }
  1158. if ( bIsMulti )
  1159. {
  1160. pControlValue->SetValue< float >( "multilevel", value.m_pValue[ANIM_CONTROL_MULTILEVEL] );
  1161. }
  1162. else
  1163. {
  1164. pControlValue->RemoveAttribute( "multilevel" );
  1165. }
  1166. }
  1167. }
  1168. void CBaseAnimSetPresetFaderPanel::AddNewPreset( CDmePreset *pPreset )
  1169. {
  1170. if ( !pPreset )
  1171. return;
  1172. SetPresetFromSliders( pPreset );
  1173. CPresetSlider *pSlider = new CPresetSlider( this, pPreset->GetName(), pPreset );
  1174. if ( pSlider )
  1175. {
  1176. pSlider->SetPos( 0 );
  1177. pSlider->SetSize( 100, 20 );
  1178. pSlider->SetGradientColor( Color( 194, 120, 0, 255 ) );
  1179. pSlider->SetControlValues( );
  1180. m_pSliders->AddItem( NULL, pSlider );
  1181. }
  1182. }
  1183. void CBaseAnimSetPresetFaderPanel::OnAddNewPreset( KeyValues *pKeyValues )
  1184. {
  1185. CDmePreset *pPreset = GetElementKeyValue<CDmePreset>( pKeyValues, "preset" );
  1186. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Overwrite Preset" );
  1187. AddNewPreset( pPreset );
  1188. guard.Release();
  1189. ChangeAnimationSet( m_AnimSet );
  1190. }
  1191. void CBaseAnimSetPresetFaderPanel::AddNewPreset( const char *pGroupName, const char *pName )
  1192. {
  1193. if ( !m_AnimSet.Get() )
  1194. return;
  1195. CBaseAnimSetAttributeSliderPanel *sliderPanel = m_hEditor->GetAttributeSlider();
  1196. if ( !sliderPanel )
  1197. return;
  1198. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Add Preset" );
  1199. CDmePresetGroup *pPresetGroup = m_AnimSet->FindOrAddPresetGroup( pGroupName );
  1200. CDmePreset *pPreset = pPresetGroup->FindOrAddPreset( pName );
  1201. AddNewPreset( pPreset );
  1202. guard.Release();
  1203. ChangeAnimationSet( m_AnimSet );
  1204. }
  1205. void CBaseAnimSetPresetFaderPanel::OnOverwritePreset( CDmePreset *pPreset )
  1206. {
  1207. SetPresetFromSliders( pPreset );
  1208. UpdateControlValues();
  1209. }
  1210. void CBaseAnimSetPresetFaderPanel::OnDeletePreset( CDmePreset *pPreset )
  1211. {
  1212. {
  1213. // Delete it from various things
  1214. CUndoScopeGuard guard( 0, NOTIFY_SETDIRTYFLAG, "Delete Preset" );
  1215. m_AnimSet->RemovePreset( pPreset );
  1216. }
  1217. ChangeAnimationSet( m_AnimSet );
  1218. }
  1219. void CBaseAnimSetPresetFaderPanel::ProceduralPreset_UpdateCrossfade( CDmePreset *pPreset, bool bFadeIn )
  1220. {
  1221. // Handled by derived class in SFM
  1222. }