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.

169 lines
5.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "dme_controls/AttributeStringChoicePanel.h"
  9. #include "tier1/KeyValues.h"
  10. #include "vgui_controls/ComboBox.h"
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmelementfactoryhelper.h"
  13. #include "dme_controls/inotifyui.h"
  14. #include "dme_controls/dmecontrols.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. using namespace vgui;
  18. //-----------------------------------------------------------------------------
  19. // Expose DmeEditorAttributeInfo to the scene database
  20. //-----------------------------------------------------------------------------
  21. IMPLEMENT_ELEMENT_FACTORY( DmeEditorStringChoicesInfo, CDmeEditorStringChoicesInfo );
  22. //-----------------------------------------------------------------------------
  23. // Constructor, destructor
  24. //-----------------------------------------------------------------------------
  25. void CDmeEditorStringChoicesInfo::OnConstruction()
  26. {
  27. }
  28. void CDmeEditorStringChoicesInfo::OnDestruction()
  29. {
  30. }
  31. //-----------------------------------------------------------------------------
  32. // Add a choice
  33. //-----------------------------------------------------------------------------
  34. CDmElement *CDmeEditorStringChoicesInfo::AddChoice( const char *pValueString, const char *pChoiceString )
  35. {
  36. CDmElement *pChoice = CreateChoice( pChoiceString );
  37. pChoice->SetValue<CUtlString>( "value", pValueString );
  38. return pChoice;
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Gets the choices
  42. //-----------------------------------------------------------------------------
  43. const char *CDmeEditorStringChoicesInfo::GetChoiceValue( int nIndex ) const
  44. {
  45. Assert( ( nIndex < GetChoiceCount() ) && ( nIndex >= 0 ) );
  46. CDmElement *pChoice = m_Choices[nIndex];
  47. if ( !pChoice )
  48. return 0;
  49. return pChoice->GetValue<CUtlString>( "value" );
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Constructor
  53. //-----------------------------------------------------------------------------
  54. CAttributeStringChoicePanel::CAttributeStringChoicePanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  55. BaseClass( parent, info )
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Derived classes can re-implement this to fill the combo box however they like
  60. //-----------------------------------------------------------------------------
  61. void CAttributeStringChoicePanel::PopulateComboBox( vgui::ComboBox *pComboBox )
  62. {
  63. pComboBox->DeleteAllItems();
  64. CDmeEditorStringChoicesInfo *pInfo = CastElement<CDmeEditorStringChoicesInfo>( GetEditorInfo() );
  65. if ( !pInfo )
  66. return;
  67. // Fill in the standard choices first
  68. int c = pInfo->GetChoiceCount();
  69. for ( int i = 0; i < c; ++i )
  70. {
  71. KeyValues *kv = new KeyValues( "entry" );
  72. kv->SetString( "value", pInfo->GetChoiceValue( i ) );
  73. pComboBox->AddItem( pInfo->GetChoiceString( i ) , kv );
  74. }
  75. // Add the dynamic choices next
  76. if ( pInfo->HasChoiceType() )
  77. {
  78. StringChoiceList_t choices;
  79. if ( ElementPropertiesChoices()->GetStringChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  80. {
  81. c = choices.Count();
  82. for ( int i = 0; i < c; ++i )
  83. {
  84. KeyValues *kv = new KeyValues( "entry" );
  85. kv->SetString( "value", choices[i].m_pValue );
  86. pComboBox->AddItem( choices[i].m_pChoiceString, kv );
  87. }
  88. }
  89. }
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Sets the attribute based on the combo box
  93. //-----------------------------------------------------------------------------
  94. void CAttributeStringChoicePanel::SetAttributeFromComboBox( vgui::ComboBox *pComboBox, KeyValues *pKeyValues )
  95. {
  96. const char *pOldString = GetAttributeValue<CUtlString>();
  97. const char *pNewString = pKeyValues->GetString( "value", "" );
  98. if ( pOldString == pNewString )
  99. return;
  100. CElementTreeUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, GetNotify(), "Set Attribute Value", "Set Attribute Value" );
  101. SetAttributeValue( pNewString );
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Sets the combo box from the attribute
  105. //-----------------------------------------------------------------------------
  106. void CAttributeStringChoicePanel::SetComboBoxFromAttribute( vgui::ComboBox *pComboBox )
  107. {
  108. CDmeEditorStringChoicesInfo *pInfo = CastElement<CDmeEditorStringChoicesInfo>( GetEditorInfo() );
  109. if ( !pInfo )
  110. return;
  111. const char *pValue = GetAttributeValue<CUtlString>();
  112. int c = pInfo->GetChoiceCount();
  113. for ( int i = 0; i < c; ++i )
  114. {
  115. if ( !Q_stricmp( pValue, pInfo->GetChoiceValue( i ) ) )
  116. {
  117. pComboBox->SetText( pInfo->GetChoiceString( i ) );
  118. return;
  119. }
  120. }
  121. // Check the dynamic choices next
  122. if ( pInfo->HasChoiceType() )
  123. {
  124. StringChoiceList_t choices;
  125. if ( ElementPropertiesChoices()->GetStringChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  126. {
  127. c = choices.Count();
  128. for ( int i = 0; i < c; ++i )
  129. {
  130. if ( !Q_stricmp( pValue, choices[i].m_pValue ) )
  131. {
  132. pComboBox->SetText( choices[i].m_pChoiceString );
  133. return;
  134. }
  135. }
  136. }
  137. }
  138. pComboBox->SetText( "Unknown value" );
  139. }