ElementImage.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "ElementImage.h"
  29. #include "../TextureDatabase.h"
  30. #include "../../../Include/RmlUi/Core/URL.h"
  31. #include "../../../Include/RmlUi/Core/PropertyIdSet.h"
  32. #include "../../../Include/RmlUi/Core/GeometryUtilities.h"
  33. #include "../../../Include/RmlUi/Core/ElementDocument.h"
  34. #include "../../../Include/RmlUi/Core/StyleSheet.h"
  35. namespace Rml {
  36. // Constructs a new ElementImage.
  37. ElementImage::ElementImage(const String& tag) : Element(tag), dimensions(-1, -1), rect_source(RectSource::None), geometry(this)
  38. {
  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 (rect_source != RectSource::None)
  55. dimensions.x = rect.width;
  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 (rect_source != RectSource::None)
  62. dimensions.y = rect.height;
  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 call to 'onresize' below which 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 'rect' changes but does not result in a resize).
  74. if (geometry_dirty)
  75. GenerateGeometry();
  76. // Render the geometry beginning at this element's content region.
  77. geometry.Render(GetAbsoluteOffset(Box::CONTENT).Round());
  78. }
  79. // Called when attributes on the element are changed.
  80. void ElementImage::OnAttributeChange(const ElementAttributes& changed_attributes)
  81. {
  82. // Call through to the base element's OnAttributeChange().
  83. Element::OnAttributeChange(changed_attributes);
  84. float dirty_layout = false;
  85. // Check for a changed 'src' attribute. If this changes, the old texture handle is released,
  86. // forcing a reload when the layout is regenerated.
  87. if (changed_attributes.find("src") != changed_attributes.end() ||
  88. changed_attributes.find("sprite") != 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 'rect' attribute. If this changes, the coordinates are
  101. // recomputed and a layout forced. If a sprite is set to source, then that will override any attribute.
  102. if (changed_attributes.find("rect") != changed_attributes.end())
  103. {
  104. UpdateRect();
  105. // Rectangle has changed; this will most likely result in a size change, so we need to force a layout.
  106. dirty_layout = true;
  107. }
  108. if (dirty_layout)
  109. DirtyLayout();
  110. }
  111. void ElementImage::OnPropertyChange(const PropertyIdSet& changed_properties)
  112. {
  113. Element::OnPropertyChange(changed_properties);
  114. if (changed_properties.Contains(PropertyId::ImageColor) ||
  115. changed_properties.Contains(PropertyId::Opacity)) {
  116. GenerateGeometry();
  117. }
  118. }
  119. // Regenerates the element's geometry.
  120. void ElementImage::OnResize()
  121. {
  122. GenerateGeometry();
  123. }
  124. void ElementImage::GenerateGeometry()
  125. {
  126. // Release the old geometry before specifying the new vertices.
  127. geometry.Release(true);
  128. Vector< Vertex >& vertices = geometry.GetVertices();
  129. Vector< int >& indices = geometry.GetIndices();
  130. vertices.resize(4);
  131. indices.resize(6);
  132. // Generate the texture coordinates.
  133. Vector2f texcoords[2];
  134. if (rect_source != RectSource::None)
  135. {
  136. Vector2f texture_dimensions((float) texture.GetDimensions(GetRenderInterface()).x, (float) texture.GetDimensions(GetRenderInterface()).y);
  137. if (texture_dimensions.x == 0)
  138. texture_dimensions.x = 1;
  139. if (texture_dimensions.y == 0)
  140. texture_dimensions.y = 1;
  141. texcoords[0].x = rect.x / texture_dimensions.x;
  142. texcoords[0].y = rect.y / texture_dimensions.y;
  143. texcoords[1].x = (rect.x + rect.width) / texture_dimensions.x;
  144. texcoords[1].y = (rect.y + rect.height) / texture_dimensions.y;
  145. }
  146. else
  147. {
  148. texcoords[0] = Vector2f(0, 0);
  149. texcoords[1] = Vector2f(1, 1);
  150. }
  151. const ComputedValues& computed = GetComputedValues();
  152. float opacity = computed.opacity;
  153. Colourb quad_colour = computed.image_color;
  154. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  155. Vector2f quad_size = GetBox().GetSize(Box::CONTENT).Round();
  156. GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), quad_size, quad_colour, texcoords[0], texcoords[1]);
  157. geometry_dirty = false;
  158. }
  159. bool ElementImage::LoadTexture()
  160. {
  161. texture_dirty = false;
  162. geometry_dirty = true;
  163. // Check for a sprite first, this takes precedence.
  164. const String sprite_name = GetAttribute< String >("sprite", "");
  165. if (!sprite_name.empty())
  166. {
  167. // Load sprite.
  168. bool valid_sprite = false;
  169. if (ElementDocument* document = GetOwnerDocument())
  170. {
  171. if (auto& style_sheet = document->GetStyleSheet())
  172. {
  173. if (const Sprite* sprite = style_sheet->GetSprite(sprite_name))
  174. {
  175. rect = sprite->rectangle;
  176. rect_source = RectSource::Sprite;
  177. texture = sprite->sprite_sheet->texture;
  178. valid_sprite = true;
  179. }
  180. }
  181. }
  182. if (!valid_sprite)
  183. {
  184. texture = Texture();
  185. rect_source = RectSource::None;
  186. UpdateRect();
  187. Log::Message(Log::LT_WARNING, "Could not find sprite '%s' specified in img element.", sprite_name.c_str());
  188. return false;
  189. }
  190. }
  191. else
  192. {
  193. // Load image from source URL.
  194. const String source_name = GetAttribute< String >("src", "");
  195. if (source_name.empty())
  196. {
  197. texture = Texture();
  198. rect_source = RectSource::None;
  199. return false;
  200. }
  201. URL source_url;
  202. if (ElementDocument* document = GetOwnerDocument())
  203. source_url.SetURL(document->GetSourceURL());
  204. texture.Set(source_name, source_url.GetPath());
  205. }
  206. // Set the texture onto our geometry object.
  207. geometry.SetTexture(&texture);
  208. return true;
  209. }
  210. void ElementImage::UpdateRect()
  211. {
  212. if(rect_source != RectSource::Sprite)
  213. {
  214. bool valid_rect = false;
  215. String rect_string = GetAttribute< String >("rect", "");
  216. if (!rect_string.empty())
  217. {
  218. StringList coords_list;
  219. StringUtilities::ExpandString(coords_list, rect_string, ' ');
  220. if (coords_list.size() != 4)
  221. {
  222. Log::Message(Log::LT_WARNING, "Element '%s' has an invalid 'rect' attribute; rect requires 4 space-separated values, found %d.", GetAddress().c_str(), coords_list.size());
  223. }
  224. else
  225. {
  226. rect.x = (float)std::atof(coords_list[0].c_str());
  227. rect.y = (float)std::atof(coords_list[1].c_str());
  228. rect.width = (float)std::atof(coords_list[2].c_str());
  229. rect.height = (float)std::atof(coords_list[3].c_str());
  230. // We have new, valid coordinates; force the geometry to be regenerated.
  231. valid_rect = true;
  232. geometry_dirty = true;
  233. rect_source = RectSource::Attribute;
  234. }
  235. }
  236. if (!valid_rect)
  237. {
  238. rect = {};
  239. rect_source = RectSource::None;
  240. }
  241. }
  242. }
  243. } // namespace Rml