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.

194 lines
5.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Dialog allowing users to select presets from within a preset group
  4. //
  5. //===========================================================================//
  6. #include "dme_controls/presetpicker.h"
  7. #include "tier1/KeyValues.h"
  8. #include "tier1/utlbuffer.h"
  9. #include "vgui/IVGui.h"
  10. #include "vgui_controls/button.h"
  11. #include "vgui_controls/listpanel.h"
  12. #include "vgui_controls/splitter.h"
  13. #include "vgui_controls/messagebox.h"
  14. #include "movieobjects/dmeanimationset.h"
  15. #include "datamodel/dmelement.h"
  16. #include "matsys_controls/picker.h"
  17. #include "dme_controls/dmecontrols_utils.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include <tier0/memdbgon.h>
  20. using namespace vgui;
  21. static int __cdecl PresetNameSortFunc( vgui::ListPanel *pPanel, const vgui::ListPanelItem &item1, const vgui::ListPanelItem &item2 )
  22. {
  23. const char *string1 = item1.kv->GetString( "name" );
  24. const char *string2 = item2.kv->GetString( "name" );
  25. return Q_stricmp( string1, string2 );
  26. }
  27. CPresetPickerFrame::CPresetPickerFrame( vgui::Panel *pParent, const char *pTitle, bool bAllowMultiSelect ) :
  28. BaseClass( pParent, "PresetPickerFrame" )
  29. {
  30. SetDeleteSelfOnClose( true );
  31. m_pContextKeyValues = NULL;
  32. m_pPresetList = new vgui::ListPanel( this, "PresetList" );
  33. m_pPresetList->AddColumnHeader( 0, "name", "Preset Name", 52, 0 );
  34. m_pPresetList->SetSelectIndividualCells( false );
  35. m_pPresetList->SetMultiselectEnabled( bAllowMultiSelect );
  36. m_pPresetList->SetEmptyListText( "No presets" );
  37. m_pPresetList->AddActionSignalTarget( this );
  38. m_pPresetList->SetSortFunc( 0, PresetNameSortFunc );
  39. m_pPresetList->SetSortColumn( 0 );
  40. m_pOpenButton = new vgui::Button( this, "OkButton", "#MessageBox_OK", this, "Ok" );
  41. m_pCancelButton = new vgui::Button( this, "CancelButton", "#MessageBox_Cancel", this, "Cancel" );
  42. SetBlockDragChaining( true );
  43. LoadControlSettingsAndUserConfig( "resource/presetpicker.res" );
  44. SetTitle( pTitle, false );
  45. }
  46. CPresetPickerFrame::~CPresetPickerFrame()
  47. {
  48. CleanUpMessage();
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Refreshes the list of presets
  52. //-----------------------------------------------------------------------------
  53. void CPresetPickerFrame::RefreshPresetList( CDmElement *pPresetGroup, bool bSelectAll )
  54. {
  55. m_pPresetList->RemoveAll();
  56. const CDmrElementArray< CDmePreset > presets( pPresetGroup, "presets" );
  57. if ( !presets.IsValid() )
  58. return;
  59. int nCount = presets.Count();
  60. if ( nCount == 0 )
  61. return;
  62. for ( int i = 0; i < nCount; ++i )
  63. {
  64. CDmePreset *pPreset = presets[i];
  65. const char *pName = pPreset->GetName();
  66. if ( !pName || !pName[0] )
  67. {
  68. pName = "<no name>";
  69. }
  70. KeyValues *kv = new KeyValues( "node" );
  71. kv->SetString( "name", pName );
  72. SetElementKeyValue( kv, "preset", pPreset );
  73. int nItemID = m_pPresetList->AddItem( kv, 0, false, false );
  74. if ( bSelectAll )
  75. {
  76. m_pPresetList->AddSelectedItem( nItemID );
  77. }
  78. }
  79. m_pPresetList->SortList();
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Deletes the message
  83. //-----------------------------------------------------------------------------
  84. void CPresetPickerFrame::CleanUpMessage()
  85. {
  86. if ( m_pContextKeyValues )
  87. {
  88. m_pContextKeyValues->deleteThis();
  89. m_pContextKeyValues = NULL;
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Sets the current scene + animation list
  94. //-----------------------------------------------------------------------------
  95. void CPresetPickerFrame::DoModal( CDmElement *pPresetGroup, bool bSelectAll, KeyValues *pContextKeyValues )
  96. {
  97. CleanUpMessage();
  98. RefreshPresetList( pPresetGroup, bSelectAll );
  99. m_pContextKeyValues = pContextKeyValues;
  100. BaseClass::DoModal();
  101. }
  102. //-----------------------------------------------------------------------------
  103. // On command
  104. //-----------------------------------------------------------------------------
  105. void CPresetPickerFrame::OnCommand( const char *pCommand )
  106. {
  107. if ( !Q_stricmp( pCommand, "Ok" ) )
  108. {
  109. int nSelectedItemCount = m_pPresetList->GetSelectedItemsCount();
  110. if ( nSelectedItemCount == 0 )
  111. return;
  112. KeyValues *pActionKeys = new KeyValues( "PresetPicked" );
  113. if ( m_pPresetList->IsMultiselectEnabled() )
  114. {
  115. pActionKeys->SetInt( "count", nSelectedItemCount );
  116. // Adds them in selection order
  117. for ( int i = 0; i < nSelectedItemCount; ++i )
  118. {
  119. char pBuf[32];
  120. Q_snprintf( pBuf, sizeof(pBuf), "%d", i );
  121. int nItemID = m_pPresetList->GetSelectedItem( i );
  122. KeyValues *pKeyValues = m_pPresetList->GetItem( nItemID );
  123. CDmePreset *pPreset = GetElementKeyValue<CDmePreset>( pKeyValues, "preset" );
  124. SetElementKeyValue( pActionKeys, pBuf, pPreset );
  125. }
  126. }
  127. else
  128. {
  129. int nItemID = m_pPresetList->GetSelectedItem( 0 );
  130. KeyValues *pKeyValues = m_pPresetList->GetItem( nItemID );
  131. CDmePreset *pPreset = GetElementKeyValue<CDmePreset>( pKeyValues, "preset" );
  132. SetElementKeyValue( pActionKeys, "preset", pPreset );
  133. }
  134. if ( m_pContextKeyValues )
  135. {
  136. pActionKeys->AddSubKey( m_pContextKeyValues );
  137. // This prevents them from being deleted later
  138. m_pContextKeyValues = NULL;
  139. }
  140. PostActionSignal( pActionKeys );
  141. CloseModal();
  142. return;
  143. }
  144. if ( !Q_stricmp( pCommand, "Cancel" ) )
  145. {
  146. KeyValues *pActionKeys = new KeyValues( "PresetPickCancelled" );
  147. if ( m_pContextKeyValues )
  148. {
  149. pActionKeys->AddSubKey( m_pContextKeyValues );
  150. // This prevents them from being deleted later
  151. m_pContextKeyValues = NULL;
  152. }
  153. PostActionSignal( pActionKeys );
  154. CloseModal();
  155. return;
  156. }
  157. BaseClass::OnCommand( pCommand );
  158. }