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.

245 lines
6.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #include <vgui/IBorder.h>
  9. #include <vgui/ISurface.h>
  10. #include <vgui/IScheme.h>
  11. #include <vgui/IBorder.h>
  12. #include <keyvalues.h>
  13. #include <vgui_controls/ScalableImagePanel.h>
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include <tier0/memdbgon.h>
  16. using namespace vgui;
  17. DECLARE_BUILD_FACTORY( ScalableImagePanel );
  18. //-----------------------------------------------------------------------------
  19. // Purpose:
  20. //-----------------------------------------------------------------------------
  21. ScalableImagePanel::ScalableImagePanel(Panel *parent, const char *name) : Panel(parent, name)
  22. {
  23. m_iSrcCornerHeight = 0;
  24. m_iSrcCornerWidth = 0;
  25. m_iCornerHeight = 0;
  26. m_iCornerWidth = 0;
  27. m_pszImageName = NULL;
  28. m_pszDrawColorName = NULL;
  29. m_DrawColor = Color(255,255,255,255);
  30. m_iTextureID = surface()->CreateNewTextureID();
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose:
  34. //-----------------------------------------------------------------------------
  35. ScalableImagePanel::~ScalableImagePanel()
  36. {
  37. delete [] m_pszImageName;
  38. delete [] m_pszDrawColorName;
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose:
  42. //-----------------------------------------------------------------------------
  43. void ScalableImagePanel::SetImage(const char *imageName)
  44. {
  45. delete [] m_pszImageName;
  46. m_pszImageName = NULL;
  47. if (*imageName)
  48. {
  49. int len = Q_strlen(imageName) + 1 + 5; // 5 for "vgui/"
  50. delete [] m_pszImageName;
  51. m_pszImageName = new char[ len ];
  52. Q_snprintf( m_pszImageName, len, "vgui/%s", imageName );
  53. InvalidateLayout();
  54. }
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. void ScalableImagePanel::PaintBackground()
  60. {
  61. int wide, tall;
  62. GetSize(wide, tall);
  63. surface()->DrawSetColor( m_DrawColor.r(), m_DrawColor.g(), m_DrawColor.b(), GetAlpha() );
  64. surface()->DrawSetTexture( m_iTextureID );
  65. int x = 0;
  66. int y = 0;
  67. float uvx = 0;
  68. float uvy = 0;
  69. float uvw, uvh;
  70. float drawW, drawH;
  71. int row, col;
  72. for ( row=0;row<3;row++ )
  73. {
  74. x = 0;
  75. uvx = 0;
  76. if ( row == 0 || row == 2 )
  77. {
  78. //uvh - row 0 or 2, is src_corner_height
  79. uvh = m_flCornerHeightPercent;
  80. drawH = m_iCornerHeight;
  81. }
  82. else
  83. {
  84. //uvh - row 1, is tall - ( 2 * src_corner_height ) ( min 0 )
  85. uvh = MAX( 1.0 - 2 * m_flCornerHeightPercent, 0.0f );
  86. drawH = MAX( 0, ( tall - 2 * m_iCornerHeight ) );
  87. }
  88. for ( col=0;col<3;col++ )
  89. {
  90. if ( col == 0 || col == 2 )
  91. {
  92. //uvw - col 0 or 2, is src_corner_width
  93. uvw = m_flCornerWidthPercent;
  94. drawW = m_iCornerWidth;
  95. }
  96. else
  97. {
  98. //uvw - col 1, is wide - ( 2 * src_corner_width ) ( min 0 )
  99. uvw = MAX( 1.0 - 2 * m_flCornerWidthPercent, 0.0f );
  100. drawW = MAX( 0, ( wide - 2 * m_iCornerWidth ) );
  101. }
  102. Vector2D uv11( uvx, uvy );
  103. Vector2D uv21( uvx+uvw, uvy );
  104. Vector2D uv22( uvx+uvw, uvy+uvh );
  105. Vector2D uv12( uvx, uvy+uvh );
  106. vgui::Vertex_t verts[4];
  107. verts[0].Init( Vector2D( x, y ), uv11 );
  108. verts[1].Init( Vector2D( x+drawW, y ), uv21 );
  109. verts[2].Init( Vector2D( x+drawW, y+drawH ), uv22 );
  110. verts[3].Init( Vector2D( x, y+drawH ), uv12 );
  111. vgui::surface()->DrawTexturedPolygon( 4, verts );
  112. x += drawW;
  113. uvx += uvw;
  114. }
  115. y += drawH;
  116. uvy += uvh;
  117. }
  118. vgui::surface()->DrawSetTexture(0);
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Purpose: Gets control settings for editing
  122. //-----------------------------------------------------------------------------
  123. void ScalableImagePanel::GetSettings(KeyValues *outResourceData)
  124. {
  125. BaseClass::GetSettings(outResourceData);
  126. if (m_pszDrawColorName)
  127. {
  128. outResourceData->SetString("drawcolor", m_pszDrawColorName);
  129. }
  130. outResourceData->SetInt("src_corner_height", m_iSrcCornerHeight);
  131. outResourceData->SetInt("src_corner_width", m_iSrcCornerWidth);
  132. outResourceData->SetInt("draw_corner_height", m_iCornerHeight);
  133. outResourceData->SetInt("draw_corner_width", m_iCornerWidth);
  134. if (m_pszImageName)
  135. {
  136. outResourceData->SetString("image", m_pszImageName);
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose: Applies designer settings from res file
  141. //-----------------------------------------------------------------------------
  142. void ScalableImagePanel::ApplySettings(KeyValues *inResourceData)
  143. {
  144. BaseClass::ApplySettings(inResourceData);
  145. delete [] m_pszDrawColorName;
  146. m_pszDrawColorName = NULL;
  147. const char *pszDrawColor = inResourceData->GetString("drawcolor", "");
  148. if (*pszDrawColor)
  149. {
  150. int r = 0, g = 0, b = 0, a = 255;
  151. int len = Q_strlen(pszDrawColor) + 1;
  152. m_pszDrawColorName = new char[ len ];
  153. Q_strncpy( m_pszDrawColorName, pszDrawColor, len );
  154. if (sscanf(pszDrawColor, "%d %d %d %d", &r, &g, &b, &a) >= 3)
  155. {
  156. // it's a direct color
  157. m_DrawColor = Color(r, g, b, a);
  158. }
  159. else
  160. {
  161. IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  162. m_DrawColor = pScheme->GetColor(pszDrawColor, Color(0, 0, 0, 0));
  163. }
  164. }
  165. m_iSrcCornerHeight = inResourceData->GetInt( "src_corner_height" );
  166. m_iSrcCornerWidth = inResourceData->GetInt( "src_corner_width" );
  167. m_iCornerHeight = inResourceData->GetInt( "draw_corner_height" );
  168. m_iCornerWidth = inResourceData->GetInt( "draw_corner_width" );
  169. if ( IsProportional() )
  170. {
  171. // scale the x and y up to our screen co-ords
  172. m_iCornerHeight = scheme()->GetProportionalScaledValueEx(GetScheme(), m_iCornerHeight);
  173. m_iCornerWidth = scheme()->GetProportionalScaledValueEx(GetScheme(), m_iCornerWidth);
  174. }
  175. const char *imageName = inResourceData->GetString("image", "");
  176. SetImage( imageName );
  177. InvalidateLayout();
  178. }
  179. void ScalableImagePanel::PerformLayout( void )
  180. {
  181. if ( m_pszImageName )
  182. {
  183. surface()->DrawSetTextureFile( m_iTextureID, m_pszImageName, true, false);
  184. }
  185. // get image dimensions, compare to m_iSrcCornerHeight, m_iSrcCornerWidth
  186. int wide,tall;
  187. surface()->DrawGetTextureSize( m_iTextureID, wide, tall );
  188. m_flCornerWidthPercent = ( wide > 0 ) ? ( (float)m_iSrcCornerWidth / (float)wide ) : 0;
  189. m_flCornerHeightPercent = ( tall > 0 ) ? ( (float)m_iSrcCornerHeight / (float)tall ) : 0;
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Purpose: Describes editing details
  193. //-----------------------------------------------------------------------------
  194. const char *ScalableImagePanel::GetDescription()
  195. {
  196. static char buf[1024];
  197. _snprintf(buf, sizeof(buf), "%s string image, int src_corner_height, int src_corner_width, int draw_corner_height, int draw_corner_width", BaseClass::GetDescription());
  198. return buf;
  199. }