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.

337 lines
7.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "hud.h"
  10. #include "hudelement.h"
  11. #include "iclientmode.h"
  12. #include <KeyValues.h>
  13. #include <vgui/ISurface.h>
  14. #include <vgui/IScheme.h>
  15. #include <vgui/ILocalize.h>
  16. #include <vgui_controls/AnimationController.h>
  17. #include <vgui_controls/Panel.h>
  18. #include <vgui/IPanel.h>
  19. #include "vgui_int.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. using namespace vgui;
  23. #define ANIM_INFO_START_Y 50
  24. #define ANIM_INFO_WIDTH XRES( 300 )
  25. class CHudAnimationInfo : public CHudElement, public vgui::Panel
  26. {
  27. DECLARE_CLASS_SIMPLE( CHudAnimationInfo, vgui::Panel );
  28. public:
  29. CHudAnimationInfo( const char *pElementName );
  30. virtual bool ShouldDraw();
  31. // vgui::Panel overrides.
  32. virtual void Paint( void );
  33. virtual void ApplySchemeSettings( IScheme *scheme );
  34. void SetWatch( Panel *element )
  35. {
  36. m_pWatch = element;
  37. }
  38. protected:
  39. void PaintMappingInfo( int& x, int& y, Panel *element, PanelAnimationMap *map );
  40. void PaintString( int& x, int &y, const char *sz, Color *pLegendColor );
  41. CPanelAnimationVar( vgui::HFont, m_LabelFont, "LabelFont", "DebugFixed" );
  42. CPanelAnimationVar( vgui::HFont, m_ItemFont, "ItemFont", "DebugFixedSmall" );
  43. CPanelAnimationVar( Color, m_LabelColor, "LabelColor", "DebugLabel" );
  44. CPanelAnimationVar( Color, m_ItemColor, "ItemColor", "DebugText");
  45. Panel *m_pWatch;
  46. };
  47. DECLARE_HUDELEMENT( CHudAnimationInfo );
  48. //-----------------------------------------------------------------------------
  49. // Purpose:
  50. // Input : *pElementName -
  51. // *panelName -
  52. //-----------------------------------------------------------------------------
  53. CHudAnimationInfo::CHudAnimationInfo( const char *pElementName )
  54. : CHudElement( pElementName ), BaseClass( NULL, "HudAnimationInfo" )
  55. {
  56. vgui::Panel *pParent = g_pClientMode->GetViewport();
  57. SetParent( pParent );
  58. SetActive( true );
  59. m_pWatch = NULL;
  60. // Make sure we render on top of other hud elements since we are debugging info for them.
  61. SetZPos( 100 );
  62. }
  63. void CHudAnimationInfo::ApplySchemeSettings( IScheme *scheme )
  64. {
  65. BaseClass::ApplySchemeSettings( scheme );
  66. m_LabelFont = scheme->GetFont( "DebugFixed", true );
  67. m_ItemFont= scheme->GetFont( "DebugFixedSmall", true );
  68. m_LabelColor = scheme->GetColor( "DebugLabel", GetFgColor() );
  69. m_ItemColor= scheme->GetColor( "DebugText", GetFgColor() );
  70. SetPaintBackgroundEnabled( false );
  71. }
  72. bool CHudAnimationInfo::ShouldDraw()
  73. {
  74. return ( m_pWatch && CHudElement::ShouldDraw() );
  75. }
  76. void CHudAnimationInfo::PaintString( int& x, int &y, const char *sz, Color *pLegendColor )
  77. {
  78. surface()->DrawSetTextFont( m_ItemFont );
  79. surface()->DrawSetTextPos( x, y );
  80. wchar_t szconverted[ 512 ];
  81. g_pVGuiLocalize->ConvertANSIToUnicode( "O->", szconverted, sizeof(szconverted) );
  82. if ( pLegendColor )
  83. {
  84. surface()->DrawSetTextColor( *pLegendColor );
  85. }
  86. else
  87. {
  88. surface()->DrawSetTextColor( Color( 0, 0, 0, 0 ) );
  89. }
  90. surface()->DrawPrintText( szconverted, wcslen( szconverted ) );
  91. g_pVGuiLocalize->ConvertANSIToUnicode( sz, szconverted, sizeof(szconverted) );
  92. surface()->DrawSetTextColor( m_ItemColor );
  93. surface()->DrawPrintText( szconverted, wcslen( szconverted ) );
  94. int fontHeight = surface()->GetFontTall( m_ItemFont );
  95. y += fontHeight;
  96. if ( y + fontHeight >= ScreenHeight() )
  97. {
  98. y = ANIM_INFO_START_Y;
  99. x += ANIM_INFO_WIDTH;
  100. }
  101. }
  102. void CHudAnimationInfo::PaintMappingInfo( int& x, int& y, Panel *element, PanelAnimationMap *map )
  103. {
  104. if ( !map )
  105. return;
  106. // Draw label
  107. surface()->DrawSetTextFont( m_LabelFont );
  108. surface()->DrawSetTextColor( m_LabelColor );
  109. surface()->DrawSetTextPos( x, y );
  110. const char *className = "";
  111. if ( map->pfnClassName )
  112. {
  113. className = (*map->pfnClassName)();
  114. }
  115. const char *p = className;
  116. while ( *p )
  117. {
  118. surface()->DrawUnicodeChar( *p );
  119. p++;
  120. }
  121. y += surface()->GetFontTall( m_LabelFont ) + 1;
  122. x += 10;
  123. int c = map->entries.Count();
  124. for ( int i = 0; i < c; i++ )
  125. {
  126. PanelAnimationMapEntry *e = &map->entries[ i ];
  127. char sz[ 512 ];
  128. char value[ 256 ];
  129. Color col( 0, 0, 0, 0 );
  130. Color *pColor = NULL;
  131. KeyValues *kv = new KeyValues( e->name() );
  132. if ( element->RequestInfo( kv ) )
  133. {
  134. KeyValues *dat = kv->FindKey(e->name());
  135. if ( dat && dat->GetDataType() == KeyValues::TYPE_COLOR )
  136. {
  137. col = dat->GetColor();
  138. Q_snprintf( value, sizeof( value ), "%i, %i, %i, %i",
  139. col[0], col[1], col[2], col[3] );
  140. pColor = &col;
  141. }
  142. else
  143. {
  144. Q_snprintf( value, sizeof( value ), "%s",
  145. dat->GetString() );
  146. }
  147. }
  148. else
  149. {
  150. Q_strncpy( value, "???", sizeof( value ) );
  151. }
  152. Q_snprintf( sz, sizeof( sz ), "%-30s %-20s (%s)",
  153. e->name(), e->type(), value );
  154. kv->deleteThis();
  155. PaintString( x, y, sz, pColor );
  156. }
  157. x -= 10;
  158. if ( map->baseMap )
  159. {
  160. PaintMappingInfo( x, y, element, map->baseMap );
  161. }
  162. }
  163. void CHudAnimationInfo::Paint()
  164. {
  165. vgui::Panel *panel = m_pWatch;
  166. if ( !panel )
  167. return;
  168. // See if it has any animation info registered
  169. PanelAnimationMap *map = panel->GetAnimMap();
  170. if ( !map )
  171. return;
  172. int x = 15;
  173. int y = ANIM_INFO_START_Y;
  174. PaintMappingInfo( x, y, panel, map );
  175. x += 10;
  176. // Paint panel bounds
  177. int bounds[4];
  178. panel->GetBounds( bounds[0], bounds[1], bounds[2], bounds[3] );
  179. char sz[ 256 ];
  180. Q_snprintf( sz, sizeof( sz ), "%-30s %-20s (%i %i)", "Position", "pos", bounds[0], bounds[1] );
  181. PaintString( x, y, sz, NULL );
  182. Q_snprintf( sz, sizeof( sz ), "%-30s %-20s (%i %i)", "Size", "size", bounds[2], bounds[3] );
  183. PaintString( x, y, sz, NULL );
  184. }
  185. static int HudElementCompletion( const char *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
  186. {
  187. const char *cmdname = "cl_animationinfo";
  188. char *substring = (char *)partial;
  189. if ( Q_strstr( partial, cmdname ) )
  190. {
  191. substring = (char *)partial + strlen( cmdname ) + 1;
  192. }
  193. int current = 0;
  194. int c = gHUD.m_HudList.Count();
  195. int i;
  196. for ( i = 0; i < c; i++ )
  197. {
  198. CHudElement *e = gHUD.m_HudList[ i ];
  199. if ( !e )
  200. continue;
  201. bool add = false;
  202. // Insert into lookup
  203. if ( substring[0] )
  204. {
  205. if ( !Q_strncasecmp( e->GetName(), substring, strlen( substring ) ) )
  206. {
  207. add = true;
  208. }
  209. }
  210. else
  211. {
  212. add = true;
  213. }
  214. if ( add )
  215. {
  216. Q_snprintf( commands[ current ], sizeof( commands[ current ] ), "%s %s", cmdname, e->GetName() );
  217. current++;
  218. }
  219. }
  220. return current;
  221. }
  222. CON_COMMAND_F_COMPLETION( cl_animationinfo, "Hud element to examine.", 0, HudElementCompletion )
  223. {
  224. CHudAnimationInfo *info = GET_HUDELEMENT( CHudAnimationInfo );
  225. Assert( info );
  226. if ( !info )
  227. return;
  228. if ( args.ArgC() != 2 )
  229. {
  230. info->SetWatch( NULL );
  231. return;
  232. }
  233. // Find it
  234. CHudElement *element = NULL;
  235. for ( int i = 0; i < gHUD.m_HudList.Size(); i++ )
  236. {
  237. if ( stricmp( gHUD.m_HudList[i]->GetName(), args[1] ) == 0 )
  238. {
  239. element = gHUD.m_HudList[i];
  240. break;
  241. }
  242. }
  243. if ( element )
  244. {
  245. info->SetWatch( dynamic_cast< Panel * >( element ) );
  246. }
  247. else
  248. {
  249. VPANEL root = VGui_GetClientDLLRootPanel();
  250. vgui::Panel *rootPanel = ipanel()->GetPanel( root, info->GetModuleName() );
  251. Panel *panel = NULL;
  252. if ( rootPanel )
  253. {
  254. panel = rootPanel->FindChildByName( args[1], true );
  255. }
  256. if ( panel )
  257. {
  258. info->SetWatch( panel );
  259. }
  260. else
  261. {
  262. Msg( "No such element %s\n", args[1] );
  263. }
  264. }
  265. }