ElementBackground.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 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. RMLUI_ZoneScoped;
  62. // Fetch the new colour for the background. If the colour is transparent, then we don't render any background.
  63. auto& computed = element->GetComputedValues();
  64. Colourb colour = computed.background_color;
  65. float opacity = computed.opacity;
  66. // Apply opacity
  67. colour.alpha = (byte)(opacity * (float)colour.alpha);
  68. if (colour.alpha <= 0)
  69. {
  70. geometry.GetVertices().clear();
  71. geometry.GetIndices().clear();
  72. geometry.Release();
  73. return;
  74. }
  75. // Work out how many boxes we need to generate geometry for.
  76. int num_boxes = 0;
  77. for (int i = 0; i < element->GetNumBoxes(); ++i)
  78. {
  79. const Box& box = element->GetBox(i);
  80. Vector2f size = box.GetSize(Box::PADDING);
  81. if (size.x > 0 && size.y > 0)
  82. num_boxes++;
  83. }
  84. std::vector< Vertex >& vertices = geometry.GetVertices();
  85. std::vector< int >& indices = geometry.GetIndices();
  86. int index_offset = 0;
  87. vertices.resize(4 * num_boxes);
  88. indices.resize(6 * num_boxes);
  89. if (num_boxes > 0)
  90. {
  91. Vertex* raw_vertices = &vertices[0];
  92. int* raw_indices = &indices[0];
  93. for (int i = 0; i < element->GetNumBoxes(); ++i)
  94. GenerateBackground(raw_vertices, raw_indices, index_offset, element->GetBox(i), colour);
  95. }
  96. geometry.Release();
  97. }
  98. // Generates the background geometry for a single box.
  99. void ElementBackground::GenerateBackground(Vertex*& vertices, int*& indices, int& index_offset, const Box& box, const Colourb& colour)
  100. {
  101. Vector2f padded_size = box.GetSize(Box::PADDING);
  102. if (padded_size.x <= 0 ||
  103. padded_size.y <= 0)
  104. return;
  105. GeometryUtilities::GenerateQuad(vertices, indices, box.GetOffset(), padded_size, colour, index_offset);
  106. vertices += 4;
  107. indices += 6;
  108. index_offset += 4;
  109. }
  110. }
  111. }