DecoratorTiled.h 4.6 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 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. {
  41. public:
  42. DecoratorTiled();
  43. virtual ~DecoratorTiled();
  44. /**
  45. Stores the orientation of a tile.
  46. */
  47. enum TileOrientation
  48. {
  49. ORIENTATION_NONE, // No orientation.
  50. FLIP_HORIZONTAL, // Flipped horizontally.
  51. FLIP_VERTICAL, // Flipped vertically.
  52. ROTATE_180, // Rotated 180 degrees clockwise.
  53. };
  54. /**
  55. Stores the fit mode of a tile.
  56. */
  57. enum TileFitMode
  58. {
  59. FILL, // Tile is stretched to boundaries.
  60. CONTAIN, // Tile is stretched to boundaries, keeping aspect ratio fixed, 'letter-boxed'.
  61. COVER, // Tile is stretched to cover the boundaries, keeping aspect ratio fixed.
  62. SCALE_NONE, // Tile is never scaled.
  63. SCALE_DOWN, // Tile acts like 'scale-none' if smaller than boundaries, or like 'contain' otherwise.
  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. {
  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(Element* element, const 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] element The element hosting the decorator.
  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(Vector< Vertex >& vertices, Vector< int >& indices, Element* element, Vector2f surface_origin, Vector2f surface_dimensions, Vector2f tile_dimensions) const;
  87. struct TileData
  88. {
  89. Vector2f size; // 'px' units
  90. Vector2f texcoords[2]; // relative units
  91. };
  92. using TileDataMap = SmallUnorderedMap< RenderInterface*, TileData >;
  93. int texture_index;
  94. // Scales the desired displayed size of the tile from raw pixel size. Eg. the 'display_scale' in a sprite sheet.
  95. float display_scale;
  96. // Position and size within the texture, absolute units (px)
  97. Vector2f position, size;
  98. mutable TileDataMap data;
  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. } // namespace Rml
  112. #endif