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.

239 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "gameeventeditpanel.h"
  7. #include "tier1/KeyValues.h"
  8. #include "tier1/utlbuffer.h"
  9. #include "iregistry.h"
  10. #include "vgui/ivgui.h"
  11. #include "vgui_controls/listpanel.h"
  12. #include "vgui_controls/textentry.h"
  13. #include "vgui_controls/checkbutton.h"
  14. #include "vgui_controls/combobox.h"
  15. #include "vgui_controls/radiobutton.h"
  16. #include "vgui_controls/messagebox.h"
  17. #include "vgui_controls/scrollbar.h"
  18. #include "vgui_controls/scrollableeditablepanel.h"
  19. #include "datamodel/dmelement.h"
  20. #include "matsys_controls/picker.h"
  21. #include "vgui_controls/fileopendialog.h"
  22. #include "filesystem.h"
  23. #include "tier2/fileutils.h"
  24. #include "igameevents.h"
  25. #include "toolutils/enginetools_int.h"
  26. // memdbgon must be the last include file in a .cpp file!!!
  27. #include <tier0/memdbgon.h>
  28. using namespace vgui;
  29. char *VarArgs( PRINTF_FORMAT_STRING const char *format, ... )
  30. {
  31. va_list argptr;
  32. static char string[1024];
  33. va_start (argptr, format);
  34. Q_vsnprintf (string, sizeof( string ), format,argptr);
  35. va_end (argptr);
  36. return string;
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Constructor
  40. //-----------------------------------------------------------------------------
  41. CGameEventEditPanel::CGameEventEditPanel( CGameEventEditDoc *pDoc, vgui::Panel* pParent )
  42. : BaseClass( pParent, "GameEventEditPanel" ), m_pDoc( pDoc )
  43. {
  44. SetPaintBackgroundEnabled( true );
  45. SetKeyBoardInputEnabled( true );
  46. m_pEvents = new KeyValues( "events" );
  47. m_EventFiles.RemoveAll();
  48. m_EventFileNames.RemoveAll();
  49. // load the game events
  50. LoadEventsFromFile( "resource/GameEvents.res" );
  51. LoadEventsFromFile( "resource/ModEvents.res" );
  52. m_pEventCombo = new vgui::ComboBox( this, "EventComboBox", 30, false );
  53. m_pEventCombo->SetNumberOfEditLines( 30 );
  54. KeyValues *subkey = m_pEvents->GetFirstSubKey();
  55. while ( subkey )
  56. {
  57. m_pEventCombo->AddItem( subkey->GetName(), subkey );
  58. subkey = subkey->GetNextKey();
  59. }
  60. if ( m_pEventCombo->GetItemCount() > 0 )
  61. {
  62. m_pEventCombo->ActivateItemByRow( 0 );
  63. }
  64. for ( int i=0;i<MAX_GAME_EVENT_PARAMS;i++ )
  65. {
  66. m_pParamLabels[i] = new vgui::Label( this, VarArgs( "ParamLabel%d", i+1 ), VarArgs( "event param %d:", i+1 ) );
  67. m_pParamLabels[i]->AddActionSignalTarget( this );
  68. m_pParams[i] = new vgui::TextEntry( this, VarArgs( "Param%d", i+1 ) );
  69. m_pParams[i]->AddActionSignalTarget( this );
  70. }
  71. m_pSendEventButton = new vgui::Button( this, "SendEvent", "", this, "SendEvent" );
  72. m_pFilterBox = new vgui::TextEntry( this, "FilterBox" );
  73. LoadControlSettings( "resource/gameeventeditpanel.res" );
  74. }
  75. CGameEventEditPanel::~CGameEventEditPanel()
  76. {
  77. m_pEvents->deleteThis();
  78. }
  79. void CGameEventEditPanel::OnTextChanged( KeyValues *params )
  80. {
  81. Panel *panel = reinterpret_cast<vgui::Panel *>( params->GetPtr("panel") );
  82. if ( panel == m_pFilterBox )
  83. {
  84. // repopulate the list based on the filter substr
  85. char filter[128];
  86. m_pFilterBox->GetText( filter, sizeof(filter) );
  87. int len = Q_strlen(filter);
  88. m_pEventCombo->RemoveAll();
  89. KeyValues *subkey = m_pEvents->GetFirstSubKey();
  90. while ( subkey )
  91. {
  92. if ( len == 0 || Q_strstr( subkey->GetName(), filter ) )
  93. {
  94. m_pEventCombo->AddItem( subkey->GetName(), subkey );
  95. }
  96. subkey = subkey->GetNextKey();
  97. }
  98. if ( m_pEventCombo->GetItemCount() > 0 )
  99. {
  100. m_pEventCombo->ActivateItemByRow( 0 );
  101. }
  102. }
  103. if ( panel == m_pEventCombo )
  104. {
  105. Msg( "%s", params->GetName() );
  106. KeyValues *kv = m_pEventCombo->GetActiveItemUserData();
  107. int i = 0;
  108. if ( kv )
  109. {
  110. KeyValues *subkey = kv->GetFirstSubKey();
  111. while ( subkey && i < MAX_GAME_EVENT_PARAMS )
  112. {
  113. Msg( subkey->GetName() );
  114. char buf[128];
  115. Q_snprintf( buf, sizeof(buf), "%s (%s)", subkey->GetName(), subkey->GetString() );
  116. m_pParamLabels[i]->SetText( buf );
  117. m_pParamLabels[i]->SetVisible( true );
  118. const char *type = subkey->GetString();
  119. if ( !Q_strcmp( type, "string" ) )
  120. {
  121. m_pParams[i]->SetAllowNumericInputOnly( false );
  122. }
  123. else
  124. {
  125. m_pParams[i]->SetAllowNumericInputOnly( true );
  126. }
  127. m_pParams[i]->SetText( "" );
  128. m_pParams[i]->SetVisible( true );
  129. subkey = subkey->GetNextKey();
  130. i++;
  131. }
  132. while( i < MAX_GAME_EVENT_PARAMS )
  133. {
  134. m_pParamLabels[i]->SetVisible( false );
  135. m_pParams[i]->SetVisible( false );
  136. i++;
  137. }
  138. }
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. // Called when buttons are clicked
  143. //-----------------------------------------------------------------------------
  144. void CGameEventEditPanel::OnCommand( const char *pCommand )
  145. {
  146. if ( !Q_stricmp( pCommand, "SendEvent" ) )
  147. {
  148. KeyValues *pData = m_pEventCombo->GetActiveItemUserData();
  149. if ( pData )
  150. {
  151. const char *pEventName = pData->GetName();
  152. IGameEvent *event = gameeventmanager->CreateEvent( pEventName );
  153. if ( event )
  154. {
  155. KeyValues *subkey = pData->GetFirstSubKey();
  156. int i = 0;
  157. while( subkey && i < MAX_GAME_EVENT_PARAMS )
  158. {
  159. char text[128];
  160. m_pParams[i]->GetText( text, sizeof(text) );
  161. event->SetString( subkey->GetName(), text );
  162. subkey = subkey->GetNextKey();
  163. i++;
  164. }
  165. gameeventmanager->FireEvent( event );
  166. }
  167. }
  168. return;
  169. }
  170. BaseClass::OnCommand( pCommand );
  171. }
  172. void CGameEventEditPanel::LoadEventsFromFile( const char *filename )
  173. {
  174. if ( UTL_INVAL_SYMBOL == m_EventFiles.Find( filename ) )
  175. {
  176. CUtlSymbol id = m_EventFiles.AddString( filename );
  177. m_EventFileNames.AddToTail( id );
  178. }
  179. KeyValues * key = new KeyValues(filename);
  180. KeyValues::AutoDelete autodelete_key( key );
  181. if ( !key->LoadFromFile( g_pFileSystem, filename, "GAME" ) )
  182. return;
  183. KeyValues *subkey = key->GetFirstSubKey();
  184. while ( subkey )
  185. {
  186. KeyValues *copy = subkey->MakeCopy();
  187. m_pEvents->AddSubKey( copy );
  188. subkey = subkey->GetNextKey();
  189. }
  190. }