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.

131 lines
3.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include <vgui_controls/tgaimagepanel.h>
  7. #include "bitmap/tgaloader.h"
  8. #include "vgui/ISurface.h"
  9. #include <keyvalues.h>
  10. #include "tier1/fmtstr.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. using namespace vgui;
  14. CTGAImagePanel::CTGAImagePanel( vgui::Panel *parent, const char *name ) : BaseClass( parent, name )
  15. {
  16. m_iTextureID = -1;
  17. m_bHasValidTexture = false;
  18. m_bLoadedTexture = false;
  19. m_bScaleImage = false;
  20. m_ImageColor = Color( 255, 255, 255, 255 );
  21. SetPaintBackgroundEnabled( false );
  22. }
  23. CTGAImagePanel::~CTGAImagePanel()
  24. {
  25. // release the texture memory
  26. }
  27. void CTGAImagePanel::SetTGAFilename( const char *filename )
  28. {
  29. m_sTGAFilenameWithPath = CFmtStr( "//MOD/%s", filename );
  30. m_sTGAFilename = filename;
  31. }
  32. void CTGAImagePanel::SetTGAFilenameNonMod( const char *filename )
  33. {
  34. m_sTGAFilenameWithPath = filename;
  35. m_sTGAFilename = filename;
  36. }
  37. char const *CTGAImagePanel::GetTGAFilename() const
  38. {
  39. return m_sTGAFilename.String();
  40. }
  41. void CTGAImagePanel::SetShouldScaleImage( bool state )
  42. {
  43. m_bScaleImage = state;
  44. }
  45. void CTGAImagePanel::SetImageColor( Color imageColor )
  46. {
  47. m_ImageColor = imageColor;
  48. }
  49. void CTGAImagePanel::GetSettings(KeyValues *outResourceData)
  50. {
  51. BaseClass::GetSettings(outResourceData);
  52. outResourceData->SetBool("scaleImage", m_bScaleImage);
  53. }
  54. void CTGAImagePanel::ApplySettings(KeyValues *inResourceData)
  55. {
  56. m_bScaleImage = inResourceData->GetBool("scaleImage", false);
  57. BaseClass::ApplySettings(inResourceData);
  58. }
  59. void CTGAImagePanel::Paint()
  60. {
  61. if ( !m_bLoadedTexture )
  62. {
  63. m_bLoadedTexture = true;
  64. // get a texture id, if we haven't already
  65. if ( m_iTextureID < 0 )
  66. {
  67. m_iTextureID = vgui::surface()->CreateNewTextureID( true );
  68. if ( !m_bScaleImage )
  69. {
  70. SetSize( 180, 100 );
  71. }
  72. }
  73. // load the file
  74. CUtlMemory<unsigned char> tga;
  75. if ( TGALoader::LoadRGBA8888( m_sTGAFilenameWithPath, tga, m_iImageWidth, m_iImageHeight ) )
  76. {
  77. // set the textureID
  78. surface()->DrawSetTextureRGBA( m_iTextureID, tga.Base(), m_iImageWidth, m_iImageHeight );
  79. m_bHasValidTexture = true;
  80. if ( !m_bScaleImage )
  81. {
  82. // set our size to be the size of the tga
  83. SetSize( m_iImageWidth, m_iImageHeight );
  84. }
  85. }
  86. else
  87. {
  88. m_bHasValidTexture = false;
  89. }
  90. }
  91. // draw the image
  92. int wide, tall;
  93. if ( m_bHasValidTexture )
  94. {
  95. surface()->DrawGetTextureSize( m_iTextureID, wide, tall );
  96. surface()->DrawSetTexture( m_iTextureID );
  97. surface()->DrawSetColor( m_ImageColor );
  98. int iScaledWide = ( m_bScaleImage ) ? ( GetWide() ) : ( wide );
  99. int iScaledTall = ( m_bScaleImage ) ? ( GetTall() ) : ( tall );
  100. surface()->DrawTexturedRect( 0, 0, iScaledWide, iScaledTall );
  101. }
  102. else
  103. {
  104. // draw a black fill instead
  105. wide = 180, tall = 100;
  106. surface()->DrawSetColor( 0, 0, 0, 255 );
  107. surface()->DrawFilledRect( 0, 0, wide, tall );
  108. }
  109. }