UIBatch.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "PixelShader.h"
  25. #include "Renderer.h"
  26. #include "Texture.h"
  27. #include "UIElement.h"
  28. #include "VertexShader.h"
  29. #include "DebugNew.h"
  30. void UIBatch::begin(std::vector<UIQuad>* quads)
  31. {
  32. if (quads)
  33. {
  34. mQuads = quads;
  35. mQuadStart = mQuads->size();
  36. mQuadCount = 0;
  37. }
  38. }
  39. void UIBatch::addQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY)
  40. {
  41. if (!mQuads)
  42. return;
  43. static UIQuad quad;
  44. const IntVector2& screenPos = element.getScreenPosition();
  45. quad.mLeft = x + screenPos.mX;
  46. quad.mTop = y + screenPos.mY;
  47. quad.mRight = quad.mLeft + width;
  48. quad.mBottom = quad.mTop + height;
  49. quad.mLeftUV = texOffsetX;
  50. quad.mTopUV = texOffsetY;
  51. quad.mRightUV = texOffsetX + width;
  52. quad.mBottomUV = texOffsetY + height;
  53. if (element.hasColorGradient())
  54. {
  55. quad.mTopLeftColor = getInterpolatedColor(element, x, y);
  56. quad.mTopRightColor = getInterpolatedColor(element, x + width, y);
  57. quad.mBottomLeftColor = getInterpolatedColor(element, x, y + height);
  58. quad.mBottomRightColor = getInterpolatedColor(element, x + width, y + height);
  59. }
  60. else
  61. {
  62. Color color = element.getColor(C_TOPLEFT);
  63. if (element.isHovering())
  64. color += element.getHoverColor();
  65. color.mA *= element.getDerivedOpacity();
  66. // If alpha is 0, nothing will be rendered, so exit without adding the quad
  67. if (color.mA <= 0.0f)
  68. return;
  69. unsigned uintColor = getD3DColor(color);
  70. quad.mTopLeftColor = uintColor;
  71. quad.mTopRightColor = uintColor;
  72. quad.mBottomLeftColor = uintColor;
  73. quad.mBottomRightColor = uintColor;
  74. }
  75. mQuads->push_back(quad);
  76. mQuadCount++;
  77. }
  78. void UIBatch::addQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  79. {
  80. if (!mQuads)
  81. return;
  82. static UIQuad quad;
  83. const IntVector2& screenPos = element.getScreenPosition();
  84. quad.mLeft = x + screenPos.mX;
  85. quad.mTop = y + screenPos.mY;
  86. quad.mRight = quad.mLeft + width;
  87. quad.mBottom = quad.mTop + height;
  88. quad.mLeftUV = texOffsetX;
  89. quad.mTopUV = texOffsetY;
  90. quad.mRightUV = texOffsetX + texWidth;
  91. quad.mBottomUV = texOffsetY + texHeight;
  92. if (element.hasColorGradient())
  93. {
  94. quad.mTopLeftColor = getInterpolatedColor(element, x, y);
  95. quad.mTopRightColor = getInterpolatedColor(element, x + width, y);
  96. quad.mBottomLeftColor = getInterpolatedColor(element, x, y + height);
  97. quad.mBottomRightColor = getInterpolatedColor(element, x + width, y + height);
  98. }
  99. else
  100. {
  101. Color color = element.getColor(C_TOPLEFT);
  102. if (element.isHovering())
  103. color += element.getHoverColor();
  104. color.mA *= element.getDerivedOpacity();
  105. // If alpha is 0, nothing will be rendered, so exit without adding the quad
  106. if (color.mA <= 0.0f)
  107. return;
  108. unsigned uintColor = getD3DColor(color);
  109. quad.mTopLeftColor = uintColor;
  110. quad.mTopRightColor = uintColor;
  111. quad.mBottomLeftColor = uintColor;
  112. quad.mBottomRightColor = uintColor;
  113. }
  114. mQuads->push_back(quad);
  115. mQuadCount++;
  116. }
  117. bool UIBatch::merge(const UIBatch& batch)
  118. {
  119. if ((batch.mBlendMode != mBlendMode) ||
  120. (batch.mScissor != mScissor) ||
  121. (batch.mTexture != mTexture) ||
  122. (batch.mQuads != mQuads) ||
  123. (batch.mQuadStart != mQuadStart + mQuadCount))
  124. return false;
  125. mQuadCount += batch.mQuadCount;
  126. return true;
  127. }
  128. void UIBatch::draw(Renderer* renderer, VertexShader* vs, PixelShader* ps) const
  129. {
  130. if ((!mQuads) || (!mQuadCount))
  131. return;
  132. // Use alpha test if not alpha blending
  133. if ((mBlendMode != BLEND_ALPHA) && (mBlendMode != BLEND_ADDALPHA) && (mBlendMode != BLEND_PREMULALPHA))
  134. renderer->setAlphaTest(true, CMP_GREATEREQUAL, 0.5f);
  135. else
  136. renderer->setAlphaTest(false);
  137. renderer->setBlendMode(mBlendMode);
  138. renderer->setScissorTest(true, mScissor);
  139. renderer->setTexture(0, mTexture);
  140. renderer->setVertexShader(vs);
  141. renderer->setPixelShader(ps);
  142. static const Vector2 posAdjust(0.5f, 0.5f);
  143. Vector2 invScreenSize(1.0f / (float)renderer->getWidth(), 1.0f / (float)renderer->getHeight());
  144. const std::vector<UIQuad>& quads = *mQuads;
  145. if (mTexture)
  146. {
  147. Vector2 invTextureSize(1.0f / (float)mTexture->getWidth(), 1.0f / (float)mTexture->getHeight());
  148. renderer->beginImmediate(TRIANGLE_LIST, mQuadCount * 6, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  149. float* dest = (float*)renderer->getImmediateDataPtr();
  150. for (unsigned i = mQuadStart; i < mQuadStart + mQuadCount; ++i)
  151. {
  152. const UIQuad& quad = quads[i];
  153. static Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  154. topLeft = (Vector2((float)quad.mLeft, (float)quad.mTop) - posAdjust) * invScreenSize;
  155. bottomRight = (Vector2((float)quad.mRight, (float)quad.mBottom) - posAdjust) * invScreenSize;
  156. topLeftUV = Vector2((float)quad.mLeftUV, (float)quad.mTopUV) * invTextureSize;
  157. bottomRightUV = Vector2((float)quad.mRightUV, (float)quad.mBottomUV) * invTextureSize;
  158. *dest++ = topLeft.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  159. *((unsigned*)dest) = quads[i].mTopLeftColor; dest++;
  160. *dest++ = topLeftUV.mX; *dest++ = topLeftUV.mY;
  161. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  162. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  163. *dest++ = bottomRightUV.mX; *dest++ = topLeftUV.mY;
  164. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  165. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  166. *dest++ = topLeftUV.mX; *dest++ = bottomRightUV.mY;
  167. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  168. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  169. *dest++ = bottomRightUV.mX; *dest++ = topLeftUV.mY;
  170. *dest++ = bottomRight.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  171. *((unsigned*)dest) = quads[i].mBottomRightColor; dest++;
  172. *dest++ = bottomRightUV.mX; *dest++ = bottomRightUV.mY;
  173. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  174. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  175. *dest++ = topLeftUV.mX; *dest++ = bottomRightUV.mY;
  176. }
  177. renderer->endImmediate();
  178. }
  179. else
  180. {
  181. renderer->beginImmediate(TRIANGLE_LIST, mQuadCount * 6, MASK_POSITION | MASK_COLOR);
  182. float* dest = (float*)renderer->getImmediateDataPtr();
  183. for (unsigned i = mQuadStart; i < mQuadStart + mQuadCount; ++i)
  184. {
  185. const UIQuad& quad = quads[i];
  186. static Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  187. topLeft = (Vector2((float)quad.mLeft, (float)quad.mTop) - posAdjust) * invScreenSize;
  188. bottomRight = (Vector2((float)quad.mRight, (float)quad.mBottom) - posAdjust) * invScreenSize;
  189. *dest++ = topLeft.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  190. *((unsigned*)dest) = quads[i].mTopLeftColor; dest++;
  191. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  192. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  193. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  194. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  195. *dest++ = bottomRight.mX; *dest++ = topLeft.mY; *dest++ = 0.0f;
  196. *((unsigned*)dest) = quads[i].mTopRightColor; dest++;
  197. *dest++ = bottomRight.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  198. *((unsigned*)dest) = quads[i].mBottomRightColor; dest++;
  199. *dest++ = topLeft.mX; *dest++ = bottomRight.mY; *dest++ = 0.0f;
  200. *((unsigned*)dest) = quads[i].mBottomLeftColor; dest++;
  201. }
  202. renderer->endImmediate();
  203. }
  204. }
  205. void UIBatch::addOrMerge(const UIBatch& batch, std::vector<UIBatch>& batches)
  206. {
  207. if (!batches.size())
  208. batches.push_back(batch);
  209. else
  210. {
  211. if (batches.back().merge(batch))
  212. return;
  213. else
  214. batches.push_back(batch);
  215. }
  216. }
  217. unsigned UIBatch::getInterpolatedColor(UIElement& element, int x, int y)
  218. {
  219. const IntVector2& size = element.getSize();
  220. if ((size.mX) && (size.mY))
  221. {
  222. float cLerpX = clamp((float)x / (float)size.mX, 0.0f, 1.0f);
  223. float cLerpY = clamp((float)y / (float)size.mY, 0.0f, 1.0f);
  224. Color topColor = element.getColor(C_TOPLEFT).lerp(element.getColor(C_TOPRIGHT), cLerpX);
  225. Color bottomColor = element.getColor(C_BOTTOMLEFT).lerp(element.getColor(C_BOTTOMRIGHT), cLerpX);
  226. Color color = topColor.lerp(bottomColor, cLerpY);
  227. if (element.isHovering())
  228. color += element.getHoverColor();
  229. color.mA *= element.getDerivedOpacity();
  230. return getD3DColor(color);
  231. }
  232. else
  233. {
  234. Color color = element.getColor(C_TOPLEFT);
  235. if (element.isHovering())
  236. color += element.getHoverColor();
  237. color.mA *= element.getDerivedOpacity();
  238. return getD3DColor(color);
  239. }
  240. }