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.

133 lines
3.7 KiB

  1. //====== Copyright � 1996-2005, 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 "movieobjects/dmechannel.h"
  15. #include "dme_controls/inotifyui.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. using namespace vgui;
  19. //-----------------------------------------------------------------------------
  20. // CAttributeTextPanel constructor
  21. //-----------------------------------------------------------------------------
  22. CAttributeTextPanel::CAttributeTextPanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  23. BaseClass( parent, info ), m_pData( 0 ), m_bShowMemoryUsage( info.m_bShowMemoryUsage )
  24. {
  25. m_pData = new CAttributeTextEntry( this, "AttributeValue" );
  26. m_pData->SetEnabled( !HasFlag( READONLY ) && FindChannelTargetingAttribute( GetAttribute() ) == NULL );
  27. m_pData->AddActionSignalTarget(this);
  28. SetAllowKeyBindingChainToParent( false );
  29. }
  30. void CAttributeTextPanel::SetFont( HFont font )
  31. {
  32. BaseClass::SetFont( font );
  33. m_pData->SetFont( font );
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Returns the text type
  37. //-----------------------------------------------------------------------------
  38. const char *CAttributeTextPanel::GetTextType()
  39. {
  40. // If a specific text type is specified, then filter if it doesn't match
  41. CDmeEditorAttributeInfo *pInfo = GetEditorInfo();
  42. const char *pTextType = pInfo ? pInfo->GetValueString( "texttype" ) : NULL;
  43. return pTextType ? pTextType : "";
  44. }
  45. void CAttributeTextPanel::Apply()
  46. {
  47. char txt[ 256 ];
  48. m_pData->GetText( txt, sizeof( txt ) );
  49. // Apply means we no longer look blue
  50. SetDirty( false );
  51. if ( GetAttributeType( ) == AT_UNKNOWN )
  52. {
  53. CElementTreeUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, GetNotify(), "Set Attribute Value", "Set Attribute Value" );
  54. SetAttributeValue( "" );
  55. return;
  56. }
  57. char curvalue[ 256 ];
  58. GetAttributeValueAsString( curvalue, sizeof( curvalue ) );
  59. // Only if differnt
  60. if ( Q_strcmp( curvalue, txt ) )
  61. {
  62. CElementTreeUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, GetNotify(), "Set Attribute Value", "Set Attribute Value" );
  63. SetAttributeValueFromString( txt );
  64. }
  65. }
  66. vgui::Panel *CAttributeTextPanel::GetDataPanel()
  67. {
  68. return static_cast< vgui::Panel * >( m_pData );
  69. }
  70. void CAttributeTextPanel::Refresh()
  71. {
  72. char buf[ 512 ];
  73. if ( IsArrayType( GetAttributeType() ) )
  74. {
  75. int count = GetAttributeArrayCount();
  76. if ( m_bShowMemoryUsage )
  77. {
  78. CDmAttribute *pAttr = GetPanelElement()->GetAttribute( GetAttributeName() );
  79. Q_snprintf( buf, sizeof( buf ), "%d %s (%.3fMB)", count, (count == 1) ? "item" : "items", pAttr->EstimateMemoryUsage( TD_DEEP ) / float( 1 << 20 ) );
  80. }
  81. else
  82. {
  83. Q_snprintf( buf, sizeof( buf ), "%d %s", count, (count == 1) ? "item" : "items" );
  84. }
  85. m_pData->SetText( buf );
  86. m_pData->SetEnabled(false);
  87. }
  88. else if ( GetAttributeType() == AT_ELEMENT )
  89. {
  90. m_pData->SetText( "" );
  91. }
  92. else
  93. {
  94. GetAttributeValueAsString( buf, sizeof( buf ) );
  95. // This is a hack because VMatrix has \n characters in the text and the TextEntry field doesn't show them.
  96. if ( GetAttributeType() == AT_VMATRIX )
  97. {
  98. // Replace \n with ' '
  99. char *p = buf;
  100. while ( *p )
  101. {
  102. if ( *p == '\n' )
  103. *p = ' ';
  104. ++p;
  105. }
  106. }
  107. m_pData->SetText( buf );
  108. m_pData->SetEnabled( !HasFlag( READONLY ) && FindChannelTargetingAttribute( GetAttribute() ) == NULL );
  109. }
  110. }
  111. void CAttributeTextPanel::PostConstructor()
  112. {
  113. Refresh();
  114. }