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.

290 lines
7.0 KiB

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