UIBatch.cpp 12 KB

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