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.

220 lines
5.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This is a panel which is rendered image on top of an entity
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #pragma warning (disable: 4514)
  10. #include "vgui_bitmappanel.h"
  11. #include <KeyValues.h>
  12. #include "panelmetaclassmgr.h"
  13. #include "vgui_bitmapimage.h"
  14. #ifdef INVASION_CLIENT_DLL
  15. #include "hud_commander_statuspanel.h"
  16. #endif
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. DECLARE_BUILD_FACTORY( CBitmapPanel );
  20. //-----------------------------------------------------------------------------
  21. // Constructor
  22. //-----------------------------------------------------------------------------
  23. CBitmapPanel::CBitmapPanel( ) : BaseClass( NULL, "CBitmapPanel" ), m_pImage(0)
  24. {
  25. SetPaintBackgroundEnabled( false );
  26. m_szMouseOverText[ 0 ] = 0;
  27. m_r = m_g = m_b = m_a = 255;
  28. m_bOwnsImage = true;
  29. }
  30. CBitmapPanel::CBitmapPanel( vgui::Panel *pParent, const char *pName ) :
  31. BaseClass( pParent, pName ), m_pImage(0)
  32. {
  33. SetPaintBackgroundEnabled( false );
  34. m_szMouseOverText[ 0 ] = 0;
  35. m_r = m_g = m_b = m_a = 255;
  36. m_bOwnsImage = true;
  37. }
  38. CBitmapPanel::~CBitmapPanel()
  39. {
  40. if (m_pImage && m_bOwnsImage)
  41. {
  42. delete m_pImage;
  43. m_pImage = NULL;
  44. }
  45. }
  46. //-----------------------------------------------------------------------------
  47. // initialization
  48. //-----------------------------------------------------------------------------
  49. bool CBitmapPanel::Init( KeyValues* pInitData )
  50. {
  51. Assert( pInitData );
  52. // modulation color
  53. if (!ParseRGBA( pInitData, "color", m_r, m_g, m_b, m_a ))
  54. return false;
  55. int x, y, w, h;
  56. if (!ParseRect( pInitData, "position", x, y, w, h ))
  57. return false;
  58. const char *mouseover = pInitData->GetString( "mousehint", "" );
  59. if ( mouseover && mouseover[ 0 ] )
  60. {
  61. Q_strncpy( m_szMouseOverText, mouseover, sizeof( m_szMouseOverText ) );
  62. }
  63. // Set the size...
  64. SetPos( x, y );
  65. SetSize( w, h );
  66. char const* pClassImage = pInitData->GetString( "material" );
  67. if ( !pClassImage || !pClassImage[ 0 ] )
  68. return false;
  69. // hook in the bitmap
  70. m_pImage = new BitmapImage( GetVPanel(), pClassImage );
  71. m_bOwnsImage = true;
  72. return true;
  73. }
  74. //-----------------------------------------------------------------------------
  75. // initialization from build-mode dialog style .res files
  76. //-----------------------------------------------------------------------------
  77. void CBitmapPanel::ApplySettings(KeyValues *pInitData)
  78. {
  79. BaseClass::ApplySettings(pInitData);
  80. if (m_pImage && m_bOwnsImage)
  81. {
  82. delete m_pImage;
  83. m_pImage = NULL;
  84. }
  85. // modulation color. Can't use ParseRGBA since this uses a vgui::KeyValues (feh)
  86. m_r = m_g = m_b = m_a = 255;
  87. const char *pColorString = pInitData->GetString( "color", "255 255 255 255" );
  88. if ( pColorString && pColorString[ 0 ] )
  89. {
  90. // Try and scan them in
  91. int r = 0, g = 0, b = 0, a = 0;
  92. int scanned;
  93. scanned = sscanf( pColorString, "%i %i %i %i", &r, &g, &b, &a );
  94. if ( scanned == 4 )
  95. {
  96. m_r = r; m_g = g; m_b = b; m_a = a;
  97. }
  98. else
  99. {
  100. Warning( "Couldn't scan four color values from %s\n", pColorString );
  101. }
  102. }
  103. m_szMouseOverText[0] = 0;
  104. char const* pClassImage = pInitData->GetString( "material" );
  105. if ( pClassImage && pClassImage[ 0 ] && m_bOwnsImage )
  106. {
  107. // hook in the bitmap
  108. m_pImage = new BitmapImage( GetVPanel(), pClassImage );
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Draws the puppy
  113. //-----------------------------------------------------------------------------
  114. void CBitmapPanel::Paint( void )
  115. {
  116. if ( !m_pImage )
  117. return;
  118. Color color;
  119. color.SetColor( m_r, m_g, m_b, m_a );
  120. m_pImage->SetColor( color );
  121. m_pImage->DoPaint( GetVPanel() );
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose:
  125. //-----------------------------------------------------------------------------
  126. void CBitmapPanel::OnCursorEntered()
  127. {
  128. #ifdef INVASION_CLIENT_DLL
  129. if ( m_szMouseOverText[ 0 ] )
  130. {
  131. StatusPrint( TYPE_HINT, "%s", m_szMouseOverText );
  132. }
  133. #endif
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Purpose:
  137. //-----------------------------------------------------------------------------
  138. void CBitmapPanel::OnCursorExited()
  139. {
  140. #ifdef INVASION_CLIENT_DLL
  141. if ( m_szMouseOverText[ 0 ] )
  142. {
  143. StatusClear();
  144. }
  145. #endif
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose:
  149. // Output : char const
  150. //-----------------------------------------------------------------------------
  151. const char *CBitmapPanel::GetMouseOverText( void )
  152. {
  153. return m_szMouseOverText;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose: Sets up the panel
  157. // Used by panels that aren't created by the commander overlay factory (i.e. aren't parsed from a keyvalues file)
  158. //-----------------------------------------------------------------------------
  159. void CBitmapPanel::SetImage( BitmapImage *pImage )
  160. {
  161. m_pImage = pImage;
  162. m_bOwnsImage = (pImage == NULL);
  163. // Get the color from the image
  164. if ( m_pImage )
  165. {
  166. m_pImage->GetColor( m_r, m_g, m_b, m_a );
  167. }
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Purpose: Set image data directly
  171. //-----------------------------------------------------------------------------
  172. void CBitmapPanel::SetBitmap( const Bitmap_t &bitmap )
  173. {
  174. // Make sure we have an image that we own
  175. if ( m_pImage == NULL || !m_bOwnsImage )
  176. {
  177. delete m_pImage;
  178. m_pImage = new BitmapImage( GetVPanel(), NULL );
  179. m_bOwnsImage = true;
  180. }
  181. // Set the bitmap
  182. m_pImage->SetBitmap( bitmap );
  183. }