UIBatch.cpp 9.9 KB

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