ElementImage.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementImage.h"
  29. #include "../../Include/Rocket/Core.h"
  30. #include "TextureDatabase.h"
  31. #include "TextureResource.h"
  32. namespace Rocket {
  33. namespace Core {
  34. // Constructs a new ElementImage.
  35. ElementImage::ElementImage(const String& tag) : Element(tag), dimensions(-1, -1), geometry(this)
  36. {
  37. ResetCoords();
  38. geometry_dirty = false;
  39. texture_dirty = true;
  40. }
  41. ElementImage::~ElementImage()
  42. {
  43. }
  44. // Sizes the box to the element's inherent size.
  45. bool ElementImage::GetIntrinsicDimensions(Vector2f& _dimensions)
  46. {
  47. // Check if we need to reload the texture.
  48. if (texture_dirty)
  49. LoadTexture();
  50. // Calculate the x dimension.
  51. if (HasAttribute("width"))
  52. dimensions.x = GetAttribute< float >("width", -1);
  53. else if (using_coords)
  54. dimensions.x = (float) (coords[2] - coords[0]);
  55. else
  56. dimensions.x = (float) texture.GetDimensions(GetRenderInterface()).x;
  57. // Calculate the y dimension.
  58. if (HasAttribute("height"))
  59. dimensions.y = GetAttribute< float >("height", -1);
  60. else if (using_coords)
  61. dimensions.y = (float) (coords[3] - coords[1]);
  62. else
  63. dimensions.y = (float) texture.GetDimensions(GetRenderInterface()).y;
  64. // Return the calculated dimensions. If this changes the size of the element, it will result in
  65. // a 'resize' event which is caught below and will regenerate the geometry.
  66. _dimensions = dimensions;
  67. return true;
  68. }
  69. // Renders the element.
  70. void ElementImage::OnRender()
  71. {
  72. // Regenerate the geometry if required (this will be set if 'coords' changes but does not
  73. // 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(Rocket::Core::Box::CONTENT));
  78. }
  79. // Called when attributes on the element are changed.
  80. void ElementImage::OnAttributeChange(const Rocket::Core::AttributeNameList& changed_attributes)
  81. {
  82. // Call through to the base element's OnAttributeChange().
  83. Rocket::Core::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. {
  89. texture_dirty = true;
  90. dirty_layout = true;
  91. }
  92. // Check for a changed 'width' attribute. If this changes, a layout is forced which will
  93. // recalculate the dimensions.
  94. if (changed_attributes.find("width") != changed_attributes.end() ||
  95. changed_attributes.find("height") != changed_attributes.end())
  96. {
  97. dirty_layout = true;
  98. }
  99. // Check for a change to the 'coords' attribute. If this changes, the coordinates are
  100. // recomputed and a layout forced.
  101. if (changed_attributes.find("coords") != changed_attributes.end())
  102. {
  103. if (HasAttribute("coords"))
  104. {
  105. StringList coords_list;
  106. StringUtilities::ExpandString(coords_list, GetAttribute< String >("coords", ""));
  107. if (coords_list.size() != 4)
  108. {
  109. Rocket::Core::Log::Message(Log::LT_WARNING, "Element '%s' has an invalid 'coords' attribute; coords requires 4 values, found %d.", GetAddress().CString(), coords_list.size());
  110. ResetCoords();
  111. }
  112. else
  113. {
  114. for (size_t i = 0; i < 4; ++i)
  115. coords[i] = atoi(coords_list[i].CString());
  116. // Check for the validity of the coordinates.
  117. if (coords[0] < 0 || coords[2] < coords[0] ||
  118. coords[1] < 0 || coords[3] < coords[1])
  119. {
  120. Rocket::Core::Log::Message(Log::LT_WARNING, "Element '%s' has an invalid 'coords' attribute; invalid coordinate values specified.", GetAddress().CString());
  121. ResetCoords();
  122. }
  123. else
  124. {
  125. // We have new, valid coordinates; force the geometry to be regenerated.
  126. geometry_dirty = true;
  127. using_coords = true;
  128. }
  129. }
  130. }
  131. else
  132. ResetCoords();
  133. // Coordinates have changes; this will most likely result in a size change, so we need to force a layout.
  134. dirty_layout = true;
  135. }
  136. if (dirty_layout)
  137. DirtyLayout();
  138. }
  139. void ElementImage::OnPropertyChange(const PropertyNameList& changed_properties)
  140. {
  141. Element::OnPropertyChange(changed_properties);
  142. if (changed_properties.find(IMAGE_COLOR) != changed_properties.end() ||
  143. changed_properties.find(OPACITY) != changed_properties.end()) {
  144. GenerateGeometry();
  145. }
  146. }
  147. // Regenerates the element's geometry.
  148. void ElementImage::ProcessEvent(Rocket::Core::Event& event)
  149. {
  150. Element::ProcessEvent(event);
  151. if (event.GetTargetElement() == this &&
  152. event == RESIZE)
  153. {
  154. GenerateGeometry();
  155. }
  156. }
  157. void ElementImage::GenerateGeometry()
  158. {
  159. // Release the old geometry before specifying the new vertices.
  160. geometry.Release(true);
  161. std::vector< Rocket::Core::Vertex >& vertices = geometry.GetVertices();
  162. std::vector< int >& indices = geometry.GetIndices();
  163. vertices.resize(4);
  164. indices.resize(6);
  165. // Generate the texture coordinates.
  166. Vector2f texcoords[2];
  167. if (using_coords)
  168. {
  169. Vector2f texture_dimensions((float) texture.GetDimensions(GetRenderInterface()).x, (float) texture.GetDimensions(GetRenderInterface()).y);
  170. if (texture_dimensions.x == 0)
  171. texture_dimensions.x = 1;
  172. if (texture_dimensions.y == 0)
  173. texture_dimensions.y = 1;
  174. texcoords[0].x = (float) coords[0] / texture_dimensions.x;
  175. texcoords[0].y = (float) coords[1] / texture_dimensions.y;
  176. texcoords[1].x = (float) coords[2] / texture_dimensions.x;
  177. texcoords[1].y = (float) coords[3] / texture_dimensions.y;
  178. }
  179. else
  180. {
  181. texcoords[0] = Vector2f(0, 0);
  182. texcoords[1] = Vector2f(1, 1);
  183. }
  184. float opacity = GetProperty<float>(OPACITY);
  185. Colourb quad_colour = GetProperty<Colourb>(IMAGE_COLOR);
  186. // Apply opacity
  187. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  188. Rocket::Core::GeometryUtilities::GenerateQuad(&vertices[0], // vertices to write to
  189. &indices[0], // indices to write to
  190. Vector2f(0, 0), // origin of the quad
  191. GetBox().GetSize(Rocket::Core::Box::CONTENT), // size of the quad
  192. quad_colour, // colour of the vertices
  193. texcoords[0], // top-left texture coordinate
  194. texcoords[1]); // top-right texture coordinate
  195. geometry_dirty = false;
  196. }
  197. bool ElementImage::LoadTexture()
  198. {
  199. texture_dirty = false;
  200. // Get the source URL for the image.
  201. String image_source = GetAttribute< String >("src", "");
  202. if (image_source.Empty())
  203. return false;
  204. geometry_dirty = true;
  205. Rocket::Core::ElementDocument* document = GetOwnerDocument();
  206. URL source_url(document == NULL ? "" : document->GetSourceURL());
  207. if (!texture.Load(image_source, source_url.GetPath()))
  208. {
  209. geometry.SetTexture(NULL);
  210. return false;
  211. }
  212. // Set the texture onto our geometry object.
  213. geometry.SetTexture(&texture);
  214. return true;
  215. }
  216. void ElementImage::ResetCoords()
  217. {
  218. using_coords = false;
  219. for (int i = 0; i < 4; ++i)
  220. coords[i] = -1;
  221. }
  222. }
  223. }