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.

212 lines
5.4 KiB

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