ElementImage.h 5.1 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_ELEMENTS_ELEMENTIMAGE_H
  29. #define RMLUI_CORE_ELEMENTS_ELEMENTIMAGE_H
  30. #include "../../../Include/RmlUi/Core/Element.h"
  31. #include "../../../Include/RmlUi/Core/Geometry.h"
  32. #include "../../../Include/RmlUi/Core/Header.h"
  33. #include "../../../Include/RmlUi/Core/Spritesheet.h"
  34. #include "../../../Include/RmlUi/Core/Texture.h"
  35. namespace Rml {
  36. /**
  37. The 'img' element can render images and sprites.
  38. The 'src' attribute is used to specify an image url. If instead the `sprite` attribute is set,
  39. it will load a sprite and ignore the `src` and `rect` attributes.
  40. The 'rect' attribute takes four space-separated integer values, specifying a rectangle
  41. using 'x y width height' in pixel coordinates inside the image. No clamping to the
  42. dimensions of the source image will occur; rendered results in this case will
  43. depend on the user's texture addressing mode.
  44. The intrinsic dimensions of the image can now come from three different sources. They are
  45. used in the following order:
  46. 1) 'width' / 'height' attributes if present
  47. 2) pixel width / height of the sprite
  48. 3) pixel width / height given by the 'rect' attribute
  49. 4) width / height of the image texture
  50. This has the result of sizing the element to the pixel-size of the rendered image, unless
  51. overridden by the 'width' or 'height' attributes.
  52. @author Peter Curry
  53. */
  54. class RMLUICORE_API ElementImage : public Element {
  55. public:
  56. RMLUI_RTTI_DefineWithParent(ElementImage, Element)
  57. /// Constructs a new ElementImage. This should not be called directly; use the Factory instead.
  58. /// @param[in] tag The tag the element was declared as in RML.
  59. ElementImage(const String& tag);
  60. virtual ~ElementImage();
  61. /// Returns the element's inherent size.
  62. bool GetIntrinsicDimensions(Vector2f& dimensions, float& ratio) override;
  63. /// Loads the current source file if needed. This normally happens automatically during layouting.
  64. void EnsureSourceLoaded();
  65. protected:
  66. /// Renders the image.
  67. void OnRender() override;
  68. /// Regenerates the element's geometry.
  69. void OnResize() override;
  70. /// Our intrinsic dimensions may change with the dp-ratio.
  71. void OnDpRatioChange() override;
  72. /// The sprite may have changed when the style sheet is recompiled.
  73. void OnStyleSheetChange() override;
  74. /// Checks for changes to the image's source or dimensions.
  75. /// @param[in] changed_attributes A list of attributes changed on the element.
  76. void OnAttributeChange(const ElementAttributes& changed_attributes) override;
  77. /// Called when properties on the element are changed.
  78. /// @param[in] changed_properties The properties changed on the element.
  79. void OnPropertyChange(const PropertyIdSet& changed_properties) override;
  80. /// Detect when we have been added to the document.
  81. void OnChildAdd(Element* child) override;
  82. private:
  83. // Generates the element's geometry.
  84. void GenerateGeometry();
  85. // Loads the element's texture, as specified by the 'src' attribute.
  86. bool LoadTexture();
  87. // Loads the rect value from the element's attribute, but only if we're not a sprite.
  88. void UpdateRect();
  89. // The texture this element is rendering from.
  90. Texture texture;
  91. // True if we need to refetch the texture's source from the element's attributes.
  92. bool texture_dirty;
  93. // A factor which scales the intrinsic dimensions based on the dp-ratio and image scale.
  94. float dimensions_scale;
  95. // The element's computed intrinsic dimensions. If either of these values are set to -1, then
  96. // that dimension has not been computed yet.
  97. Vector2f dimensions;
  98. // The rectangle extracted from the sprite or 'rect' attribute. The rect_source will be None if
  99. // these have not been specified or are invalid.
  100. Rectanglef rect;
  101. enum class RectSource { None, Attribute, Sprite } rect_source;
  102. // The geometry used to render this element.
  103. Geometry geometry;
  104. bool geometry_dirty;
  105. };
  106. } // namespace Rml
  107. #endif