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.

199 lines
4.9 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "vgui_controls/subrectimage.h"
  7. #include "tier0/dbg.h"
  8. #include "vgui/ISurface.h"
  9. #include "vgui_controls/Controls.h"
  10. // NOTE: This has to be the last file included!
  11. #include "tier0/memdbgon.h"
  12. using namespace vgui;
  13. //-----------------------------------------------------------------------------
  14. // Constructor, destructor
  15. //-----------------------------------------------------------------------------
  16. CSubRectImage::CSubRectImage( const char *filename, bool hardwareFiltered, int subx, int suby, int subw, int subh )
  17. {
  18. SetSize( subw, subh );
  19. sub[ 0 ] = subx;
  20. sub[ 1 ] = suby;
  21. sub[ 2 ] = subw;
  22. sub[ 3 ] = subh;
  23. _filtered = hardwareFiltered;
  24. // HACKHACK - force VGUI materials to be in the vgui/ directory
  25. // This needs to be revisited once GoldSRC is grandfathered off.
  26. //!! need to make this work with goldsrc
  27. int size = strlen(filename) + 1 + strlen("vgui/");
  28. _filename = (char *)malloc( size );
  29. Assert( _filename );
  30. Q_snprintf( _filename, size, "vgui/%s", filename );
  31. _id = 0;
  32. _uploaded = false;
  33. _color = Color(255, 255, 255, 255);
  34. _pos[0] = _pos[1] = 0;
  35. _valid = true;
  36. _wide = subw;
  37. _tall = subh;
  38. ForceUpload();
  39. }
  40. CSubRectImage::~CSubRectImage()
  41. {
  42. if ( _filename )
  43. {
  44. free( _filename );
  45. }
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: data accessor
  49. //-----------------------------------------------------------------------------
  50. void CSubRectImage::GetSize(int &wide, int &tall)
  51. {
  52. wide = _wide;
  53. tall = _tall;
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose: size of the bitmap
  57. //-----------------------------------------------------------------------------
  58. void CSubRectImage::GetContentSize(int &wide, int &tall)
  59. {
  60. wide = 0;
  61. tall = 0;
  62. if (!_valid)
  63. return;
  64. surface()->DrawGetTextureSize(_id, wide, tall);
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: ignored
  68. //-----------------------------------------------------------------------------
  69. void CSubRectImage::SetSize(int x, int y)
  70. {
  71. _wide = x;
  72. _tall = y;
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: data accessor
  76. //-----------------------------------------------------------------------------
  77. void CSubRectImage::SetPos(int x, int y)
  78. {
  79. _pos[0] = x;
  80. _pos[1] = y;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: data accessor
  84. //-----------------------------------------------------------------------------
  85. void CSubRectImage::SetColor(Color col)
  86. {
  87. _color = col;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: returns the file name of the bitmap
  91. //-----------------------------------------------------------------------------
  92. const char *CSubRectImage::GetName()
  93. {
  94. return _filename;
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose: Renders the loaded image, uploading it if necessary
  98. // Assumes a valid image is always returned from uploading
  99. //-----------------------------------------------------------------------------
  100. void CSubRectImage::Paint()
  101. {
  102. if ( !_valid )
  103. return;
  104. // if we don't have an _id then lets make one
  105. if ( !_id )
  106. {
  107. _id = surface()->CreateNewTextureID();
  108. }
  109. // if we have not uploaded yet, lets go ahead and do so
  110. if ( !_uploaded )
  111. {
  112. ForceUpload();
  113. }
  114. // set the texture current, set the color, and draw the biatch
  115. surface()->DrawSetColor( _color[0], _color[1], _color[2], _color[3] );
  116. surface()->DrawSetTexture( _id );
  117. if ( _wide == 0 || _tall == 0 )
  118. return;
  119. int cw, ch;
  120. GetContentSize( cw, ch );
  121. if ( cw == 0 || ch == 0 )
  122. return;
  123. float s[ 2 ];
  124. float t[ 2 ];
  125. s[ 0 ] = (float)sub[ 0 ] / (float)cw;
  126. s[ 1 ] = (float)(sub[ 0 ]+sub[ 2 ]) / (float)cw;
  127. t[ 0 ] = (float)sub[ 1 ] / (float)ch;
  128. t[ 1 ] = (float)(sub[ 1 ]+sub[ 3 ]) / (float)ch;
  129. surface()->DrawTexturedSubRect(
  130. _pos[0],
  131. _pos[1],
  132. _pos[0] + _wide,
  133. _pos[1] + _tall,
  134. s[ 0 ],
  135. t[ 0 ],
  136. s[ 1 ],
  137. t[ 1 ] );
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose: ensures the bitmap has been uploaded
  141. //-----------------------------------------------------------------------------
  142. void CSubRectImage::ForceUpload()
  143. {
  144. if ( !_valid || _uploaded )
  145. return;
  146. if ( !_id )
  147. {
  148. _id = surface()->CreateNewTextureID( false );
  149. }
  150. surface()->DrawSetTextureFile( _id, _filename, _filtered, false );
  151. _uploaded = true;
  152. _valid = surface()->IsTextureIDValid( _id );
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: data accessor
  156. //-----------------------------------------------------------------------------
  157. HTexture CSubRectImage::GetID()
  158. {
  159. return _id;
  160. }
  161. bool CSubRectImage::IsValid()
  162. {
  163. return _valid;
  164. }