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.

280 lines
8.5 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "dme_controls/DmePicker.h"
  7. #include "tier1/keyvalues.h"
  8. #include "vgui_controls/TextEntry.h"
  9. #include "vgui_controls/ListPanel.h"
  10. #include "vgui_controls/Button.h"
  11. #include "datamodel/dmelement.h"
  12. #include "vgui/isurface.h"
  13. #include "vgui/iinput.h"
  14. #include "dme_controls/dmecontrols_utils.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. using namespace vgui;
  18. //-----------------------------------------------------------------------------
  19. //
  20. // Dme Picker
  21. //
  22. //-----------------------------------------------------------------------------
  23. //-----------------------------------------------------------------------------
  24. // Sort by MDL name
  25. //-----------------------------------------------------------------------------
  26. static int __cdecl DmeBrowserSortFunc( vgui::ListPanel *pPanel, const ListPanelItem &item1, const ListPanelItem &item2 )
  27. {
  28. const char *string1 = item1.kv->GetString("dme");
  29. const char *string2 = item2.kv->GetString("dme");
  30. return stricmp( string1, string2 );
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Constructor
  34. //-----------------------------------------------------------------------------
  35. CDmePicker::CDmePicker( vgui::Panel *pParent ) : BaseClass( pParent, "DmePicker" )
  36. {
  37. // FIXME: Make this an image browser
  38. m_pDmeBrowser = new vgui::ListPanel( this, "DmeBrowser" );
  39. m_pDmeBrowser->AddColumnHeader( 0, "dme", "Dme Elements", 52, 0 );
  40. m_pDmeBrowser->SetSelectIndividualCells( true );
  41. m_pDmeBrowser->SetEmptyListText( "No Dme Elements" );
  42. m_pDmeBrowser->SetDragEnabled( true );
  43. m_pDmeBrowser->AddActionSignalTarget( this );
  44. m_pDmeBrowser->SetSortFunc( 0, DmeBrowserSortFunc );
  45. m_pDmeBrowser->SetSortColumn( 0 );
  46. // filter selection
  47. m_pFilterList = new TextEntry( this, "FilterList" );
  48. m_pFilterList->AddActionSignalTarget( this );
  49. m_pFilterList->RequestFocus();
  50. LoadControlSettingsAndUserConfig( "resource/dmepicker.res" );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose: Destructor
  54. //-----------------------------------------------------------------------------
  55. CDmePicker::~CDmePicker()
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose: called to open
  60. //-----------------------------------------------------------------------------
  61. void CDmePicker::Activate( const CUtlVector< DmePickerInfo_t >&vec )
  62. {
  63. m_pDmeBrowser->RemoveAll();
  64. int nCount = vec.Count();
  65. for ( int i = 0; i < nCount; ++i )
  66. {
  67. CDmElement *pElement = GetElement<CDmElement>( vec[i].m_hElement );
  68. const char *pElementName = pElement ? pElement->GetName() : "<null element>";
  69. const char *pItemName = vec[i].m_pChoiceString ? vec[i].m_pChoiceString : pElementName;
  70. KeyValues *kv = new KeyValues( "node", "dme", pItemName );
  71. kv->SetInt( "dmeHandle", vec[i].m_hElement );
  72. int nItemID = m_pDmeBrowser->AddItem( kv, 0, false, false );
  73. KeyValues *pDrag = new KeyValues( "drag", "text", pElementName );
  74. pDrag->SetString( "texttype", "dmeName" );
  75. pDrag->SetInt( "dmeelement", vec[i].m_hElement );
  76. m_pDmeBrowser->SetItemDragData( nItemID, pDrag );
  77. }
  78. RefreshDmeList();
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose:
  82. //-----------------------------------------------------------------------------
  83. void CDmePicker::OnKeyCodeTyped( KeyCode code )
  84. {
  85. if (( code == KEY_UP ) || ( code == KEY_DOWN ) || ( code == KEY_PAGEUP ) || ( code == KEY_PAGEDOWN ))
  86. {
  87. KeyValues *pMsg = new KeyValues("KeyCodeTyped", "code", code);
  88. vgui::ipanel()->SendMessage( m_pDmeBrowser->GetVPanel(), pMsg, GetVPanel());
  89. pMsg->deleteThis();
  90. }
  91. else
  92. {
  93. BaseClass::OnKeyCodeTyped( code );
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose: refreshes the file list
  98. //-----------------------------------------------------------------------------
  99. void CDmePicker::RefreshDmeList()
  100. {
  101. // Check the filter matches
  102. int nMatchingElements = 0;
  103. int nTotalCount = 0;
  104. for ( int nItemID = m_pDmeBrowser->FirstItem(); nItemID != m_pDmeBrowser->InvalidItemID(); nItemID = m_pDmeBrowser->NextItem( nItemID ) )
  105. {
  106. KeyValues *kv = m_pDmeBrowser->GetItem( nItemID );
  107. const char *pElementName = kv->GetString( "dme" );
  108. bool bIsVisible = !m_Filter.Length() || Q_stristr( pElementName, m_Filter.Get() );
  109. m_pDmeBrowser->SetItemVisible( nItemID, bIsVisible );
  110. if ( bIsVisible )
  111. {
  112. ++nMatchingElements;
  113. }
  114. ++nTotalCount;
  115. }
  116. m_pDmeBrowser->SortList();
  117. char pColumnTitle[512];
  118. Q_snprintf( pColumnTitle, sizeof(pColumnTitle), "%s (%d/%d)",
  119. "Dme Elements", nMatchingElements, nTotalCount );
  120. m_pDmeBrowser->SetColumnHeaderText( 0, pColumnTitle );
  121. if ( ( m_pDmeBrowser->GetItemCount() > 0 ) && ( m_pDmeBrowser->GetSelectedItemsCount() == 0 ) )
  122. {
  123. int nItemID = m_pDmeBrowser->GetItemIDFromRow( 0 );
  124. m_pDmeBrowser->SetSelectedCell( nItemID, 0 );
  125. }
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Purpose: refreshes dialog on text changing
  129. //-----------------------------------------------------------------------------
  130. void CDmePicker::OnTextChanged( )
  131. {
  132. int nLength = m_pFilterList->GetTextLength();
  133. m_Filter.SetLength( nLength );
  134. if ( nLength > 0 )
  135. {
  136. m_pFilterList->GetText( m_Filter.Get(), nLength+1 );
  137. }
  138. RefreshDmeList();
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Returns the selceted model name
  142. //-----------------------------------------------------------------------------
  143. CDmElement *CDmePicker::GetSelectedDme( )
  144. {
  145. if ( m_pDmeBrowser->GetSelectedItemsCount() == 0 )
  146. return NULL;
  147. int nIndex = m_pDmeBrowser->GetSelectedItem( 0 );
  148. KeyValues *pItemKeyValues = m_pDmeBrowser->GetItem( nIndex );
  149. return GetElementKeyValue< CDmElement >( pItemKeyValues, "dmeHandle" );
  150. }
  151. //-----------------------------------------------------------------------------
  152. //
  153. // Purpose: Modal picker frame
  154. //
  155. //-----------------------------------------------------------------------------
  156. CDmePickerFrame::CDmePickerFrame( vgui::Panel *pParent, const char *pTitle ) :
  157. BaseClass( pParent, "DmePickerFrame" )
  158. {
  159. m_pContextKeyValues = NULL;
  160. SetDeleteSelfOnClose( true );
  161. m_pPicker = new CDmePicker( this );
  162. m_pPicker->AddActionSignalTarget( this );
  163. m_pOpenButton = new Button( this, "OpenButton", "#FileOpenDialog_Open", this, "Open" );
  164. m_pCancelButton = new Button( this, "CancelButton", "#FileOpenDialog_Cancel", this, "Cancel" );
  165. SetBlockDragChaining( true );
  166. LoadControlSettingsAndUserConfig( "resource/dmepickerframe.res" );
  167. SetTitle( pTitle, false );
  168. }
  169. CDmePickerFrame::~CDmePickerFrame()
  170. {
  171. CleanUpMessage();
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Deletes the message
  175. //-----------------------------------------------------------------------------
  176. void CDmePickerFrame::CleanUpMessage()
  177. {
  178. if ( m_pContextKeyValues )
  179. {
  180. m_pContextKeyValues->deleteThis();
  181. m_pContextKeyValues = NULL;
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Purpose: Activate the dialog
  186. //-----------------------------------------------------------------------------
  187. void CDmePickerFrame::DoModal( const CUtlVector< DmePickerInfo_t >& vec, KeyValues *pKeyValues )
  188. {
  189. CleanUpMessage();
  190. m_pContextKeyValues = pKeyValues;
  191. m_pPicker->Activate( vec );
  192. m_pOpenButton->SetEnabled( vec.Count() != 0 );
  193. BaseClass::DoModal();
  194. }
  195. //-----------------------------------------------------------------------------
  196. // On command
  197. //-----------------------------------------------------------------------------
  198. void CDmePickerFrame::OnCommand( const char *pCommand )
  199. {
  200. if ( !Q_stricmp( pCommand, "Open" ) )
  201. {
  202. CDmElement *pElement = m_pPicker->GetSelectedDme( );
  203. KeyValues *pActionKeys = new KeyValues( "DmeSelected" );
  204. SetElementKeyValue( pActionKeys, "dme", pElement );
  205. if ( m_pContextKeyValues )
  206. {
  207. pActionKeys->AddSubKey( m_pContextKeyValues );
  208. // This prevents them from being deleted later
  209. m_pContextKeyValues = NULL;
  210. }
  211. PostActionSignal( pActionKeys );
  212. CloseModal();
  213. return;
  214. }
  215. if ( !Q_stricmp( pCommand, "Cancel" ) )
  216. {
  217. KeyValues *pActionKeys = new KeyValues( "DmeSelectionCancelled" );
  218. if ( m_pContextKeyValues )
  219. {
  220. pActionKeys->AddSubKey( m_pContextKeyValues );
  221. // This prevents them from being deleted later
  222. m_pContextKeyValues = NULL;
  223. }
  224. PostActionSignal( pActionKeys );
  225. CloseModal();
  226. return;
  227. }
  228. BaseClass::OnCommand( pCommand );
  229. }