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.

174 lines
5.5 KiB

  1. //====== Copyright � 1996-2005, 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( "value", pChoiceString );
  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. CUtlSymbolLarge symbol = pChoice->GetValue< CUtlSymbolLarge >( "value" );
  50. if ( symbol == UTL_INVAL_SYMBOL_LARGE )
  51. return NULL;
  52. return symbol.String();
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Constructor
  56. //-----------------------------------------------------------------------------
  57. CAttributeStringChoicePanel::CAttributeStringChoicePanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  58. BaseClass( parent, info )
  59. {
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Derived classes can re-implement this to fill the combo box however they like
  63. //-----------------------------------------------------------------------------
  64. void CAttributeStringChoicePanel::PopulateComboBox( vgui::ComboBox *pComboBox )
  65. {
  66. pComboBox->DeleteAllItems();
  67. CDmeEditorStringChoicesInfo *pInfo = CastElement<CDmeEditorStringChoicesInfo>( GetEditorInfo() );
  68. if ( !pInfo )
  69. return;
  70. // Fill in the standard choices first
  71. int c = pInfo->GetChoiceCount();
  72. for ( int i = 0; i < c; ++i )
  73. {
  74. KeyValues *kv = new KeyValues( "entry" );
  75. kv->SetString( "value", pInfo->GetChoiceValue( i ) );
  76. pComboBox->AddItem( pInfo->GetChoiceString( i ) , kv );
  77. }
  78. // Add the dynamic choices next
  79. if ( pInfo->HasChoiceType() )
  80. {
  81. StringChoiceList_t choices;
  82. if ( ElementPropertiesChoices()->GetStringChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  83. {
  84. c = choices.Count();
  85. for ( int i = 0; i < c; ++i )
  86. {
  87. KeyValues *kv = new KeyValues( "entry" );
  88. kv->SetString( "value", choices[i].m_pValue );
  89. pComboBox->AddItem( choices[i].m_pChoiceString, kv );
  90. }
  91. }
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Sets the attribute based on the combo box
  96. //-----------------------------------------------------------------------------
  97. void CAttributeStringChoicePanel::SetAttributeFromComboBox( vgui::ComboBox *pComboBox, KeyValues *pKeyValues )
  98. {
  99. CUtlSymbolLarge oldSymbol = GetAttributeValue<CUtlSymbolLarge>();
  100. const char *pOldString = oldSymbol.String();
  101. const char *pNewString = pKeyValues->GetString( "value", "" );
  102. if ( pOldString == pNewString )
  103. return;
  104. CElementTreeUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, GetNotify(), "Set Attribute Value", "Set Attribute Value" );
  105. SetAttributeValue( pNewString );
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Sets the combo box from the attribute
  109. //-----------------------------------------------------------------------------
  110. void CAttributeStringChoicePanel::SetComboBoxFromAttribute( vgui::ComboBox *pComboBox )
  111. {
  112. CDmeEditorStringChoicesInfo *pInfo = CastElement<CDmeEditorStringChoicesInfo>( GetEditorInfo() );
  113. if ( !pInfo )
  114. return;
  115. CUtlSymbolLarge symbol = GetAttributeValue<CUtlSymbolLarge>();
  116. const char *pValue = symbol.String();
  117. int c = pInfo->GetChoiceCount();
  118. for ( int i = 0; i < c; ++i )
  119. {
  120. if ( !Q_stricmp( pValue, pInfo->GetChoiceValue( i ) ) )
  121. {
  122. pComboBox->SetText( pInfo->GetChoiceString( i ) );
  123. return;
  124. }
  125. }
  126. // Check the dynamic choices next
  127. if ( pInfo->HasChoiceType() )
  128. {
  129. StringChoiceList_t choices;
  130. if ( ElementPropertiesChoices()->GetStringChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  131. {
  132. c = choices.Count();
  133. for ( int i = 0; i < c; ++i )
  134. {
  135. if ( !Q_stricmp( pValue, choices[i].m_pValue ) )
  136. {
  137. pComboBox->SetText( choices[i].m_pChoiceString );
  138. return;
  139. }
  140. }
  141. }
  142. }
  143. pComboBox->SetText( "Unknown value" );
  144. }