DecoratorTiled.cpp 9.5 KB

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