DecoratorTiledBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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) 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. Vector2f padded_size = element->GetBox().GetSize(BoxArea::Padding);
  94. // Calculate the natural dimensions of tile corners and edges.
  95. const Vector2f natural_top_left = tiles[TOP_LEFT_CORNER].GetNaturalDimensions(element);
  96. const Vector2f natural_top = tiles[TOP_EDGE].GetNaturalDimensions(element);
  97. const Vector2f natural_top_right = tiles[TOP_RIGHT_CORNER].GetNaturalDimensions(element);
  98. const Vector2f natural_bottom_left = tiles[BOTTOM_LEFT_CORNER].GetNaturalDimensions(element);
  99. const Vector2f natural_bottom = tiles[BOTTOM_EDGE].GetNaturalDimensions(element);
  100. const Vector2f natural_bottom_right = tiles[BOTTOM_RIGHT_CORNER].GetNaturalDimensions(element);
  101. const Vector2f natural_left = tiles[LEFT_EDGE].GetNaturalDimensions(element);
  102. const Vector2f natural_right = tiles[RIGHT_EDGE].GetNaturalDimensions(element);
  103. // Initialize the to-be-determined dimensions of the tiles.
  104. Vector2f top_left = natural_top_left;
  105. Vector2f top = natural_top;
  106. Vector2f top_right = natural_top_right;
  107. Vector2f bottom_left = natural_bottom_left;
  108. Vector2f bottom = natural_bottom;
  109. Vector2f bottom_right = natural_bottom_right;
  110. Vector2f left = natural_left;
  111. Vector2f right = natural_right;
  112. // Scale the top corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  113. // if they shared a width with their corner. Best solution? Don't know.
  114. if (padded_size.x < top_left.x + top_right.x)
  115. {
  116. float minimum_width = top_left.x + top_right.x;
  117. top_left.x = padded_size.x * (top_left.x / minimum_width);
  118. if (natural_top_left.x == natural_left.x)
  119. left.x = top_left.x;
  120. top_right.x = padded_size.x * (top_right.x / minimum_width);
  121. if (natural_top_right.x == natural_right.x)
  122. right.x = top_right.x;
  123. }
  124. // Scale the bottom corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  125. // if they shared a width with their corner. Best solution? Don't know.
  126. if (padded_size.x < bottom_left.x + bottom_right.x)
  127. {
  128. float minimum_width = bottom_left.x + bottom_right.x;
  129. bottom_left.x = padded_size.x * (bottom_left.x / minimum_width);
  130. if (natural_bottom_left.x == natural_left.x)
  131. left.x = bottom_left.x;
  132. bottom_right.x = padded_size.x * (bottom_right.x / minimum_width);
  133. if (natural_bottom_right.x == natural_right.x)
  134. right.x = bottom_right.x;
  135. }
  136. // Scale the left corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  137. // if they shared a width with their corner. Best solution? Don't know.
  138. if (padded_size.y < top_left.y + bottom_left.y)
  139. {
  140. float minimum_height = top_left.y + bottom_left.y;
  141. top_left.y = padded_size.y * (top_left.y / minimum_height);
  142. if (natural_top_left.y == natural_top.y)
  143. top.y = top_left.y;
  144. bottom_left.y = padded_size.y * (bottom_left.y / minimum_height);
  145. if (natural_bottom_left.y == natural_bottom.y)
  146. bottom.y = bottom_left.y;
  147. }
  148. // Scale the right corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  149. // if they shared a width with their corner. Best solution? Don't know.
  150. if (padded_size.y < top_right.y + bottom_right.y)
  151. {
  152. float minimum_height = top_right.y + bottom_right.y;
  153. top_right.y = padded_size.y * (top_right.y / minimum_height);
  154. if (natural_top_right.y == natural_top.y)
  155. top.y = top_right.y;
  156. bottom_right.y = padded_size.y * (bottom_right.y / minimum_height);
  157. if (natural_bottom_right.y == natural_bottom.y)
  158. bottom.y = bottom_right.y;
  159. }
  160. const int num_textures = GetNumTextures();
  161. DecoratorTiledBoxData* data = new DecoratorTiledBoxData(num_textures);
  162. const ComputedValues& computed = element->GetComputedValues();
  163. // Generate the geometry for the top-left tile.
  164. tiles[TOP_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_LEFT_CORNER].texture_index].GetVertices(),
  165. data->geometry[tiles[TOP_LEFT_CORNER].texture_index].GetIndices(), computed, Vector2f(0, 0), top_left, top_left);
  166. // Generate the geometry for the top edge tiles.
  167. tiles[TOP_EDGE].GenerateGeometry(data->geometry[tiles[TOP_EDGE].texture_index].GetVertices(),
  168. data->geometry[tiles[TOP_EDGE].texture_index].GetIndices(), computed, Vector2f(top_left.x, 0),
  169. Vector2f(padded_size.x - (top_left.x + top_right.x), top.y), top);
  170. // Generate the geometry for the top-right tile.
  171. tiles[TOP_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetVertices(),
  172. data->geometry[tiles[TOP_RIGHT_CORNER].texture_index].GetIndices(), computed, Vector2f(padded_size.x - top_right.x, 0), top_right, top_right);
  173. // Generate the geometry for the left side.
  174. tiles[LEFT_EDGE].GenerateGeometry(data->geometry[tiles[LEFT_EDGE].texture_index].GetVertices(),
  175. data->geometry[tiles[LEFT_EDGE].texture_index].GetIndices(), computed, Vector2f(0, top_left.y),
  176. Vector2f(left.x, padded_size.y - (top_left.y + bottom_left.y)), left);
  177. // Generate the geometry for the right side.
  178. tiles[RIGHT_EDGE].GenerateGeometry(data->geometry[tiles[RIGHT_EDGE].texture_index].GetVertices(),
  179. data->geometry[tiles[RIGHT_EDGE].texture_index].GetIndices(), computed, Vector2f((padded_size.x - right.x), top_right.y),
  180. Vector2f(right.x, padded_size.y - (top_right.y + bottom_right.y)), right);
  181. // Generate the geometry for the bottom-left tile.
  182. tiles[BOTTOM_LEFT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetVertices(),
  183. data->geometry[tiles[BOTTOM_LEFT_CORNER].texture_index].GetIndices(), computed, Vector2f(0, padded_size.y - bottom_left.y), bottom_left,
  184. bottom_left);
  185. // Generate the geometry for the bottom edge tiles.
  186. tiles[BOTTOM_EDGE].GenerateGeometry(data->geometry[tiles[BOTTOM_EDGE].texture_index].GetVertices(),
  187. data->geometry[tiles[BOTTOM_EDGE].texture_index].GetIndices(), computed, Vector2f(bottom_left.x, padded_size.y - bottom.y),
  188. Vector2f(padded_size.x - (bottom_left.x + bottom_right.x), bottom.y), bottom);
  189. // Generate the geometry for the bottom-right tile.
  190. tiles[BOTTOM_RIGHT_CORNER].GenerateGeometry(data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetVertices(),
  191. data->geometry[tiles[BOTTOM_RIGHT_CORNER].texture_index].GetIndices(), computed,
  192. Vector2f(padded_size.x - bottom_right.x, padded_size.y - bottom_right.y), bottom_right, bottom_right);
  193. // Generate the centre geometry.
  194. Vector2f centre_dimensions = tiles[CENTRE].GetNaturalDimensions(element);
  195. Vector2f centre_surface_dimensions(padded_size.x - (left.x + right.x), padded_size.y - (top.y + bottom.y));
  196. tiles[CENTRE].GenerateGeometry(data->geometry[tiles[CENTRE].texture_index].GetVertices(),
  197. data->geometry[tiles[CENTRE].texture_index].GetIndices(), computed, Vector2f(left.x, top.y), centre_surface_dimensions, centre_dimensions);
  198. // Set the textures on the geometry.
  199. const Texture* texture = nullptr;
  200. int texture_index = 0;
  201. while ((texture = GetTexture(texture_index)) != nullptr)
  202. data->geometry[texture_index++].SetTexture(texture);
  203. return reinterpret_cast<DecoratorDataHandle>(data);
  204. }
  205. void DecoratorTiledBox::ReleaseElementData(DecoratorDataHandle element_data) const
  206. {
  207. delete reinterpret_cast<DecoratorTiledBoxData*>(element_data);
  208. }
  209. void DecoratorTiledBox::RenderElement(Element* element, DecoratorDataHandle element_data) const
  210. {
  211. Vector2f translation = element->GetAbsoluteOffset(BoxArea::Padding).Round();
  212. DecoratorTiledBoxData* data = reinterpret_cast<DecoratorTiledBoxData*>(element_data);
  213. for (int i = 0; i < data->num_textures; i++)
  214. data->geometry[i].Render(translation);
  215. }
  216. DecoratorTiledBoxInstancer::DecoratorTiledBoxInstancer() : DecoratorTiledInstancer(9)
  217. {
  218. RegisterTileProperty("top-left-image");
  219. RegisterTileProperty("top-right-image");
  220. RegisterTileProperty("bottom-left-image");
  221. RegisterTileProperty("bottom-right-image");
  222. RegisterTileProperty("left-image");
  223. RegisterTileProperty("right-image");
  224. RegisterTileProperty("top-image");
  225. RegisterTileProperty("bottom-image");
  226. RegisterTileProperty("center-image");
  227. RegisterShorthand("decorator",
  228. "top-left-image, top-image, top-right-image, left-image, center-image, right-image, bottom-left-image, bottom-image, bottom-right-image",
  229. ShorthandType::RecursiveCommaSeparated);
  230. }
  231. DecoratorTiledBoxInstancer::~DecoratorTiledBoxInstancer() {}
  232. SharedPtr<Decorator> DecoratorTiledBoxInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties,
  233. const DecoratorInstancerInterface& instancer_interface)
  234. {
  235. constexpr size_t num_tiles = 9;
  236. DecoratorTiled::Tile tiles[num_tiles];
  237. Texture textures[num_tiles];
  238. if (!GetTileProperties(tiles, textures, num_tiles, properties, instancer_interface))
  239. return nullptr;
  240. auto decorator = MakeShared<DecoratorTiledBox>();
  241. if (!decorator->Initialise(tiles, textures))
  242. return nullptr;
  243. return decorator;
  244. }
  245. } // namespace Rml