DecoratorTiledHorizontal.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/Texture.h"
  32. namespace Rml {
  33. struct DecoratorTiledHorizontalData {
  34. DecoratorTiledHorizontalData(Element* host_element, int num_textures) : num_textures(num_textures)
  35. {
  36. geometry = new Geometry[num_textures];
  37. for (int i = 0; i < num_textures; i++)
  38. geometry[i].SetHostElement(host_element);
  39. }
  40. ~DecoratorTiledHorizontalData() { delete[] geometry; }
  41. const int num_textures;
  42. Geometry* geometry;
  43. };
  44. DecoratorTiledHorizontal::DecoratorTiledHorizontal() {}
  45. DecoratorTiledHorizontal::~DecoratorTiledHorizontal() {}
  46. bool DecoratorTiledHorizontal::Initialise(const Tile* _tiles, const Texture* _textures)
  47. {
  48. // Load the textures.
  49. for (int i = 0; i < 3; i++)
  50. {
  51. tiles[i] = _tiles[i];
  52. tiles[i].texture_index = AddTexture(_textures[i]);
  53. }
  54. // If only one side of the decorator has been configured, then mirror the texture for the other side.
  55. if (tiles[LEFT].texture_index == -1 && tiles[RIGHT].texture_index > -1)
  56. {
  57. tiles[LEFT] = tiles[RIGHT];
  58. tiles[LEFT].orientation = FLIP_HORIZONTAL;
  59. }
  60. else if (tiles[RIGHT].texture_index == -1 && tiles[LEFT].texture_index > -1)
  61. {
  62. tiles[RIGHT] = tiles[LEFT];
  63. tiles[RIGHT].orientation = FLIP_HORIZONTAL;
  64. }
  65. else if (tiles[LEFT].texture_index == -1 && tiles[RIGHT].texture_index == -1)
  66. return false;
  67. if (tiles[CENTRE].texture_index == -1)
  68. return false;
  69. return true;
  70. }
  71. DecoratorDataHandle DecoratorTiledHorizontal::GenerateElementData(Element* element) const
  72. {
  73. // Initialise the tiles for this element.
  74. for (int i = 0; i < 3; i++)
  75. tiles[i].CalculateDimensions(element, *(GetTexture(tiles[i].texture_index)));
  76. const int num_textures = GetNumTextures();
  77. DecoratorTiledHorizontalData* data = new DecoratorTiledHorizontalData(element, num_textures);
  78. Vector2f padded_size = element->GetBox().GetSize(Box::PADDING);
  79. Vector2f left_dimensions = tiles[LEFT].GetNaturalDimensions(element);
  80. Vector2f right_dimensions = tiles[RIGHT].GetNaturalDimensions(element);
  81. Vector2f centre_dimensions = tiles[CENTRE].GetNaturalDimensions(element);
  82. // Scale the tile sizes by the height scale.
  83. ScaleTileDimensions(left_dimensions, padded_size.y, Axis::Vertical);
  84. ScaleTileDimensions(right_dimensions, padded_size.y, Axis::Vertical);
  85. ScaleTileDimensions(centre_dimensions, padded_size.y, Axis::Vertical);
  86. // Round the outer tile widths now so that we don't get gaps when rounding again in GenerateGeometry.
  87. left_dimensions.x = Math::Round(left_dimensions.x);
  88. right_dimensions.x = Math::Round(right_dimensions.x);
  89. // Shrink the x-sizes on the left and right tiles if necessary.
  90. if (padded_size.x < left_dimensions.x + right_dimensions.x)
  91. {
  92. float minimum_width = left_dimensions.x + right_dimensions.x;
  93. left_dimensions.x = padded_size.x * (left_dimensions.x / minimum_width);
  94. right_dimensions.x = padded_size.x * (right_dimensions.x / minimum_width);
  95. }
  96. // Generate the geometry for the left tile.
  97. tiles[LEFT].GenerateGeometry(data->geometry[tiles[LEFT].texture_index].GetVertices(), data->geometry[tiles[LEFT].texture_index].GetIndices(),
  98. element, Vector2f(0, 0), left_dimensions, left_dimensions);
  99. // Generate the geometry for the centre tiles.
  100. tiles[CENTRE].GenerateGeometry(data->geometry[tiles[CENTRE].texture_index].GetVertices(),
  101. data->geometry[tiles[CENTRE].texture_index].GetIndices(), element, Vector2f(left_dimensions.x, 0),
  102. Vector2f(padded_size.x - (left_dimensions.x + right_dimensions.x), centre_dimensions.y), centre_dimensions);
  103. // Generate the geometry for the right tile.
  104. tiles[RIGHT].GenerateGeometry(data->geometry[tiles[RIGHT].texture_index].GetVertices(), data->geometry[tiles[RIGHT].texture_index].GetIndices(),
  105. element, Vector2f(padded_size.x - right_dimensions.x, 0), right_dimensions, right_dimensions);
  106. // Set the textures on the geometry.
  107. const Texture* texture = nullptr;
  108. int texture_index = 0;
  109. while ((texture = GetTexture(texture_index)) != nullptr)
  110. data->geometry[texture_index++].SetTexture(texture);
  111. return reinterpret_cast<DecoratorDataHandle>(data);
  112. }
  113. void DecoratorTiledHorizontal::ReleaseElementData(DecoratorDataHandle element_data) const
  114. {
  115. delete reinterpret_cast<DecoratorTiledHorizontalData*>(element_data);
  116. }
  117. void DecoratorTiledHorizontal::RenderElement(Element* element, DecoratorDataHandle element_data) const
  118. {
  119. Vector2f translation = element->GetAbsoluteOffset(Box::PADDING).Round();
  120. DecoratorTiledHorizontalData* data = reinterpret_cast<DecoratorTiledHorizontalData*>(element_data);
  121. for (int i = 0; i < data->num_textures; i++)
  122. data->geometry[i].Render(translation);
  123. }
  124. } // namespace Rml