ElementImage.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREELEMENTIMAGE_H
  28. #define ROCKETCOREELEMENTIMAGE_H
  29. #include "../../Include/Rocket/Core/Header.h"
  30. #include "../../Include/Rocket/Core/Element.h"
  31. #include "../../Include/Rocket/Core/Geometry.h"
  32. #include "../../Include/Rocket/Core/Texture.h"
  33. namespace Rocket {
  34. namespace Core {
  35. class TextureResource;
  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 ROCKETCORE_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);
  66. protected:
  67. /// Renders the image.
  68. virtual void OnRender();
  69. /// Checks for changes to the image's source or dimensions.
  70. /// @param[in] changed_attributes A list of attributes changed on the element.
  71. virtual void OnAttributeChange(const AttributeNameList& changed_attributes);
  72. /// Regenerates the element's geometry on a resize event.
  73. /// @param[in] event The event to process.
  74. virtual void ProcessEvent(Event& event);
  75. private:
  76. // Generates the element's geometry.
  77. void GenerateGeometry();
  78. // Loads the element's texture, as specified by the 'src' attribute.
  79. bool LoadTexture();
  80. // Resets the values of the 'coords' attribute to mark them as unused.
  81. void ResetCoords();
  82. // The texture this element is rendering from.
  83. Texture texture;
  84. // True if we need to refetch the texture's source from the element's attributes.
  85. bool texture_dirty;
  86. // The element's computed intrinsic dimensions. If either of these values are set to -1, then
  87. // that dimension has not been computed yet.
  88. Vector2f dimensions;
  89. // The integer coords extracted from the 'coords' attribute. using_coords will be false if
  90. // these have not been specified or are invalid.
  91. int coords[4];
  92. bool using_coords;
  93. // The geometry used to render this element.
  94. Geometry geometry;
  95. bool geometry_dirty;
  96. };
  97. }
  98. }
  99. #endif