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.

162 lines
5.2 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "dme_controls/AttributeBoolChoicePanel.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( DmeEditorBoolChoicesInfo, CDmeEditorBoolChoicesInfo );
  23. //-----------------------------------------------------------------------------
  24. // Constructor, destructor
  25. //-----------------------------------------------------------------------------
  26. void CDmeEditorBoolChoicesInfo::OnConstruction()
  27. {
  28. // Add a true + false method, default
  29. CreateChoice( "false" );
  30. CreateChoice( "true" );
  31. }
  32. void CDmeEditorBoolChoicesInfo::OnDestruction()
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Add a choice
  37. //-----------------------------------------------------------------------------
  38. void CDmeEditorBoolChoicesInfo::SetFalseChoice( const char *pChoiceString )
  39. {
  40. //m_Choices[0]->SetValue<CUtlString>( "string", pChoiceString );
  41. CUtlSymbolLarge symbol = g_pDataModel->GetSymbol( pChoiceString );
  42. m_Choices[0]->SetValue<CUtlSymbolLarge>( "string", symbol );
  43. }
  44. void CDmeEditorBoolChoicesInfo::SetTrueChoice( const char *pChoiceString )
  45. {
  46. CUtlSymbolLarge symbol = g_pDataModel->GetSymbol( pChoiceString );
  47. m_Choices[0]->SetValue<CUtlSymbolLarge>( "string", symbol );
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Gets the choices
  51. //-----------------------------------------------------------------------------
  52. const char *CDmeEditorBoolChoicesInfo::GetFalseChoiceString( ) const
  53. {
  54. return GetChoiceString( 0 );
  55. }
  56. const char *CDmeEditorBoolChoicesInfo::GetTrueChoiceString( ) const
  57. {
  58. return GetChoiceString( 1 );
  59. }
  60. //-----------------------------------------------------------------------------
  61. //
  62. // Constructor
  63. //
  64. //-----------------------------------------------------------------------------
  65. CAttributeBoolChoicePanel::CAttributeBoolChoicePanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  66. BaseClass( parent, info )
  67. {
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Derived classes can re-implement this to fill the combo box however they like
  71. //-----------------------------------------------------------------------------
  72. void CAttributeBoolChoicePanel::PopulateComboBox( vgui::ComboBox *pComboBox )
  73. {
  74. pComboBox->DeleteAllItems();
  75. CDmeEditorBoolChoicesInfo *pInfo = CastElement<CDmeEditorBoolChoicesInfo>( GetEditorInfo() );
  76. if ( !pInfo )
  77. return;
  78. // Fill in the choices
  79. const char *pFalseChoice = pInfo->GetFalseChoiceString( );
  80. const char *pTrueChoice = pInfo->GetTrueChoiceString( );
  81. // Add the dynamic choices next
  82. if ( pInfo->HasChoiceType() )
  83. {
  84. const char *choices[2];
  85. if ( ElementPropertiesChoices()->GetBoolChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  86. {
  87. pFalseChoice = choices[0];
  88. pTrueChoice = choices[1];
  89. }
  90. }
  91. KeyValues *kv = new KeyValues( "entry" );
  92. kv->SetInt( "value", false );
  93. pComboBox->AddItem( pFalseChoice, kv );
  94. kv = new KeyValues( "entry" );
  95. kv->SetInt( "value", true );
  96. pComboBox->AddItem( pTrueChoice, kv );
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Sets the attribute based on the combo box
  100. //-----------------------------------------------------------------------------
  101. void CAttributeBoolChoicePanel::SetAttributeFromComboBox( vgui::ComboBox *pComboBox, KeyValues *pKeyValues )
  102. {
  103. bool bOldValue = GetAttributeValue<bool>();
  104. bool bValue = pKeyValues->GetInt( "value", 0 ) != 0;
  105. if ( bOldValue == bValue )
  106. return;
  107. CUndoScopeGuard guard( NOTIFY_SOURCE_PROPERTIES_TREE, NOTIFY_SETDIRTYFLAG, "Set Attribute Value", "Set Attribute Value" );
  108. SetAttributeValue( bValue );
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Sets the combo box from the attribute
  112. //-----------------------------------------------------------------------------
  113. void CAttributeBoolChoicePanel::SetComboBoxFromAttribute( vgui::ComboBox *pComboBox )
  114. {
  115. CDmeEditorBoolChoicesInfo *pInfo = CastElement<CDmeEditorBoolChoicesInfo>( GetEditorInfo() );
  116. if ( !pInfo )
  117. return;
  118. bool bValue = GetAttributeValue<bool>();
  119. // Check the dynamic choices next
  120. if ( pInfo->HasChoiceType() )
  121. {
  122. const char *choices[2];
  123. if ( ElementPropertiesChoices()->GetBoolChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  124. {
  125. pComboBox->SetText( choices[ bValue ] );
  126. return;
  127. }
  128. }
  129. pComboBox->SetText( pInfo->GetChoiceString( bValue ) );
  130. }