ElementImage.cpp 7.9 KB

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