UIBatch.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 "../../Graphics/Graphics.h"
  23. #include "../../Graphics/Texture.h"
  24. #include "UIElement.h"
  25. #include "../../DebugNew.h"
  26. namespace Atomic
  27. {
  28. Vector3 UIBatch::posAdjust(0.0f, 0.0f, 0.0f);
  29. UIBatch::UIBatch() :
  30. element_(0),
  31. blendMode_(BLEND_REPLACE),
  32. texture_(0),
  33. invTextureSize_(Vector2::ONE),
  34. vertexData_(0),
  35. vertexStart_(0),
  36. vertexEnd_(0)
  37. {
  38. SetDefaultColor();
  39. }
  40. UIBatch::UIBatch(UIElement* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
  41. element_(element),
  42. blendMode_(blendMode),
  43. scissor_(scissor),
  44. texture_(texture),
  45. invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
  46. vertexData_(vertexData),
  47. vertexStart_(vertexData->Size()),
  48. vertexEnd_(vertexData->Size())
  49. {
  50. SetDefaultColor();
  51. }
  52. void UIBatch::SetColor(const Color& color, bool overrideAlpha)
  53. {
  54. if (!element_)
  55. overrideAlpha = true;
  56. useGradient_ = false;
  57. color_ =
  58. overrideAlpha ? color.ToUInt() : Color(color.r_, color.g_, color.b_, color.a_ * element_->GetDerivedOpacity()).ToUInt();
  59. }
  60. void UIBatch::SetDefaultColor()
  61. {
  62. if (element_)
  63. {
  64. color_ = element_->GetDerivedColor().ToUInt();
  65. useGradient_ = element_->HasColorGradient();
  66. }
  67. else
  68. {
  69. color_ = 0xffffffff;
  70. useGradient_ = false;
  71. }
  72. }
  73. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  74. {
  75. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  76. if (!useGradient_)
  77. {
  78. // If alpha is 0, nothing will be rendered, so do not add the quad
  79. if (!(color_ & 0xff000000))
  80. return;
  81. topLeftColor = color_;
  82. topRightColor = color_;
  83. bottomLeftColor = color_;
  84. bottomRightColor = color_;
  85. }
  86. else
  87. {
  88. topLeftColor = GetInterpolatedColor(x, y);
  89. topRightColor = GetInterpolatedColor(x + width, y);
  90. bottomLeftColor = GetInterpolatedColor(x, y + height);
  91. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  92. }
  93. const IntVector2& screenPos = element_->GetScreenPosition();
  94. float left = (float)(x + screenPos.x_) - posAdjust.x_;
  95. float right = left + (float)width;
  96. float top = (float)(y + screenPos.y_) - posAdjust.x_;
  97. float bottom = top + (float)height;
  98. float leftUV = texOffsetX * invTextureSize_.x_;
  99. float topUV = texOffsetY * invTextureSize_.y_;
  100. float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
  101. float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
  102. unsigned begin = vertexData_->Size();
  103. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  104. float* dest = &(vertexData_->At(begin));
  105. vertexEnd_ = vertexData_->Size();
  106. dest[0] = left;
  107. dest[1] = top;
  108. dest[2] = 0.0f;
  109. ((unsigned&)dest[3]) = topLeftColor;
  110. dest[4] = leftUV;
  111. dest[5] = topUV;
  112. dest[6] = right;
  113. dest[7] = top;
  114. dest[8] = 0.0f;
  115. ((unsigned&)dest[9]) = topRightColor;
  116. dest[10] = rightUV;
  117. dest[11] = topUV;
  118. dest[12] = left;
  119. dest[13] = bottom;
  120. dest[14] = 0.0f;
  121. ((unsigned&)dest[15]) = bottomLeftColor;
  122. dest[16] = leftUV;
  123. dest[17] = bottomUV;
  124. dest[18] = right;
  125. dest[19] = top;
  126. dest[20] = 0.0f;
  127. ((unsigned&)dest[21]) = topRightColor;
  128. dest[22] = rightUV;
  129. dest[23] = topUV;
  130. dest[24] = right;
  131. dest[25] = bottom;
  132. dest[26] = 0.0f;
  133. ((unsigned&)dest[27]) = bottomRightColor;
  134. dest[28] = rightUV;
  135. dest[29] = bottomUV;
  136. dest[30] = left;
  137. dest[31] = bottom;
  138. dest[32] = 0.0f;
  139. ((unsigned&)dest[33]) = bottomLeftColor;
  140. dest[34] = leftUV;
  141. dest[35] = bottomUV;
  142. }
  143. void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
  144. int texWidth, int texHeight)
  145. {
  146. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  147. if (!useGradient_)
  148. {
  149. // If alpha is 0, nothing will be rendered, so do not add the quad
  150. if (!(color_ & 0xff000000))
  151. return;
  152. topLeftColor = color_;
  153. topRightColor = color_;
  154. bottomLeftColor = color_;
  155. bottomRightColor = color_;
  156. }
  157. else
  158. {
  159. topLeftColor = GetInterpolatedColor(x, y);
  160. topRightColor = GetInterpolatedColor(x + width, y);
  161. bottomLeftColor = GetInterpolatedColor(x, y + height);
  162. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  163. }
  164. Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjust;
  165. Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjust;
  166. Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjust;
  167. Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjust;
  168. float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
  169. float topUV = ((float)texOffsetY) * invTextureSize_.y_;
  170. float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
  171. float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
  172. unsigned begin = vertexData_->Size();
  173. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  174. float* dest = &(vertexData_->At(begin));
  175. vertexEnd_ = vertexData_->Size();
  176. dest[0] = v1.x_;
  177. dest[1] = v1.y_;
  178. dest[2] = 0.0f;
  179. ((unsigned&)dest[3]) = topLeftColor;
  180. dest[4] = leftUV;
  181. dest[5] = topUV;
  182. dest[6] = v2.x_;
  183. dest[7] = v2.y_;
  184. dest[8] = 0.0f;
  185. ((unsigned&)dest[9]) = topRightColor;
  186. dest[10] = rightUV;
  187. dest[11] = topUV;
  188. dest[12] = v3.x_;
  189. dest[13] = v3.y_;
  190. dest[14] = 0.0f;
  191. ((unsigned&)dest[15]) = bottomLeftColor;
  192. dest[16] = leftUV;
  193. dest[17] = bottomUV;
  194. dest[18] = v2.x_;
  195. dest[19] = v2.y_;
  196. dest[20] = 0.0f;
  197. ((unsigned&)dest[21]) = topRightColor;
  198. dest[22] = rightUV;
  199. dest[23] = topUV;
  200. dest[24] = v4.x_;
  201. dest[25] = v4.y_;
  202. dest[26] = 0.0f;
  203. ((unsigned&)dest[27]) = bottomRightColor;
  204. dest[28] = rightUV;
  205. dest[29] = bottomUV;
  206. dest[30] = v3.x_;
  207. dest[31] = v3.y_;
  208. dest[32] = 0.0f;
  209. ((unsigned&)dest[33]) = bottomLeftColor;
  210. dest[34] = leftUV;
  211. dest[35] = bottomUV;
  212. }
  213. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
  214. {
  215. if (!(element_->HasColorGradient() || element_->GetDerivedColor().ToUInt() & 0xff000000))
  216. return; // No gradient and alpha is 0, so do not add the quad
  217. if (!tiled)
  218. {
  219. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  220. return;
  221. }
  222. int tileX = 0;
  223. int tileY = 0;
  224. int tileW = 0;
  225. int tileH = 0;
  226. while (tileY < height)
  227. {
  228. tileX = 0;
  229. tileH = Min(height - tileY, texHeight);
  230. while (tileX < width)
  231. {
  232. tileW = Min(width - tileX, texWidth);
  233. AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  234. tileX += tileW;
  235. }
  236. tileY += tileH;
  237. }
  238. }
  239. bool UIBatch::Merge(const UIBatch& batch)
  240. {
  241. if (batch.blendMode_ != blendMode_ ||
  242. batch.scissor_ != scissor_ ||
  243. batch.texture_ != texture_ ||
  244. batch.vertexData_ != vertexData_ ||
  245. batch.vertexStart_ != vertexEnd_)
  246. return false;
  247. vertexEnd_ = batch.vertexEnd_;
  248. return true;
  249. }
  250. unsigned UIBatch::GetInterpolatedColor(int x, int y)
  251. {
  252. const IntVector2& size = element_->GetSize();
  253. if (size.x_ && size.y_)
  254. {
  255. float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
  256. float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
  257. Color topColor = element_->GetColor(C_TOPLEFT).Lerp(element_->GetColor(C_TOPRIGHT), cLerpX);
  258. Color bottomColor = element_->GetColor(C_BOTTOMLEFT).Lerp(element_->GetColor(C_BOTTOMRIGHT), cLerpX);
  259. Color color = topColor.Lerp(bottomColor, cLerpY);
  260. color.a_ *= element_->GetDerivedOpacity();
  261. return color.ToUInt();
  262. }
  263. else
  264. {
  265. Color color = element_->GetColor(C_TOPLEFT);
  266. color.a_ *= element_->GetDerivedOpacity();
  267. return color.ToUInt();
  268. }
  269. }
  270. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  271. {
  272. if (batch.vertexEnd_ == batch.vertexStart_)
  273. return;
  274. if (!batches.Empty() && batches.Back().Merge(batch))
  275. return;
  276. batches.Push(batch);
  277. }
  278. }