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.

107 lines
2.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Simple HUD element
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "hudelement.h"
  8. #include "hud_macros.h"
  9. #include "iclientmode.h"
  10. #include "view.h"
  11. using namespace vgui;
  12. #include <vgui_controls/Panel.h>
  13. #include <vgui_controls/Frame.h>
  14. #include <vgui/IScheme.h>
  15. #include <vgui/ISurface.h>
  16. #include <vgui/ILocalize.h>
  17. class CHudGameMessage : public CHudElement, public vgui::Panel
  18. {
  19. private:
  20. DECLARE_CLASS_SIMPLE( CHudGameMessage, vgui::Panel );
  21. public:
  22. CHudGameMessage( const char *pElementName ) : CHudElement( pElementName ), vgui::Panel( NULL, "HudGameMessage" )
  23. {
  24. // Set our parent window
  25. SetParent( g_pClientMode->GetViewport() );
  26. m_pIcon = NULL;
  27. // Never hide
  28. SetHiddenBits( 0 );
  29. };
  30. void Init( void );
  31. void VidInit( void );
  32. void Paint( void );
  33. // Callback function for the "GameMessage" user message
  34. void MsgFunc_GameMessage( bf_read &msg );
  35. private:
  36. CHudTexture *m_pIcon; // Icon texture reference
  37. wchar_t m_pText[256]; // Unicode text buffer
  38. float m_flStartTime; // When the message was recevied
  39. float m_flDuration; // Duration of the message
  40. };
  41. DECLARE_HUDELEMENT( CHudGameMessage );
  42. DECLARE_HUD_MESSAGE( CHudGameMessage, GameMessage );
  43. void CHudGameMessage::VidInit( void )
  44. {
  45. // Store off a reference to our icon
  46. m_pIcon = gHUD.GetIcon( "message_icon" );
  47. m_pText[0] = '\0';
  48. }
  49. void CHudGameMessage::Init( void )
  50. {
  51. HOOK_HUD_MESSAGE( CHudGameMessage, GameMessage );
  52. }
  53. void CHudGameMessage::MsgFunc_GameMessage( bf_read &msg )
  54. {
  55. // Read in our string
  56. char szString[256];
  57. msg.ReadString( szString, sizeof(szString) );
  58. // Convert it to localize friendly unicode
  59. g_pVGuiLocalize->ConvertANSIToUnicode( szString, m_pText, sizeof(m_pText) );
  60. // Setup our time trackers
  61. m_flStartTime = gpGlobals->curtime;
  62. m_flDuration = 5.0f;
  63. }
  64. void CHudGameMessage::Paint( void )
  65. {
  66. if ( !m_pIcon )
  67. return;
  68. // Find our fade based on our time shown
  69. float dt = ( m_flStartTime - gpGlobals->curtime );
  70. float flAlpha = SimpleSplineRemapVal( dt, 0.0f, m_flDuration, 255, 0 );
  71. flAlpha = clamp( flAlpha, 0.0f, 255.0f );
  72. // Draw our icon
  73. m_pIcon->DrawSelf( 0, 0, 32, 32, Color(255,255,255,flAlpha) );
  74. // Get our scheme and font information
  75. vgui::HScheme scheme = vgui::scheme()->GetScheme( "ClientScheme" );
  76. vgui::HFont hFont = vgui::scheme()->GetIScheme(scheme)->GetFont( "Default" );
  77. // Draw our text
  78. surface()->DrawSetTextFont( hFont ); // set the font
  79. surface()->DrawSetTextColor( 255, 255, 255, flAlpha ); // white
  80. surface()->DrawSetTextPos( 32, 8 ); // x,y position
  81. surface()->DrawPrintText( m_pText, wcslen(m_pText) ); // print text
  82. }