ElementImage.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include "precompiled.h"
  29. #include "ElementImage.h"
  30. #include "../../Include/RmlUi/Core.h"
  31. #include "TextureDatabase.h"
  32. #include "TextureResource.h"
  33. namespace Rml {
  34. namespace Core {
  35. // Constructs a new ElementImage.
  36. ElementImage::ElementImage(const String& tag) : Element(tag), dimensions(-1, -1), geometry(this)
  37. {
  38. ResetCoords();
  39. geometry_dirty = false;
  40. texture_dirty = true;
  41. }
  42. ElementImage::~ElementImage()
  43. {
  44. }
  45. // Sizes the box to the element's inherent size.
  46. bool ElementImage::GetIntrinsicDimensions(Vector2f& _dimensions)
  47. {
  48. // Check if we need to reload the texture.
  49. if (texture_dirty)
  50. LoadTexture();
  51. // Calculate the x dimension.
  52. if (HasAttribute("width"))
  53. dimensions.x = GetAttribute< float >("width", -1);
  54. else if (using_coords)
  55. dimensions.x = (float) (coords[2] - coords[0]);
  56. else
  57. dimensions.x = (float) texture.GetDimensions(GetRenderInterface()).x;
  58. // Calculate the y dimension.
  59. if (HasAttribute("height"))
  60. dimensions.y = GetAttribute< float >("height", -1);
  61. else if (using_coords)
  62. dimensions.y = (float) (coords[3] - coords[1]);
  63. else
  64. dimensions.y = (float) texture.GetDimensions(GetRenderInterface()).y;
  65. // Return the calculated dimensions. If this changes the size of the element, it will result in
  66. // a 'resize' event which is caught below and will regenerate the geometry.
  67. _dimensions = dimensions;
  68. return true;
  69. }
  70. // Renders the element.
  71. void ElementImage::OnRender()
  72. {
  73. // Regenerate the geometry if required (this will be set if 'coords' changes but does not
  74. // result in a resize).
  75. if (geometry_dirty)
  76. GenerateGeometry();
  77. // Render the geometry beginning at this element's content region.
  78. geometry.Render(GetAbsoluteOffset(Rml::Core::Box::CONTENT).Round());
  79. }
  80. // Called when attributes on the element are changed.
  81. void ElementImage::OnAttributeChange(const Rml::Core::ElementAttributes& changed_attributes)
  82. {
  83. // Call through to the base element's OnAttributeChange().
  84. Rml::Core::Element::OnAttributeChange(changed_attributes);
  85. float dirty_layout = false;
  86. // Check for a changed 'src' attribute. If this changes, the old texture handle is released,
  87. // forcing a reload when the layout is regenerated.
  88. if (changed_attributes.find("src") != changed_attributes.end())
  89. {
  90. texture_dirty = true;
  91. dirty_layout = true;
  92. }
  93. // Check for a changed 'width' attribute. If this changes, a layout is forced which will
  94. // recalculate the dimensions.
  95. if (changed_attributes.find("width") != changed_attributes.end() ||
  96. changed_attributes.find("height") != changed_attributes.end())
  97. {
  98. dirty_layout = true;
  99. }
  100. // Check for a change to the 'coords' attribute. If this changes, the coordinates are
  101. // recomputed and a layout forced.
  102. if (changed_attributes.find("coords") != changed_attributes.end())
  103. {
  104. if (HasAttribute("coords"))
  105. {
  106. StringList coords_list;
  107. StringUtilities::ExpandString(coords_list, GetAttribute< String >("coords", ""));
  108. if (coords_list.size() != 4)
  109. {
  110. Rml::Core::Log::Message(Log::LT_WARNING, "Element '%s' has an invalid 'coords' attribute; coords requires 4 values, found %d.", GetAddress().c_str(), coords_list.size());
  111. ResetCoords();
  112. }
  113. else
  114. {
  115. for (size_t i = 0; i < 4; ++i)
  116. coords[i] = atoi(coords_list[i].c_str());
  117. // Check for the validity of the coordinates.
  118. if (coords[0] < 0 || coords[2] < coords[0] ||
  119. coords[1] < 0 || coords[3] < coords[1])
  120. {
  121. Rml::Core::Log::Message(Log::LT_WARNING, "Element '%s' has an invalid 'coords' attribute; invalid coordinate values specified.", GetAddress().c_str());
  122. ResetCoords();
  123. }
  124. else
  125. {
  126. // We have new, valid coordinates; force the geometry to be regenerated.
  127. geometry_dirty = true;
  128. using_coords = true;
  129. }
  130. }
  131. }
  132. else
  133. ResetCoords();
  134. // Coordinates have changes; this will most likely result in a size change, so we need to force a layout.
  135. dirty_layout = true;
  136. }
  137. if (dirty_layout)
  138. DirtyLayout();
  139. }
  140. void ElementImage::OnPropertyChange(const PropertyNameList& changed_properties)
  141. {
  142. Element::OnPropertyChange(changed_properties);
  143. if (changed_properties.find(PropertyId::ImageColor) != changed_properties.end() ||
  144. changed_properties.find(PropertyId::Opacity) != changed_properties.end()) {
  145. GenerateGeometry();
  146. }
  147. }
  148. // Regenerates the element's geometry.
  149. void ElementImage::OnResize()
  150. {
  151. GenerateGeometry();
  152. }
  153. void ElementImage::GenerateGeometry()
  154. {
  155. // Release the old geometry before specifying the new vertices.
  156. geometry.Release(true);
  157. std::vector< Rml::Core::Vertex >& vertices = geometry.GetVertices();
  158. std::vector< int >& indices = geometry.GetIndices();
  159. vertices.resize(4);
  160. indices.resize(6);
  161. // Generate the texture coordinates.
  162. Vector2f texcoords[2];
  163. if (using_coords)
  164. {
  165. Vector2f texture_dimensions((float) texture.GetDimensions(GetRenderInterface()).x, (float) texture.GetDimensions(GetRenderInterface()).y);
  166. if (texture_dimensions.x == 0)
  167. texture_dimensions.x = 1;
  168. if (texture_dimensions.y == 0)
  169. texture_dimensions.y = 1;
  170. texcoords[0].x = (float) coords[0] / texture_dimensions.x;
  171. texcoords[0].y = (float) coords[1] / texture_dimensions.y;
  172. texcoords[1].x = (float) coords[2] / texture_dimensions.x;
  173. texcoords[1].y = (float) coords[3] / texture_dimensions.y;
  174. }
  175. else
  176. {
  177. texcoords[0] = Vector2f(0, 0);
  178. texcoords[1] = Vector2f(1, 1);
  179. }
  180. const ComputedValues& computed = GetComputedValues();
  181. float opacity = computed.opacity;
  182. Colourb quad_colour = computed.image_color;
  183. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  184. Vector2f quad_size = GetBox().GetSize(Rml::Core::Box::CONTENT).Round();
  185. Rml::Core::GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), quad_size, quad_colour, texcoords[0], texcoords[1]);
  186. geometry_dirty = false;
  187. }
  188. bool ElementImage::LoadTexture()
  189. {
  190. texture_dirty = false;
  191. // Get the source URL for the image.
  192. String image_source = GetAttribute< String >("src", "");
  193. if (image_source.empty())
  194. return false;
  195. geometry_dirty = true;
  196. Rml::Core::ElementDocument* document = GetOwnerDocument();
  197. URL source_url(document == NULL ? "" : document->GetSourceURL());
  198. if (!texture.Load(image_source, source_url.GetPath()))
  199. {
  200. geometry.SetTexture(NULL);
  201. return false;
  202. }
  203. // Set the texture onto our geometry object.
  204. geometry.SetTexture(&texture);
  205. return true;
  206. }
  207. void ElementImage::ResetCoords()
  208. {
  209. using_coords = false;
  210. for (int i = 0; i < 4; ++i)
  211. coords[i] = -1;
  212. }
  213. }
  214. }