ElementImage.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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::AttributeNameList& 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().CString(), 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].CString());
  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().CString());
  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(IMAGE_COLOR) != changed_properties.end() ||
  144. changed_properties.find(OPACITY) != changed_properties.end()) {
  145. GenerateGeometry();
  146. }
  147. }
  148. // Regenerates the element's geometry.
  149. void ElementImage::ProcessEvent(Rml::Core::Event& event)
  150. {
  151. Element::ProcessEvent(event);
  152. if (event.GetTargetElement() == this &&
  153. event == RESIZE)
  154. {
  155. GenerateGeometry();
  156. }
  157. }
  158. void ElementImage::GenerateGeometry()
  159. {
  160. // Release the old geometry before specifying the new vertices.
  161. geometry.Release(true);
  162. std::vector< Rml::Core::Vertex >& vertices = geometry.GetVertices();
  163. std::vector< int >& indices = geometry.GetIndices();
  164. vertices.resize(4);
  165. indices.resize(6);
  166. // Generate the texture coordinates.
  167. Vector2f texcoords[2];
  168. if (using_coords)
  169. {
  170. Vector2f texture_dimensions((float) texture.GetDimensions(GetRenderInterface()).x, (float) texture.GetDimensions(GetRenderInterface()).y);
  171. if (texture_dimensions.x == 0)
  172. texture_dimensions.x = 1;
  173. if (texture_dimensions.y == 0)
  174. texture_dimensions.y = 1;
  175. texcoords[0].x = (float) coords[0] / texture_dimensions.x;
  176. texcoords[0].y = (float) coords[1] / texture_dimensions.y;
  177. texcoords[1].x = (float) coords[2] / texture_dimensions.x;
  178. texcoords[1].y = (float) coords[3] / texture_dimensions.y;
  179. }
  180. else
  181. {
  182. texcoords[0] = Vector2f(0, 0);
  183. texcoords[1] = Vector2f(1, 1);
  184. }
  185. float opacity = GetProperty<float>(OPACITY);
  186. Colourb quad_colour = GetProperty<Colourb>(IMAGE_COLOR);
  187. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  188. Vector2f quad_size = GetBox().GetSize(Rml::Core::Box::CONTENT).Round();
  189. Rml::Core::GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), quad_size, quad_colour, texcoords[0], texcoords[1]);
  190. geometry_dirty = false;
  191. }
  192. bool ElementImage::LoadTexture()
  193. {
  194. texture_dirty = false;
  195. // Get the source URL for the image.
  196. String image_source = GetAttribute< String >("src", "");
  197. if (image_source.Empty())
  198. return false;
  199. geometry_dirty = true;
  200. Rml::Core::ElementDocument* document = GetOwnerDocument();
  201. URL source_url(document == NULL ? "" : document->GetSourceURL());
  202. if (!texture.Load(image_source, source_url.GetPath()))
  203. {
  204. geometry.SetTexture(NULL);
  205. return false;
  206. }
  207. // Set the texture onto our geometry object.
  208. geometry.SetTexture(&texture);
  209. return true;
  210. }
  211. void ElementImage::ResetCoords()
  212. {
  213. using_coords = false;
  214. for (int i = 0; i < 4; ++i)
  215. coords[i] = -1;
  216. }
  217. }
  218. }