UIBatch.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Graphics/Graphics.h"
  24. #include "../Graphics/Texture.h"
  25. #include "../UI/UIElement.h"
  26. #include "../DebugNew.h"
  27. namespace Urho3D
  28. {
  29. Vector3 UIBatch::posAdjust(0.0f, 0.0f, 0.0f);
  30. UIBatch::UIBatch() :
  31. element_(0),
  32. blendMode_(BLEND_REPLACE),
  33. texture_(0),
  34. invTextureSize_(Vector2::ONE),
  35. vertexData_(0),
  36. vertexStart_(0),
  37. vertexEnd_(0)
  38. {
  39. SetDefaultColor();
  40. }
  41. UIBatch::UIBatch(UIElement* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
  42. element_(element),
  43. blendMode_(blendMode),
  44. scissor_(scissor),
  45. texture_(texture),
  46. invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
  47. vertexData_(vertexData),
  48. vertexStart_(vertexData->Size()),
  49. vertexEnd_(vertexData->Size())
  50. {
  51. SetDefaultColor();
  52. }
  53. void UIBatch::SetColor(const Color& color, bool overrideAlpha)
  54. {
  55. if (!element_)
  56. overrideAlpha = true;
  57. useGradient_ = false;
  58. color_ =
  59. overrideAlpha ? color.ToUInt() : Color(color.r_, color.g_, color.b_, color.a_ * element_->GetDerivedOpacity()).ToUInt();
  60. }
  61. void UIBatch::SetDefaultColor()
  62. {
  63. if (element_)
  64. {
  65. color_ = element_->GetDerivedColor().ToUInt();
  66. useGradient_ = element_->HasColorGradient();
  67. }
  68. else
  69. {
  70. color_ = 0xffffffff;
  71. useGradient_ = false;
  72. }
  73. }
  74. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  75. {
  76. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  77. if (!useGradient_)
  78. {
  79. // If alpha is 0, nothing will be rendered, so do not add the quad
  80. if (!(color_ & 0xff000000))
  81. return;
  82. topLeftColor = color_;
  83. topRightColor = color_;
  84. bottomLeftColor = color_;
  85. bottomRightColor = color_;
  86. }
  87. else
  88. {
  89. topLeftColor = GetInterpolatedColor(x, y);
  90. topRightColor = GetInterpolatedColor(x + width, y);
  91. bottomLeftColor = GetInterpolatedColor(x, y + height);
  92. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  93. }
  94. const IntVector2& screenPos = element_->GetScreenPosition();
  95. float left = (float)(x + screenPos.x_) - posAdjust.x_;
  96. float right = left + (float)width;
  97. float top = (float)(y + screenPos.y_) - posAdjust.x_;
  98. float bottom = top + (float)height;
  99. float leftUV = texOffsetX * invTextureSize_.x_;
  100. float topUV = texOffsetY * invTextureSize_.y_;
  101. float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
  102. float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
  103. unsigned begin = vertexData_->Size();
  104. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  105. float* dest = &(vertexData_->At(begin));
  106. vertexEnd_ = vertexData_->Size();
  107. dest[0] = left;
  108. dest[1] = top;
  109. dest[2] = 0.0f;
  110. ((unsigned&)dest[3]) = topLeftColor;
  111. dest[4] = leftUV;
  112. dest[5] = topUV;
  113. dest[6] = right;
  114. dest[7] = top;
  115. dest[8] = 0.0f;
  116. ((unsigned&)dest[9]) = topRightColor;
  117. dest[10] = rightUV;
  118. dest[11] = topUV;
  119. dest[12] = left;
  120. dest[13] = bottom;
  121. dest[14] = 0.0f;
  122. ((unsigned&)dest[15]) = bottomLeftColor;
  123. dest[16] = leftUV;
  124. dest[17] = bottomUV;
  125. dest[18] = right;
  126. dest[19] = top;
  127. dest[20] = 0.0f;
  128. ((unsigned&)dest[21]) = topRightColor;
  129. dest[22] = rightUV;
  130. dest[23] = topUV;
  131. dest[24] = right;
  132. dest[25] = bottom;
  133. dest[26] = 0.0f;
  134. ((unsigned&)dest[27]) = bottomRightColor;
  135. dest[28] = rightUV;
  136. dest[29] = bottomUV;
  137. dest[30] = left;
  138. dest[31] = bottom;
  139. dest[32] = 0.0f;
  140. ((unsigned&)dest[33]) = bottomLeftColor;
  141. dest[34] = leftUV;
  142. dest[35] = bottomUV;
  143. }
  144. void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
  145. int texWidth, int texHeight)
  146. {
  147. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  148. if (!useGradient_)
  149. {
  150. // If alpha is 0, nothing will be rendered, so do not add the quad
  151. if (!(color_ & 0xff000000))
  152. return;
  153. topLeftColor = color_;
  154. topRightColor = color_;
  155. bottomLeftColor = color_;
  156. bottomRightColor = color_;
  157. }
  158. else
  159. {
  160. topLeftColor = GetInterpolatedColor(x, y);
  161. topRightColor = GetInterpolatedColor(x + width, y);
  162. bottomLeftColor = GetInterpolatedColor(x, y + height);
  163. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  164. }
  165. Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjust;
  166. Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjust;
  167. Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjust;
  168. Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjust;
  169. float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
  170. float topUV = ((float)texOffsetY) * invTextureSize_.y_;
  171. float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
  172. float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
  173. unsigned begin = vertexData_->Size();
  174. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  175. float* dest = &(vertexData_->At(begin));
  176. vertexEnd_ = vertexData_->Size();
  177. dest[0] = v1.x_;
  178. dest[1] = v1.y_;
  179. dest[2] = 0.0f;
  180. ((unsigned&)dest[3]) = topLeftColor;
  181. dest[4] = leftUV;
  182. dest[5] = topUV;
  183. dest[6] = v2.x_;
  184. dest[7] = v2.y_;
  185. dest[8] = 0.0f;
  186. ((unsigned&)dest[9]) = topRightColor;
  187. dest[10] = rightUV;
  188. dest[11] = topUV;
  189. dest[12] = v3.x_;
  190. dest[13] = v3.y_;
  191. dest[14] = 0.0f;
  192. ((unsigned&)dest[15]) = bottomLeftColor;
  193. dest[16] = leftUV;
  194. dest[17] = bottomUV;
  195. dest[18] = v2.x_;
  196. dest[19] = v2.y_;
  197. dest[20] = 0.0f;
  198. ((unsigned&)dest[21]) = topRightColor;
  199. dest[22] = rightUV;
  200. dest[23] = topUV;
  201. dest[24] = v4.x_;
  202. dest[25] = v4.y_;
  203. dest[26] = 0.0f;
  204. ((unsigned&)dest[27]) = bottomRightColor;
  205. dest[28] = rightUV;
  206. dest[29] = bottomUV;
  207. dest[30] = v3.x_;
  208. dest[31] = v3.y_;
  209. dest[32] = 0.0f;
  210. ((unsigned&)dest[33]) = bottomLeftColor;
  211. dest[34] = leftUV;
  212. dest[35] = bottomUV;
  213. }
  214. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
  215. {
  216. if (!(element_->HasColorGradient() || element_->GetDerivedColor().ToUInt() & 0xff000000))
  217. return; // No gradient and alpha is 0, so do not add the quad
  218. if (!tiled)
  219. {
  220. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  221. return;
  222. }
  223. int tileX = 0;
  224. int tileY = 0;
  225. int tileW = 0;
  226. int tileH = 0;
  227. while (tileY < height)
  228. {
  229. tileX = 0;
  230. tileH = Min(height - tileY, texHeight);
  231. while (tileX < width)
  232. {
  233. tileW = Min(width - tileX, texWidth);
  234. AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  235. tileX += tileW;
  236. }
  237. tileY += tileH;
  238. }
  239. }
  240. bool UIBatch::Merge(const UIBatch& batch)
  241. {
  242. if (batch.blendMode_ != blendMode_ ||
  243. batch.scissor_ != scissor_ ||
  244. batch.texture_ != texture_ ||
  245. batch.vertexData_ != vertexData_ ||
  246. batch.vertexStart_ != vertexEnd_)
  247. return false;
  248. vertexEnd_ = batch.vertexEnd_;
  249. return true;
  250. }
  251. unsigned UIBatch::GetInterpolatedColor(int x, int y)
  252. {
  253. const IntVector2& size = element_->GetSize();
  254. if (size.x_ && size.y_)
  255. {
  256. float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
  257. float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
  258. Color topColor = element_->GetColor(C_TOPLEFT).Lerp(element_->GetColor(C_TOPRIGHT), cLerpX);
  259. Color bottomColor = element_->GetColor(C_BOTTOMLEFT).Lerp(element_->GetColor(C_BOTTOMRIGHT), cLerpX);
  260. Color color = topColor.Lerp(bottomColor, cLerpY);
  261. color.a_ *= element_->GetDerivedOpacity();
  262. return color.ToUInt();
  263. }
  264. else
  265. {
  266. Color color = element_->GetColor(C_TOPLEFT);
  267. color.a_ *= element_->GetDerivedOpacity();
  268. return color.ToUInt();
  269. }
  270. }
  271. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  272. {
  273. if (batch.vertexEnd_ == batch.vertexStart_)
  274. return;
  275. if (!batches.Empty() && batches.Back().Merge(batch))
  276. return;
  277. batches.Push(batch);
  278. }
  279. }