DecoratorTiled.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "DecoratorTiled.h"
  29. #include "../../Include/Rocket/Core.h"
  30. namespace Rocket {
  31. namespace Core {
  32. DecoratorTiled::DecoratorTiled()
  33. {
  34. }
  35. DecoratorTiled::~DecoratorTiled()
  36. {
  37. }
  38. static Vector2f oriented_texcoords[6][2] = {{Vector2f(0, 0), Vector2f(1, 1)},
  39. {Vector2f(0, 1), Vector2f(1, 0)},
  40. {Vector2f(1, 1), Vector2f(0, 0)},
  41. {Vector2f(1, 0), Vector2f(0, 1)},
  42. {Vector2f(1, 0), Vector2f(0, 1)},
  43. {Vector2f(0, 1), Vector2f(1, 0)}};
  44. DecoratorTiled::Tile::Tile()
  45. {
  46. texture_index = -1;
  47. repeat_mode = STRETCH;
  48. orientation = ROTATE_0_CW;
  49. texcoords[0].x = 0;
  50. texcoords[0].y = 0;
  51. texcoords[1].x = 1;
  52. texcoords[1].y = 1;
  53. texcoords_absolute[0][0] = false;
  54. texcoords_absolute[0][1] = false;
  55. texcoords_absolute[1][0] = false;
  56. texcoords_absolute[1][1] = false;
  57. }
  58. struct TileDataMapCmp {
  59. RenderInterface* find;
  60. using Element = std::pair< RenderInterface*, DecoratorTiled::Tile::TileData >;
  61. bool operator() (const Element& el) const {
  62. return el.first == find;
  63. }
  64. };
  65. // Calculates the tile's dimensions from the texture and texture coordinates.
  66. void DecoratorTiled::Tile::CalculateDimensions(Element* element, const Texture& texture)
  67. {
  68. RenderInterface* render_interface = element->GetRenderInterface();
  69. TileDataMap::iterator data_iterator = std::find_if(data.begin(), data.end(), TileDataMapCmp{ render_interface });
  70. if (data_iterator == data.end())
  71. {
  72. TileData new_data;
  73. Vector2i texture_dimensions = texture.GetDimensions(render_interface);
  74. for (int i = 0; i < 2; i++)
  75. {
  76. new_data.texcoords[i] = texcoords[i];
  77. if (texcoords_absolute[i][0] &&
  78. texture_dimensions.x > 0)
  79. new_data.texcoords[i].x /= texture_dimensions.x;
  80. if (texcoords_absolute[i][1] &&
  81. texture_dimensions.y > 0)
  82. new_data.texcoords[i].y /= texture_dimensions.y;
  83. }
  84. new_data.dimensions.x = Math::AbsoluteValue((new_data.texcoords[1].x * texture_dimensions.x) - (new_data.texcoords[0].x * texture_dimensions.x));
  85. new_data.dimensions.y = Math::AbsoluteValue((new_data.texcoords[1].y * texture_dimensions.y) - (new_data.texcoords[0].y * texture_dimensions.y));
  86. data.emplace_back( render_interface, new_data );
  87. }
  88. }
  89. // Get this tile's dimensions.
  90. Vector2f DecoratorTiled::Tile::GetDimensions(Element* element)
  91. {
  92. RenderInterface* render_interface = element->GetRenderInterface();
  93. TileDataMap::iterator data_iterator = std::find_if(data.begin(), data.end(), TileDataMapCmp{ render_interface });
  94. if (data_iterator == data.end())
  95. return Vector2f(0, 0);
  96. return data_iterator->second.dimensions;
  97. }
  98. // Generates geometry to render this tile across a surface.
  99. 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
  100. {
  101. RenderInterface* render_interface = element->GetRenderInterface();
  102. float opacity = element->GetProperty<float>(PropertyId::Opacity);
  103. Colourb quad_colour = element->GetProperty<Colourb>(PropertyId::ImageColor);
  104. // Apply opacity
  105. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  106. TileDataMap::iterator data_iterator = std::find_if(data.begin(), data.end(), TileDataMapCmp{ render_interface });
  107. if (data_iterator == data.end())
  108. return;
  109. const TileData& data = data_iterator->second;
  110. int num_tiles[2];
  111. Vector2f final_tile_dimensions;
  112. // Generate the oriented texture coordinates for the tiles.
  113. Vector2f scaled_texcoords[3];
  114. for (int i = 0; i < 2; i++)
  115. {
  116. scaled_texcoords[i].x = data.texcoords[0].x + oriented_texcoords[orientation][i].x * (data.texcoords[1].x - data.texcoords[0].x);
  117. scaled_texcoords[i].y = data.texcoords[0].y + oriented_texcoords[orientation][i].y * (data.texcoords[1].y - data.texcoords[0].y);
  118. }
  119. scaled_texcoords[2] = scaled_texcoords[1];
  120. // Resize the dimensions (if necessary) to fit this tile's repeat mode.
  121. for (int i = 0; i < 2; i++)
  122. {
  123. if (surface_dimensions[i] <= 0)
  124. num_tiles[i] = 0;
  125. else
  126. {
  127. switch (repeat_mode)
  128. {
  129. // If the tile is stretched, we only need one quad.
  130. case STRETCH:
  131. {
  132. num_tiles[i] = 1;
  133. final_tile_dimensions[i] = surface_dimensions[i];
  134. }
  135. break;
  136. // If the tile is clamped, we only need one quad if the surface is smaller than the tile, or two if it's
  137. // larger (to take the last stretched pixel).
  138. case CLAMP_STRETCH:
  139. case CLAMP_TRUNCATE:
  140. {
  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. }
  167. // If any of the axes are zero or below, then we have a zero surface area and nothing to render.
  168. if (num_tiles[0] <= 0 || num_tiles[1] <= 0)
  169. return;
  170. // Resize the vertex and index arrays to fit the new geometry.
  171. int index_offset = (int) vertices.size();
  172. vertices.resize(vertices.size() + num_tiles[0] * num_tiles[1] * 4);
  173. Vertex* new_vertices = &vertices[0] + index_offset;
  174. size_t num_indices = indices.size();
  175. indices.resize(indices.size() + num_tiles[0] * num_tiles[1] * 6);
  176. int* new_indices = &indices[0] + num_indices;
  177. // Generate the vertices for the tiled surface.
  178. for (int y = 0; y < num_tiles[1]; y++)
  179. {
  180. Vector2f tile_position;
  181. tile_position.y = surface_origin.y + (float) tile_dimensions.y * y;
  182. Vector2f tile_size;
  183. tile_size.y = (float) (y < num_tiles[1] - 1 ? data.dimensions.y : final_tile_dimensions.y);
  184. // Squish the texture coordinates in the y if we're clamping and this is the last in a double-tile.
  185. Vector2f tile_texcoords[2];
  186. if (num_tiles[1] == 2 &&
  187. y == 1 &&
  188. (repeat_mode == CLAMP_STRETCH ||
  189. repeat_mode == CLAMP_TRUNCATE))
  190. {
  191. tile_texcoords[0].y = scaled_texcoords[1].y;
  192. tile_texcoords[1].y = scaled_texcoords[1].y;
  193. }
  194. else
  195. {
  196. tile_texcoords[0].y = scaled_texcoords[0].y;
  197. // The last tile might have truncated texture coords
  198. if (y == num_tiles[1] - 1)
  199. tile_texcoords[1].y = scaled_texcoords[2].y;
  200. else
  201. tile_texcoords[1].y = scaled_texcoords[1].y;
  202. }
  203. for (int x = 0; x < num_tiles[0]; x++)
  204. {
  205. // Squish the texture coordinates in the x if we're clamping and this is the last in a double-tile.
  206. if (num_tiles[0] == 2 &&
  207. x == 1 &&
  208. (repeat_mode == CLAMP_STRETCH ||
  209. repeat_mode == CLAMP_TRUNCATE))
  210. {
  211. tile_texcoords[0].x = scaled_texcoords[1].x;
  212. tile_texcoords[1].x = scaled_texcoords[1].x;
  213. }
  214. else
  215. {
  216. tile_texcoords[0].x = scaled_texcoords[0].x;
  217. // The last tile might have truncated texture coords
  218. if (x == num_tiles[0] - 1)
  219. tile_texcoords[1].x = scaled_texcoords[2].x;
  220. else
  221. tile_texcoords[1].x = scaled_texcoords[1].x;
  222. }
  223. tile_position.x = surface_origin.x + (float) tile_dimensions.x * x;
  224. tile_size.x = (float) (x < num_tiles[0] - 1 ? tile_dimensions.x : final_tile_dimensions.x);
  225. tile_position = tile_position.Round();
  226. tile_size = tile_size.Round();
  227. GeometryUtilities::GenerateQuad(new_vertices, new_indices, tile_position, tile_size, quad_colour, tile_texcoords[0], tile_texcoords[1], index_offset);
  228. new_vertices += 4;
  229. new_indices += 6;
  230. index_offset += 4;
  231. }
  232. }
  233. }
  234. // Scales a tile dimensions by a fixed value along one axis.
  235. void DecoratorTiled::ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, int axis)
  236. {
  237. if (tile_dimensions[axis] != axis_value)
  238. {
  239. tile_dimensions[1 - axis] = tile_dimensions[1 - axis] * (axis_value / tile_dimensions[axis]);
  240. tile_dimensions[axis] = axis_value;
  241. }
  242. }
  243. }
  244. }