ElementBackground.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "ElementBackground.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/GeometryUtilities.h"
  31. #include "../../Include/RmlUi/Core/Property.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. namespace Rml {
  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. RMLUI_ZoneScoped;
  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. Vector< Vertex >& vertices = geometry.GetVertices();
  84. 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. } // namespace Rml