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.

217 lines
5.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <vgui_controls/Panel.h>
  10. #include "vgui/IPanel.h"
  11. #include "vgui/IScheme.h"
  12. #include "vgui/ISurface.h"
  13. #include "vgui_internal.h"
  14. #include "ImageBorder.h"
  15. #include "keyvalues.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. using namespace vgui;
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Constructor
  21. //-----------------------------------------------------------------------------
  22. ImageBorder::ImageBorder()
  23. {
  24. _name = NULL;
  25. m_eBackgroundType = IBorder::BACKGROUND_TEXTURED;
  26. m_pszImageName = NULL;
  27. m_iTextureID = g_pSurface->CreateNewTextureID();
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Destructor
  31. //-----------------------------------------------------------------------------
  32. ImageBorder::~ImageBorder()
  33. {
  34. delete [] _name;
  35. if ( m_pszImageName )
  36. {
  37. delete [] m_pszImageName;
  38. }
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose:
  42. //-----------------------------------------------------------------------------
  43. void ImageBorder::SetImage(const char *imageName)
  44. {
  45. if ( m_pszImageName )
  46. {
  47. delete [] m_pszImageName;
  48. m_pszImageName = NULL;
  49. }
  50. if (*imageName)
  51. {
  52. int len = Q_strlen(imageName) + 1 + 5; // 5 for "vgui/"
  53. delete [] m_pszImageName;
  54. m_pszImageName = new char[ len ];
  55. Q_snprintf( m_pszImageName, len, "vgui/%s", imageName );
  56. g_pSurface->DrawSetTextureFile( m_iTextureID, m_pszImageName, true, false);
  57. }
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose:
  61. //-----------------------------------------------------------------------------
  62. void ImageBorder::SetInset(int left,int top,int right,int bottom)
  63. {
  64. _inset[SIDE_LEFT] = left;
  65. _inset[SIDE_TOP] = top;
  66. _inset[SIDE_RIGHT] = right;
  67. _inset[SIDE_BOTTOM] = bottom;
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. void ImageBorder::GetInset(int& left,int& top,int& right,int& bottom)
  73. {
  74. left = _inset[SIDE_LEFT];
  75. top = _inset[SIDE_TOP];
  76. right = _inset[SIDE_RIGHT];
  77. bottom = _inset[SIDE_BOTTOM];
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. //-----------------------------------------------------------------------------
  82. void ImageBorder::Paint(int x, int y, int wide, int tall)
  83. {
  84. Paint(x, y, wide, tall, -1, 0, 0);
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose: Draws the border with the specified size
  88. //-----------------------------------------------------------------------------
  89. void ImageBorder::Paint(int x, int y, int wide, int tall, int breakSide, int breakStart, int breakEnd)
  90. {
  91. if ( !m_pszImageName || !m_pszImageName[0] )
  92. return;
  93. g_pSurface->DrawSetColor( 255, 255, 255, 255 );
  94. g_pSurface->DrawSetTexture( m_iTextureID );
  95. float uvx = 0;
  96. float uvy = 0;
  97. float uvw = 1.0;
  98. float uvh = 1.0;
  99. Vector2D uv11( uvx, uvy );
  100. Vector2D uv21( uvx+uvw, uvy );
  101. Vector2D uv22( uvx+uvw, uvy+uvh );
  102. Vector2D uv12( uvx, uvy+uvh );
  103. if ( m_bTiled )
  104. {
  105. int imageWide, imageTall;
  106. g_pSurface->DrawGetTextureSize( m_iTextureID, imageWide, imageTall );
  107. int y = 0;
  108. while ( y < tall )
  109. {
  110. int x = 0;
  111. while (x < wide)
  112. {
  113. vgui::Vertex_t verts[4];
  114. verts[0].Init( Vector2D( x, y ), uv11 );
  115. verts[1].Init( Vector2D( x+imageWide, y ), uv21 );
  116. verts[2].Init( Vector2D( x+imageWide, y+imageTall ), uv22 );
  117. verts[3].Init( Vector2D( x, y+imageTall ), uv12 );
  118. g_pSurface->DrawTexturedPolygon( 4, verts );
  119. x += imageWide;
  120. }
  121. y += imageTall;
  122. }
  123. }
  124. else
  125. {
  126. vgui::Vertex_t verts[4];
  127. verts[0].Init( Vector2D( x, y ), uv11 );
  128. verts[1].Init( Vector2D( x+wide, y ), uv21 );
  129. verts[2].Init( Vector2D( x+wide, y+tall ), uv22 );
  130. verts[3].Init( Vector2D( x, y+tall ), uv12 );
  131. g_pSurface->DrawTexturedPolygon( 4, verts );
  132. }
  133. g_pSurface->DrawSetTexture(0);
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Purpose:
  137. //-----------------------------------------------------------------------------
  138. void ImageBorder::Paint(VPANEL panel)
  139. {
  140. // get panel size
  141. int wide, tall;
  142. ipanel()->GetSize( panel, wide, tall );
  143. Paint(0, 0, wide, tall, -1, 0, 0);
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Purpose:
  147. //-----------------------------------------------------------------------------
  148. void ImageBorder::ApplySchemeSettings(IScheme *pScheme, KeyValues *inResourceData)
  149. {
  150. m_eBackgroundType = (backgroundtype_e)inResourceData->GetInt("backgroundtype");
  151. m_bTiled = inResourceData->GetBool( "tiled" );
  152. const char *imageName = inResourceData->GetString("image", "");
  153. SetImage( imageName );
  154. m_bPaintFirst = inResourceData->GetBool("paintfirst", true );
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose: data accessor
  158. //-----------------------------------------------------------------------------
  159. const char *ImageBorder::GetName()
  160. {
  161. if (_name)
  162. return _name;
  163. return "";
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose: data accessor
  167. //-----------------------------------------------------------------------------
  168. void ImageBorder::SetName(const char *name)
  169. {
  170. if (_name)
  171. {
  172. delete [] _name;
  173. }
  174. int len = Q_strlen(name) + 1;
  175. _name = new char[ len ];
  176. Q_strncpy( _name, name, len );
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Purpose:
  180. //-----------------------------------------------------------------------------
  181. IBorder::backgroundtype_e ImageBorder::GetBackgroundType()
  182. {
  183. return m_eBackgroundType;
  184. }