DecoratorTiled.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. class Texture;
  35. struct Mesh;
  36. /**
  37. Base class for tiled decorators.
  38. @author Peter Curry
  39. */
  40. class DecoratorTiled : public Decorator {
  41. public:
  42. DecoratorTiled();
  43. virtual ~DecoratorTiled();
  44. /**
  45. Stores the orientation of a tile.
  46. */
  47. enum TileOrientation {
  48. ORIENTATION_NONE, // No orientation.
  49. FLIP_HORIZONTAL, // Flipped horizontally.
  50. FLIP_VERTICAL, // Flipped vertically.
  51. ROTATE_180, // Rotated 180 degrees clockwise.
  52. };
  53. /**
  54. Stores the fit mode of a tile.
  55. */
  56. enum TileFitMode {
  57. FILL, // Tile is stretched to boundaries.
  58. CONTAIN, // Tile is stretched to boundaries, keeping aspect ratio fixed, 'letter-boxed'.
  59. COVER, // Tile is stretched to cover the boundaries, keeping aspect ratio fixed.
  60. SCALE_NONE, // Tile is never scaled.
  61. SCALE_DOWN, // Tile acts like 'scale-none' if smaller than boundaries, or like 'contain' otherwise.
  62. REPEAT, // Tile is repeated on both x and y axis
  63. REPEAT_X, // Tile is repeated on the x axis
  64. REPEAT_Y, // Tile is repeated on the y axis
  65. };
  66. /**
  67. Structure for storing the different tiles the tiled decorator uses internally over its
  68. surface.
  69. @author Peter Curry
  70. */
  71. struct Tile {
  72. /// Constructs the tile with safe default values.
  73. Tile();
  74. /// Calculates the tile's dimensions from the texture and texture coordinates.
  75. void CalculateDimensions(Texture texture) const;
  76. /// Get the dimensions (in px) that this tile is ideally displayed as.
  77. /// Uses the dp-ratio of the current element and 'display_scale' to calculate the dimensions.
  78. Vector2f GetNaturalDimensions(Element* element) const;
  79. /// Generates geometry to render this tile across a surface.
  80. /// @param[out] vertices The array to store the generated vertex data.
  81. /// @param[out] indices The array to store the generated index data.
  82. /// @param[in] computed_values The computed values of the element being decorated.
  83. /// @param[in] surface_origin The starting point of the first tile to generate.
  84. /// @param[in] surface_dimensions The dimensions of the surface to be tiled.
  85. /// @param[in] tile_dimensions The dimensions to render this tile at.
  86. void GenerateGeometry(Mesh& mesh, const ComputedValues& computed_values, Vector2f surface_origin, Vector2f surface_dimensions,
  87. Vector2f tile_dimensions) const;
  88. struct TileData {
  89. Vector2f size; // 'px' units
  90. Vector2f texcoords[2]; // relative units
  91. };
  92. int texture_index;
  93. // Scales the desired displayed size of the tile from raw pixel size. Eg. the 'display_scale' in a sprite sheet.
  94. float display_scale;
  95. // Position and size within the texture, absolute units (px)
  96. Vector2f position, size;
  97. mutable TileData tile_data;
  98. mutable bool tile_data_calculated = false;
  99. TileOrientation orientation;
  100. TileFitMode fit_mode;
  101. Style::LengthPercentage align[2];
  102. };
  103. protected:
  104. enum class Axis { Horizontal, Vertical };
  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.
  109. void ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, Axis axis) const;
  110. };
  111. class DecoratorTiledInstancer : public DecoratorInstancer {
  112. public:
  113. DecoratorTiledInstancer(size_t num_tiles);
  114. protected:
  115. /// Adds the property declarations for a tile.
  116. /// @param[in] name The name of the tile property.
  117. /// @param[in] register_fit_modes If true, the tile will have the fit modes registered.
  118. void RegisterTileProperty(const String& name, bool register_fit_modes = false);
  119. /// Retrieves all the properties for a tile from the property dictionary.
  120. /// @param[out] tile The tile structure for storing the tile properties.
  121. /// @param[out] textures Holds the textures declared for the tile.
  122. /// @param[in] properties The user-defined list of parameters for the decorator.
  123. /// @param[in] instancer_interface An interface for querying the active style sheet.
  124. bool GetTileProperties(DecoratorTiled::Tile* tiles, Texture* textures, size_t num_tiles_and_textures, const PropertyDictionary& properties,
  125. const DecoratorInstancerInterface& instancer_interface) const;
  126. private:
  127. struct TilePropertyIds {
  128. PropertyId src, fit, align_x, align_y, orientation;
  129. };
  130. Vector<TilePropertyIds> tile_property_ids;
  131. };
  132. } // namespace Rml
  133. #endif