DecoratorTiledBox.cpp 13 KB

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