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.

159 lines
5.0 KiB

  1. //========= Copyright 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. }
  42. void CDmeEditorBoolChoicesInfo::SetTrueChoice( const char *pChoiceString )
  43. {
  44. m_Choices[1]->SetValue<CUtlString>( "string", pChoiceString );
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Gets the choices
  48. //-----------------------------------------------------------------------------
  49. const char *CDmeEditorBoolChoicesInfo::GetFalseChoiceString( ) const
  50. {
  51. return GetChoiceString( 0 );
  52. }
  53. const char *CDmeEditorBoolChoicesInfo::GetTrueChoiceString( ) const
  54. {
  55. return GetChoiceString( 1 );
  56. }
  57. //-----------------------------------------------------------------------------
  58. //
  59. // Constructor
  60. //
  61. //-----------------------------------------------------------------------------
  62. CAttributeBoolChoicePanel::CAttributeBoolChoicePanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  63. BaseClass( parent, info )
  64. {
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Derived classes can re-implement this to fill the combo box however they like
  68. //-----------------------------------------------------------------------------
  69. void CAttributeBoolChoicePanel::PopulateComboBox( vgui::ComboBox *pComboBox )
  70. {
  71. pComboBox->DeleteAllItems();
  72. CDmeEditorBoolChoicesInfo *pInfo = CastElement<CDmeEditorBoolChoicesInfo>( GetEditorInfo() );
  73. if ( !pInfo )
  74. return;
  75. // Fill in the choices
  76. const char *pFalseChoice = pInfo->GetFalseChoiceString( );
  77. const char *pTrueChoice = pInfo->GetTrueChoiceString( );
  78. // Add the dynamic choices next
  79. if ( pInfo->HasChoiceType() )
  80. {
  81. const char *choices[2];
  82. if ( ElementPropertiesChoices()->GetBoolChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  83. {
  84. pFalseChoice = choices[0];
  85. pTrueChoice = choices[1];
  86. }
  87. }
  88. KeyValues *kv = new KeyValues( "entry" );
  89. kv->SetInt( "value", false );
  90. pComboBox->AddItem( pFalseChoice, kv );
  91. kv = new KeyValues( "entry" );
  92. kv->SetInt( "value", true );
  93. pComboBox->AddItem( pTrueChoice, kv );
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Sets the attribute based on the combo box
  97. //-----------------------------------------------------------------------------
  98. void CAttributeBoolChoicePanel::SetAttributeFromComboBox( vgui::ComboBox *pComboBox, KeyValues *pKeyValues )
  99. {
  100. bool bOldValue = GetAttributeValue<bool>();
  101. bool bValue = pKeyValues->GetInt( "value", 0 ) != 0;
  102. if ( bOldValue == bValue )
  103. return;
  104. CUndoScopeGuard guard( NOTIFY_SOURCE_PROPERTIES_TREE, NOTIFY_SETDIRTYFLAG, "Set Attribute Value", "Set Attribute Value" );
  105. SetAttributeValue( bValue );
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Sets the combo box from the attribute
  109. //-----------------------------------------------------------------------------
  110. void CAttributeBoolChoicePanel::SetComboBoxFromAttribute( vgui::ComboBox *pComboBox )
  111. {
  112. CDmeEditorBoolChoicesInfo *pInfo = CastElement<CDmeEditorBoolChoicesInfo>( GetEditorInfo() );
  113. if ( !pInfo )
  114. return;
  115. bool bValue = GetAttributeValue<bool>();
  116. // Check the dynamic choices next
  117. if ( pInfo->HasChoiceType() )
  118. {
  119. const char *choices[2];
  120. if ( ElementPropertiesChoices()->GetBoolChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
  121. {
  122. pComboBox->SetText( choices[ bValue ] );
  123. return;
  124. }
  125. }
  126. pComboBox->SetText( pInfo->GetChoiceString( bValue ) );
  127. }