DecoratorTiled.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 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()
  46. {
  47. texture_index = -1;
  48. repeat_mode = STRETCH;
  49. orientation = ROTATE_0_CW;
  50. texcoords[0].x = 0;
  51. texcoords[0].y = 0;
  52. texcoords[1].x = 1;
  53. texcoords[1].y = 1;
  54. texcoords_absolute[0][0] = false;
  55. texcoords_absolute[0][1] = false;
  56. texcoords_absolute[1][0] = false;
  57. texcoords_absolute[1][1] = false;
  58. }
  59. // Calculates the tile's dimensions from the texture and texture coordinates.
  60. void DecoratorTiled::Tile::CalculateDimensions(Element* element, const Texture& texture)
  61. {
  62. RenderInterface* render_interface = element->GetRenderInterface();
  63. TileDataMap::iterator data_iterator = data.find(render_interface);
  64. if (data_iterator == data.end())
  65. {
  66. TileData new_data;
  67. Vector2i texture_dimensions = texture.GetDimensions(render_interface);
  68. for (int i = 0; i < 2; i++)
  69. {
  70. new_data.texcoords[i] = texcoords[i];
  71. if (texcoords_absolute[i][0] &&
  72. texture_dimensions.x > 0)
  73. new_data.texcoords[i].x /= texture_dimensions.x;
  74. if (texcoords_absolute[i][1] &&
  75. texture_dimensions.y > 0)
  76. new_data.texcoords[i].y /= texture_dimensions.y;
  77. }
  78. new_data.dimensions.x = Math::AbsoluteValue((new_data.texcoords[1].x * texture_dimensions.x) - (new_data.texcoords[0].x * texture_dimensions.x));
  79. new_data.dimensions.y = Math::AbsoluteValue((new_data.texcoords[1].y * texture_dimensions.y) - (new_data.texcoords[0].y * texture_dimensions.y));
  80. data[render_interface] = new_data;
  81. }
  82. }
  83. // Get this tile's dimensions.
  84. Vector2f DecoratorTiled::Tile::GetDimensions(Element* element)
  85. {
  86. RenderInterface* render_interface = element->GetRenderInterface();
  87. TileDataMap::iterator data_iterator = data.find(render_interface);
  88. if (data_iterator == data.end())
  89. return Vector2f(0, 0);
  90. return data_iterator->second.dimensions;
  91. }
  92. // Generates geometry to render this tile across a surface.
  93. 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
  94. {
  95. RenderInterface* render_interface = element->GetRenderInterface();
  96. float opacity = element->GetProperty<float>(OPACITY);
  97. Colourb quad_colour = element->GetProperty<Colourb>(IMAGE_COLOR);
  98. // Apply opacity
  99. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  100. TileDataMap::iterator data_iterator = data.find(render_interface);
  101. if (data_iterator == data.end())
  102. return;
  103. const TileData& data = data_iterator->second;
  104. int num_tiles[2];
  105. Vector2f final_tile_dimensions;
  106. // Generate the oriented texture coordinates for the tiles.
  107. Vector2f scaled_texcoords[3];
  108. for (int i = 0; i < 2; i++)
  109. {
  110. scaled_texcoords[i].x = data.texcoords[0].x + oriented_texcoords[orientation][i].x * (data.texcoords[1].x - data.texcoords[0].x);
  111. scaled_texcoords[i].y = data.texcoords[0].y + oriented_texcoords[orientation][i].y * (data.texcoords[1].y - data.texcoords[0].y);
  112. }
  113. scaled_texcoords[2] = scaled_texcoords[1];
  114. // Resize the dimensions (if necessary) to fit this tile's repeat mode.
  115. for (int i = 0; i < 2; i++)
  116. {
  117. if (surface_dimensions[i] <= 0)
  118. num_tiles[i] = 0;
  119. else
  120. {
  121. switch (repeat_mode)
  122. {
  123. // If the tile is stretched, we only need one quad.
  124. case STRETCH:
  125. {
  126. num_tiles[i] = 1;
  127. final_tile_dimensions[i] = surface_dimensions[i];
  128. }
  129. break;
  130. // If the tile is clamped, we only need one quad if the surface is smaller than the tile, or two if it's
  131. // larger (to take the last stretched pixel).
  132. case CLAMP_STRETCH:
  133. case CLAMP_TRUNCATE:
  134. {
  135. num_tiles[i] = surface_dimensions[i] > tile_dimensions[i] ? 2 : 1;
  136. if (num_tiles[i] == 1)
  137. {
  138. final_tile_dimensions[i] = surface_dimensions[i];
  139. if (repeat_mode == CLAMP_TRUNCATE)
  140. scaled_texcoords[1][i] -= (scaled_texcoords[1][i] - scaled_texcoords[0][i]) * (1.0f - (final_tile_dimensions[i] / tile_dimensions[i]));
  141. }
  142. else
  143. final_tile_dimensions[i] = surface_dimensions[i] - tile_dimensions[i];
  144. }
  145. break;
  146. case REPEAT_STRETCH:
  147. case REPEAT_TRUNCATE:
  148. {
  149. num_tiles[i] = Math::RealToInteger((surface_dimensions[i] + (tile_dimensions[i] - 1)) / tile_dimensions[i]);
  150. num_tiles[i] = Math::Max(0, num_tiles[i]);
  151. final_tile_dimensions[i] = surface_dimensions[i] - (num_tiles[i] - 1) * tile_dimensions[i];
  152. if (final_tile_dimensions[i] <= 0)
  153. final_tile_dimensions[i] = tile_dimensions[i];
  154. if (repeat_mode == REPEAT_TRUNCATE)
  155. scaled_texcoords[2][i] -= (scaled_texcoords[1][i] - scaled_texcoords[0][i]) * (1.0f - (final_tile_dimensions[i] / tile_dimensions[i]));
  156. }
  157. break;
  158. }
  159. }
  160. }
  161. // If any of the axes are zero or below, then we have a zero surface area and nothing to render.
  162. if (num_tiles[0] <= 0 || num_tiles[1] <= 0)
  163. return;
  164. // Resize the vertex and index arrays to fit the new geometry.
  165. int index_offset = (int) vertices.size();
  166. vertices.resize(vertices.size() + num_tiles[0] * num_tiles[1] * 4);
  167. Vertex* new_vertices = &vertices[0] + index_offset;
  168. size_t num_indices = indices.size();
  169. indices.resize(indices.size() + num_tiles[0] * num_tiles[1] * 6);
  170. int* new_indices = &indices[0] + num_indices;
  171. // Generate the vertices for the tiled surface.
  172. for (int y = 0; y < num_tiles[1]; y++)
  173. {
  174. Vector2f tile_position;
  175. tile_position.y = surface_origin.y + (float) tile_dimensions.y * y;
  176. Vector2f tile_size;
  177. tile_size.y = (float) (y < num_tiles[1] - 1 ? data.dimensions.y : final_tile_dimensions.y);
  178. // Squish the texture coordinates in the y if we're clamping and this is the last in a double-tile.
  179. Vector2f tile_texcoords[2];
  180. if (num_tiles[1] == 2 &&
  181. y == 1 &&
  182. (repeat_mode == CLAMP_STRETCH ||
  183. repeat_mode == CLAMP_TRUNCATE))
  184. {
  185. tile_texcoords[0].y = scaled_texcoords[1].y;
  186. tile_texcoords[1].y = scaled_texcoords[1].y;
  187. }
  188. else
  189. {
  190. tile_texcoords[0].y = scaled_texcoords[0].y;
  191. // The last tile might have truncated texture coords
  192. if (y == num_tiles[1] - 1)
  193. tile_texcoords[1].y = scaled_texcoords[2].y;
  194. else
  195. tile_texcoords[1].y = scaled_texcoords[1].y;
  196. }
  197. for (int x = 0; x < num_tiles[0]; x++)
  198. {
  199. // Squish the texture coordinates in the x if we're clamping and this is the last in a double-tile.
  200. if (num_tiles[0] == 2 &&
  201. x == 1 &&
  202. (repeat_mode == CLAMP_STRETCH ||
  203. repeat_mode == CLAMP_TRUNCATE))
  204. {
  205. tile_texcoords[0].x = scaled_texcoords[1].x;
  206. tile_texcoords[1].x = scaled_texcoords[1].x;
  207. }
  208. else
  209. {
  210. tile_texcoords[0].x = scaled_texcoords[0].x;
  211. // The last tile might have truncated texture coords
  212. if (x == num_tiles[0] - 1)
  213. tile_texcoords[1].x = scaled_texcoords[2].x;
  214. else
  215. tile_texcoords[1].x = scaled_texcoords[1].x;
  216. }
  217. tile_position.x = surface_origin.x + (float) tile_dimensions.x * x;
  218. tile_size.x = (float) (x < num_tiles[0] - 1 ? tile_dimensions.x : final_tile_dimensions.x);
  219. tile_position = tile_position.Round();
  220. tile_size = tile_size.Round();
  221. GeometryUtilities::GenerateQuad(new_vertices, new_indices, tile_position, tile_size, quad_colour, tile_texcoords[0], tile_texcoords[1], index_offset);
  222. new_vertices += 4;
  223. new_indices += 6;
  224. index_offset += 4;
  225. }
  226. }
  227. }
  228. // Scales a tile dimensions by a fixed value along one axis.
  229. void DecoratorTiled::ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, int axis)
  230. {
  231. if (tile_dimensions[axis] != axis_value)
  232. {
  233. tile_dimensions[1 - axis] = tile_dimensions[1 - axis] * (axis_value / tile_dimensions[axis]);
  234. tile_dimensions[axis] = axis_value;
  235. }
  236. }
  237. }
  238. }