DecoratorTiledBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 String* _texture_names, const String* _rcss_paths)
  56. {
  57. // Load the textures.
  58. for (int i = 0; i < 9; i++)
  59. {
  60. if (!_texture_names[i].Empty())
  61. {
  62. tiles[i] = _tiles[i];
  63. tiles[i].texture_index = LoadTexture(_texture_names[i], _rcss_paths[i]);
  64. if (tiles[i].texture_index < 0)
  65. return false;
  66. }
  67. }
  68. // If only one side of the left / right edges have been configured, then mirror the tile for the other side.
  69. if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index > -1)
  70. {
  71. tiles[LEFT_EDGE] = tiles[RIGHT_EDGE];
  72. tiles[LEFT_EDGE].orientation = FLIP_HORIZONTAL;
  73. }
  74. else if (tiles[RIGHT_EDGE].texture_index == -1 && tiles[LEFT_EDGE].texture_index > -1)
  75. {
  76. tiles[RIGHT_EDGE] = tiles[LEFT_EDGE];
  77. tiles[RIGHT_EDGE].orientation = FLIP_HORIZONTAL;
  78. }
  79. else if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index == -1)
  80. return false;
  81. // If only one side of the top / bottom edges have been configured, then mirror the tile for the other side.
  82. if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index > -1)
  83. {
  84. tiles[TOP_EDGE] = tiles[BOTTOM_EDGE];
  85. tiles[TOP_EDGE].orientation = FLIP_VERTICAL;
  86. }
  87. else if (tiles[BOTTOM_EDGE].texture_index == -1 && tiles[TOP_EDGE].texture_index > -1)
  88. {
  89. tiles[BOTTOM_EDGE] = tiles[TOP_EDGE];
  90. tiles[BOTTOM_EDGE].orientation = FLIP_VERTICAL;
  91. }
  92. else if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index == -1)
  93. return false;
  94. // Check that the centre tile has been specified.
  95. if (tiles[CENTRE].texture_index < 0)
  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)
  101. {
  102. // Initialise the tiles for this element.
  103. for (int i = 0; i < 9; i++)
  104. {
  105. if (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 size for the top row of tiles.
  110. Vector2f top_left_dimensions = tiles[TOP_LEFT_CORNER].GetDimensions(element);
  111. Vector2f top_dimensions = tiles[TOP_EDGE].GetDimensions(element);
  112. Vector2f top_right_dimensions = tiles[TOP_RIGHT_CORNER].GetDimensions(element);
  113. // Calculate the size for the bottom row of tiles.
  114. Vector2f bottom_left_dimensions = tiles[BOTTOM_LEFT_CORNER].GetDimensions(element);
  115. Vector2f bottom_dimensions = tiles[BOTTOM_EDGE].GetDimensions(element);
  116. Vector2f bottom_right_dimensions = tiles[BOTTOM_RIGHT_CORNER].GetDimensions(element);
  117. // The size of the left and right tiles.
  118. Vector2f left_dimensions = tiles[LEFT_EDGE].GetDimensions(element);
  119. Vector2f right_dimensions = tiles[RIGHT_EDGE].GetDimensions(element);
  120. // Scale the top corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  121. // if they shared a width with their corner. Best solution? Don't know.
  122. if (padded_size.x < top_left_dimensions.x + top_right_dimensions.x)
  123. {
  124. float minimum_width = top_left_dimensions.x + top_right_dimensions.x;
  125. top_left_dimensions.x = padded_size.x * (top_left_dimensions.x / minimum_width);
  126. if (tiles[TOP_LEFT_CORNER].GetDimensions(element).x == tiles[LEFT_EDGE].GetDimensions(element).x)
  127. left_dimensions.x = top_left_dimensions.x;
  128. top_right_dimensions.x = padded_size.x * (top_right_dimensions.x / minimum_width);
  129. if (tiles[TOP_RIGHT_CORNER].GetDimensions(element).x == tiles[RIGHT_EDGE].GetDimensions(element).x)
  130. right_dimensions.x = top_right_dimensions.x;
  131. }
  132. // Scale the bottom corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  133. // if they shared a width with their corner. Best solution? Don't know.
  134. if (padded_size.x < bottom_left_dimensions.x + bottom_right_dimensions.x)
  135. {
  136. float minimum_width = bottom_left_dimensions.x + bottom_right_dimensions.x;
  137. bottom_left_dimensions.x = padded_size.x * (bottom_left_dimensions.x / minimum_width);
  138. if (tiles[BOTTOM_LEFT_CORNER].GetDimensions(element).x == tiles[LEFT_EDGE].GetDimensions(element).x)
  139. left_dimensions.x = bottom_left_dimensions.x;
  140. bottom_right_dimensions.x = padded_size.x * (bottom_right_dimensions.x / minimum_width);
  141. if (tiles[BOTTOM_RIGHT_CORNER].GetDimensions(element).x == tiles[RIGHT_EDGE].GetDimensions(element).x)
  142. right_dimensions.x = bottom_right_dimensions.x;
  143. }
  144. // Scale the left corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  145. // if they shared a width with their corner. Best solution? Don't know.
  146. if (padded_size.y < top_left_dimensions.y + bottom_left_dimensions.y)
  147. {
  148. float minimum_height = top_left_dimensions.y + bottom_left_dimensions.y;
  149. top_left_dimensions.y = padded_size.y * (top_left_dimensions.y / minimum_height);
  150. if (tiles[TOP_LEFT_CORNER].GetDimensions(element).y == tiles[TOP_EDGE].GetDimensions(element).y)
  151. top_dimensions.y = top_left_dimensions.y;
  152. bottom_left_dimensions.y = padded_size.y * (bottom_left_dimensions.y / minimum_height);
  153. if (tiles[BOTTOM_LEFT_CORNER].GetDimensions(element).y == tiles[BOTTOM_EDGE].GetDimensions(element).y)
  154. bottom_dimensions.y = bottom_left_dimensions.y;
  155. }
  156. // Scale the right corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  157. // if they shared a width with their corner. Best solution? Don't know.
  158. if (padded_size.y < top_right_dimensions.y + bottom_right_dimensions.y)
  159. {
  160. float minimum_height = top_right_dimensions.y + bottom_right_dimensions.y;
  161. top_right_dimensions.y = padded_size.y * (top_right_dimensions.y / minimum_height);
  162. if (tiles[TOP_RIGHT_CORNER].GetDimensions(element).y == tiles[TOP_EDGE].GetDimensions(element).y)
  163. top_dimensions.y = top_right_dimensions.y;
  164. bottom_right_dimensions.y = padded_size.y * (bottom_right_dimensions.y / minimum_height);
  165. if (tiles[BOTTOM_RIGHT_CORNER].GetDimensions(element).y == tiles[BOTTOM_EDGE].GetDimensions(element).y)
  166. bottom_dimensions.y = bottom_right_dimensions.y;
  167. }
  168. DecoratorTiledBoxData* data = new DecoratorTiledBoxData(element);
  169. // Generate the geometry for the top-left tile.
  170. tiles[TOP_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_LEFT_CORNER].texture_index]->GetVertices(),
  171. data->geometry[tiles[TOP_LEFT_CORNER].texture_index]->GetIndices(),
  172. element,
  173. Vector2f(0, 0),
  174. top_left_dimensions,
  175. top_left_dimensions);
  176. // Generate the geometry for the top edge tiles.
  177. tiles[TOP_EDGE].GenerateGeometry(data->geometry[tiles[TOP_EDGE].texture_index]->GetVertices(),
  178. data->geometry[tiles[TOP_EDGE].texture_index]->GetIndices(),
  179. element,
  180. Vector2f(top_left_dimensions.x, 0),
  181. Vector2f(padded_size.x - (top_left_dimensions.x + top_right_dimensions.x), top_dimensions.y),
  182. top_dimensions);
  183. // Generate the geometry for the top-right tile.
  184. tiles[TOP_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_RIGHT_CORNER].texture_index]->GetVertices(),
  185. data->geometry[tiles[TOP_RIGHT_CORNER].texture_index]->GetIndices(),
  186. element,
  187. Vector2f(padded_size.x - top_right_dimensions.x, 0),
  188. top_right_dimensions,
  189. top_right_dimensions);
  190. // Generate the geometry for the left side.
  191. tiles[LEFT_EDGE].GenerateGeometry(data->geometry[tiles[LEFT_EDGE].texture_index]->GetVertices(),
  192. data->geometry[tiles[LEFT_EDGE].texture_index]->GetIndices(),
  193. element,
  194. Vector2f(0, top_left_dimensions.y),
  195. Vector2f(left_dimensions.x, padded_size.y - (top_left_dimensions.y + bottom_left_dimensions.y)),
  196. left_dimensions);
  197. // Generate the geometry for the right side.
  198. tiles[RIGHT_EDGE].GenerateGeometry(data->geometry[tiles[RIGHT_EDGE].texture_index]->GetVertices(),
  199. data->geometry[tiles[RIGHT_EDGE].texture_index]->GetIndices(),
  200. element,
  201. Vector2f((padded_size.x - right_dimensions.x), top_right_dimensions.y),
  202. Vector2f(right_dimensions.x, padded_size.y - (top_right_dimensions.y + bottom_right_dimensions.y)),
  203. right_dimensions);
  204. // Generate the geometry for the bottom-left tile.
  205. tiles[BOTTOM_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index]->GetVertices(),
  206. data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index]->GetIndices(),
  207. element,
  208. Vector2f(0, padded_size.y - bottom_left_dimensions.y),
  209. bottom_left_dimensions,
  210. bottom_left_dimensions);
  211. // Generate the geometry for the bottom edge tiles.
  212. tiles[BOTTOM_EDGE].GenerateGeometry(data->geometry[tiles[BOTTOM_EDGE].texture_index]->GetVertices(),
  213. data->geometry[tiles[BOTTOM_EDGE].texture_index]->GetIndices(),
  214. element,
  215. Vector2f(bottom_left_dimensions.x, padded_size.y - bottom_dimensions.y),
  216. Vector2f(padded_size.x - (bottom_left_dimensions.x + bottom_right_dimensions.x), bottom_dimensions.y),
  217. bottom_dimensions);
  218. // Generate the geometry for the bottom-right tile.
  219. tiles[BOTTOM_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index]->GetVertices(),
  220. data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index]->GetIndices(),
  221. element,
  222. Vector2f(padded_size.x - bottom_right_dimensions.x, padded_size.y - bottom_right_dimensions.y),
  223. bottom_right_dimensions,
  224. bottom_right_dimensions);
  225. // Generate the centre geometry.
  226. if (tiles[CENTRE].texture_index >= 0)
  227. {
  228. Vector2f centre_dimensions = tiles[CENTRE].GetDimensions(element);
  229. Vector2f centre_surface_dimensions(padded_size.x - (left_dimensions.x + right_dimensions.x),
  230. padded_size.y - (top_dimensions.y + bottom_dimensions.y));
  231. tiles[CENTRE].GenerateGeometry(data->geometry[tiles[CENTRE].texture_index]->GetVertices(),
  232. data->geometry[tiles[CENTRE].texture_index]->GetIndices(),
  233. element,
  234. Vector2f(left_dimensions.x, top_dimensions.y),
  235. centre_surface_dimensions,
  236. centre_dimensions);
  237. }
  238. // Set the textures on the geometry.
  239. const Texture* texture = NULL;
  240. int texture_index = 0;
  241. while ((texture = GetTexture(texture_index)) != NULL)
  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)
  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)
  252. {
  253. Vector2f translation = element->GetAbsoluteOffset(Box::PADDING).Round();
  254. DecoratorTiledBoxData* data = reinterpret_cast< DecoratorTiledBoxData* >(element_data);
  255. for (int i = 0; i < 9; i++)
  256. data->geometry[i]->Render(translation);
  257. }
  258. }
  259. }