ElementImage.cpp 8.4 KB

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