ElementImage.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. namespace Rml {
  35. namespace Core {
  36. /**
  37. The 'img' element. The image element can have a rectangular sub-region of its source texture
  38. specified with the 'coords' attribute; the element will render this region rather than the
  39. entire image.
  40. The 'coords' attribute is similar to that of the HTML imagemap. It takes four comma-separated
  41. integer values, specifying the top-left and the bottom right of the region in
  42. pixel-coordinates, in that order. So for example, the attribute "coords" = "0, 10, 100, 210"
  43. will render a 100 x 200 region, beginning at (0, 10) and rendering through to (100, 210). No
  44. clamping to the dimensions of the source image will occur; rendered results in this case will
  45. depend on the texture addressing mode.
  46. The intrinsic dimensions of the image can now come from three different sources. They are
  47. used in the following order:
  48. 1) 'width' / 'height' attributes if present
  49. 2) pixel width / height given by the 'coords' attribute
  50. 3) width / height of the source 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. /// Constructs a new ElementImage. This should not be called directly; use the Factory instead.
  59. /// @param[in] tag The tag the element was declared as in RML.
  60. ElementImage(const String& tag);
  61. virtual ~ElementImage();
  62. /// Returns the element's inherent size.
  63. /// @param[out] The element's intrinsic dimensions.
  64. /// @return True.
  65. bool GetIntrinsicDimensions(Vector2f& dimensions) override;
  66. protected:
  67. /// Renders the image.
  68. void OnRender() override;
  69. /// Regenerates the element's geometry.
  70. void OnResize() override;
  71. /// Checks for changes to the image's source or dimensions.
  72. /// @param[in] changed_attributes A list of attributes changed on the element.
  73. void OnAttributeChange(const ElementAttributes& changed_attributes) override;
  74. /// Called when properties on the element are changed.
  75. /// @param[in] changed_properties The properties changed on the element.
  76. void OnPropertyChange(const PropertyIdSet& changed_properties) override;
  77. private:
  78. // Generates the element's geometry.
  79. void GenerateGeometry();
  80. // Loads the element's texture, as specified by the 'src' attribute.
  81. bool LoadTexture();
  82. // Resets the values of the 'coords' attribute to mark them as unused.
  83. void ResetCoords();
  84. // The texture this element is rendering from.
  85. Texture texture;
  86. // True if we need to refetch the texture's source from the element's attributes.
  87. bool texture_dirty;
  88. // The element's computed intrinsic dimensions. If either of these values are set to -1, then
  89. // that dimension has not been computed yet.
  90. Vector2f dimensions;
  91. // The coords extracted from the sprite or 'coords' attribute. The coords_source will be None if
  92. // these have not been specified or are invalid.
  93. Rectangle coords;
  94. enum class CoordsSource { None, Attribute, Sprite } coords_source;
  95. // The geometry used to render this element.
  96. Geometry geometry;
  97. bool geometry_dirty;
  98. };
  99. }
  100. }
  101. #endif