DecoratorTiledBox.cpp 13 KB

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