2
0

DecoratorTiledBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "DecoratorTiledBox.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Geometry.h"
  31. namespace Rml {
  32. struct DecoratorTiledBoxData
  33. {
  34. DecoratorTiledBoxData(Element* host_element, int num_textures) : num_textures(num_textures)
  35. {
  36. geometry = new Geometry[num_textures];
  37. for (int i = 0; i < num_textures; i++)
  38. geometry[i].SetHostElement(host_element);
  39. }
  40. ~DecoratorTiledBoxData()
  41. {
  42. delete[] geometry;
  43. }
  44. const int num_textures;
  45. Geometry* geometry;
  46. };
  47. DecoratorTiledBox::DecoratorTiledBox()
  48. {
  49. }
  50. DecoratorTiledBox::~DecoratorTiledBox()
  51. {
  52. }
  53. // Initialises the tiles for the decorator.
  54. bool DecoratorTiledBox::Initialise(const Tile* _tiles, const Texture* _textures)
  55. {
  56. // Load the textures.
  57. for (int i = 0; i < 9; i++)
  58. {
  59. tiles[i] = _tiles[i];
  60. tiles[i].texture_index = AddTexture(_textures[i]);
  61. }
  62. // All corners must have a valid texture.
  63. for (int i = TOP_LEFT_CORNER; i <= BOTTOM_RIGHT_CORNER; i++)
  64. {
  65. if (tiles[i].texture_index == -1)
  66. return false;
  67. }
  68. // Check that the centre tile has been specified.
  69. if (tiles[CENTRE].texture_index < 0)
  70. return false;
  71. // If only one side of the left / right edges have been configured, then mirror the tile for the other side.
  72. if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index > -1)
  73. {
  74. tiles[LEFT_EDGE] = tiles[RIGHT_EDGE];
  75. tiles[LEFT_EDGE].orientation = FLIP_HORIZONTAL;
  76. }
  77. else if (tiles[RIGHT_EDGE].texture_index == -1 && tiles[LEFT_EDGE].texture_index > -1)
  78. {
  79. tiles[RIGHT_EDGE] = tiles[LEFT_EDGE];
  80. tiles[RIGHT_EDGE].orientation = FLIP_HORIZONTAL;
  81. }
  82. else if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index == -1)
  83. return false;
  84. // If only one side of the top / bottom edges have been configured, then mirror the tile for the other side.
  85. if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index > -1)
  86. {
  87. tiles[TOP_EDGE] = tiles[BOTTOM_EDGE];
  88. tiles[TOP_EDGE].orientation = FLIP_VERTICAL;
  89. }
  90. else if (tiles[BOTTOM_EDGE].texture_index == -1 && tiles[TOP_EDGE].texture_index > -1)
  91. {
  92. tiles[BOTTOM_EDGE] = tiles[TOP_EDGE];
  93. tiles[BOTTOM_EDGE].orientation = FLIP_VERTICAL;
  94. }
  95. else if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index == -1)
  96. return false;
  97. return true;
  98. }
  99. // Called on a decorator to generate any required per-element data for a newly decorated element.
  100. DecoratorDataHandle DecoratorTiledBox::GenerateElementData(Element* element) const
  101. {
  102. // Initialise the tiles for this element.
  103. for (int i = 0; i < 9; i++)
  104. {
  105. RMLUI_ASSERT(tiles[i].texture_index >= 0);
  106. tiles[i].CalculateDimensions(element, *GetTexture(tiles[i].texture_index));
  107. }
  108. Vector2f padded_size = element->GetBox().GetSize(Box::PADDING);
  109. // Calculate the natural dimensions of tile corners and edges.
  110. const Vector2f natural_top_left = tiles[TOP_LEFT_CORNER].GetNaturalDimensions(element);
  111. const Vector2f natural_top = tiles[TOP_EDGE].GetNaturalDimensions(element);
  112. const Vector2f natural_top_right = tiles[TOP_RIGHT_CORNER].GetNaturalDimensions(element);
  113. const Vector2f natural_bottom_left = tiles[BOTTOM_LEFT_CORNER].GetNaturalDimensions(element);
  114. const Vector2f natural_bottom = tiles[BOTTOM_EDGE].GetNaturalDimensions(element);
  115. const Vector2f natural_bottom_right = tiles[BOTTOM_RIGHT_CORNER].GetNaturalDimensions(element);
  116. const Vector2f natural_left = tiles[LEFT_EDGE].GetNaturalDimensions(element);
  117. const Vector2f natural_right = tiles[RIGHT_EDGE].GetNaturalDimensions(element);
  118. // Initialize the to-be-determined dimensions of the tiles.
  119. Vector2f top_left = natural_top_left;
  120. Vector2f top = natural_top;
  121. Vector2f top_right = natural_top_right;
  122. Vector2f bottom_left = natural_bottom_left;
  123. Vector2f bottom = natural_bottom;
  124. Vector2f bottom_right = natural_bottom_right;
  125. Vector2f left = natural_left;
  126. Vector2f right = natural_right;
  127. // Scale the top corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  128. // if they shared a width with their corner. Best solution? Don't know.
  129. if (padded_size.x < top_left.x + top_right.x)
  130. {
  131. float minimum_width = top_left.x + top_right.x;
  132. top_left.x = padded_size.x * (top_left.x / minimum_width);
  133. if (natural_top_left.x == natural_left.x)
  134. left.x = top_left.x;
  135. top_right.x = padded_size.x * (top_right.x / minimum_width);
  136. if (natural_top_right.x == natural_right.x)
  137. right.x = top_right.x;
  138. }
  139. // Scale the bottom corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  140. // if they shared a width with their corner. Best solution? Don't know.
  141. if (padded_size.x < bottom_left.x + bottom_right.x)
  142. {
  143. float minimum_width = bottom_left.x + bottom_right.x;
  144. bottom_left.x = padded_size.x * (bottom_left.x / minimum_width);
  145. if (natural_bottom_left.x == natural_left.x)
  146. left.x = bottom_left.x;
  147. bottom_right.x = padded_size.x * (bottom_right.x / minimum_width);
  148. if (natural_bottom_right.x == natural_right.x)
  149. right.x = bottom_right.x;
  150. }
  151. // Scale the left corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  152. // if they shared a width with their corner. Best solution? Don't know.
  153. if (padded_size.y < top_left.y + bottom_left.y)
  154. {
  155. float minimum_height = top_left.y + bottom_left.y;
  156. top_left.y = padded_size.y * (top_left.y / minimum_height);
  157. if (natural_top_left.y == natural_top.y)
  158. top.y = top_left.y;
  159. bottom_left.y = padded_size.y * (bottom_left.y / minimum_height);
  160. if (natural_bottom_left.y == natural_bottom.y)
  161. bottom.y = bottom_left.y;
  162. }
  163. // Scale the right corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  164. // if they shared a width with their corner. Best solution? Don't know.
  165. if (padded_size.y < top_right.y + bottom_right.y)
  166. {
  167. float minimum_height = top_right.y + bottom_right.y;
  168. top_right.y = padded_size.y * (top_right.y / minimum_height);
  169. if (natural_top_right.y == natural_top.y)
  170. top.y = top_right.y;
  171. bottom_right.y = padded_size.y * (bottom_right.y / minimum_height);
  172. if (natural_bottom_right.y == natural_bottom.y)
  173. bottom.y = bottom_right.y;
  174. }
  175. const int num_textures = GetNumTextures();
  176. DecoratorTiledBoxData* data = new DecoratorTiledBoxData(element, num_textures);
  177. // Generate the geometry for the top-left tile.
  178. tiles[TOP_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_LEFT_CORNER].texture_index].GetVertices(),
  179. data->geometry[tiles[TOP_LEFT_CORNER].texture_index].GetIndices(),
  180. element,
  181. Vector2f(0, 0),
  182. top_left,
  183. top_left);
  184. // Generate the geometry for the top edge tiles.
  185. tiles[TOP_EDGE].GenerateGeometry(data->geometry[tiles[TOP_EDGE].texture_index].GetVertices(),
  186. data->geometry[tiles[TOP_EDGE].texture_index].GetIndices(),
  187. element,
  188. Vector2f(top_left.x, 0),
  189. Vector2f(padded_size.x - (top_left.x + top_right.x), top.y),
  190. top);
  191. // Generate the geometry for the top-right tile.
  192. tiles[TOP_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetVertices(),
  193. data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetIndices(),
  194. element,
  195. Vector2f(padded_size.x - top_right.x, 0),
  196. top_right,
  197. top_right);
  198. // Generate the geometry for the left side.
  199. tiles[LEFT_EDGE].GenerateGeometry(data->geometry[tiles[LEFT_EDGE].texture_index].GetVertices(),
  200. data->geometry[tiles[LEFT_EDGE].texture_index].GetIndices(),
  201. element,
  202. Vector2f(0, top_left.y),
  203. Vector2f(left.x, padded_size.y - (top_left.y + bottom_left.y)),
  204. left);
  205. // Generate the geometry for the right side.
  206. tiles[RIGHT_EDGE].GenerateGeometry(data->geometry[tiles[RIGHT_EDGE].texture_index].GetVertices(),
  207. data->geometry[tiles[RIGHT_EDGE].texture_index].GetIndices(),
  208. element,
  209. Vector2f((padded_size.x - right.x), top_right.y),
  210. Vector2f(right.x, padded_size.y - (top_right.y + bottom_right.y)),
  211. right);
  212. // Generate the geometry for the bottom-left tile.
  213. tiles[BOTTOM_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetVertices(),
  214. data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetIndices(),
  215. element,
  216. Vector2f(0, padded_size.y - bottom_left.y),
  217. bottom_left,
  218. bottom_left);
  219. // Generate the geometry for the bottom edge tiles.
  220. tiles[BOTTOM_EDGE].GenerateGeometry(data->geometry[tiles[BOTTOM_EDGE].texture_index].GetVertices(),
  221. data->geometry[tiles[BOTTOM_EDGE].texture_index].GetIndices(),
  222. element,
  223. Vector2f(bottom_left.x, padded_size.y - bottom.y),
  224. Vector2f(padded_size.x - (bottom_left.x + bottom_right.x), bottom.y),
  225. bottom);
  226. // Generate the geometry for the bottom-right tile.
  227. tiles[BOTTOM_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetVertices(),
  228. data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetIndices(),
  229. element,
  230. Vector2f(padded_size.x - bottom_right.x, padded_size.y - bottom_right.y),
  231. bottom_right,
  232. bottom_right);
  233. // Generate the centre geometry.
  234. Vector2f centre_dimensions = tiles[CENTRE].GetNaturalDimensions(element);
  235. Vector2f centre_surface_dimensions(padded_size.x - (left.x + right.x),
  236. padded_size.y - (top.y + bottom.y));
  237. tiles[CENTRE].GenerateGeometry(data->geometry[tiles[CENTRE].texture_index].GetVertices(),
  238. data->geometry[tiles[CENTRE].texture_index].GetIndices(),
  239. element,
  240. Vector2f(left.x, top.y),
  241. centre_surface_dimensions,
  242. centre_dimensions);
  243. // Set the textures on the geometry.
  244. const Texture* texture = nullptr;
  245. int texture_index = 0;
  246. while ((texture = GetTexture(texture_index)) != nullptr)
  247. data->geometry[texture_index++].SetTexture(texture);
  248. return reinterpret_cast<DecoratorDataHandle>(data);
  249. }
  250. // Called to release element data generated by this decorator.
  251. void DecoratorTiledBox::ReleaseElementData(DecoratorDataHandle element_data) const
  252. {
  253. delete reinterpret_cast< DecoratorTiledBoxData* >(element_data);
  254. }
  255. // Called to render the decorator on an element.
  256. void DecoratorTiledBox::RenderElement(Element* element, DecoratorDataHandle element_data) const
  257. {
  258. Vector2f translation = element->GetAbsoluteOffset(Box::PADDING).Round();
  259. DecoratorTiledBoxData* data = reinterpret_cast< DecoratorTiledBoxData* >(element_data);
  260. for (int i = 0; i < data->num_textures; i++)
  261. data->geometry[i].Render(translation);
  262. }
  263. } // namespace Rml