DecoratorTiled.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef ROCKETCOREDECORATORTILED_H
  28. #define ROCKETCOREDECORATORTILED_H
  29. #include "../../Include/Rocket/Core/Decorator.h"
  30. #include "../../Include/Rocket/Core/Vertex.h"
  31. namespace Rocket {
  32. namespace Core {
  33. struct Texture;
  34. /**
  35. Base class for tiled decorators.
  36. @author Peter Curry
  37. */
  38. class DecoratorTiled : public Decorator
  39. {
  40. public:
  41. DecoratorTiled();
  42. virtual ~DecoratorTiled();
  43. /**
  44. Stores the repetition mode for a tile, for when it is rendered on a surface that is a
  45. different size than itself.
  46. */
  47. enum TileRepeatMode
  48. {
  49. STRETCH = 0, // Stretches a single tile across the required surface.
  50. CLAMP_STRETCH = 1, // Clamps the tile to the upper left, stretching the tile inwards to fit into the element if it is too small.
  51. CLAMP_TRUNCATE = 2, // Clamps the tile to the upper left, truncating the tile to fit into the element if it is too small.
  52. REPEAT_STRETCH = 3, // Repeats the tile, stretching the final tile inwards.
  53. REPEAT_TRUNCATE = 4, // Repeats the tile, truncating the final tile.
  54. };
  55. /**
  56. Stores the orientation of a tile.
  57. */
  58. enum TileOrientation
  59. {
  60. ROTATE_0_CW = 0, // Rotated zero degrees clockwise.
  61. ROTATE_90_CW = 1, // Rotated 90 degrees clockwise.
  62. ROTATE_180_CW = 2, // Rotated 180 degrees clockwise.
  63. ROTATE_270_CW = 3, // Rotated 270 degrees clockwise.
  64. FLIP_HORIZONTAL = 4, // Flipped horizontally.
  65. FLIP_VERTICAL = 5 // Flipped vertically.
  66. };
  67. /**
  68. Structure for storing the different tiles the tiled decorator uses internally over its
  69. surface.
  70. @author Peter Curry
  71. */
  72. struct Tile
  73. {
  74. /// Constructs the tile with safe default values.
  75. Tile();
  76. /// Calculates the tile's dimensions from the texture and texture coordinates.
  77. void CalculateDimensions(Element* element, const Texture& texture);
  78. /// Get this tile's dimensions.
  79. Vector2f GetDimensions(Element* element);
  80. /// Generates geometry to render this tile across a surface.
  81. /// @param[out] vertices The array to store the generated vertex data.
  82. /// @param[out] indices The array to store the generated index data.
  83. /// @param[in] element The element hosting the decorator.
  84. /// @param[in] surface_origin The starting point of the first tile to generate.
  85. /// @param[in] surface_dimensions The dimensions of the surface to be tiled.
  86. /// @param[in] tile_dimensions The dimensions to render this tile at.
  87. void GenerateGeometry(std::vector< Vertex >& vertices, std::vector< int >& indices, Element* element, const Vector2f& surface_origin, const Vector2f& surface_dimensions, const Vector2f& tile_dimensions) const;
  88. struct TileData
  89. {
  90. Vector2f dimensions;
  91. Vector2f texcoords[2];
  92. };
  93. typedef std::map< RenderInterface*, TileData > TileDataMap;
  94. int texture_index;
  95. Vector2f texcoords[2];
  96. bool texcoords_absolute[2][2];
  97. mutable TileDataMap data;
  98. TileRepeatMode repeat_mode;
  99. TileOrientation orientation;
  100. };
  101. protected:
  102. /// Scales a tile dimensions by a fixed value along one axis.
  103. /// @param tile_dimensions[in, out] The tile dimensions to scale.
  104. /// @param axis_value[in] The fixed value to scale against.
  105. /// @param axis[in] The axis to scale against; either 0 (for x) or 1 (for y).
  106. void ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, int axis);
  107. };
  108. }
  109. }
  110. #endif