DecoratorTiledBox.cpp 12 KB

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