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.

174 lines
4.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "dme_controls/AttributeElementPickerPanel.h"
  9. #include "dme_controls/AttributeTextEntry.h"
  10. #include "dme_controls/AttributeWidgetFactory.h"
  11. #include "datamodel/dmelement.h"
  12. #include "tier1/KeyValues.h"
  13. #include "vgui_controls/Button.h"
  14. #include "vgui_controls/ComboBox.h"
  15. #include "dme_controls/dmepicker.h"
  16. #include "movieobjects/dmeeditortypedictionary.h"
  17. #include "dme_controls/inotifyui.h"
  18. #include "dme_controls/dmecontrols.h"
  19. #include "dme_controls/dmecontrols_utils.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. using namespace vgui;
  23. // ----------------------------------------------------------------------------
  24. CAttributeElementPickerPanel::CAttributeElementPickerPanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  25. BaseClass( parent, info )
  26. {
  27. m_hEdit = new vgui::Button( this, "Open", "...", this, "open" );
  28. m_pData = new CAttributeTextEntry( this, "AttributeValue" );
  29. m_pData->SetEnabled( !HasFlag( READONLY ) );
  30. m_pData->AddActionSignalTarget(this);
  31. m_pType->SetText( "element" );
  32. m_bShowMemoryUsage = info.m_bShowMemoryUsage;
  33. }
  34. void CAttributeElementPickerPanel::PostConstructor()
  35. {
  36. Refresh();
  37. }
  38. // ----------------------------------------------------------------------------
  39. vgui::Panel *CAttributeElementPickerPanel::GetDataPanel()
  40. {
  41. return static_cast< vgui::Panel * >( m_pData );
  42. }
  43. void CAttributeElementPickerPanel::Apply()
  44. {
  45. // FIXME: Implement when needed
  46. Assert( 0 );
  47. }
  48. void CAttributeElementPickerPanel::Refresh()
  49. {
  50. char elemText[ 512 ];
  51. elemText[0] = 0;
  52. CDmElement *element = NULL;
  53. if ( !GetEditorInfo() || !GetEditorInfo()->GetValue<bool>( "hideText" ) )
  54. {
  55. if ( HasAttribute( ) )
  56. {
  57. element = GetAttributeValueElement( );
  58. }
  59. else
  60. {
  61. element = GetPanelElement();
  62. }
  63. }
  64. if ( element )
  65. {
  66. char idstr[ 37 ];
  67. UniqueIdToString( element->GetId(), idstr, sizeof( idstr ) );
  68. if ( m_bShowMemoryUsage )
  69. {
  70. Q_snprintf( elemText, sizeof( elemText ), "%s %s %.3fMB", element->GetTypeString(), idstr, element->EstimateMemoryUsage() / float( 1 << 20 ) );
  71. }
  72. else
  73. {
  74. Q_snprintf( elemText, sizeof( elemText ), "%s %s", element->GetTypeString(), idstr );
  75. }
  76. }
  77. m_pData->SetText( elemText );
  78. m_pData->SetEditable( false );
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Called when it's time to show the Dme picker
  82. //-----------------------------------------------------------------------------
  83. void CAttributeElementPickerPanel::ShowPickerDialog()
  84. {
  85. CDmeEditorChoicesInfo *pInfo = CastElement<CDmeEditorChoicesInfo>( GetEditorInfo() );
  86. if ( !pInfo )
  87. return;
  88. // FIXME: Sucky. Should we make GetElementChoiceList return a DmeHandleVec_t?
  89. ElementChoiceList_t choices;
  90. CUtlVector< DmePickerInfo_t > vec;
  91. if ( ElementPropertiesChoices()->GetElementChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  92. {
  93. int c = choices.Count();
  94. vec.EnsureCapacity( c );
  95. for ( int i = 0; i < c; ++i )
  96. {
  97. int j = vec.AddToTail( );
  98. vec[j].m_hElement = choices[i].m_pValue->GetHandle();
  99. vec[j].m_pChoiceString = choices[i].m_pChoiceString;
  100. }
  101. }
  102. CDmePickerFrame *pDmePickerDialog = new CDmePickerFrame( this, "Select DME Element" );
  103. pDmePickerDialog->AddActionSignalTarget( this );
  104. pDmePickerDialog->DoModal( vec );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Called by the dme picker dialog if a dme was selected
  108. //-----------------------------------------------------------------------------
  109. void CAttributeElementPickerPanel::OnDmeSelected( KeyValues *pKeyValues )
  110. {
  111. // We're either going to get an activity or sequence name
  112. CDmElement *pElement = GetElementKeyValue< CDmElement >( pKeyValues, "dme" );
  113. SetAttributeValueElement( pElement );
  114. Refresh( );
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Handle commands
  118. //-----------------------------------------------------------------------------
  119. void CAttributeElementPickerPanel::OnCommand( char const *cmd )
  120. {
  121. if ( !Q_stricmp( cmd, "open" ) )
  122. {
  123. ShowPickerDialog();
  124. }
  125. else
  126. {
  127. BaseClass::OnCommand( cmd );
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Lay out the panel
  132. //-----------------------------------------------------------------------------
  133. void CAttributeElementPickerPanel::PerformLayout()
  134. {
  135. BaseClass::PerformLayout();
  136. int x, y, w, h;
  137. m_pType->GetBounds( x, y, w, h );
  138. int inset = 25;
  139. m_pType->SetWide( w - inset );
  140. x += w;
  141. x -= inset;
  142. h -= 2;
  143. m_hEdit->SetBounds( x, y, inset, h );
  144. }