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.

320 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "vgui_bitmapimage.h"
  9. #include <vgui_controls/Controls.h>
  10. #include <vgui/ISurface.h>
  11. #include <vgui_controls/Panel.h>
  12. #include "panelmetaclassmgr.h"
  13. #include <KeyValues.h>
  14. #include <vgui/IPanel.h>
  15. #include <bitmap/bitmap.h>
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. //-----------------------------------------------------------------------------
  19. // Purpose: Check box image
  20. //-----------------------------------------------------------------------------
  21. BitmapImage::BitmapImage()
  22. {
  23. m_clr.SetColor( 255, 255, 255, 255 );
  24. m_pos[ 0 ] = m_pos[ 1 ] = 0;
  25. m_pPanelSize = NULL;
  26. m_nTextureId = -1;
  27. m_bProcedural = false;
  28. SetViewport( false, 0.0f, 0.0f, 0.0f, 0.0f );
  29. }
  30. BitmapImage::BitmapImage( vgui::VPANEL parent, const char *filename )
  31. {
  32. m_nTextureId = -1;
  33. m_clr.SetColor( 255, 255, 255, 255 );
  34. m_pos[ 0 ] = m_pos[ 1 ] = 0;
  35. Init( parent, filename );
  36. SetViewport( false, 0.0f, 0.0f, 0.0f, 0.0f );
  37. }
  38. BitmapImage::~BitmapImage()
  39. {
  40. DestroyTexture();
  41. }
  42. void BitmapImage::DestroyTexture()
  43. {
  44. if ( m_nTextureId != -1 )
  45. {
  46. vgui::surface()->DestroyTextureID( m_nTextureId );
  47. m_nTextureId = -1;
  48. m_bProcedural = false;
  49. }
  50. }
  51. bool BitmapImage::Init( vgui::VPANEL pParent, const char *pFileName )
  52. {
  53. UsePanelRenderSize( pParent );
  54. if ( pFileName != NULL )
  55. {
  56. DestroyTexture();
  57. m_nTextureId = vgui::surface()->CreateNewTextureID();
  58. m_bProcedural = false;
  59. vgui::surface()->DrawSetTextureFile( m_nTextureId, pFileName , true, true);
  60. GetSize( m_Size[0], m_Size[1] );
  61. }
  62. return true;
  63. }
  64. bool BitmapImage::Init( vgui::VPANEL pParent, KeyValues* pInitData )
  65. {
  66. char const* pMaterialName = pInitData->GetString( "material" );
  67. if ( !pMaterialName || !pMaterialName[ 0 ] )
  68. return false;
  69. // modulation color
  70. Color color;
  71. if (!ParseRGBA( pInitData, "color", color ))
  72. color.SetColor( 255, 255, 255, 255 );
  73. Init( pParent, pMaterialName );
  74. SetColor( color );
  75. return true;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // FIXME: Bleah!!! Don't want two different KeyValues
  79. /*-----------------------------------------------------------------------------
  80. bool BitmapImage::Init( vgui::VPANEL pParent, KeyValues* pInitData )
  81. {
  82. char const* pMaterialName = pInitData->GetString( "material" );
  83. if ( !pMaterialName || !pMaterialName[ 0 ] )
  84. return false;
  85. // modulation color
  86. Color color;
  87. if (!ParseRGBA( pInitData, "color", color ))
  88. color.SetColor( 255, 255, 255, 255 );
  89. Init( pParent, pMaterialName );
  90. SetColor( color );
  91. return true;
  92. } */
  93. void BitmapImage::SetImageFile( const char *newImage )
  94. {
  95. if ( m_nTextureId == -1 || m_bProcedural )
  96. {
  97. DestroyTexture();
  98. m_nTextureId = vgui::surface()->CreateNewTextureID();
  99. m_bProcedural = false;
  100. }
  101. vgui::surface()->DrawSetTextureFile( m_nTextureId, newImage , true, true);
  102. }
  103. void BitmapImage::UsePanelRenderSize( vgui::VPANEL pPanel )
  104. {
  105. m_pPanelSize = pPanel;
  106. }
  107. vgui::VPANEL BitmapImage::GetRenderSizePanel( void )
  108. {
  109. return m_pPanelSize;
  110. }
  111. void BitmapImage::SetRenderSize( int x, int y )
  112. {
  113. m_Size[0] = x;
  114. m_Size[1] = y;
  115. }
  116. void BitmapImage::DoPaint( int x, int y, int wide, int tall, float yaw, float flAlphaModulate )
  117. {
  118. vgui::surface()->DrawSetTexture( m_nTextureId );
  119. int r, g, b, a;
  120. m_clr.GetColor( r, g, b, a );
  121. a *= flAlphaModulate;
  122. vgui::surface()->DrawSetColor( r, g, b, a );
  123. if (yaw == 0)
  124. {
  125. if ( !m_bUseViewport )
  126. {
  127. vgui::surface()->DrawTexturedRect( x, y, x + wide, y + tall );
  128. }
  129. else
  130. {
  131. vgui::surface()->DrawTexturedSubRect( x, y, x + wide, y + tall,
  132. m_rgViewport[ 0 ],
  133. m_rgViewport[ 1 ],
  134. m_rgViewport[ 2 ],
  135. m_rgViewport[ 3 ]
  136. );
  137. }
  138. }
  139. else
  140. {
  141. // Rotated version of the bitmap!
  142. // Rotate about the center of the bitmap
  143. vgui::Vertex_t verts[4];
  144. Vector2D center( x + (wide * 0.5f), y + (tall * 0.5f) );
  145. // Choose a basis...
  146. float yawRadians = -yaw * M_PI / 180.0f;
  147. Vector2D axis[2];
  148. axis[0].x = cos(yawRadians);
  149. axis[0].y = sin(yawRadians);
  150. axis[1].x = -axis[0].y;
  151. axis[1].y = axis[0].x;
  152. verts[0].m_TexCoord.Init( 0, 0 );
  153. Vector2DMA( center, -0.5f * wide, axis[0], verts[0].m_Position );
  154. Vector2DMA( verts[0].m_Position, -0.5f * tall, axis[1], verts[0].m_Position );
  155. verts[1].m_TexCoord.Init( 1, 0 );
  156. Vector2DMA( verts[0].m_Position, wide, axis[0], verts[1].m_Position );
  157. verts[2].m_TexCoord.Init( 1, 1 );
  158. Vector2DMA( verts[1].m_Position, tall, axis[1], verts[2].m_Position );
  159. verts[3].m_TexCoord.Init( 0, 1 );
  160. Vector2DMA( verts[0].m_Position, tall, axis[1], verts[3].m_Position );
  161. vgui::surface()->DrawTexturedPolygon( 4, verts );
  162. }
  163. }
  164. void BitmapImage::DoPaint( vgui::VPANEL pPanel, float yaw, float flAlphaModulate )
  165. {
  166. int wide, tall;
  167. if ( pPanel )
  168. {
  169. vgui::ipanel()->GetSize(pPanel, wide, tall );
  170. }
  171. else
  172. {
  173. wide = m_Size[0];
  174. tall = m_Size[1];
  175. }
  176. DoPaint( m_pos[0], m_pos[1], wide, tall, yaw, flAlphaModulate );
  177. }
  178. void BitmapImage::Paint()
  179. {
  180. DoPaint( m_pPanelSize );
  181. }
  182. void BitmapImage::SetColor( const Color& clr )
  183. {
  184. m_clr = clr;
  185. }
  186. Color BitmapImage::GetColor( )
  187. {
  188. return m_clr;
  189. }
  190. void BitmapImage::GetColor( int& r,int& g,int& b,int& a )
  191. {
  192. m_clr.GetColor( r,g,b,a );
  193. }
  194. void BitmapImage::GetSize( int& wide, int& tall )
  195. {
  196. vgui::surface()->DrawGetTextureSize( m_nTextureId, wide, tall );
  197. }
  198. void BitmapImage::SetPos( int x, int y )
  199. {
  200. m_pos[ 0 ] = x;
  201. m_pos[ 1 ] = y;
  202. }
  203. //-----------------------------------------------------------------------------
  204. // Helper method to initialize a bitmap image from KeyValues data..
  205. //-----------------------------------------------------------------------------
  206. bool InitializeImage( KeyValues *pInitData, const char* pSectionName, vgui::Panel *pParent, BitmapImage* pBitmapImage )
  207. {
  208. KeyValues *pBitmapImageSection;
  209. if (pSectionName)
  210. {
  211. pBitmapImageSection = pInitData->FindKey( pSectionName );
  212. if ( !pBitmapImageSection )
  213. return false;
  214. }
  215. else
  216. {
  217. pBitmapImageSection = pInitData;
  218. }
  219. return pBitmapImage->Init( pParent->GetVPanel(), pBitmapImageSection );
  220. }
  221. //-----------------------------------------------------------------------------
  222. // FIXME: How sad. We need to make KeyValues + vgui::KeyValues be the same. Bleah
  223. /*-----------------------------------------------------------------------------
  224. bool InitializeImage( KeyValues *pInitData, const char* pSectionName, vgui::Panel *pParent, BitmapImage* pBitmapImage )
  225. {
  226. KeyValues *pBitmapImageSection;
  227. if (pSectionName)
  228. {
  229. pBitmapImageSection = pInitData->FindKey( pSectionName );
  230. if ( !pBitmapImageSection )
  231. return false;
  232. }
  233. else
  234. {
  235. pBitmapImageSection = pInitData;
  236. }
  237. return pBitmapImage->Init( pParent->GetVPanel(), pBitmapImageSection );
  238. } */
  239. //-----------------------------------------------------------------------------
  240. // Purpose:
  241. // Input : use -
  242. // left -
  243. // top -
  244. // right -
  245. // bottom -
  246. //-----------------------------------------------------------------------------
  247. void BitmapImage::SetViewport( bool use, float left, float top, float right, float bottom )
  248. {
  249. m_bUseViewport = use;
  250. m_rgViewport[ 0 ] = left;
  251. m_rgViewport[ 1 ] = top;
  252. m_rgViewport[ 2 ] = right;
  253. m_rgViewport[ 3 ] = bottom;
  254. }
  255. //-----------------------------------------------------------------------------
  256. void BitmapImage::SetBitmap( const Bitmap_t &bitmap )
  257. {
  258. if ( m_nTextureId == -1 || !m_bProcedural )
  259. {
  260. DestroyTexture();
  261. m_nTextureId = vgui::surface()->CreateNewTextureID( true );
  262. m_bProcedural = true;
  263. }
  264. vgui::surface()->DrawSetTextureRGBA( m_nTextureId, bitmap.GetBits(), bitmap.Width(), bitmap.Height(), 1, true );
  265. // Initialize render size, if we don't already have one
  266. if ( m_Size[0] == 0 )
  267. {
  268. m_Size[0] = bitmap.Width();
  269. m_Size[1] = bitmap.Height();
  270. }
  271. }