BsGUITexture.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsGUITexture.h"
  5. #include "BsImageSprite.h"
  6. #include "BsGUIWidget.h"
  7. #include "BsGUISkin.h"
  8. #include "BsSpriteTexture.h"
  9. #include "BsGUILayoutOptions.h"
  10. #include "BsTexture.h"
  11. namespace BansheeEngine
  12. {
  13. const String& GUITexture::getGUITypeName()
  14. {
  15. static String name = "Texture";
  16. return name;
  17. }
  18. GUITexture::GUITexture(const String& styleName, const HSpriteTexture& texture,
  19. GUIImageScaleMode scale, const GUILayoutOptions& layoutOptions)
  20. :GUIElement(styleName, layoutOptions), mScaleMode(scale), mUsingStyleTexture(false)
  21. {
  22. mImageSprite = bs_new<ImageSprite, PoolAlloc>();
  23. if(texture != nullptr)
  24. {
  25. mActiveTexture = texture;
  26. mUsingStyleTexture = false;
  27. }
  28. else
  29. mUsingStyleTexture = true;
  30. }
  31. GUITexture::~GUITexture()
  32. {
  33. bs_delete<PoolAlloc>(mImageSprite);
  34. }
  35. GUITexture* GUITexture::create(const HSpriteTexture& texture, GUIImageScaleMode scale,
  36. const GUIOptions& layoutOptions, const String& styleName)
  37. {
  38. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  39. texture, scale, GUILayoutOptions::create(layoutOptions));
  40. }
  41. GUITexture* GUITexture::create(const HSpriteTexture& texture, GUIImageScaleMode scale,
  42. const String& styleName)
  43. {
  44. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  45. texture, scale, GUILayoutOptions::create());
  46. }
  47. GUITexture* GUITexture::create(const HSpriteTexture& texture,
  48. const GUIOptions& layoutOptions, const String& styleName)
  49. {
  50. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  51. texture, GUIImageScaleMode::StretchToFit, GUILayoutOptions::create(layoutOptions));
  52. }
  53. GUITexture* GUITexture::create(const HSpriteTexture& texture, const String& styleName)
  54. {
  55. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  56. texture, GUIImageScaleMode::StretchToFit, GUILayoutOptions::create());
  57. }
  58. GUITexture* GUITexture::create(GUIImageScaleMode scale, const GUIOptions& layoutOptions, const String& styleName)
  59. {
  60. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  61. HTexture(), scale, GUILayoutOptions::create(layoutOptions));
  62. }
  63. GUITexture* GUITexture::create(GUIImageScaleMode scale, const String& styleName)
  64. {
  65. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  66. HSpriteTexture(), scale, GUILayoutOptions::create());
  67. }
  68. GUITexture* GUITexture::create(const GUIOptions& layoutOptions, const String& styleName)
  69. {
  70. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  71. HTexture(), GUIImageScaleMode::StretchToFit, GUILayoutOptions::create(layoutOptions));
  72. }
  73. GUITexture* GUITexture::create(const String& styleName)
  74. {
  75. return new (bs_alloc<GUITexture, PoolAlloc>()) GUITexture(getStyleName<GUITexture>(styleName),
  76. HTexture(), GUIImageScaleMode::StretchToFit, GUILayoutOptions::create());
  77. }
  78. void GUITexture::setTexture(const HSpriteTexture& texture)
  79. {
  80. mActiveTexture = texture;
  81. mUsingStyleTexture = false;
  82. markContentAsDirty();
  83. }
  84. UINT32 GUITexture::getNumRenderElements() const
  85. {
  86. return mImageSprite->getNumRenderElements();
  87. }
  88. const GUIMaterialInfo& GUITexture::getMaterial(UINT32 renderElementIdx) const
  89. {
  90. return mImageSprite->getMaterial(renderElementIdx);
  91. }
  92. UINT32 GUITexture::getNumQuads(UINT32 renderElementIdx) const
  93. {
  94. return mImageSprite->getNumQuads(renderElementIdx);
  95. }
  96. void GUITexture::updateRenderElementsInternal()
  97. {
  98. mDesc.width = mWidth;
  99. mDesc.height = mHeight;
  100. mDesc.borderLeft = _getStyle()->border.left;
  101. mDesc.borderRight = _getStyle()->border.right;
  102. mDesc.borderTop = _getStyle()->border.top;
  103. mDesc.borderBottom = _getStyle()->border.bottom;
  104. if(mUsingStyleTexture)
  105. mActiveTexture = _getStyle()->normal.texture;
  106. float optimalWidth = 0.0f;
  107. float optimalHeight = 0.0f;
  108. if(mActiveTexture != nullptr && mActiveTexture.isLoaded())
  109. {
  110. mDesc.texture = mActiveTexture.getInternalPtr();
  111. optimalWidth = (float)mDesc.texture->getTexture()->getWidth();
  112. optimalHeight = (float)mDesc.texture->getTexture()->getHeight();
  113. }
  114. switch (mScaleMode)
  115. {
  116. case GUIImageScaleMode::StretchToFit:
  117. mDesc.uvScale = Vector2(1.0f, 1.0f);
  118. break;
  119. case GUIImageScaleMode::ScaleToFit:
  120. mDesc.uvScale.x = optimalWidth / mWidth;
  121. mDesc.uvScale.y = optimalHeight / mHeight;
  122. if(mDesc.uvScale.x < mDesc.uvScale.y)
  123. {
  124. mDesc.uvScale.x = 1.0f;
  125. mDesc.uvScale.y = (mWidth * (optimalHeight / optimalWidth)) / mHeight;
  126. }
  127. else
  128. {
  129. mDesc.uvScale.x = (mHeight * (optimalWidth / optimalHeight)) / mWidth;
  130. mDesc.uvScale.y = 1.0f;
  131. }
  132. break;
  133. case GUIImageScaleMode::CropToFit:
  134. mDesc.uvScale.x = optimalWidth / mWidth;
  135. mDesc.uvScale.y = optimalHeight / mHeight;
  136. if(mDesc.uvScale.x < mDesc.uvScale.y)
  137. {
  138. mDesc.uvScale.x = (mHeight * (optimalWidth / optimalHeight)) / mWidth;
  139. mDesc.uvScale.y = 1.0f;
  140. }
  141. else
  142. {
  143. mDesc.uvScale.x = 1.0f;
  144. mDesc.uvScale.y = (mWidth * (optimalHeight / optimalWidth)) / mHeight;
  145. }
  146. break;
  147. case GUIImageScaleMode::RepeatToFit:
  148. mDesc.uvScale.x = mWidth / optimalWidth;
  149. mDesc.uvScale.y = mHeight / optimalHeight;
  150. break;
  151. default:
  152. break;
  153. }
  154. mImageSprite->update(mDesc);
  155. GUIElement::updateRenderElementsInternal();
  156. }
  157. void GUITexture::updateClippedBounds()
  158. {
  159. mClippedBounds = mImageSprite->getBounds(mOffset, mClipRect);
  160. }
  161. Vector2I GUITexture::_getOptimalSize() const
  162. {
  163. Vector2I optimalSize;
  164. if(_getLayoutOptions().fixedWidth)
  165. optimalSize.x = _getLayoutOptions().width;
  166. else
  167. {
  168. if(mActiveTexture != nullptr && mActiveTexture.isLoaded())
  169. optimalSize.x = mActiveTexture->getTexture()->getWidth();
  170. else
  171. optimalSize.x = _getLayoutOptions().maxWidth;
  172. }
  173. if(_getLayoutOptions().fixedHeight)
  174. optimalSize.y = _getLayoutOptions().height;
  175. else
  176. {
  177. if(mActiveTexture != nullptr && mActiveTexture.isLoaded())
  178. optimalSize.y = mActiveTexture->getTexture()->getHeight();
  179. else
  180. optimalSize.y = _getLayoutOptions().maxHeight;
  181. }
  182. return optimalSize;
  183. }
  184. void GUITexture::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  185. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  186. {
  187. mImageSprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads, vertexStride, indexStride, renderElementIdx, mOffset, mClipRect);
  188. }
  189. }