ElementBackground.cpp 3.8 KB

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