DecoratorTiledVertical.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "DecoratorTiledVertical.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Geometry.h"
  31. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  32. #include "../../Include/RmlUi/Core/RenderManager.h"
  33. #include "../../Include/RmlUi/Core/Texture.h"
  34. namespace Rml {
  35. struct DecoratorTiledVerticalData {
  36. DecoratorTiledVerticalData(int num_textures) : num_textures(num_textures) { geometry = new Geometry[num_textures]; }
  37. ~DecoratorTiledVerticalData() { delete[] geometry; }
  38. const int num_textures;
  39. Geometry* geometry;
  40. };
  41. DecoratorTiledVertical::DecoratorTiledVertical() {}
  42. DecoratorTiledVertical::~DecoratorTiledVertical() {}
  43. bool DecoratorTiledVertical::Initialise(const Tile* _tiles, const Texture* _textures)
  44. {
  45. // Load the textures.
  46. for (int i = 0; i < 3; i++)
  47. {
  48. tiles[i] = _tiles[i];
  49. tiles[i].texture_index = AddTexture(_textures[i]);
  50. }
  51. // If only one side of the decorator has been configured, then mirror the texture for the other side.
  52. if (tiles[TOP].texture_index == -1 && tiles[BOTTOM].texture_index > -1)
  53. {
  54. tiles[TOP] = tiles[BOTTOM];
  55. tiles[TOP].orientation = FLIP_HORIZONTAL;
  56. }
  57. else if (tiles[BOTTOM].texture_index == -1 && tiles[TOP].texture_index > -1)
  58. {
  59. tiles[BOTTOM] = tiles[TOP];
  60. tiles[BOTTOM].orientation = FLIP_HORIZONTAL;
  61. }
  62. else if (tiles[TOP].texture_index == -1 && tiles[BOTTOM].texture_index == -1)
  63. return false;
  64. if (tiles[CENTRE].texture_index == -1)
  65. return false;
  66. return true;
  67. }
  68. DecoratorDataHandle DecoratorTiledVertical::GenerateElementData(Element* element, BoxArea paint_area) const
  69. {
  70. // Initialise the tile for this element.
  71. for (int i = 0; i < 3; i++)
  72. tiles[i].CalculateDimensions(GetTexture(tiles[i].texture_index));
  73. const RenderBox render_box = element->GetRenderBox(paint_area);
  74. const Vector2f offset = render_box.GetFillOffset();
  75. const Vector2f size = render_box.GetFillSize();
  76. Vector2f top_dimensions = tiles[TOP].GetNaturalDimensions(element);
  77. Vector2f bottom_dimensions = tiles[BOTTOM].GetNaturalDimensions(element);
  78. Vector2f centre_dimensions = tiles[CENTRE].GetNaturalDimensions(element);
  79. // Scale the tile sizes by the width scale.
  80. ScaleTileDimensions(top_dimensions, size.x, Axis::Horizontal);
  81. ScaleTileDimensions(bottom_dimensions, size.x, Axis::Horizontal);
  82. ScaleTileDimensions(centre_dimensions, size.x, Axis::Horizontal);
  83. // Round the outer tile heights now so that we don't get gaps when rounding again in GenerateGeometry.
  84. top_dimensions.y = Math::Round(top_dimensions.y);
  85. bottom_dimensions.y = Math::Round(bottom_dimensions.y);
  86. // Shrink the y-sizes on the left and right tiles if necessary.
  87. if (size.y < top_dimensions.y + bottom_dimensions.y)
  88. {
  89. float minimum_height = top_dimensions.y + bottom_dimensions.y;
  90. top_dimensions.y = size.y * (top_dimensions.y / minimum_height);
  91. bottom_dimensions.y = size.y * (bottom_dimensions.y / minimum_height);
  92. }
  93. const ComputedValues& computed = element->GetComputedValues();
  94. Mesh mesh[COUNT];
  95. tiles[TOP].GenerateGeometry(mesh[tiles[TOP].texture_index], computed, offset, top_dimensions, top_dimensions);
  96. tiles[CENTRE].GenerateGeometry(mesh[tiles[CENTRE].texture_index], computed, offset + Vector2f(0, top_dimensions.y),
  97. Vector2f(centre_dimensions.x, size.y - (top_dimensions.y + bottom_dimensions.y)), centre_dimensions);
  98. tiles[BOTTOM].GenerateGeometry(mesh[tiles[BOTTOM].texture_index], computed, offset + Vector2f(0, size.y - bottom_dimensions.y), bottom_dimensions,
  99. bottom_dimensions);
  100. const int num_textures = GetNumTextures();
  101. DecoratorTiledVerticalData* data = new DecoratorTiledVerticalData(num_textures);
  102. // Set the mesh and textures on the geometry.
  103. RenderManager* render_manager = element->GetRenderManager();
  104. for (int i = 0; i < num_textures; i++)
  105. data->geometry[i] = render_manager->MakeGeometry(std::move(mesh[i]));
  106. return reinterpret_cast<DecoratorDataHandle>(data);
  107. }
  108. void DecoratorTiledVertical::ReleaseElementData(DecoratorDataHandle element_data) const
  109. {
  110. delete reinterpret_cast<DecoratorTiledVerticalData*>(element_data);
  111. }
  112. void DecoratorTiledVertical::RenderElement(Element* element, DecoratorDataHandle element_data) const
  113. {
  114. Vector2f translation = element->GetAbsoluteOffset(BoxArea::Padding).Round();
  115. DecoratorTiledVerticalData* data = reinterpret_cast<DecoratorTiledVerticalData*>(element_data);
  116. for (int i = 0; i < data->num_textures; i++)
  117. data->geometry[i].Render(translation, GetTexture(i));
  118. }
  119. DecoratorTiledVerticalInstancer::DecoratorTiledVerticalInstancer() : DecoratorTiledInstancer(3)
  120. {
  121. RegisterTileProperty("top-image");
  122. RegisterTileProperty("bottom-image");
  123. RegisterTileProperty("center-image");
  124. RegisterShorthand("decorator", "top-image, center-image, bottom-image", ShorthandType::RecursiveCommaSeparated);
  125. }
  126. DecoratorTiledVerticalInstancer::~DecoratorTiledVerticalInstancer() {}
  127. SharedPtr<Decorator> DecoratorTiledVerticalInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties,
  128. const DecoratorInstancerInterface& instancer_interface)
  129. {
  130. constexpr size_t num_tiles = 3;
  131. DecoratorTiled::Tile tiles[num_tiles];
  132. Texture textures[num_tiles];
  133. if (!GetTileProperties(tiles, textures, num_tiles, properties, instancer_interface))
  134. return nullptr;
  135. auto decorator = MakeShared<DecoratorTiledVertical>();
  136. if (!decorator->Initialise(tiles, textures))
  137. return nullptr;
  138. return decorator;
  139. }
  140. } // namespace Rml