ElementBackground.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "ElementBackground.h"
  29. #include "../../Include/Rocket/Core/Element.h"
  30. #include "../../Include/Rocket/Core/GeometryUtilities.h"
  31. #include "../../Include/Rocket/Core/Property.h"
  32. namespace Rocket {
  33. namespace Core {
  34. ElementBackground::ElementBackground(Element* _element) : geometry(_element)
  35. {
  36. element = _element;
  37. background_dirty = true;
  38. }
  39. ElementBackground::~ElementBackground()
  40. {
  41. }
  42. // Renders the element's background, if it has one.
  43. void ElementBackground::RenderBackground()
  44. {
  45. if (background_dirty)
  46. {
  47. background_dirty = false;
  48. GenerateBackground();
  49. }
  50. geometry.Render(element->GetAbsoluteOffset(Box::PADDING));
  51. }
  52. // Marks the background geometry as dirty.
  53. void ElementBackground::DirtyBackground()
  54. {
  55. background_dirty = true;
  56. }
  57. // Generates the background geometry for the element.
  58. void ElementBackground::GenerateBackground()
  59. {
  60. // Fetch the new colour for the background. If the colour is transparent, then we don't render any background.
  61. Colourb colour = element->GetProperty(BACKGROUND_COLOR)->value.Get< Colourb >();
  62. if (colour.alpha <= 0)
  63. {
  64. geometry.GetVertices().clear();
  65. geometry.GetIndices().clear();
  66. geometry.Release();
  67. return;
  68. }
  69. // Work out how many boxes we need to generate geometry for.
  70. int num_boxes = 0;
  71. for (int i = 0; i < element->GetNumBoxes(); ++i)
  72. {
  73. const Box& box = element->GetBox(i);
  74. Vector2f size = box.GetSize(Box::PADDING);
  75. if (size.x > 0 && size.y > 0)
  76. num_boxes++;
  77. }
  78. std::vector< Vertex >& vertices = geometry.GetVertices();
  79. std::vector< int >& indices = geometry.GetIndices();
  80. int index_offset = 0;
  81. vertices.resize(4 * num_boxes);
  82. indices.resize(6 * num_boxes);
  83. if (num_boxes > 0)
  84. {
  85. Vertex* raw_vertices = &vertices[0];
  86. int* raw_indices = &indices[0];
  87. for (int i = 0; i < element->GetNumBoxes(); ++i)
  88. GenerateBackground(raw_vertices, raw_indices, index_offset, element->GetBox(i), colour);
  89. }
  90. geometry.Release();
  91. }
  92. // Generates the background geometry for a single box.
  93. void ElementBackground::GenerateBackground(Vertex*& vertices, int*& indices, int& index_offset, const Box& box, const Colourb& colour)
  94. {
  95. Vector2f padded_size = box.GetSize(Box::PADDING);
  96. if (padded_size.x <= 0 ||
  97. padded_size.y <= 0)
  98. return;
  99. GeometryUtilities::GenerateQuad(vertices, indices, box.GetOffset(), padded_size, colour, index_offset);
  100. vertices += 4;
  101. indices += 6;
  102. index_offset += 4;
  103. }
  104. }
  105. }