ElementImage.h 4.6 KB

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