DecoratorTiledBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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-2023 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. DecoratorTiledBoxData(int num_textures) : num_textures(num_textures) { geometry = new Geometry[num_textures]; }
  34. ~DecoratorTiledBoxData() { delete[] geometry; }
  35. const int num_textures;
  36. Geometry* geometry;
  37. };
  38. DecoratorTiledBox::DecoratorTiledBox() {}
  39. DecoratorTiledBox::~DecoratorTiledBox() {}
  40. bool DecoratorTiledBox::Initialise(const Tile* _tiles, const Texture* _textures)
  41. {
  42. // Load the textures.
  43. for (int i = 0; i < 9; i++)
  44. {
  45. tiles[i] = _tiles[i];
  46. tiles[i].texture_index = AddTexture(_textures[i]);
  47. }
  48. // All corners must have a valid texture.
  49. for (int i = TOP_LEFT_CORNER; i <= BOTTOM_RIGHT_CORNER; i++)
  50. {
  51. if (tiles[i].texture_index == -1)
  52. return false;
  53. }
  54. // Check that the centre tile has been specified.
  55. if (tiles[CENTRE].texture_index < 0)
  56. return false;
  57. // If only one side of the left / right edges have been configured, then mirror the tile for the other side.
  58. if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index > -1)
  59. {
  60. tiles[LEFT_EDGE] = tiles[RIGHT_EDGE];
  61. tiles[LEFT_EDGE].orientation = FLIP_HORIZONTAL;
  62. }
  63. else if (tiles[RIGHT_EDGE].texture_index == -1 && tiles[LEFT_EDGE].texture_index > -1)
  64. {
  65. tiles[RIGHT_EDGE] = tiles[LEFT_EDGE];
  66. tiles[RIGHT_EDGE].orientation = FLIP_HORIZONTAL;
  67. }
  68. else if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index == -1)
  69. return false;
  70. // If only one side of the top / bottom edges have been configured, then mirror the tile for the other side.
  71. if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index > -1)
  72. {
  73. tiles[TOP_EDGE] = tiles[BOTTOM_EDGE];
  74. tiles[TOP_EDGE].orientation = FLIP_VERTICAL;
  75. }
  76. else if (tiles[BOTTOM_EDGE].texture_index == -1 && tiles[TOP_EDGE].texture_index > -1)
  77. {
  78. tiles[BOTTOM_EDGE] = tiles[TOP_EDGE];
  79. tiles[BOTTOM_EDGE].orientation = FLIP_VERTICAL;
  80. }
  81. else if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index == -1)
  82. return false;
  83. return true;
  84. }
  85. DecoratorDataHandle DecoratorTiledBox::GenerateElementData(Element* element, BoxArea paint_area) const
  86. {
  87. // Initialise the tiles for this element.
  88. for (int i = 0; i < 9; i++)
  89. {
  90. RMLUI_ASSERT(tiles[i].texture_index >= 0);
  91. tiles[i].CalculateDimensions(*GetTexture(tiles[i].texture_index));
  92. }
  93. const Vector2f offset = element->GetBox().GetPosition(paint_area);
  94. const Vector2f size = element->GetBox().GetSize(paint_area);
  95. // Calculate the natural dimensions of tile corners and edges.
  96. const Vector2f natural_top_left = tiles[TOP_LEFT_CORNER].GetNaturalDimensions(element);
  97. const Vector2f natural_top = tiles[TOP_EDGE].GetNaturalDimensions(element);
  98. const Vector2f natural_top_right = tiles[TOP_RIGHT_CORNER].GetNaturalDimensions(element);
  99. const Vector2f natural_bottom_left = tiles[BOTTOM_LEFT_CORNER].GetNaturalDimensions(element);
  100. const Vector2f natural_bottom = tiles[BOTTOM_EDGE].GetNaturalDimensions(element);
  101. const Vector2f natural_bottom_right = tiles[BOTTOM_RIGHT_CORNER].GetNaturalDimensions(element);
  102. const Vector2f natural_left = tiles[LEFT_EDGE].GetNaturalDimensions(element);
  103. const Vector2f natural_right = tiles[RIGHT_EDGE].GetNaturalDimensions(element);
  104. // Initialize the to-be-determined dimensions of the tiles.
  105. Vector2f top_left = natural_top_left;
  106. Vector2f top = natural_top;
  107. Vector2f top_right = natural_top_right;
  108. Vector2f bottom_left = natural_bottom_left;
  109. Vector2f bottom = natural_bottom;
  110. Vector2f bottom_right = natural_bottom_right;
  111. Vector2f left = natural_left;
  112. Vector2f right = natural_right;
  113. // Scale the top corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  114. // if they shared a width with their corner. Best solution? Don't know.
  115. if (size.x < top_left.x + top_right.x)
  116. {
  117. float minimum_width = top_left.x + top_right.x;
  118. top_left.x = size.x * (top_left.x / minimum_width);
  119. if (natural_top_left.x == natural_left.x)
  120. left.x = top_left.x;
  121. top_right.x = size.x * (top_right.x / minimum_width);
  122. if (natural_top_right.x == natural_right.x)
  123. right.x = top_right.x;
  124. }
  125. // Scale the bottom corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  126. // if they shared a width with their corner. Best solution? Don't know.
  127. if (size.x < bottom_left.x + bottom_right.x)
  128. {
  129. float minimum_width = bottom_left.x + bottom_right.x;
  130. bottom_left.x = size.x * (bottom_left.x / minimum_width);
  131. if (natural_bottom_left.x == natural_left.x)
  132. left.x = bottom_left.x;
  133. bottom_right.x = size.x * (bottom_right.x / minimum_width);
  134. if (natural_bottom_right.x == natural_right.x)
  135. right.x = bottom_right.x;
  136. }
  137. // Scale the left corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  138. // if they shared a width with their corner. Best solution? Don't know.
  139. if (size.y < top_left.y + bottom_left.y)
  140. {
  141. float minimum_height = top_left.y + bottom_left.y;
  142. top_left.y = size.y * (top_left.y / minimum_height);
  143. if (natural_top_left.y == natural_top.y)
  144. top.y = top_left.y;
  145. bottom_left.y = size.y * (bottom_left.y / minimum_height);
  146. if (natural_bottom_left.y == natural_bottom.y)
  147. bottom.y = bottom_left.y;
  148. }
  149. // Scale the right corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  150. // if they shared a width with their corner. Best solution? Don't know.
  151. if (size.y < top_right.y + bottom_right.y)
  152. {
  153. float minimum_height = top_right.y + bottom_right.y;
  154. top_right.y = size.y * (top_right.y / minimum_height);
  155. if (natural_top_right.y == natural_top.y)
  156. top.y = top_right.y;
  157. bottom_right.y = size.y * (bottom_right.y / minimum_height);
  158. if (natural_bottom_right.y == natural_bottom.y)
  159. bottom.y = bottom_right.y;
  160. }
  161. const int num_textures = GetNumTextures();
  162. DecoratorTiledBoxData* data = new DecoratorTiledBoxData(num_textures);
  163. const ComputedValues& computed = element->GetComputedValues();
  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(), computed, offset, top_left, top_left);
  167. // Generate the geometry for the top edge tiles.
  168. tiles[TOP_EDGE].GenerateGeometry(data->geometry[tiles[TOP_EDGE].texture_index].GetVertices(),
  169. data->geometry[tiles[TOP_EDGE].texture_index].GetIndices(), computed, offset + Vector2f(top_left.x, 0),
  170. Vector2f(size.x - (top_left.x + top_right.x), top.y), top);
  171. // Generate the geometry for the top-right tile.
  172. tiles[TOP_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetVertices(),
  173. data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetIndices(), computed, offset + Vector2f(size.x - top_right.x, 0), top_right,
  174. top_right);
  175. // Generate the geometry for the left side.
  176. tiles[LEFT_EDGE].GenerateGeometry(data->geometry[tiles[LEFT_EDGE].texture_index].GetVertices(),
  177. data->geometry[tiles[LEFT_EDGE].texture_index].GetIndices(), computed, offset + Vector2f(0, top_left.y),
  178. Vector2f(left.x, size.y - (top_left.y + bottom_left.y)), left);
  179. // Generate the geometry for the right side.
  180. tiles[RIGHT_EDGE].GenerateGeometry(data->geometry[tiles[RIGHT_EDGE].texture_index].GetVertices(),
  181. data->geometry[tiles[RIGHT_EDGE].texture_index].GetIndices(), computed, offset + Vector2f((size.x - right.x), top_right.y),
  182. Vector2f(right.x, size.y - (top_right.y + bottom_right.y)), right);
  183. // Generate the geometry for the bottom-left tile.
  184. tiles[BOTTOM_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetVertices(),
  185. data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetIndices(), computed, offset + Vector2f(0, size.y - bottom_left.y), bottom_left,
  186. bottom_left);
  187. // Generate the geometry for the bottom edge tiles.
  188. tiles[BOTTOM_EDGE].GenerateGeometry(data->geometry[tiles[BOTTOM_EDGE].texture_index].GetVertices(),
  189. data->geometry[tiles[BOTTOM_EDGE].texture_index].GetIndices(), computed, offset + Vector2f(bottom_left.x, size.y - bottom.y),
  190. Vector2f(size.x - (bottom_left.x + bottom_right.x), bottom.y), bottom);
  191. // Generate the geometry for the bottom-right tile.
  192. tiles[BOTTOM_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetVertices(),
  193. data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetIndices(), computed,
  194. offset + Vector2f(size.x - bottom_right.x, size.y - bottom_right.y), bottom_right, bottom_right);
  195. // Generate the centre geometry.
  196. Vector2f centre_dimensions = tiles[CENTRE].GetNaturalDimensions(element);
  197. Vector2f centre_surface_dimensions(size.x - (left.x + right.x), size.y - (top.y + bottom.y));
  198. tiles[CENTRE].GenerateGeometry(data->geometry[tiles[CENTRE].texture_index].GetVertices(),
  199. data->geometry[tiles[CENTRE].texture_index].GetIndices(), computed, offset + Vector2f(left.x, top.y), centre_surface_dimensions,
  200. centre_dimensions);
  201. // Set the textures on the geometry.
  202. const Texture* texture = nullptr;
  203. int texture_index = 0;
  204. while ((texture = GetTexture(texture_index)) != nullptr)
  205. data->geometry[texture_index++].SetTexture(texture);
  206. return reinterpret_cast<DecoratorDataHandle>(data);
  207. }
  208. void DecoratorTiledBox::ReleaseElementData(DecoratorDataHandle element_data) const
  209. {
  210. delete reinterpret_cast<DecoratorTiledBoxData*>(element_data);
  211. }
  212. void DecoratorTiledBox::RenderElement(Element* element, DecoratorDataHandle element_data) const
  213. {
  214. Vector2f translation = element->GetAbsoluteOffset(BoxArea::Border);
  215. DecoratorTiledBoxData* data = reinterpret_cast<DecoratorTiledBoxData*>(element_data);
  216. for (int i = 0; i < data->num_textures; i++)
  217. data->geometry[i].Render(translation);
  218. }
  219. DecoratorTiledBoxInstancer::DecoratorTiledBoxInstancer() : DecoratorTiledInstancer(9)
  220. {
  221. RegisterTileProperty("top-left-image");
  222. RegisterTileProperty("top-right-image");
  223. RegisterTileProperty("bottom-left-image");
  224. RegisterTileProperty("bottom-right-image");
  225. RegisterTileProperty("left-image");
  226. RegisterTileProperty("right-image");
  227. RegisterTileProperty("top-image");
  228. RegisterTileProperty("bottom-image");
  229. RegisterTileProperty("center-image");
  230. RegisterShorthand("decorator",
  231. "top-left-image, top-image, top-right-image, left-image, center-image, right-image, bottom-left-image, bottom-image, bottom-right-image",
  232. ShorthandType::RecursiveCommaSeparated);
  233. }
  234. DecoratorTiledBoxInstancer::~DecoratorTiledBoxInstancer() {}
  235. SharedPtr<Decorator> DecoratorTiledBoxInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties,
  236. const DecoratorInstancerInterface& instancer_interface)
  237. {
  238. constexpr size_t num_tiles = 9;
  239. DecoratorTiled::Tile tiles[num_tiles];
  240. Texture textures[num_tiles];
  241. if (!GetTileProperties(tiles, textures, num_tiles, properties, instancer_interface))
  242. return nullptr;
  243. auto decorator = MakeShared<DecoratorTiledBox>();
  244. if (!decorator->Initialise(tiles, textures))
  245. return nullptr;
  246. return decorator;
  247. }
  248. } // namespace Rml