DecoratorTiled.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 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 "DecoratorTiled.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../Include/RmlUi/Core/Math.h"
  32. #include "../../Include/RmlUi/Core/GeometryUtilities.h"
  33. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  34. #include <algorithm>
  35. namespace Rml {
  36. DecoratorTiled::DecoratorTiled()
  37. {
  38. }
  39. DecoratorTiled::~DecoratorTiled()
  40. {
  41. }
  42. static const Vector2f oriented_texcoords[4][2] = {
  43. {Vector2f(0, 0), Vector2f(1, 1)}, // ORIENTATION_NONE
  44. {Vector2f(1, 0), Vector2f(0, 1)}, // FLIP_HORIZONTAL
  45. {Vector2f(0, 1), Vector2f(1, 0)}, // FLIP_VERTICAL
  46. {Vector2f(1, 1), Vector2f(0, 0)} // ROTATE_180
  47. };
  48. DecoratorTiled::Tile::Tile() : display_scale(1), position(0, 0), size(0, 0)
  49. {
  50. texture_index = -1;
  51. fit_mode = FILL;
  52. orientation = ORIENTATION_NONE;
  53. }
  54. // Calculates the tile's dimensions from the texture and texture coordinates.
  55. void DecoratorTiled::Tile::CalculateDimensions(Element* element, const Texture& texture) const
  56. {
  57. RenderInterface* render_interface = element->GetRenderInterface();
  58. auto data_iterator = data.find(render_interface);
  59. if (data_iterator == data.end())
  60. {
  61. TileData new_data;
  62. const Vector2f texture_dimensions(texture.GetDimensions(render_interface));
  63. if (texture_dimensions.x == 0 || texture_dimensions.y == 0)
  64. {
  65. new_data.size = Vector2f(0, 0);
  66. new_data.texcoords[0] = Vector2f(0, 0);
  67. new_data.texcoords[1] = Vector2f(0, 0);
  68. }
  69. else
  70. {
  71. // Need to scale the coordinates to normalized units and 'size' to absolute size (pixels)
  72. if (size.x == 0 && size.y == 0 && position.x == 0 && position.y == 0)
  73. new_data.size = texture_dimensions;
  74. else
  75. new_data.size = size;
  76. const Vector2f size_relative = new_data.size / texture_dimensions;
  77. new_data.size = Vector2f(Math::AbsoluteValue(new_data.size.x), Math::AbsoluteValue(new_data.size.y));
  78. new_data.texcoords[0] = position / texture_dimensions;
  79. new_data.texcoords[1] = size_relative + new_data.texcoords[0];
  80. }
  81. data.emplace( render_interface, new_data );
  82. }
  83. }
  84. // Get this tile's dimensions.
  85. Vector2f DecoratorTiled::Tile::GetNaturalDimensions(Element* element) const
  86. {
  87. RenderInterface* render_interface = element->GetRenderInterface();
  88. auto data_iterator = data.find(render_interface);
  89. if (data_iterator == data.end())
  90. return Vector2f(0, 0);
  91. const float scale_raw_to_natural_dimensions = ElementUtilities::GetDensityIndependentPixelRatio(element) * display_scale;
  92. const Vector2f raw_dimensions = data_iterator->second.size;
  93. return raw_dimensions * scale_raw_to_natural_dimensions;
  94. }
  95. // Generates geometry to render this tile across a surface.
  96. void DecoratorTiled::Tile::GenerateGeometry(Vector< Vertex >& vertices, Vector< int >& indices, Element* element, const Vector2f surface_origin, const Vector2f surface_dimensions, const Vector2f tile_dimensions) const
  97. {
  98. if (surface_dimensions.x <= 0 || surface_dimensions.y <= 0)
  99. return;
  100. RenderInterface* render_interface = element->GetRenderInterface();
  101. const auto& computed = element->GetComputedValues();
  102. float opacity = computed.opacity;
  103. Colourb quad_colour = computed.image_color;
  104. // Apply opacity
  105. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  106. auto data_iterator = data.find(render_interface);
  107. if (data_iterator == data.end())
  108. return;
  109. const TileData& data = data_iterator->second;
  110. // Generate the oriented texture coordinates for the tiles.
  111. Vector2f scaled_texcoords[2];
  112. for (int i = 0; i < 2; i++)
  113. {
  114. scaled_texcoords[i] = data.texcoords[0] + oriented_texcoords[orientation][i] * (data.texcoords[1] - data.texcoords[0]);
  115. }
  116. Vector2f final_tile_dimensions;
  117. bool offset_and_clip_tile = false;
  118. switch (fit_mode)
  119. {
  120. case FILL:
  121. {
  122. final_tile_dimensions = surface_dimensions;
  123. }
  124. break;
  125. case CONTAIN:
  126. {
  127. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  128. float min_factor = std::min(scale_factor.x, scale_factor.y);
  129. final_tile_dimensions = tile_dimensions * min_factor;
  130. offset_and_clip_tile = true;
  131. }
  132. break;
  133. case COVER:
  134. {
  135. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  136. float max_factor = std::max(scale_factor.x, scale_factor.y);
  137. final_tile_dimensions = tile_dimensions * max_factor;
  138. offset_and_clip_tile = true;
  139. }
  140. break;
  141. case SCALE_NONE:
  142. {
  143. final_tile_dimensions = tile_dimensions;
  144. offset_and_clip_tile = true;
  145. }
  146. break;
  147. case SCALE_DOWN:
  148. {
  149. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  150. float min_factor = std::min(scale_factor.x, scale_factor.y);
  151. if (min_factor < 1.0f)
  152. final_tile_dimensions = tile_dimensions * min_factor;
  153. else
  154. final_tile_dimensions = tile_dimensions;
  155. offset_and_clip_tile = true;
  156. }
  157. break;
  158. }
  159. Vector2f tile_offset(0, 0);
  160. if (offset_and_clip_tile)
  161. {
  162. // Offset tile along each dimension.
  163. for(int i = 0; i < 2; i++)
  164. {
  165. switch (align[i].type) {
  166. case Style::LengthPercentage::Length: tile_offset[i] = align[i].value; break;
  167. case Style::LengthPercentage::Percentage: tile_offset[i] = (surface_dimensions[i] - final_tile_dimensions[i]) * align[i].value * 0.01f; break;
  168. }
  169. }
  170. tile_offset = tile_offset.Round();
  171. // Clip tile. See if our tile extends outside the boundary at either side, along each dimension.
  172. for(int i = 0; i < 2; i++)
  173. {
  174. // Left/right acts as top/bottom during the second iteration.
  175. float overshoot_left = std::max(-tile_offset[i], 0.0f);
  176. float overshoot_right = std::max(tile_offset[i] + final_tile_dimensions[i] - surface_dimensions[i], 0.0f);
  177. if(overshoot_left > 0.f || overshoot_right > 0.f)
  178. {
  179. float& left = scaled_texcoords[0][i];
  180. float& right = scaled_texcoords[1][i];
  181. float width = right - left;
  182. left += overshoot_left / final_tile_dimensions[i] * width;
  183. right -= overshoot_right / final_tile_dimensions[i] * width;
  184. final_tile_dimensions[i] -= overshoot_left + overshoot_right;
  185. tile_offset[i] += overshoot_left;
  186. }
  187. }
  188. }
  189. // Resize the vertex and index arrays to fit the new geometry.
  190. int index_offset = (int) vertices.size();
  191. vertices.resize(vertices.size() + 4);
  192. Vertex* new_vertices = &vertices[0] + index_offset;
  193. size_t num_indices = indices.size();
  194. indices.resize(indices.size() + 6);
  195. int* new_indices = &indices[0] + num_indices;
  196. // Generate the vertices for the tiled surface.
  197. Vector2f tile_position = (surface_origin + tile_offset);
  198. Math::SnapToPixelGrid(tile_position, final_tile_dimensions);
  199. GeometryUtilities::GenerateQuad(new_vertices, new_indices, tile_position, final_tile_dimensions, quad_colour, scaled_texcoords[0], scaled_texcoords[1], index_offset);
  200. }
  201. // Scales a tile dimensions by a fixed value along one axis.
  202. void DecoratorTiled::ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, Axis axis_enum) const
  203. {
  204. int axis = static_cast<int>(axis_enum);
  205. if (tile_dimensions[axis] != axis_value)
  206. {
  207. tile_dimensions[1 - axis] = tile_dimensions[1 - axis] * (axis_value / tile_dimensions[axis]);
  208. tile_dimensions[axis] = axis_value;
  209. }
  210. }
  211. } // namespace Rml