DecoratorTiledBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "precompiled.h"
  29. #include "DecoratorTiledBox.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/Geometry.h"
  32. namespace Rml {
  33. namespace Core {
  34. struct DecoratorTiledBoxData
  35. {
  36. DecoratorTiledBoxData(Element* host_element, int num_textures) : num_textures(num_textures)
  37. {
  38. geometry = new Geometry[num_textures];
  39. for (int i = 0; i < num_textures; i++)
  40. geometry[i].SetHostElement(host_element);
  41. }
  42. ~DecoratorTiledBoxData()
  43. {
  44. delete[] geometry;
  45. }
  46. const int num_textures;
  47. Geometry* geometry;
  48. };
  49. DecoratorTiledBox::DecoratorTiledBox()
  50. {
  51. }
  52. DecoratorTiledBox::~DecoratorTiledBox()
  53. {
  54. }
  55. // Initialises the tiles for the decorator.
  56. bool DecoratorTiledBox::Initialise(const Tile* _tiles, const Texture* _textures)
  57. {
  58. // Load the textures.
  59. for (int i = 0; i < 9; i++)
  60. {
  61. tiles[i] = _tiles[i];
  62. tiles[i].texture_index = AddTexture(_textures[i]);
  63. }
  64. // All corners must have a valid texture.
  65. for (int i = TOP_LEFT_CORNER; i <= BOTTOM_RIGHT_CORNER; i++)
  66. {
  67. if (tiles[i].texture_index == -1)
  68. return false;
  69. }
  70. // Check that the centre tile has been specified.
  71. if (tiles[CENTRE].texture_index < 0)
  72. return false;
  73. // If only one side of the left / right edges have been configured, then mirror the tile for the other side.
  74. if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index > -1)
  75. {
  76. tiles[LEFT_EDGE] = tiles[RIGHT_EDGE];
  77. tiles[LEFT_EDGE].orientation = FLIP_HORIZONTAL;
  78. }
  79. else if (tiles[RIGHT_EDGE].texture_index == -1 && tiles[LEFT_EDGE].texture_index > -1)
  80. {
  81. tiles[RIGHT_EDGE] = tiles[LEFT_EDGE];
  82. tiles[RIGHT_EDGE].orientation = FLIP_HORIZONTAL;
  83. }
  84. else if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index == -1)
  85. return false;
  86. // If only one side of the top / bottom edges have been configured, then mirror the tile for the other side.
  87. if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index > -1)
  88. {
  89. tiles[TOP_EDGE] = tiles[BOTTOM_EDGE];
  90. tiles[TOP_EDGE].orientation = FLIP_VERTICAL;
  91. }
  92. else if (tiles[BOTTOM_EDGE].texture_index == -1 && tiles[TOP_EDGE].texture_index > -1)
  93. {
  94. tiles[BOTTOM_EDGE] = tiles[TOP_EDGE];
  95. tiles[BOTTOM_EDGE].orientation = FLIP_VERTICAL;
  96. }
  97. else if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index == -1)
  98. return false;
  99. return true;
  100. }
  101. // Called on a decorator to generate any required per-element data for a newly decorated element.
  102. DecoratorDataHandle DecoratorTiledBox::GenerateElementData(Element* element) const
  103. {
  104. // Initialise the tiles for this element.
  105. for (int i = 0; i < 9; i++)
  106. {
  107. RMLUI_ASSERT(tiles[i].texture_index >= 0);
  108. tiles[i].CalculateDimensions(element, *GetTexture(tiles[i].texture_index));
  109. }
  110. Vector2f padded_size = element->GetBox().GetSize(Box::PADDING);
  111. // Calculate the size for the top row of tiles.
  112. Vector2f top_left_dimensions = tiles[TOP_LEFT_CORNER].GetDimensions(element);
  113. Vector2f top_dimensions = tiles[TOP_EDGE].GetDimensions(element);
  114. Vector2f top_right_dimensions = tiles[TOP_RIGHT_CORNER].GetDimensions(element);
  115. // Calculate the size for the bottom row of tiles.
  116. Vector2f bottom_left_dimensions = tiles[BOTTOM_LEFT_CORNER].GetDimensions(element);
  117. Vector2f bottom_dimensions = tiles[BOTTOM_EDGE].GetDimensions(element);
  118. Vector2f bottom_right_dimensions = tiles[BOTTOM_RIGHT_CORNER].GetDimensions(element);
  119. // The size of the left and right tiles.
  120. Vector2f left_dimensions = tiles[LEFT_EDGE].GetDimensions(element);
  121. Vector2f right_dimensions = tiles[RIGHT_EDGE].GetDimensions(element);
  122. // Scale the top corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  123. // if they shared a width with their corner. Best solution? Don't know.
  124. if (padded_size.x < top_left_dimensions.x + top_right_dimensions.x)
  125. {
  126. float minimum_width = top_left_dimensions.x + top_right_dimensions.x;
  127. top_left_dimensions.x = padded_size.x * (top_left_dimensions.x / minimum_width);
  128. if (tiles[TOP_LEFT_CORNER].GetDimensions(element).x == tiles[LEFT_EDGE].GetDimensions(element).x)
  129. left_dimensions.x = top_left_dimensions.x;
  130. top_right_dimensions.x = padded_size.x * (top_right_dimensions.x / minimum_width);
  131. if (tiles[TOP_RIGHT_CORNER].GetDimensions(element).x == tiles[RIGHT_EDGE].GetDimensions(element).x)
  132. right_dimensions.x = top_right_dimensions.x;
  133. }
  134. // Scale the bottom corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  135. // if they shared a width with their corner. Best solution? Don't know.
  136. if (padded_size.x < bottom_left_dimensions.x + bottom_right_dimensions.x)
  137. {
  138. float minimum_width = bottom_left_dimensions.x + bottom_right_dimensions.x;
  139. bottom_left_dimensions.x = padded_size.x * (bottom_left_dimensions.x / minimum_width);
  140. if (tiles[BOTTOM_LEFT_CORNER].GetDimensions(element).x == tiles[LEFT_EDGE].GetDimensions(element).x)
  141. left_dimensions.x = bottom_left_dimensions.x;
  142. bottom_right_dimensions.x = padded_size.x * (bottom_right_dimensions.x / minimum_width);
  143. if (tiles[BOTTOM_RIGHT_CORNER].GetDimensions(element).x == tiles[RIGHT_EDGE].GetDimensions(element).x)
  144. right_dimensions.x = bottom_right_dimensions.x;
  145. }
  146. // Scale the left corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  147. // if they shared a width with their corner. Best solution? Don't know.
  148. if (padded_size.y < top_left_dimensions.y + bottom_left_dimensions.y)
  149. {
  150. float minimum_height = top_left_dimensions.y + bottom_left_dimensions.y;
  151. top_left_dimensions.y = padded_size.y * (top_left_dimensions.y / minimum_height);
  152. if (tiles[TOP_LEFT_CORNER].GetDimensions(element).y == tiles[TOP_EDGE].GetDimensions(element).y)
  153. top_dimensions.y = top_left_dimensions.y;
  154. bottom_left_dimensions.y = padded_size.y * (bottom_left_dimensions.y / minimum_height);
  155. if (tiles[BOTTOM_LEFT_CORNER].GetDimensions(element).y == tiles[BOTTOM_EDGE].GetDimensions(element).y)
  156. bottom_dimensions.y = bottom_left_dimensions.y;
  157. }
  158. // Scale the right corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  159. // if they shared a width with their corner. Best solution? Don't know.
  160. if (padded_size.y < top_right_dimensions.y + bottom_right_dimensions.y)
  161. {
  162. float minimum_height = top_right_dimensions.y + bottom_right_dimensions.y;
  163. top_right_dimensions.y = padded_size.y * (top_right_dimensions.y / minimum_height);
  164. if (tiles[TOP_RIGHT_CORNER].GetDimensions(element).y == tiles[TOP_EDGE].GetDimensions(element).y)
  165. top_dimensions.y = top_right_dimensions.y;
  166. bottom_right_dimensions.y = padded_size.y * (bottom_right_dimensions.y / minimum_height);
  167. if (tiles[BOTTOM_RIGHT_CORNER].GetDimensions(element).y == tiles[BOTTOM_EDGE].GetDimensions(element).y)
  168. bottom_dimensions.y = bottom_right_dimensions.y;
  169. }
  170. const int num_textures = GetNumTextures();
  171. DecoratorTiledBoxData* data = new DecoratorTiledBoxData(element, num_textures);
  172. // Generate the geometry for the top-left tile.
  173. tiles[TOP_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_LEFT_CORNER].texture_index].GetVertices(),
  174. data->geometry[tiles[TOP_LEFT_CORNER].texture_index].GetIndices(),
  175. element,
  176. Vector2f(0, 0),
  177. top_left_dimensions,
  178. top_left_dimensions);
  179. // Generate the geometry for the top edge tiles.
  180. tiles[TOP_EDGE].GenerateGeometry(data->geometry[tiles[TOP_EDGE].texture_index].GetVertices(),
  181. data->geometry[tiles[TOP_EDGE].texture_index].GetIndices(),
  182. element,
  183. Vector2f(top_left_dimensions.x, 0),
  184. Vector2f(padded_size.x - (top_left_dimensions.x + top_right_dimensions.x), top_dimensions.y),
  185. top_dimensions);
  186. // Generate the geometry for the top-right tile.
  187. tiles[TOP_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetVertices(),
  188. data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetIndices(),
  189. element,
  190. Vector2f(padded_size.x - top_right_dimensions.x, 0),
  191. top_right_dimensions,
  192. top_right_dimensions);
  193. // Generate the geometry for the left side.
  194. tiles[LEFT_EDGE].GenerateGeometry(data->geometry[tiles[LEFT_EDGE].texture_index].GetVertices(),
  195. data->geometry[tiles[LEFT_EDGE].texture_index].GetIndices(),
  196. element,
  197. Vector2f(0, top_left_dimensions.y),
  198. Vector2f(left_dimensions.x, padded_size.y - (top_left_dimensions.y + bottom_left_dimensions.y)),
  199. left_dimensions);
  200. // Generate the geometry for the right side.
  201. tiles[RIGHT_EDGE].GenerateGeometry(data->geometry[tiles[RIGHT_EDGE].texture_index].GetVertices(),
  202. data->geometry[tiles[RIGHT_EDGE].texture_index].GetIndices(),
  203. element,
  204. Vector2f((padded_size.x - right_dimensions.x), top_right_dimensions.y),
  205. Vector2f(right_dimensions.x, padded_size.y - (top_right_dimensions.y + bottom_right_dimensions.y)),
  206. right_dimensions);
  207. // Generate the geometry for the bottom-left tile.
  208. tiles[BOTTOM_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetVertices(),
  209. data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetIndices(),
  210. element,
  211. Vector2f(0, padded_size.y - bottom_left_dimensions.y),
  212. bottom_left_dimensions,
  213. bottom_left_dimensions);
  214. // Generate the geometry for the bottom edge tiles.
  215. tiles[BOTTOM_EDGE].GenerateGeometry(data->geometry[tiles[BOTTOM_EDGE].texture_index].GetVertices(),
  216. data->geometry[tiles[BOTTOM_EDGE].texture_index].GetIndices(),
  217. element,
  218. Vector2f(bottom_left_dimensions.x, padded_size.y - bottom_dimensions.y),
  219. Vector2f(padded_size.x - (bottom_left_dimensions.x + bottom_right_dimensions.x), bottom_dimensions.y),
  220. bottom_dimensions);
  221. // Generate the geometry for the bottom-right tile.
  222. tiles[BOTTOM_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetVertices(),
  223. data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetIndices(),
  224. element,
  225. Vector2f(padded_size.x - bottom_right_dimensions.x, padded_size.y - bottom_right_dimensions.y),
  226. bottom_right_dimensions,
  227. bottom_right_dimensions);
  228. // Generate the centre geometry.
  229. Vector2f centre_dimensions = tiles[CENTRE].GetDimensions(element);
  230. Vector2f centre_surface_dimensions(padded_size.x - (left_dimensions.x + right_dimensions.x),
  231. padded_size.y - (top_dimensions.y + bottom_dimensions.y));
  232. tiles[CENTRE].GenerateGeometry(data->geometry[tiles[CENTRE].texture_index].GetVertices(),
  233. data->geometry[tiles[CENTRE].texture_index].GetIndices(),
  234. element,
  235. Vector2f(left_dimensions.x, top_dimensions.y),
  236. centre_surface_dimensions,
  237. centre_dimensions);
  238. // Set the textures on the geometry.
  239. const Texture* texture = nullptr;
  240. int texture_index = 0;
  241. while ((texture = GetTexture(texture_index)) != nullptr)
  242. data->geometry[texture_index++].SetTexture(texture);
  243. return reinterpret_cast<DecoratorDataHandle>(data);
  244. }
  245. // Called to release element data generated by this decorator.
  246. void DecoratorTiledBox::ReleaseElementData(DecoratorDataHandle element_data) const
  247. {
  248. delete reinterpret_cast< DecoratorTiledBoxData* >(element_data);
  249. }
  250. // Called to render the decorator on an element.
  251. void DecoratorTiledBox::RenderElement(Element* element, DecoratorDataHandle element_data) const
  252. {
  253. Vector2f translation = element->GetAbsoluteOffset(Box::PADDING).Round();
  254. DecoratorTiledBoxData* data = reinterpret_cast< DecoratorTiledBoxData* >(element_data);
  255. for (int i = 0; i < data->num_textures; i++)
  256. data->geometry[i].Render(translation);
  257. }
  258. }
  259. }