ElementImage.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // Modified by uniquejack
  28. #include "precompiled.h"
  29. #include "ElementImage.h"
  30. #include "../../Include/Rocket/Core.h"
  31. #include "TextureDatabase.h"
  32. #include "TextureResource.h"
  33. namespace Rocket {
  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(Rocket::Core::Box::CONTENT));
  79. }
  80. // Called when attributes on the element are changed.
  81. void ElementImage::OnAttributeChange(const Rocket::Core::AttributeNameList& changed_attributes)
  82. {
  83. // Call through to the base element's OnAttributeChange().
  84. Rocket::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. 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());
  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. Rocket::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(BACKGROUND_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(Rocket::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< Rocket::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. const Property* element_colour = GetProperty(BACKGROUND_COLOR);
  186. float opacity = GetProperty<float>(OPACITY);
  187. Colourb quad_colour = Colourb(255, 255, 255);
  188. if (element_colour)
  189. {
  190. Colourb background_colour = element_colour->Get<Colourb>();
  191. // Should be a non-transparent background
  192. if (background_colour.alpha != 0)
  193. quad_colour = background_colour;
  194. }
  195. // Apply opacity
  196. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  197. Rocket::Core::GeometryUtilities::GenerateQuad(&vertices[0], // vertices to write to
  198. &indices[0], // indices to write to
  199. Vector2f(0, 0), // origin of the quad
  200. GetBox().GetSize(Rocket::Core::Box::CONTENT), // size of the quad
  201. quad_colour, // colour of the vertices
  202. texcoords[0], // top-left texture coordinate
  203. texcoords[1]); // top-right texture coordinate
  204. geometry_dirty = false;
  205. }
  206. bool ElementImage::LoadTexture()
  207. {
  208. texture_dirty = false;
  209. // Get the source URL for the image.
  210. String image_source = GetAttribute< String >("src", "");
  211. if (image_source.Empty())
  212. return false;
  213. geometry_dirty = true;
  214. Rocket::Core::ElementDocument* document = GetOwnerDocument();
  215. URL source_url(document == NULL ? "" : document->GetSourceURL());
  216. if (!texture.Load(image_source, source_url.GetPath()))
  217. {
  218. geometry.SetTexture(NULL);
  219. return false;
  220. }
  221. // Set the texture onto our geometry object.
  222. geometry.SetTexture(&texture);
  223. return true;
  224. }
  225. void ElementImage::ResetCoords()
  226. {
  227. using_coords = false;
  228. for (int i = 0; i < 4; ++i)
  229. coords[i] = -1;
  230. }
  231. }
  232. }