DecoratorTiled.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_CW = 0, // Rotated zero degrees clockwise.
  62. ROTATE_90_CW = 1, // Rotated 90 degrees clockwise.
  63. ROTATE_180_CW = 2, // Rotated 180 degrees clockwise.
  64. ROTATE_270_CW = 3, // Rotated 270 degrees clockwise.
  65. FLIP_HORIZONTAL = 4, // Flipped horizontally.
  66. FLIP_VERTICAL = 5 // Flipped vertically.
  67. };
  68. /**
  69. Structure for storing the different tiles the tiled decorator uses internally over its
  70. surface.
  71. @author Peter Curry
  72. */
  73. struct Tile
  74. {
  75. /// Constructs the tile with safe default values.
  76. Tile();
  77. /// Calculates the tile's dimensions from the texture and texture coordinates.
  78. void CalculateDimensions(Element* element, const Texture& texture) const;
  79. /// Get this tile's dimensions.
  80. Vector2f GetDimensions(Element* element) const;
  81. /// Generates geometry to render this tile across a surface.
  82. /// @param[out] vertices The array to store the generated vertex data.
  83. /// @param[out] indices The array to store the generated index data.
  84. /// @param[in] element The element hosting the decorator.
  85. /// @param[in] surface_origin The starting point of the first tile to generate.
  86. /// @param[in] surface_dimensions The dimensions of the surface to be tiled.
  87. /// @param[in] tile_dimensions The dimensions to render this tile at.
  88. 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;
  89. struct TileData
  90. {
  91. Vector2f size; // 'px' units
  92. Vector2f texcoords[2]; // relative units
  93. };
  94. using TileDataMap = SmallUnorderedMap< RenderInterface*, TileData >;
  95. int texture_index;
  96. // Position and size within the texture, absolute or relative units
  97. Vector2f position, size;
  98. // Absolute is 'px' units, otherwise relative to the dimensions of the texture
  99. bool position_absolute[2], size_absolute[2];
  100. mutable TileDataMap data;
  101. TileRepeatMode repeat_mode;
  102. TileOrientation orientation;
  103. };
  104. protected:
  105. /// Scales a tile dimensions by a fixed value along one axis.
  106. /// @param tile_dimensions[in, out] The tile dimensions to scale.
  107. /// @param axis_value[in] The fixed value to scale against.
  108. /// @param axis[in] The axis to scale against; either 0 (for x) or 1 (for y).
  109. void ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, int axis) const;
  110. };
  111. }
  112. }
  113. #endif