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.

119 lines
3.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "dme_controls/AttributeTextPanel.h"
  9. #include "dme_controls/AttributeTextEntry.h"
  10. #include "dme_controls/AttributeWidgetFactory.h"
  11. #include "tier1/KeyValues.h"
  12. #include "datamodel/dmelement.h"
  13. #include "movieobjects/dmeeditortypedictionary.h"
  14. #include "dme_controls/inotifyui.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. using namespace vgui;
  18. //-----------------------------------------------------------------------------
  19. // CAttributeTextPanel constructor
  20. //-----------------------------------------------------------------------------
  21. CAttributeTextPanel::CAttributeTextPanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  22. BaseClass( parent, info ), m_pData( 0 ), m_bShowMemoryUsage( info.m_bShowMemoryUsage )
  23. {
  24. m_pData = new CAttributeTextEntry( this, "AttributeValue" );
  25. m_pData->SetEnabled( !HasFlag( READONLY ) );
  26. m_pData->AddActionSignalTarget(this);
  27. SetAllowKeyBindingChainToParent( false );
  28. }
  29. void CAttributeTextPanel::SetFont( HFont font )
  30. {
  31. BaseClass::SetFont( font );
  32. m_pData->SetFont( font );
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Returns the text type
  36. //-----------------------------------------------------------------------------
  37. const char *CAttributeTextPanel::GetTextType()
  38. {
  39. // If a specific text type is specified, then filter if it doesn't match
  40. CDmeEditorAttributeInfo *pInfo = GetEditorInfo();
  41. const char *pTextType = pInfo ? pInfo->GetValueString( "texttype" ) : NULL;
  42. return pTextType ? pTextType : "";
  43. }
  44. void CAttributeTextPanel::Apply()
  45. {
  46. char txt[ 256 ];
  47. m_pData->GetText( txt, sizeof( txt ) );
  48. // Apply means we no longer look blue
  49. SetDirty( false );
  50. if ( GetAttributeType( ) == AT_UNKNOWN )
  51. {
  52. CElementTreeUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, GetNotify(), "Set Attribute Value", "Set Attribute Value" );
  53. SetAttributeValue( "" );
  54. return;
  55. }
  56. char curvalue[ 256 ];
  57. GetAttributeValueAsString( curvalue, sizeof( curvalue ) );
  58. // Only if differnt
  59. if ( Q_strcmp( curvalue, txt ) )
  60. {
  61. CElementTreeUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, GetNotify(), "Set Attribute Value", "Set Attribute Value" );
  62. SetAttributeValueFromString( txt );
  63. }
  64. }
  65. vgui::Panel *CAttributeTextPanel::GetDataPanel()
  66. {
  67. return static_cast< vgui::Panel * >( m_pData );
  68. }
  69. void CAttributeTextPanel::Refresh()
  70. {
  71. char buf[ 512 ];
  72. if ( IsArrayType( GetAttributeType() ) )
  73. {
  74. int count = GetAttributeArrayCount();
  75. if ( m_bShowMemoryUsage )
  76. {
  77. CDmAttribute *pAttr = GetPanelElement()->GetAttribute( GetAttributeName() );
  78. Q_snprintf( buf, sizeof( buf ), "%d items %.3fMB", count, pAttr->EstimateMemoryUsage( TD_DEEP ) / float( 1 << 20 ) );
  79. }
  80. else
  81. {
  82. Q_snprintf( buf, sizeof( buf ), "%d items", count );
  83. }
  84. m_pData->SetText( buf );
  85. m_pData->SetEnabled(false);
  86. }
  87. else if ( GetAttributeType() == AT_ELEMENT )
  88. {
  89. m_pData->SetText( "" );
  90. }
  91. else
  92. {
  93. GetAttributeValueAsString( buf, sizeof( buf ) );
  94. m_pData->SetText( buf );
  95. }
  96. }
  97. void CAttributeTextPanel::PostConstructor()
  98. {
  99. Refresh();
  100. }