BsGUITexture.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUITexture.h"
  4. #include "2D/BsImageSprite.h"
  5. #include "GUI/BsGUISkin.h"
  6. #include "2D/BsSpriteTexture.h"
  7. #include "GUI/BsGUIDimensions.h"
  8. namespace bs
  9. {
  10. const String& GUITexture::getGUITypeName()
  11. {
  12. static String name = "Texture";
  13. return name;
  14. }
  15. GUITexture::GUITexture(const String& styleName, const HSpriteTexture& texture,
  16. TextureScaleMode scale, bool transparent, const GUIDimensions& dimensions)
  17. :GUIElement(styleName, dimensions), mScaleMode(scale), mTransparent(transparent), mUsingStyleTexture(false)
  18. {
  19. mImageSprite = bs_new<ImageSprite>();
  20. if(texture != nullptr)
  21. {
  22. mActiveTexture = texture;
  23. mUsingStyleTexture = false;
  24. }
  25. else
  26. {
  27. mActiveTexture = _getStyle()->normal.texture;
  28. mUsingStyleTexture = true;
  29. }
  30. }
  31. GUITexture::~GUITexture()
  32. {
  33. bs_delete(mImageSprite);
  34. }
  35. GUITexture* GUITexture::create(const HSpriteTexture& texture, TextureScaleMode scale, bool transparent,
  36. const GUIOptions& options, const String& styleName)
  37. {
  38. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  39. texture, scale, transparent, GUIDimensions::create(options));
  40. }
  41. GUITexture* GUITexture::create(const HSpriteTexture& texture, TextureScaleMode scale, bool transparent,
  42. const String& styleName)
  43. {
  44. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  45. texture, scale, transparent, GUIDimensions::create());
  46. }
  47. GUITexture* GUITexture::create(const HSpriteTexture& texture, TextureScaleMode scale,
  48. const GUIOptions& options, const String& styleName)
  49. {
  50. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  51. texture, scale, true, GUIDimensions::create(options));
  52. }
  53. GUITexture* GUITexture::create(const HSpriteTexture& texture, TextureScaleMode scale,
  54. const String& styleName)
  55. {
  56. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  57. texture, scale, true, GUIDimensions::create());
  58. }
  59. GUITexture* GUITexture::create(const HSpriteTexture& texture,
  60. const GUIOptions& options, const String& styleName)
  61. {
  62. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  63. texture, TextureScaleMode::StretchToFit, true, GUIDimensions::create(options));
  64. }
  65. GUITexture* GUITexture::create(const HSpriteTexture& texture, const String& styleName)
  66. {
  67. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  68. texture, TextureScaleMode::StretchToFit, true, GUIDimensions::create());
  69. }
  70. GUITexture* GUITexture::create(TextureScaleMode scale, const GUIOptions& options, const String& styleName)
  71. {
  72. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  73. HSpriteTexture(), scale, true, GUIDimensions::create(options));
  74. }
  75. GUITexture* GUITexture::create(TextureScaleMode scale, const String& styleName)
  76. {
  77. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  78. HSpriteTexture(), scale, true, GUIDimensions::create());
  79. }
  80. GUITexture* GUITexture::create(const GUIOptions& options, const String& styleName)
  81. {
  82. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  83. HSpriteTexture(), TextureScaleMode::StretchToFit, true, GUIDimensions::create(options));
  84. }
  85. GUITexture* GUITexture::create(const String& styleName)
  86. {
  87. return new (bs_alloc<GUITexture>()) GUITexture(getStyleName<GUITexture>(styleName),
  88. HSpriteTexture(), TextureScaleMode::StretchToFit, true, GUIDimensions::create());
  89. }
  90. void GUITexture::setTexture(const HSpriteTexture& texture)
  91. {
  92. Vector2I origSize = mDimensions.calculateSizeRange(_getOptimalSize()).optimal;
  93. mActiveTexture = texture;
  94. mUsingStyleTexture = false;
  95. Vector2I newSize = mDimensions.calculateSizeRange(_getOptimalSize()).optimal;
  96. if (origSize != newSize)
  97. _markLayoutAsDirty();
  98. else
  99. _markContentAsDirty();
  100. }
  101. UINT32 GUITexture::_getNumRenderElements() const
  102. {
  103. return mImageSprite->getNumRenderElements();
  104. }
  105. const SpriteMaterialInfo& GUITexture::_getMaterial(UINT32 renderElementIdx, SpriteMaterial** material) const
  106. {
  107. *material = mImageSprite->getMaterial(renderElementIdx);
  108. return mImageSprite->getMaterialInfo(renderElementIdx);
  109. }
  110. void GUITexture::_getMeshInfo(UINT32 renderElementIdx, UINT32& numVertices, UINT32& numIndices, GUIMeshType& type) const
  111. {
  112. UINT32 numQuads = mImageSprite->getNumQuads(renderElementIdx);
  113. numVertices = numQuads * 4;
  114. numIndices = numQuads * 6;
  115. type = GUIMeshType::Triangle;
  116. }
  117. void GUITexture::updateRenderElementsInternal()
  118. {
  119. mDesc.width = mLayoutData.area.width;
  120. mDesc.height = mLayoutData.area.height;
  121. mDesc.borderLeft = _getStyle()->border.left;
  122. mDesc.borderRight = _getStyle()->border.right;
  123. mDesc.borderTop = _getStyle()->border.top;
  124. mDesc.borderBottom = _getStyle()->border.bottom;
  125. mDesc.transparent = mTransparent;
  126. mDesc.color = getTint();
  127. Vector2I textureSize;
  128. if (SpriteTexture::checkIsLoaded(mActiveTexture))
  129. {
  130. mDesc.texture = mActiveTexture.getInternalPtr();
  131. textureSize.x = mDesc.texture->getWidth();
  132. textureSize.y = mDesc.texture->getHeight();
  133. }
  134. Vector2I destSize(mLayoutData.area.width, mLayoutData.area.height);
  135. mDesc.uvScale = ImageSprite::getTextureUVScale(textureSize, destSize, mScaleMode);
  136. mImageSprite->update(mDesc, (UINT64)_getParentWidget());
  137. GUIElement::updateRenderElementsInternal();
  138. }
  139. void GUITexture::styleUpdated()
  140. {
  141. if (mUsingStyleTexture)
  142. mActiveTexture = _getStyle()->normal.texture;
  143. }
  144. Vector2I GUITexture::_getOptimalSize() const
  145. {
  146. // TODO - Accounting for style dimensions might be redundant here, I'm pretty sure we do that on higher level anyway
  147. Vector2I optimalSize;
  148. if(_getDimensions().fixedWidth())
  149. optimalSize.x = _getDimensions().minWidth;
  150. else
  151. {
  152. if (SpriteTexture::checkIsLoaded(mActiveTexture))
  153. optimalSize.x = mActiveTexture->getWidth();
  154. else
  155. optimalSize.x = _getDimensions().maxWidth;
  156. }
  157. if(_getDimensions().fixedHeight())
  158. optimalSize.y = _getDimensions().minHeight;
  159. else
  160. {
  161. if (SpriteTexture::checkIsLoaded(mActiveTexture))
  162. optimalSize.y = mActiveTexture->getHeight();
  163. else
  164. optimalSize.y = _getDimensions().maxHeight;
  165. }
  166. return optimalSize;
  167. }
  168. void GUITexture::_fillBuffer(UINT8* vertices, UINT32* indices, UINT32 vertexOffset, UINT32 indexOffset,
  169. UINT32 maxNumVerts, UINT32 maxNumIndices, UINT32 renderElementIdx) const
  170. {
  171. UINT8* uvs = vertices + sizeof(Vector2);
  172. UINT32 vertexStride = sizeof(Vector2) * 2;
  173. UINT32 indexStride = sizeof(UINT32);
  174. Vector2I offset(mLayoutData.area.x, mLayoutData.area.y);
  175. mImageSprite->fillBuffer(vertices, uvs, indices, vertexOffset, indexOffset, maxNumVerts, maxNumIndices,
  176. vertexStride, indexStride, renderElementIdx, offset, mLayoutData.getLocalClipRect());
  177. }
  178. }