DecoratorTiled.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "precompiled.h"
  29. #include "DecoratorTiled.h"
  30. #include "../../Include/RmlUi/Core.h"
  31. namespace Rml {
  32. namespace Core {
  33. DecoratorTiled::DecoratorTiled()
  34. {
  35. }
  36. DecoratorTiled::~DecoratorTiled()
  37. {
  38. }
  39. static const Vector2f oriented_texcoords[6][2] = {{Vector2f(0, 0), Vector2f(1, 1)},
  40. {Vector2f(0, 1), Vector2f(1, 0)},
  41. {Vector2f(1, 1), Vector2f(0, 0)},
  42. {Vector2f(1, 0), Vector2f(0, 1)},
  43. {Vector2f(1, 0), Vector2f(0, 1)},
  44. {Vector2f(0, 1), Vector2f(1, 0)}};
  45. DecoratorTiled::Tile::Tile() : position(0, 0), size(1, 1)
  46. {
  47. texture_index = -1;
  48. repeat_mode = STRETCH;
  49. fit_mode = FILL;
  50. orientation = ROTATE_0;
  51. position_absolute[0] = false;
  52. position_absolute[1] = false;
  53. size_absolute[0] = false;
  54. size_absolute[1] = false;
  55. }
  56. // Calculates the tile's dimensions from the texture and texture coordinates.
  57. void DecoratorTiled::Tile::CalculateDimensions(Element* element, const Texture& texture) const
  58. {
  59. RenderInterface* render_interface = element->GetRenderInterface();
  60. auto data_iterator = data.find(render_interface);
  61. if (data_iterator == data.end())
  62. {
  63. TileData new_data;
  64. const Vector2i texture_dimensions_i = texture.GetDimensions(render_interface);
  65. const Vector2f texture_dimensions((float)texture_dimensions_i.x, (float)texture_dimensions_i.y);
  66. if (texture_dimensions.x == 0 || texture_dimensions.y == 0)
  67. {
  68. new_data.size = Vector2f(0, 0);
  69. new_data.texcoords[0] = Vector2f(0, 0);
  70. new_data.texcoords[1] = Vector2f(0, 0);
  71. }
  72. else
  73. {
  74. // Need to scale the coordinates to normalized units and 'size' to absolute size (pixels)
  75. for(int i = 0; i < 2; i++)
  76. {
  77. float size_relative = size[i];
  78. new_data.size[i] = Math::AbsoluteValue(size[i]);
  79. if (size_absolute[i])
  80. size_relative /= texture_dimensions[i];
  81. else
  82. new_data.size[i] *= texture_dimensions[i];
  83. new_data.texcoords[0][i] = position[i];
  84. if (position_absolute[i])
  85. new_data.texcoords[0][i] /= texture_dimensions[i];
  86. new_data.texcoords[1][i] = size_relative + new_data.texcoords[0][i];
  87. }
  88. }
  89. data.emplace( render_interface, new_data );
  90. }
  91. }
  92. // Get this tile's dimensions.
  93. Vector2f DecoratorTiled::Tile::GetDimensions(Element* element) const
  94. {
  95. RenderInterface* render_interface = element->GetRenderInterface();
  96. auto data_iterator = data.find(render_interface);
  97. if (data_iterator == data.end())
  98. return Vector2f(0, 0);
  99. return data_iterator->second.size;
  100. }
  101. // Generates geometry to render this tile across a surface.
  102. void DecoratorTiled::Tile::GenerateGeometry(std::vector< Vertex >& vertices, std::vector< int >& indices, Element* element, const Vector2f& surface_origin, const Vector2f& surface_dimensions, const Vector2f& tile_dimensions) const
  103. {
  104. if (surface_dimensions.x <= 0 || surface_dimensions.y <= 0)
  105. return;
  106. RenderInterface* render_interface = element->GetRenderInterface();
  107. const auto& computed = element->GetComputedValues();
  108. float opacity = computed.opacity;
  109. Colourb quad_colour = computed.image_color;
  110. // Apply opacity
  111. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  112. auto data_iterator = data.find(render_interface);
  113. if (data_iterator == data.end())
  114. return;
  115. const TileData& data = data_iterator->second;
  116. int num_tiles[2] = { 0, 0 };
  117. Vector2f final_tile_dimensions( 0, 0 );
  118. // Generate the oriented texture coordinates for the tiles.
  119. Vector2f scaled_texcoords[3];
  120. for (int i = 0; i < 2; i++)
  121. {
  122. scaled_texcoords[i] = data.texcoords[0] + oriented_texcoords[orientation][i] * (data.texcoords[1] - data.texcoords[0]);
  123. }
  124. scaled_texcoords[2] = scaled_texcoords[1];
  125. // Resize the dimensions (if necessary) to fit this tile's repeat mode.
  126. for (int i = 0; i < 2; i++)
  127. {
  128. switch (repeat_mode)
  129. {
  130. case STRETCH:
  131. {
  132. // If the tile is stretched, we only need one quad.
  133. num_tiles[i] = 1;
  134. final_tile_dimensions[i] = surface_dimensions[i];
  135. }
  136. break;
  137. case CLAMP_STRETCH:
  138. case CLAMP_TRUNCATE:
  139. {
  140. // If the tile is clamped, we only need one quad if the surface is smaller than the tile, or two if it's larger (to take the last stretched pixel).
  141. num_tiles[i] = surface_dimensions[i] > tile_dimensions[i] ? 2 : 1;
  142. if (num_tiles[i] == 1)
  143. {
  144. final_tile_dimensions[i] = surface_dimensions[i];
  145. if (repeat_mode == CLAMP_TRUNCATE)
  146. scaled_texcoords[1][i] -= (scaled_texcoords[1][i] - scaled_texcoords[0][i]) * (1.0f - (final_tile_dimensions[i] / tile_dimensions[i]));
  147. }
  148. else
  149. final_tile_dimensions[i] = surface_dimensions[i] - tile_dimensions[i];
  150. }
  151. break;
  152. case REPEAT_STRETCH:
  153. case REPEAT_TRUNCATE:
  154. {
  155. num_tiles[i] = Math::RealToInteger((surface_dimensions[i] + (tile_dimensions[i] - 1)) / tile_dimensions[i]);
  156. num_tiles[i] = Math::Max(0, num_tiles[i]);
  157. final_tile_dimensions[i] = surface_dimensions[i] - (num_tiles[i] - 1) * tile_dimensions[i];
  158. if (final_tile_dimensions[i] <= 0)
  159. final_tile_dimensions[i] = tile_dimensions[i];
  160. if (repeat_mode == REPEAT_TRUNCATE)
  161. scaled_texcoords[2][i] -= (scaled_texcoords[1][i] - scaled_texcoords[0][i]) * (1.0f - (final_tile_dimensions[i] / tile_dimensions[i]));
  162. }
  163. break;
  164. }
  165. }
  166. // For now, we assume that fit mode and repeat mode cannot both be set on a single tile. The two code paths won't work well together.
  167. RMLUI_ASSERT(repeat_mode == STRETCH || fit_mode == FILL);
  168. bool offset_and_clip_tile = false;
  169. switch (fit_mode)
  170. {
  171. case FILL:
  172. // Do nothing, use results from above.
  173. break;
  174. case CONTAIN:
  175. {
  176. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  177. float min_factor = std::min(scale_factor.x, scale_factor.y);
  178. final_tile_dimensions = tile_dimensions * min_factor;
  179. offset_and_clip_tile = true;
  180. }
  181. break;
  182. case COVER:
  183. {
  184. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  185. float max_factor = std::max(scale_factor.x, scale_factor.y);
  186. final_tile_dimensions = tile_dimensions * max_factor;
  187. offset_and_clip_tile = true;
  188. }
  189. break;
  190. case SCALE_NONE:
  191. {
  192. final_tile_dimensions = tile_dimensions;
  193. offset_and_clip_tile = true;
  194. }
  195. break;
  196. case SCALE_DOWN:
  197. {
  198. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  199. float min_factor = std::min(scale_factor.x, scale_factor.y);
  200. if (min_factor < 1.0f)
  201. final_tile_dimensions = tile_dimensions * min_factor;
  202. else
  203. final_tile_dimensions = tile_dimensions;
  204. offset_and_clip_tile = true;
  205. }
  206. break;
  207. }
  208. Vector2f tile_offset(0, 0);
  209. if (offset_and_clip_tile)
  210. {
  211. // Offset tile along each dimension.
  212. for(int i = 0; i < 2; i++)
  213. {
  214. switch (align[i].type) {
  215. case Style::LengthPercentage::Length: tile_offset[i] = align[i].value; break;
  216. case Style::LengthPercentage::Percentage: tile_offset[i] = (surface_dimensions[i] - final_tile_dimensions[i]) * align[i].value * 0.01f; break;
  217. }
  218. }
  219. tile_offset = tile_offset.Round();
  220. // Clip tile. See if our tile extends outside the boundary at either side, along each dimension.
  221. for(int i = 0; i < 2; i++)
  222. {
  223. // Left/right acts as top/bottom during the second iteration.
  224. float overshoot_left = std::max(-tile_offset[i], 0.0f);
  225. float overshoot_right = std::max(tile_offset[i] + final_tile_dimensions[i] - surface_dimensions[i], 0.0f);
  226. if(overshoot_left > 0.f || overshoot_right > 0.f)
  227. {
  228. float& left = scaled_texcoords[0][i];
  229. float& right = scaled_texcoords[1][i];
  230. float width = right - left;
  231. left += overshoot_left / final_tile_dimensions[i] * width;
  232. right -= overshoot_right / final_tile_dimensions[i] * width;
  233. final_tile_dimensions[i] -= overshoot_left + overshoot_right;
  234. tile_offset[i] += overshoot_left;
  235. }
  236. }
  237. scaled_texcoords[2] = scaled_texcoords[1];
  238. }
  239. // If any of the axes are zero or below, then we have a zero surface area and nothing to render.
  240. if (num_tiles[0] <= 0 || num_tiles[1] <= 0)
  241. return;
  242. // Resize the vertex and index arrays to fit the new geometry.
  243. int index_offset = (int) vertices.size();
  244. vertices.resize(vertices.size() + num_tiles[0] * num_tiles[1] * 4);
  245. Vertex* new_vertices = &vertices[0] + index_offset;
  246. size_t num_indices = indices.size();
  247. indices.resize(indices.size() + num_tiles[0] * num_tiles[1] * 6);
  248. int* new_indices = &indices[0] + num_indices;
  249. // Generate the vertices for the tiled surface.
  250. for (int y = 0; y < num_tiles[1]; y++)
  251. {
  252. Vector2f tile_position;
  253. tile_position.y = surface_origin.y + (float) tile_dimensions.y * y;
  254. Vector2f tile_size;
  255. tile_size.y = (float) (y < num_tiles[1] - 1 ? data.size.y : final_tile_dimensions.y);
  256. // Squish the texture coordinates in the y if we're clamping and this is the last in a double-tile.
  257. Vector2f tile_texcoords[2];
  258. if (num_tiles[1] == 2 &&
  259. y == 1 &&
  260. (repeat_mode == CLAMP_STRETCH ||
  261. repeat_mode == CLAMP_TRUNCATE))
  262. {
  263. tile_texcoords[0].y = scaled_texcoords[1].y;
  264. tile_texcoords[1].y = scaled_texcoords[1].y;
  265. }
  266. else
  267. {
  268. tile_texcoords[0].y = scaled_texcoords[0].y;
  269. // The last tile might have truncated texture coords
  270. if (y == num_tiles[1] - 1)
  271. tile_texcoords[1].y = scaled_texcoords[2].y;
  272. else
  273. tile_texcoords[1].y = scaled_texcoords[1].y;
  274. }
  275. for (int x = 0; x < num_tiles[0]; x++)
  276. {
  277. // Squish the texture coordinates in the x if we're clamping and this is the last in a double-tile.
  278. if (num_tiles[0] == 2 &&
  279. x == 1 &&
  280. (repeat_mode == CLAMP_STRETCH ||
  281. repeat_mode == CLAMP_TRUNCATE))
  282. {
  283. tile_texcoords[0].x = scaled_texcoords[1].x;
  284. tile_texcoords[1].x = scaled_texcoords[1].x;
  285. }
  286. else
  287. {
  288. tile_texcoords[0].x = scaled_texcoords[0].x;
  289. // The last tile might have truncated texture coords
  290. if (x == num_tiles[0] - 1)
  291. tile_texcoords[1].x = scaled_texcoords[2].x;
  292. else
  293. tile_texcoords[1].x = scaled_texcoords[1].x;
  294. }
  295. tile_position.x = surface_origin.x + (float) tile_dimensions.x * x;
  296. tile_size.x = (float) (x < num_tiles[0] - 1 ? tile_dimensions.x : final_tile_dimensions.x);
  297. tile_position = (tile_position + tile_offset).Round();
  298. tile_size = tile_size.Round();
  299. GeometryUtilities::GenerateQuad(new_vertices, new_indices, tile_position, tile_size, quad_colour, tile_texcoords[0], tile_texcoords[1], index_offset);
  300. new_vertices += 4;
  301. new_indices += 6;
  302. index_offset += 4;
  303. }
  304. }
  305. }
  306. // Scales a tile dimensions by a fixed value along one axis.
  307. void DecoratorTiled::ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, int axis) const
  308. {
  309. if (tile_dimensions[axis] != axis_value)
  310. {
  311. tile_dimensions[1 - axis] = tile_dimensions[1 - axis] * (axis_value / tile_dimensions[axis]);
  312. tile_dimensions[axis] = axis_value;
  313. }
  314. }
  315. }
  316. }