DecoratorTiledBox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "DecoratorTiledBox.h"
  2. #include "../../Include/RmlUi/Core/Element.h"
  3. #include "../../Include/RmlUi/Core/Geometry.h"
  4. #include "../../Include/RmlUi/Core/RenderManager.h"
  5. namespace Rml {
  6. struct DecoratorTiledBoxData {
  7. DecoratorTiledBoxData(int num_textures) : num_textures(num_textures) { geometry = new Geometry[num_textures]; }
  8. ~DecoratorTiledBoxData() { delete[] geometry; }
  9. const int num_textures;
  10. Geometry* geometry;
  11. };
  12. DecoratorTiledBox::DecoratorTiledBox() {}
  13. DecoratorTiledBox::~DecoratorTiledBox() {}
  14. bool DecoratorTiledBox::Initialise(const Tile* _tiles, const Texture* _textures)
  15. {
  16. // Load the textures.
  17. for (int i = 0; i < 9; i++)
  18. {
  19. tiles[i] = _tiles[i];
  20. tiles[i].texture_index = AddTexture(_textures[i]);
  21. }
  22. // All corners must have a valid texture.
  23. for (int i = TOP_LEFT_CORNER; i <= BOTTOM_RIGHT_CORNER; i++)
  24. {
  25. if (tiles[i].texture_index == -1)
  26. return false;
  27. }
  28. // Check that the centre tile has been specified.
  29. if (tiles[CENTRE].texture_index < 0)
  30. return false;
  31. // If only one side of the left / right edges have been configured, then mirror the tile for the other side.
  32. if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index > -1)
  33. {
  34. tiles[LEFT_EDGE] = tiles[RIGHT_EDGE];
  35. tiles[LEFT_EDGE].orientation = FLIP_HORIZONTAL;
  36. }
  37. else if (tiles[RIGHT_EDGE].texture_index == -1 && tiles[LEFT_EDGE].texture_index > -1)
  38. {
  39. tiles[RIGHT_EDGE] = tiles[LEFT_EDGE];
  40. tiles[RIGHT_EDGE].orientation = FLIP_HORIZONTAL;
  41. }
  42. else if (tiles[LEFT_EDGE].texture_index == -1 && tiles[RIGHT_EDGE].texture_index == -1)
  43. return false;
  44. // If only one side of the top / bottom edges have been configured, then mirror the tile for the other side.
  45. if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index > -1)
  46. {
  47. tiles[TOP_EDGE] = tiles[BOTTOM_EDGE];
  48. tiles[TOP_EDGE].orientation = FLIP_VERTICAL;
  49. }
  50. else if (tiles[BOTTOM_EDGE].texture_index == -1 && tiles[TOP_EDGE].texture_index > -1)
  51. {
  52. tiles[BOTTOM_EDGE] = tiles[TOP_EDGE];
  53. tiles[BOTTOM_EDGE].orientation = FLIP_VERTICAL;
  54. }
  55. else if (tiles[TOP_EDGE].texture_index == -1 && tiles[BOTTOM_EDGE].texture_index == -1)
  56. return false;
  57. return true;
  58. }
  59. DecoratorDataHandle DecoratorTiledBox::GenerateElementData(Element* element, BoxArea paint_area) const
  60. {
  61. // Initialise the tiles for this element.
  62. for (int i = 0; i < 9; i++)
  63. {
  64. RMLUI_ASSERT(tiles[i].texture_index >= 0);
  65. tiles[i].CalculateDimensions(GetTexture(tiles[i].texture_index));
  66. }
  67. const RenderBox render_box = element->GetRenderBox(paint_area);
  68. const Vector2f offset = render_box.GetFillOffset();
  69. const Vector2f size = render_box.GetFillSize();
  70. // Calculate the natural dimensions of tile corners and edges.
  71. const Vector2f natural_top_left = tiles[TOP_LEFT_CORNER].GetNaturalDimensions(element);
  72. const Vector2f natural_top = tiles[TOP_EDGE].GetNaturalDimensions(element);
  73. const Vector2f natural_top_right = tiles[TOP_RIGHT_CORNER].GetNaturalDimensions(element);
  74. const Vector2f natural_bottom_left = tiles[BOTTOM_LEFT_CORNER].GetNaturalDimensions(element);
  75. const Vector2f natural_bottom = tiles[BOTTOM_EDGE].GetNaturalDimensions(element);
  76. const Vector2f natural_bottom_right = tiles[BOTTOM_RIGHT_CORNER].GetNaturalDimensions(element);
  77. const Vector2f natural_left = tiles[LEFT_EDGE].GetNaturalDimensions(element);
  78. const Vector2f natural_right = tiles[RIGHT_EDGE].GetNaturalDimensions(element);
  79. // Initialize the to-be-determined dimensions of the tiles.
  80. Vector2f top_left = natural_top_left;
  81. Vector2f top = natural_top;
  82. Vector2f top_right = natural_top_right;
  83. Vector2f bottom_left = natural_bottom_left;
  84. Vector2f bottom = natural_bottom;
  85. Vector2f bottom_right = natural_bottom_right;
  86. Vector2f left = natural_left;
  87. Vector2f right = natural_right;
  88. // Scale the top corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  89. // if they shared a width with their corner. Best solution? Don't know.
  90. if (size.x < top_left.x + top_right.x)
  91. {
  92. float minimum_width = top_left.x + top_right.x;
  93. top_left.x = size.x * (top_left.x / minimum_width);
  94. if (natural_top_left.x == natural_left.x)
  95. left.x = top_left.x;
  96. top_right.x = size.x * (top_right.x / minimum_width);
  97. if (natural_top_right.x == natural_right.x)
  98. right.x = top_right.x;
  99. }
  100. // Scale the bottom corners down if appropriate. If they are scaled, then the left and right edges are also scaled
  101. // if they shared a width with their corner. Best solution? Don't know.
  102. if (size.x < bottom_left.x + bottom_right.x)
  103. {
  104. float minimum_width = bottom_left.x + bottom_right.x;
  105. bottom_left.x = size.x * (bottom_left.x / minimum_width);
  106. if (natural_bottom_left.x == natural_left.x)
  107. left.x = bottom_left.x;
  108. bottom_right.x = size.x * (bottom_right.x / minimum_width);
  109. if (natural_bottom_right.x == natural_right.x)
  110. right.x = bottom_right.x;
  111. }
  112. // Scale the left corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  113. // if they shared a width with their corner. Best solution? Don't know.
  114. if (size.y < top_left.y + bottom_left.y)
  115. {
  116. float minimum_height = top_left.y + bottom_left.y;
  117. top_left.y = size.y * (top_left.y / minimum_height);
  118. if (natural_top_left.y == natural_top.y)
  119. top.y = top_left.y;
  120. bottom_left.y = size.y * (bottom_left.y / minimum_height);
  121. if (natural_bottom_left.y == natural_bottom.y)
  122. bottom.y = bottom_left.y;
  123. }
  124. // Scale the right corners down if appropriate. If they are scaled, then the top and bottom edges are also scaled
  125. // if they shared a width with their corner. Best solution? Don't know.
  126. if (size.y < top_right.y + bottom_right.y)
  127. {
  128. float minimum_height = top_right.y + bottom_right.y;
  129. top_right.y = size.y * (top_right.y / minimum_height);
  130. if (natural_top_right.y == natural_top.y)
  131. top.y = top_right.y;
  132. bottom_right.y = size.y * (bottom_right.y / minimum_height);
  133. if (natural_bottom_right.y == natural_bottom.y)
  134. bottom.y = bottom_right.y;
  135. }
  136. const ComputedValues& computed = element->GetComputedValues();
  137. Mesh mesh[COUNT];
  138. // Generate the geometry for the top-left tile.
  139. tiles[TOP_LEFT_CORNER].GenerateGeometry(mesh[tiles[TOP_LEFT_CORNER].texture_index], computed, offset, top_left, top_left);
  140. // Generate the geometry for the top edge tiles.
  141. tiles[TOP_EDGE].GenerateGeometry(mesh[tiles[TOP_EDGE].texture_index], computed, offset + Vector2f(top_left.x, 0),
  142. Vector2f(size.x - (top_left.x + top_right.x), top.y), top);
  143. // Generate the geometry for the top-right tile.
  144. tiles[TOP_RIGHT_CORNER].GenerateGeometry(mesh[tiles[TOP_RIGHT_CORNER].texture_index], computed, offset + Vector2f(size.x - top_right.x, 0),
  145. top_right, top_right);
  146. // Generate the geometry for the left side.
  147. tiles[LEFT_EDGE].GenerateGeometry(mesh[tiles[LEFT_EDGE].texture_index], computed, offset + Vector2f(0, top_left.y),
  148. Vector2f(left.x, size.y - (top_left.y + bottom_left.y)), left);
  149. // Generate the geometry for the right side.
  150. tiles[RIGHT_EDGE].GenerateGeometry(mesh[tiles[RIGHT_EDGE].texture_index], computed, offset + Vector2f((size.x - right.x), top_right.y),
  151. Vector2f(right.x, size.y - (top_right.y + bottom_right.y)), right);
  152. // Generate the geometry for the bottom-left tile.
  153. tiles[BOTTOM_LEFT_CORNER].GenerateGeometry(mesh[tiles[BOTTOM_LEFT_CORNER].texture_index], computed, offset + Vector2f(0, size.y - bottom_left.y),
  154. bottom_left, bottom_left);
  155. // Generate the geometry for the bottom edge tiles.
  156. tiles[BOTTOM_EDGE].GenerateGeometry(mesh[tiles[BOTTOM_EDGE].texture_index], computed, offset + Vector2f(bottom_left.x, size.y - bottom.y),
  157. Vector2f(size.x - (bottom_left.x + bottom_right.x), bottom.y), bottom);
  158. // Generate the geometry for the bottom-right tile.
  159. tiles[BOTTOM_RIGHT_CORNER].GenerateGeometry(mesh[tiles[BOTTOM_RIGHT_CORNER].texture_index], computed,
  160. offset + Vector2f(size.x - bottom_right.x, size.y - bottom_right.y), bottom_right, bottom_right);
  161. // Generate the centre geometry.
  162. Vector2f centre_dimensions = tiles[CENTRE].GetNaturalDimensions(element);
  163. Vector2f centre_surface_dimensions(size.x - (left.x + right.x), size.y - (top.y + bottom.y));
  164. tiles[CENTRE].GenerateGeometry(mesh[tiles[CENTRE].texture_index], computed, offset + Vector2f(left.x, top.y), centre_surface_dimensions,
  165. centre_dimensions);
  166. const int num_textures = GetNumTextures();
  167. DecoratorTiledBoxData* data = new DecoratorTiledBoxData(num_textures);
  168. RenderManager* render_manager = element->GetRenderManager();
  169. // Set the mesh and textures on the geometry.
  170. for (int i = 0; i < num_textures; i++)
  171. data->geometry[i] = render_manager->MakeGeometry(std::move(mesh[i]));
  172. return reinterpret_cast<DecoratorDataHandle>(data);
  173. }
  174. void DecoratorTiledBox::ReleaseElementData(DecoratorDataHandle element_data) const
  175. {
  176. delete reinterpret_cast<DecoratorTiledBoxData*>(element_data);
  177. }
  178. void DecoratorTiledBox::RenderElement(Element* element, DecoratorDataHandle element_data) const
  179. {
  180. Vector2f translation = element->GetAbsoluteOffset(BoxArea::Border);
  181. DecoratorTiledBoxData* data = reinterpret_cast<DecoratorTiledBoxData*>(element_data);
  182. for (int i = 0; i < data->num_textures; i++)
  183. data->geometry[i].Render(translation, GetTexture(i));
  184. }
  185. DecoratorTiledBoxInstancer::DecoratorTiledBoxInstancer() : DecoratorTiledInstancer(9)
  186. {
  187. RegisterTileProperty("top-left-image");
  188. RegisterTileProperty("top-right-image");
  189. RegisterTileProperty("bottom-left-image");
  190. RegisterTileProperty("bottom-right-image");
  191. RegisterTileProperty("left-image");
  192. RegisterTileProperty("right-image");
  193. RegisterTileProperty("top-image");
  194. RegisterTileProperty("bottom-image");
  195. RegisterTileProperty("center-image");
  196. RegisterShorthand("decorator",
  197. "top-left-image, top-image, top-right-image, left-image, center-image, right-image, bottom-left-image, bottom-image, bottom-right-image",
  198. ShorthandType::RecursiveCommaSeparated);
  199. }
  200. DecoratorTiledBoxInstancer::~DecoratorTiledBoxInstancer() {}
  201. SharedPtr<Decorator> DecoratorTiledBoxInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties,
  202. const DecoratorInstancerInterface& instancer_interface)
  203. {
  204. constexpr size_t num_tiles = 9;
  205. DecoratorTiled::Tile tiles[num_tiles];
  206. Texture textures[num_tiles];
  207. if (!GetTileProperties(tiles, textures, num_tiles, properties, instancer_interface))
  208. return nullptr;
  209. auto decorator = MakeShared<DecoratorTiledBox>();
  210. if (!decorator->Initialise(tiles, textures))
  211. return nullptr;
  212. return decorator;
  213. }
  214. } // namespace Rml