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.

265 lines
7.2 KiB

  1. //========= Copyright 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_flCornerWidthPercent = 0;
  31. m_flCornerHeightPercent = 0;
  32. m_iTextureID = surface()->CreateNewTextureID();
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Purpose:
  36. //-----------------------------------------------------------------------------
  37. ScalableImagePanel::~ScalableImagePanel()
  38. {
  39. delete [] m_pszImageName;
  40. delete [] m_pszDrawColorName;
  41. if ( vgui::surface() && m_iTextureID != -1 )
  42. {
  43. vgui::surface()->DestroyTextureID( m_iTextureID );
  44. m_iTextureID = -1;
  45. }
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose:
  49. //-----------------------------------------------------------------------------
  50. void ScalableImagePanel::SetImage(const char *imageName)
  51. {
  52. if ( *imageName )
  53. {
  54. char szImage[MAX_PATH];
  55. const char *pszDir = "vgui/";
  56. int len = Q_strlen(imageName) + 1;
  57. len += strlen(pszDir);
  58. Q_snprintf( szImage, len, "%s%s", pszDir, imageName );
  59. if ( m_pszImageName && V_stricmp( szImage, m_pszImageName ) == 0 )
  60. return;
  61. delete [] m_pszImageName;
  62. m_pszImageName = new char[ len ];
  63. Q_strncpy(m_pszImageName, szImage, len );
  64. }
  65. else
  66. {
  67. delete [] m_pszImageName;
  68. m_pszImageName = NULL;
  69. }
  70. InvalidateLayout();
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. void ScalableImagePanel::PaintBackground()
  76. {
  77. int wide, tall;
  78. GetSize(wide, tall);
  79. surface()->DrawSetColor( m_DrawColor.r(), m_DrawColor.g(), m_DrawColor.b(), GetAlpha() );
  80. surface()->DrawSetTexture( m_iTextureID );
  81. int x = 0;
  82. int y = 0;
  83. float uvx = 0;
  84. float uvy = 0;
  85. float uvw = 0, uvh = 0;
  86. float drawW, drawH;
  87. int row, col;
  88. for ( row=0;row<3;row++ )
  89. {
  90. x = 0;
  91. uvx = 0;
  92. if ( row == 0 || row == 2 )
  93. {
  94. //uvh - row 0 or 2, is src_corner_height
  95. uvh = m_flCornerHeightPercent;
  96. drawH = m_iCornerHeight;
  97. }
  98. else
  99. {
  100. //uvh - row 1, is tall - ( 2 * src_corner_height ) ( min 0 )
  101. uvh = max( 1.f - 2.f * m_flCornerHeightPercent, 0.0f );
  102. drawH = max( 0, ( tall - 2 * m_iCornerHeight ) );
  103. }
  104. for ( col=0;col<3;col++ )
  105. {
  106. if ( col == 0 || col == 2 )
  107. {
  108. //uvw - col 0 or 2, is src_corner_width
  109. uvw = m_flCornerWidthPercent;
  110. drawW = m_iCornerWidth;
  111. }
  112. else
  113. {
  114. //uvw - col 1, is wide - ( 2 * src_corner_width ) ( min 0 )
  115. uvw = max( 1.f - 2.f * m_flCornerWidthPercent, 0.0f );
  116. drawW = max( 0, ( wide - 2 * m_iCornerWidth ) );
  117. }
  118. Vector2D uv11( uvx, uvy );
  119. Vector2D uv21( uvx+uvw, uvy );
  120. Vector2D uv22( uvx+uvw, uvy+uvh );
  121. Vector2D uv12( uvx, uvy+uvh );
  122. vgui::Vertex_t verts[4];
  123. verts[0].Init( Vector2D( x, y ), uv11 );
  124. verts[1].Init( Vector2D( x+drawW, y ), uv21 );
  125. verts[2].Init( Vector2D( x+drawW, y+drawH ), uv22 );
  126. verts[3].Init( Vector2D( x, y+drawH ), uv12 );
  127. vgui::surface()->DrawTexturedPolygon( 4, verts );
  128. x += drawW;
  129. uvx += uvw;
  130. }
  131. y += drawH;
  132. uvy += uvh;
  133. }
  134. vgui::surface()->DrawSetTexture(0);
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Purpose: Gets control settings for editing
  138. //-----------------------------------------------------------------------------
  139. void ScalableImagePanel::GetSettings(KeyValues *outResourceData)
  140. {
  141. BaseClass::GetSettings(outResourceData);
  142. if (m_pszDrawColorName)
  143. {
  144. outResourceData->SetString("drawcolor", m_pszDrawColorName);
  145. }
  146. outResourceData->SetInt("src_corner_height", m_iSrcCornerHeight);
  147. outResourceData->SetInt("src_corner_width", m_iSrcCornerWidth);
  148. outResourceData->SetInt("draw_corner_height", m_iCornerHeight);
  149. outResourceData->SetInt("draw_corner_width", m_iCornerWidth);
  150. if (m_pszImageName)
  151. {
  152. outResourceData->SetString("image", m_pszImageName);
  153. }
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose: Applies designer settings from res file
  157. //-----------------------------------------------------------------------------
  158. void ScalableImagePanel::ApplySettings(KeyValues *inResourceData)
  159. {
  160. BaseClass::ApplySettings(inResourceData);
  161. delete [] m_pszDrawColorName;
  162. m_pszDrawColorName = NULL;
  163. const char *pszDrawColor = inResourceData->GetString("drawcolor", "");
  164. if (*pszDrawColor)
  165. {
  166. int r = 0, g = 0, b = 0, a = 255;
  167. int len = Q_strlen(pszDrawColor) + 1;
  168. m_pszDrawColorName = new char[ len ];
  169. Q_strncpy( m_pszDrawColorName, pszDrawColor, len );
  170. if (sscanf(pszDrawColor, "%d %d %d %d", &r, &g, &b, &a) >= 3)
  171. {
  172. // it's a direct color
  173. m_DrawColor = Color(r, g, b, a);
  174. }
  175. else
  176. {
  177. IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  178. m_DrawColor = pScheme->GetColor(pszDrawColor, Color(0, 0, 0, 0));
  179. }
  180. }
  181. m_iSrcCornerHeight = inResourceData->GetInt( "src_corner_height" );
  182. m_iSrcCornerWidth = inResourceData->GetInt( "src_corner_width" );
  183. m_iCornerHeight = inResourceData->GetInt( "draw_corner_height" );
  184. m_iCornerWidth = inResourceData->GetInt( "draw_corner_width" );
  185. if ( IsProportional() )
  186. {
  187. // scale the x and y up to our screen co-ords
  188. m_iCornerHeight = scheme()->GetProportionalScaledValueEx(GetScheme(), m_iCornerHeight);
  189. m_iCornerWidth = scheme()->GetProportionalScaledValueEx(GetScheme(), m_iCornerWidth);
  190. }
  191. const char *imageName = inResourceData->GetString("image", "");
  192. SetImage( imageName );
  193. InvalidateLayout();
  194. }
  195. void ScalableImagePanel::PerformLayout( void )
  196. {
  197. if ( m_pszImageName )
  198. {
  199. surface()->DrawSetTextureFile( m_iTextureID, m_pszImageName, true, false);
  200. }
  201. // get image dimensions, compare to m_iSrcCornerHeight, m_iSrcCornerWidth
  202. int wide,tall;
  203. surface()->DrawGetTextureSize( m_iTextureID, wide, tall );
  204. m_flCornerWidthPercent = ( wide > 0 ) ? ( (float)m_iSrcCornerWidth / (float)wide ) : 0;
  205. m_flCornerHeightPercent = ( tall > 0 ) ? ( (float)m_iSrcCornerHeight / (float)tall ) : 0;
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Purpose: Describes editing details
  209. //-----------------------------------------------------------------------------
  210. const char *ScalableImagePanel::GetDescription()
  211. {
  212. static char buf[1024];
  213. _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());
  214. return buf;
  215. }