2
0

DecoratorTiled.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef RMLUICOREDECORATORTILED_H
  29. #define RMLUICOREDECORATORTILED_H
  30. #include "../../Include/RmlUi/Core/Decorator.h"
  31. #include "../../Include/RmlUi/Core/Vertex.h"
  32. namespace Rml {
  33. namespace Core {
  34. struct Texture;
  35. /**
  36. Base class for tiled decorators.
  37. @author Peter Curry
  38. */
  39. class DecoratorTiled : public Decorator
  40. {
  41. public:
  42. DecoratorTiled();
  43. virtual ~DecoratorTiled();
  44. /**
  45. Stores the repetition mode for a tile, for when it is rendered on a surface that is a
  46. different size than itself.
  47. */
  48. enum TileRepeatMode
  49. {
  50. STRETCH = 0, // Stretches a single tile across the required surface.
  51. CLAMP_STRETCH = 1, // Clamps the tile to the upper left, stretching the tile inwards to fit into the element if it is too small.
  52. CLAMP_TRUNCATE = 2, // Clamps the tile to the upper left, truncating the tile to fit into the element if it is too small.
  53. REPEAT_STRETCH = 3, // Repeats the tile, stretching the final tile inwards.
  54. REPEAT_TRUNCATE = 4, // Repeats the tile, truncating the final tile.
  55. };
  56. /**
  57. Stores the orientation of a tile.
  58. */
  59. enum TileOrientation
  60. {
  61. ROTATE_0 = 0, // Rotated zero degrees clockwise.
  62. ROTATE_90 = 1, // Rotated 90 degrees clockwise.
  63. ROTATE_180 = 2, // Rotated 180 degrees clockwise.
  64. ROTATE_270 = 3, // Rotated 270 degrees clockwise.
  65. FLIP_HORIZONTAL = 4, // Flipped horizontally.
  66. FLIP_VERTICAL = 5 // Flipped vertically.
  67. };
  68. /**
  69. Stores the fit mode of a tile.
  70. */
  71. enum TileFitMode
  72. {
  73. FILL, // Tile is stretched to boundaries.
  74. CONTAIN, // Tile is stretched to boundaries, keeping aspect ratio fixed, 'letter-boxed'.
  75. COVER, // Tile is stretched to cover the boundaries, keeping aspect ratio fixed, and clipped.
  76. CENTER, // Tile is centered and never stretched, clipped if too large.
  77. SCALE_DOWN, // Tile acts like 'center' if smaller than boundaries, or like 'contain' otherwise.
  78. };
  79. /**
  80. Structure for storing the different tiles the tiled decorator uses internally over its
  81. surface.
  82. @author Peter Curry
  83. */
  84. struct Tile
  85. {
  86. /// Constructs the tile with safe default values.
  87. Tile();
  88. /// Calculates the tile's dimensions from the texture and texture coordinates.
  89. void CalculateDimensions(Element* element, const Texture& texture) const;
  90. /// Get this tile's dimensions.
  91. Vector2f GetDimensions(Element* element) const;
  92. /// Generates geometry to render this tile across a surface.
  93. /// @param[out] vertices The array to store the generated vertex data.
  94. /// @param[out] indices The array to store the generated index data.
  95. /// @param[in] element The element hosting the decorator.
  96. /// @param[in] surface_origin The starting point of the first tile to generate.
  97. /// @param[in] surface_dimensions The dimensions of the surface to be tiled.
  98. /// @param[in] tile_dimensions The dimensions to render this tile at.
  99. 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;
  100. struct TileData
  101. {
  102. Vector2f size; // 'px' units
  103. Vector2f texcoords[2]; // relative units
  104. };
  105. using TileDataMap = SmallUnorderedMap< RenderInterface*, TileData >;
  106. int texture_index;
  107. // Position and size within the texture, absolute or relative units
  108. Vector2f position, size;
  109. // Absolute is 'px' units, otherwise relative to the dimensions of the texture
  110. bool position_absolute[2], size_absolute[2];
  111. mutable TileDataMap data;
  112. TileRepeatMode repeat_mode;
  113. TileFitMode fit_mode;
  114. TileOrientation orientation;
  115. };
  116. protected:
  117. /// Scales a tile dimensions by a fixed value along one axis.
  118. /// @param tile_dimensions[in, out] The tile dimensions to scale.
  119. /// @param axis_value[in] The fixed value to scale against.
  120. /// @param axis[in] The axis to scale against; either 0 (for x) or 1 (for y).
  121. void ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, int axis) const;
  122. };
  123. }
  124. }
  125. #endif