UIBatch.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "Graphics.h"
  25. #include "ShaderVariation.h"
  26. #include "Texture.h"
  27. #include "UIElement.h"
  28. #include "DebugNew.h"
  29. void UIBatch::Begin(PODVector<UIQuad>* quads)
  30. {
  31. if (quads)
  32. {
  33. quads_ = quads;
  34. quadStart_ = quads_->Size();
  35. quadCount_ = 0;
  36. }
  37. }
  38. void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY)
  39. {
  40. if (!quads_)
  41. return;
  42. UIQuad quad;
  43. const IntVector2& screenPos = element.GetScreenPosition();
  44. quad.left_ = x + screenPos.x_;
  45. quad.top_ = y + screenPos.y_;
  46. quad.right_ = quad.left_ + width;
  47. quad.bottom_ = quad.top_ + height;
  48. quad.leftUV_ = texOffsetX;
  49. quad.topUV_ = texOffsetY;
  50. quad.rightUV_ = texOffsetX + width;
  51. quad.bottomUV_ = texOffsetY + height;
  52. if (element.HasColorGradient())
  53. {
  54. quad.topLeftColor_ = GetInterpolatedColor(element, x, y);
  55. quad.topRightColor_ = GetInterpolatedColor(element, x + width, y);
  56. quad.bottomLeftColor_ = GetInterpolatedColor(element, x, y + height);
  57. quad.bottomRightColor_ = GetInterpolatedColor(element, x + width, y + height);
  58. }
  59. else
  60. {
  61. unsigned uintColor = element.GetUIntColor();
  62. // If alpha is 0, nothing will be rendered, so do not add the quad
  63. if (!(uintColor & 0xff000000))
  64. return;
  65. quad.topLeftColor_ = uintColor;
  66. quad.topRightColor_ = uintColor;
  67. quad.bottomLeftColor_ = uintColor;
  68. quad.bottomRightColor_ = uintColor;
  69. }
  70. quads_->Push(quad);
  71. quadCount_++;
  72. }
  73. void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  74. int texHeight)
  75. {
  76. if (!quads_)
  77. return;
  78. UIQuad quad;
  79. const IntVector2& screenPos = element.GetScreenPosition();
  80. quad.left_ = x + screenPos.x_;
  81. quad.top_ = y + screenPos.y_;
  82. quad.right_ = quad.left_ + width;
  83. quad.bottom_ = quad.top_ + height;
  84. quad.leftUV_ = texOffsetX;
  85. quad.topUV_ = texOffsetY;
  86. quad.rightUV_ = texOffsetX + texWidth;
  87. quad.bottomUV_ = texOffsetY + texHeight;
  88. if (element.HasColorGradient())
  89. {
  90. quad.topLeftColor_ = GetInterpolatedColor(element, x, y);
  91. quad.topRightColor_ = GetInterpolatedColor(element, x + width, y);
  92. quad.bottomLeftColor_ = GetInterpolatedColor(element, x, y + height);
  93. quad.bottomRightColor_ = GetInterpolatedColor(element, x + width, y + height);
  94. }
  95. else
  96. {
  97. unsigned uintColor = element.GetUIntColor();
  98. // If alpha is 0, nothing will be rendered, so do not add the quad
  99. if (!(uintColor & 0xff000000))
  100. return;
  101. quad.topLeftColor_ = uintColor;
  102. quad.topRightColor_ = uintColor;
  103. quad.bottomLeftColor_ = uintColor;
  104. quad.bottomRightColor_ = uintColor;
  105. }
  106. quads_->Push(quad);
  107. quadCount_++;
  108. }
  109. void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
  110. int texHeight, const Color& color)
  111. {
  112. if (!quads_)
  113. return;
  114. UIQuad quad;
  115. const IntVector2& screenPos = element.GetScreenPosition();
  116. quad.left_ = x + screenPos.x_;
  117. quad.top_ = y + screenPos.y_;
  118. quad.right_ = quad.left_ + width;
  119. quad.bottom_ = quad.top_ + height;
  120. quad.leftUV_ = texOffsetX;
  121. quad.topUV_ = texOffsetY;
  122. quad.rightUV_ = texOffsetX + texWidth;
  123. quad.bottomUV_ = texOffsetY + texHeight;
  124. Color derivedColor(color.r_, color.g_, color.b_, color.a_ * element.GetDerivedOpacity());
  125. // If alpha is 0, nothing will be rendered, so exit without adding the quad
  126. if (derivedColor.a_ <= 0.0f)
  127. return;
  128. unsigned uintColor = derivedColor.ToUInt();
  129. quad.topLeftColor_ = uintColor;
  130. quad.topRightColor_ = uintColor;
  131. quad.bottomLeftColor_ = uintColor;
  132. quad.bottomRightColor_ = uintColor;
  133. quads_->Push(quad);
  134. quadCount_++;
  135. }
  136. bool UIBatch::Merge(const UIBatch& batch)
  137. {
  138. if ((batch.blendMode_ != blendMode_) ||
  139. (batch.scissor_ != scissor_) ||
  140. (batch.texture_ != texture_) ||
  141. (batch.quads_ != quads_) ||
  142. (batch.quadStart_ != quadStart_ + quadCount_))
  143. return false;
  144. quadCount_ += batch.quadCount_;
  145. return true;
  146. }
  147. void UIBatch::UpdateGeometry(Graphics* graphics, void* lockedData)
  148. {
  149. if (!quadCount_)
  150. return;
  151. #ifdef USE_OPENGL
  152. Vector2 posAdjust(Vector2::ZERO);
  153. #else
  154. Vector2 posAdjust(0.5f, 0.5f);
  155. #endif
  156. Vector2 invScreenSize(1.0f / (float)graphics->GetWidth(), 1.0f / (float)graphics->GetHeight());
  157. float* dest = (float*)lockedData;
  158. if (texture_)
  159. {
  160. Vector2 invTextureSize(1.0f / (float)texture_->GetWidth(), 1.0f / (float)texture_->GetHeight());
  161. for (unsigned i = 0; i < quadCount_; ++i)
  162. {
  163. const UIQuad& quad = quads_->At(quadStart_ + i);
  164. Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  165. topLeft = (Vector2((float)quad.left_, (float)quad.top_) - posAdjust) * invScreenSize;
  166. bottomRight = (Vector2((float)quad.right_, (float)quad.bottom_) - posAdjust) * invScreenSize;
  167. topLeftUV = Vector2((float)quad.leftUV_, (float)quad.topUV_) * invTextureSize;
  168. bottomRightUV = Vector2((float)quad.rightUV_, (float)quad.bottomUV_) * invTextureSize;
  169. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  170. *((unsigned*)dest) = quad.topLeftColor_; dest++;
  171. *dest++ = topLeftUV.x_; *dest++ = topLeftUV.y_;
  172. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  173. *((unsigned*)dest) = quad.topRightColor_; dest++;
  174. *dest++ = bottomRightUV.x_; *dest++ = topLeftUV.y_;
  175. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  176. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  177. *dest++ = topLeftUV.x_; *dest++ = bottomRightUV.y_;
  178. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  179. *((unsigned*)dest) = quad.topRightColor_; dest++;
  180. *dest++ = bottomRightUV.x_; *dest++ = topLeftUV.y_;
  181. *dest++ = bottomRight.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  182. *((unsigned*)dest) = quad.bottomRightColor_; dest++;
  183. *dest++ = bottomRightUV.x_; *dest++ = bottomRightUV.y_;
  184. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  185. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  186. *dest++ = topLeftUV.x_; *dest++ = bottomRightUV.y_;
  187. }
  188. }
  189. else
  190. {
  191. for (unsigned i = 0; i < quadCount_; ++i)
  192. {
  193. const UIQuad& quad = quads_->At(quadStart_ + i);
  194. Vector2 topLeft, bottomRight, topLeftUV, bottomRightUV;
  195. topLeft = (Vector2((float)quad.left_, (float)quad.top_) - posAdjust) * invScreenSize;
  196. bottomRight = (Vector2((float)quad.right_, (float)quad.bottom_) - posAdjust) * invScreenSize;
  197. *dest++ = topLeft.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  198. *((unsigned*)dest) = quad.topLeftColor_; dest++;
  199. dest += 2; // Jump over unused UV coordinates
  200. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  201. *((unsigned*)dest) = quad.topRightColor_; dest++;
  202. dest += 2;
  203. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  204. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  205. dest += 2;
  206. *dest++ = bottomRight.x_; *dest++ = topLeft.y_; *dest++ = 0.0f;
  207. *((unsigned*)dest) = quad.topRightColor_; dest++;
  208. dest += 2;
  209. *dest++ = bottomRight.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  210. *((unsigned*)dest) = quad.bottomRightColor_; dest++;
  211. dest += 2;
  212. *dest++ = topLeft.x_; *dest++ = bottomRight.y_; *dest++ = 0.0f;
  213. *((unsigned*)dest) = quad.bottomLeftColor_; dest++;
  214. dest += 2;
  215. }
  216. }
  217. }
  218. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  219. {
  220. if (!batch.quadCount_)
  221. return;
  222. if (!batches.Size())
  223. batches.Push(batch);
  224. else
  225. {
  226. if (batches.Back().Merge(batch))
  227. return;
  228. else
  229. batches.Push(batch);
  230. }
  231. }
  232. unsigned UIBatch::GetInterpolatedColor(UIElement& element, int x, int y)
  233. {
  234. const IntVector2& size = element.GetSize();
  235. if (size.x_ && size.y_)
  236. {
  237. float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
  238. float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
  239. Color topColor = element.GetColor(C_TOPLEFT).Lerp(element.GetColor(C_TOPRIGHT), cLerpX);
  240. Color bottocolor_ = element.GetColor(C_BOTTOMLEFT).Lerp(element.GetColor(C_BOTTOMRIGHT), cLerpX);
  241. Color color = topColor.Lerp(bottocolor_, cLerpY);
  242. color.a_ *= element.GetDerivedOpacity();
  243. return color.ToUInt();
  244. }
  245. else
  246. {
  247. Color color = element.GetColor(C_TOPLEFT);
  248. color.a_ *= element.GetDerivedOpacity();
  249. return color.ToUInt();
  250. }
  251. }