DecoratorTiledHorizontal.cpp 6.6 KB

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