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.

382 lines
9.1 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "client_pch.h"
  8. #include "ivideomode.h"
  9. #include "vgui_DebugSystemPanel.h"
  10. #include <vgui/ISurface.h>
  11. #include <vgui_controls/Controls.h>
  12. #include <vgui_controls/MenuButton.h>
  13. #include <vgui_controls/Menu.h>
  14. #include <vgui_controls/MenuItem.h>
  15. #include <vgui/Cursor.h>
  16. #include <vgui_controls/TreeView.h>
  17. #include <vgui_controls/ImageList.h>
  18. #include <vgui/IScheme.h>
  19. #include <vgui/IVGui.h>
  20. #include <vgui_controls/Frame.h>
  21. #include <vgui_controls/CheckButton.h>
  22. #include <vgui_controls/PropertyPage.h>
  23. #include <vgui_controls/PropertyDialog.h>
  24. #include <vgui_controls/PropertySheet.h>
  25. #include "tier1/commandbuffer.h"
  26. #include "tier1/tier1.h"
  27. // memdbgon must be the last include file in a .cpp file!!!
  28. #include "tier0/memdbgon.h"
  29. using namespace vgui;
  30. //-----------------------------------------------------------------------------
  31. // Purpose: A menu button that knows how to parse cvar/command menu data from gamedir\scripts\debugmenu.txt
  32. //-----------------------------------------------------------------------------
  33. class CDebugMenuButton : public MenuButton
  34. {
  35. typedef MenuButton BaseClass;
  36. public:
  37. // Construction
  38. CDebugMenuButton( Panel *parent, const char *panelName, const char *text );
  39. private:
  40. // Menu associated with this button
  41. Menu *m_pMenu;
  42. };
  43. class CDebugCommandButton : public vgui::Button
  44. {
  45. typedef vgui::Button BaseClass;
  46. public:
  47. CDebugCommandButton( vgui::Panel *parent, const char *panelName, const char *labelText, const char *command )
  48. : BaseClass( parent, panelName, labelText )
  49. {
  50. AddActionSignalTarget( this );
  51. SetCommand( command );
  52. }
  53. virtual void OnCommand( const char *command )
  54. {
  55. Cbuf_AddText( Cbuf_GetCurrentPlayer(), va( "%s\n", (char *)command ) );
  56. }
  57. virtual void OnTick( void )
  58. {
  59. }
  60. };
  61. class CDebugCommandCheckbox : public vgui::CheckButton
  62. {
  63. typedef vgui::CheckButton BaseClass;
  64. public:
  65. CDebugCommandCheckbox( vgui::Panel *parent, const char *panelName, const char *labelText, const char *command )
  66. : BaseClass( parent, panelName, labelText )
  67. {
  68. m_pVar = ( ConVar * )g_pCVar->FindVar( command );
  69. SetCommand( command );
  70. AddActionSignalTarget( this );
  71. }
  72. virtual void OnCommand( const char *command )
  73. {
  74. if ( m_pVar )
  75. {
  76. Cbuf_AddText( Cbuf_GetCurrentPlayer(), va( "%s %d\n", m_pVar->GetName(), !m_pVar->GetInt() ) );
  77. }
  78. }
  79. private:
  80. ConVar *m_pVar;
  81. };
  82. class CDebugIncrementCVarButton : public vgui::Button
  83. {
  84. typedef vgui::Button BaseClass;
  85. public:
  86. CDebugIncrementCVarButton( vgui::Panel *pParent, const char *pPanelName, const char *pLabelText, const char *pCommand )
  87. : BaseClass( pParent, pPanelName, pLabelText )
  88. {
  89. CCommand args;
  90. args.Tokenize( pCommand );
  91. m_pVar = NULL;
  92. if ( args.ArgC() >= 4 )
  93. {
  94. m_pVar = ( ConVar * )g_pCVar->FindVar( args[0] );
  95. m_flMinvalue = (float)atof( args[1] );
  96. m_flMaxvalue = (float)atof( args[2] );
  97. m_flIncrement = (float)atof( args[3] );
  98. }
  99. SetCommand( "increment" );
  100. AddActionSignalTarget( this );
  101. m_flPreviousValue = -9999.0f;
  102. OnTick();
  103. }
  104. virtual void OnCommand( const char *command )
  105. {
  106. //
  107. if ( !m_pVar )
  108. return;
  109. float curValue = m_pVar->GetFloat();
  110. curValue += m_flIncrement;
  111. if ( curValue > m_flMaxvalue )
  112. {
  113. curValue = m_flMinvalue;
  114. }
  115. else if ( curValue < m_flMinvalue )
  116. {
  117. curValue = m_flMaxvalue;
  118. }
  119. m_pVar->SetValue( curValue );
  120. }
  121. virtual void OnTick( void )
  122. {
  123. if ( !m_pVar )
  124. return;
  125. if ( m_pVar->GetFloat() == m_flPreviousValue )
  126. return;
  127. char sz[ 512 ];
  128. Q_snprintf( sz, sizeof( sz ), "%s %.2f", m_pVar->GetName(), m_pVar->GetFloat() );
  129. SetText( sz );
  130. SizeToContents();
  131. m_flPreviousValue = m_pVar->GetFloat();
  132. }
  133. private:
  134. ConVar *m_pVar;
  135. float m_flMinvalue;
  136. float m_flMaxvalue;
  137. float m_flIncrement;
  138. float m_flPreviousValue;
  139. };
  140. class CDebugOptionsPage : public vgui::PropertyPage
  141. {
  142. typedef vgui::PropertyPage BaseClass;
  143. public:
  144. CDebugOptionsPage ( vgui::Panel *parent, const char *panelName )
  145. : BaseClass( parent, panelName )
  146. {
  147. vgui::ivgui()->AddTickSignal( GetVPanel(), 250 );
  148. }
  149. virtual void OnTick( void )
  150. {
  151. BaseClass::OnTick();
  152. if ( !IsVisible() )
  153. return;
  154. int c = m_LayoutItems.Count();
  155. for ( int i = 0; i < c; i++ )
  156. {
  157. vgui::Panel *p = m_LayoutItems[ i ];
  158. p->OnTick();
  159. }
  160. }
  161. virtual void PerformLayout( void )
  162. {
  163. BaseClass::PerformLayout();
  164. int c = m_LayoutItems.Count();
  165. int x = 5;
  166. int y = 5;
  167. int w = 150;
  168. int h = 18;
  169. int gap = 2;
  170. int tall = GetTall();
  171. // LoadControlSettings( va( "resource\\%s.res", kv->GetName() ) );
  172. for ( int i = 0; i < c; i++ )
  173. {
  174. vgui::Panel *p = m_LayoutItems[ i ];
  175. p->SetBounds( x, y, w, h );
  176. y += ( h + gap );
  177. if ( y >= tall - h )
  178. {
  179. x += ( w + gap );
  180. y = 5;
  181. }
  182. }
  183. }
  184. void Init( KeyValues *kv )
  185. {
  186. // LoadControlSettings( va( "resource\\%s.res", kv->GetName() ) );
  187. for (KeyValues *control = kv->GetFirstSubKey(); control != NULL; control = control->GetNextKey())
  188. {
  189. const char *t;
  190. t = control->GetString( "command", "" );
  191. if ( t && t[0] )
  192. {
  193. CDebugCommandButton *btn = new CDebugCommandButton( this, "CommandButton", control->GetName(), t );
  194. m_LayoutItems.AddToTail( btn );
  195. continue;
  196. }
  197. t = control->GetString( "togglecvar", "" );
  198. if ( t && t[0] )
  199. {
  200. CDebugCommandCheckbox *checkbox = new CDebugCommandCheckbox( this, "CommandCheck", control->GetName(), t );
  201. m_LayoutItems.AddToTail( checkbox );
  202. continue;
  203. }
  204. t = control->GetString( "incrementcvar", "" );
  205. if ( t && t[0] )
  206. {
  207. CDebugIncrementCVarButton *increment = new CDebugIncrementCVarButton( this, "IncrementCVar", control->GetName(), t );
  208. m_LayoutItems.AddToTail( increment );
  209. continue;
  210. }
  211. }
  212. }
  213. private:
  214. CUtlVector< vgui::Panel * > m_LayoutItems;
  215. };
  216. class CDebugOptionsPanel : public vgui::PropertyDialog
  217. {
  218. typedef vgui::PropertyDialog BaseClass;
  219. public:
  220. CDebugOptionsPanel( vgui::Panel *parent, const char *panelName )
  221. : BaseClass( parent, panelName )
  222. {
  223. SetTitle( "Debug Options", true );
  224. KeyValues *kv = new KeyValues( "DebugOptions" );
  225. if ( kv )
  226. {
  227. if ( kv->LoadFromFile(g_pFullFileSystem, "scripts/DebugOptions.txt") )
  228. {
  229. for (KeyValues *dat = kv->GetFirstSubKey(); dat != NULL; dat = dat->GetNextKey())
  230. {
  231. if ( !Q_strcasecmp( dat->GetName(), "width" ) )
  232. {
  233. SetWide( dat->GetInt() );
  234. continue;
  235. }
  236. else if ( !Q_strcasecmp( dat->GetName(), "height" ) )
  237. {
  238. SetTall( dat->GetInt() );
  239. continue;
  240. }
  241. CDebugOptionsPage *page = new CDebugOptionsPage( this, dat->GetName() );
  242. page->Init( dat );
  243. AddPage( page, dat->GetName() );
  244. }
  245. }
  246. kv->deleteThis();
  247. }
  248. GetPropertySheet()->SetTabWidth(72);
  249. SetPos( videomode->GetModeWidth() - GetWide() - 10 , 10 );
  250. SetVisible( true );
  251. LoadControlSettings( "resource\\DebugOptionsPanel.res" );
  252. }
  253. void Init( KeyValues *kv );
  254. };
  255. void CDebugOptionsPanel::Init( KeyValues *kv )
  256. {
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Purpose: Constructor
  260. //-----------------------------------------------------------------------------
  261. CDebugMenuButton::CDebugMenuButton(Panel *parent, const char *panelName, const char *text)
  262. : BaseClass( parent, panelName, text )
  263. {
  264. MakePopup();
  265. // Assume no menu
  266. m_pMenu = new Menu( this, "DebugMenu" );
  267. m_pMenu->AddMenuItem( "Debug Panel", "toggledebugpanel", parent );
  268. m_pMenu->AddMenuItem( "Quit", "Quit", parent );
  269. MenuButton::SetMenu(m_pMenu);
  270. SetOpenDirection(Menu::DOWN);
  271. }
  272. //-----------------------------------------------------------------------------
  273. // Purpose: Container for menu button
  274. // Input : *parent -
  275. // *panelName -
  276. //-----------------------------------------------------------------------------
  277. CDebugSystemPanel::CDebugSystemPanel( Panel *parent, const char *panelName )
  278. : BaseClass( parent, panelName )
  279. {
  280. SetBounds( 0, 0, videomode->GetModeWidth(), videomode->GetModeHeight() );
  281. // Show arrow cursor while in this mode
  282. SetCursor( vgui::dc_arrow );
  283. SetVisible( false );
  284. SetPaintEnabled( false );
  285. SetPaintBackgroundEnabled( false );
  286. m_pDebugMenu = new CDebugMenuButton( this, "Debug Menu", "Debug Menu" );
  287. int h = 24;
  288. // Locate it at top left
  289. m_pDebugMenu->SetPos( 0, 0 );
  290. m_pDebugMenu->SetSize( 110, h );
  291. m_hDebugOptions = new CDebugOptionsPanel( this, "DebugOptions" );
  292. }
  293. //-----------------------------------------------------------------------------
  294. // Purpose: Hook so we can force cursor visible
  295. // Input : state -
  296. //-----------------------------------------------------------------------------
  297. void CDebugSystemPanel::SetVisible( bool state )
  298. {
  299. BaseClass::SetVisible( state );
  300. if ( state )
  301. {
  302. surface()->SetCursor( GetCursor() );
  303. }
  304. }
  305. void CDebugSystemPanel::OnCommand( const char *command )
  306. {
  307. if ( !Q_strcasecmp( command, "toggledebugpanel" ) )
  308. {
  309. if ( m_hDebugOptions )
  310. {
  311. m_hDebugOptions->SetVisible( !m_hDebugOptions->IsVisible() );
  312. }
  313. return;
  314. }
  315. else if ( !Q_strcasecmp( command, "quit" ) )
  316. {
  317. Cbuf_AddText( Cbuf_GetCurrentPlayer(), "quit\n" );
  318. }
  319. BaseClass::OnCommand( command );
  320. }