DecoratorTiled.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-2023 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 RMLUI_CORE_DECORATORTILED_H
  29. #define RMLUI_CORE_DECORATORTILED_H
  30. #include "../../Include/RmlUi/Core/ComputedValues.h"
  31. #include "../../Include/RmlUi/Core/Decorator.h"
  32. #include "../../Include/RmlUi/Core/Vertex.h"
  33. namespace Rml {
  34. struct Texture;
  35. /**
  36. Base class for tiled decorators.
  37. @author Peter Curry
  38. */
  39. class DecoratorTiled : public Decorator {
  40. public:
  41. DecoratorTiled();
  42. virtual ~DecoratorTiled();
  43. /**
  44. Stores the orientation of a tile.
  45. */
  46. enum TileOrientation {
  47. ORIENTATION_NONE, // No orientation.
  48. FLIP_HORIZONTAL, // Flipped horizontally.
  49. FLIP_VERTICAL, // Flipped vertically.
  50. ROTATE_180, // Rotated 180 degrees clockwise.
  51. };
  52. /**
  53. Stores the fit mode of a tile.
  54. */
  55. enum TileFitMode {
  56. FILL, // Tile is stretched to boundaries.
  57. CONTAIN, // Tile is stretched to boundaries, keeping aspect ratio fixed, 'letter-boxed'.
  58. COVER, // Tile is stretched to cover the boundaries, keeping aspect ratio fixed.
  59. SCALE_NONE, // Tile is never scaled.
  60. SCALE_DOWN, // Tile acts like 'scale-none' if smaller than boundaries, or like 'contain' otherwise.
  61. REPEAT, // Tile is repeated on both x and y axis
  62. REPEAT_X, // Tile is repeated on the x axis
  63. REPEAT_Y, // Tile is repeated on the y axis
  64. };
  65. /**
  66. Structure for storing the different tiles the tiled decorator uses internally over its
  67. surface.
  68. @author Peter Curry
  69. */
  70. struct Tile {
  71. /// Constructs the tile with safe default values.
  72. Tile();
  73. /// Calculates the tile's dimensions from the texture and texture coordinates.
  74. void CalculateDimensions(const Texture& texture) const;
  75. /// Get the dimensions (in px) that this tile is ideally displayed as.
  76. /// Uses the dp-ratio of the current element and 'display_scale' to calculate the dimensions.
  77. Vector2f GetNaturalDimensions(Element* element) const;
  78. /// Generates geometry to render this tile across a surface.
  79. /// @param[out] vertices The array to store the generated vertex data.
  80. /// @param[out] indices The array to store the generated index data.
  81. /// @param[in] computed_values The computed values of the element being decorated.
  82. /// @param[in] surface_origin The starting point of the first tile to generate.
  83. /// @param[in] surface_dimensions The dimensions of the surface to be tiled.
  84. /// @param[in] tile_dimensions The dimensions to render this tile at.
  85. void GenerateGeometry(Vector<Vertex>& vertices, Vector<int>& indices, const ComputedValues& computed_values, Vector2f surface_origin,
  86. Vector2f surface_dimensions, Vector2f tile_dimensions) const;
  87. struct TileData {
  88. Vector2f size; // 'px' units
  89. Vector2f texcoords[2]; // relative units
  90. };
  91. int texture_index;
  92. // Scales the desired displayed size of the tile from raw pixel size. Eg. the 'display_scale' in a sprite sheet.
  93. float display_scale;
  94. // Position and size within the texture, absolute units (px)
  95. Vector2f position, size;
  96. mutable TileData tile_data;
  97. mutable bool tile_data_calculated = false;
  98. TileOrientation orientation;
  99. TileFitMode fit_mode;
  100. Style::LengthPercentage align[2];
  101. };
  102. protected:
  103. enum class Axis { Horizontal, Vertical };
  104. /// Scales a tile dimensions by a fixed value along one axis.
  105. /// @param tile_dimensions[in, out] The tile dimensions to scale.
  106. /// @param axis_value[in] The fixed value to scale against.
  107. /// @param axis[in] The axis to scale against.
  108. void ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, Axis axis) const;
  109. };
  110. } // namespace Rml
  111. #endif