DecoratorTiledBox.cpp 12 KB

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