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.

170 lines
5.1 KiB

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