DecoratorTiled.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. };
  62. /**
  63. Structure for storing the different tiles the tiled decorator uses internally over its
  64. surface.
  65. @author Peter Curry
  66. */
  67. struct Tile {
  68. /// Constructs the tile with safe default values.
  69. Tile();
  70. /// Calculates the tile's dimensions from the texture and texture coordinates.
  71. void CalculateDimensions(const Texture& texture) const;
  72. /// Get the dimensions (in px) that this tile is ideally displayed as.
  73. /// Uses the dp-ratio of the current element and 'display_scale' to calculate the dimensions.
  74. Vector2f GetNaturalDimensions(Element* element) const;
  75. /// Generates geometry to render this tile across a surface.
  76. /// @param[out] vertices The array to store the generated vertex data.
  77. /// @param[out] indices The array to store the generated index data.
  78. /// @param[in] computed_values The computed values of the element being decorated.
  79. /// @param[in] surface_origin The starting point of the first tile to generate.
  80. /// @param[in] surface_dimensions The dimensions of the surface to be tiled.
  81. /// @param[in] tile_dimensions The dimensions to render this tile at.
  82. void GenerateGeometry(Vector<Vertex>& vertices, Vector<int>& indices, const ComputedValues& computed_values, Vector2f surface_origin,
  83. Vector2f surface_dimensions, Vector2f tile_dimensions) const;
  84. struct TileData {
  85. Vector2f size; // 'px' units
  86. Vector2f texcoords[2]; // relative units
  87. };
  88. int texture_index;
  89. // Scales the desired displayed size of the tile from raw pixel size. Eg. the 'display_scale' in a sprite sheet.
  90. float display_scale;
  91. // Position and size within the texture, absolute units (px)
  92. Vector2f position, size;
  93. mutable TileData tile_data;
  94. mutable bool tile_data_calculated = false;
  95. TileOrientation orientation;
  96. TileFitMode fit_mode;
  97. Style::LengthPercentage align[2];
  98. };
  99. protected:
  100. enum class Axis { Horizontal, Vertical };
  101. /// Scales a tile dimensions by a fixed value along one axis.
  102. /// @param tile_dimensions[in, out] The tile dimensions to scale.
  103. /// @param axis_value[in] The fixed value to scale against.
  104. /// @param axis[in] The axis to scale against.
  105. void ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, Axis axis) const;
  106. };
  107. class DecoratorTiledInstancer : public DecoratorInstancer {
  108. public:
  109. DecoratorTiledInstancer(size_t num_tiles);
  110. protected:
  111. /// Adds the property declarations for a tile.
  112. /// @param[in] name The name of the tile property.
  113. /// @param[in] register_fit_modes If true, the tile will have the fit modes registered.
  114. void RegisterTileProperty(const String& name, bool register_fit_modes = false);
  115. /// Retrieves all the properties for a tile from the property dictionary.
  116. /// @param[out] tile The tile structure for storing the tile properties.
  117. /// @param[out] textures Holds the textures declared for the tile.
  118. /// @param[in] properties The user-defined list of parameters for the decorator.
  119. /// @param[in] instancer_interface An interface for querying the active style sheet.
  120. bool GetTileProperties(DecoratorTiled::Tile* tiles, Texture* textures, size_t num_tiles_and_textures, const PropertyDictionary& properties,
  121. const DecoratorInstancerInterface& instancer_interface) const;
  122. private:
  123. struct TilePropertyIds {
  124. PropertyId src, fit, align_x, align_y, orientation;
  125. };
  126. Vector<TilePropertyIds> tile_property_ids;
  127. };
  128. } // namespace Rml
  129. #endif