ElementBackgroundBorder.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-2023 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 "ElementBackgroundBorder.h"
  29. #include "../../Include/RmlUi/Core/Box.h"
  30. #include "../../Include/RmlUi/Core/ComputedValues.h"
  31. #include "../../Include/RmlUi/Core/Context.h"
  32. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  33. #include "../../Include/RmlUi/Core/Element.h"
  34. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  35. #include "../../Include/RmlUi/Core/RenderManager.h"
  36. #include "GeometryBoxShadow.h"
  37. namespace Rml {
  38. ElementBackgroundBorder::ElementBackgroundBorder() {}
  39. void ElementBackgroundBorder::Render(Element* element)
  40. {
  41. if (background_dirty || border_dirty)
  42. {
  43. for (auto& background : backgrounds)
  44. {
  45. if (background.first != BackgroundType::BackgroundBorder)
  46. background.second.geometry.Release();
  47. }
  48. GenerateGeometry(element);
  49. background_dirty = false;
  50. border_dirty = false;
  51. }
  52. Background* shadow = GetBackground(BackgroundType::BoxShadow);
  53. if (shadow && shadow->geometry)
  54. shadow->geometry.Render(element->GetAbsoluteOffset(BoxArea::Border), shadow->texture);
  55. else if (Background* background = GetBackground(BackgroundType::BackgroundBorder))
  56. background->geometry.Render(element->GetAbsoluteOffset(BoxArea::Border));
  57. }
  58. void ElementBackgroundBorder::DirtyBackground()
  59. {
  60. background_dirty = true;
  61. }
  62. void ElementBackgroundBorder::DirtyBorder()
  63. {
  64. border_dirty = true;
  65. }
  66. Geometry* ElementBackgroundBorder::GetClipGeometry(Element* element, BoxArea clip_area)
  67. {
  68. BackgroundType type = {};
  69. switch (clip_area)
  70. {
  71. case Rml::BoxArea::Border: type = BackgroundType::ClipBorder; break;
  72. case Rml::BoxArea::Padding: type = BackgroundType::ClipPadding; break;
  73. case Rml::BoxArea::Content: type = BackgroundType::ClipContent; break;
  74. default: RMLUI_ERROR; return nullptr;
  75. }
  76. Geometry& geometry = GetOrCreateBackground(type).geometry;
  77. if (!geometry)
  78. {
  79. Mesh mesh = geometry.Release(Geometry::ReleaseMode::ClearMesh);
  80. const Box& box = element->GetBox();
  81. const Vector4f border_radius = element->GetComputedValues().border_radius();
  82. MeshUtilities::GenerateBackground(mesh, box, {}, border_radius, ColourbPremultiplied(255), clip_area);
  83. if (RenderManager* render_manager = element->GetRenderManager())
  84. geometry = render_manager->MakeGeometry(std::move(mesh));
  85. }
  86. return &geometry;
  87. }
  88. ElementBackgroundBorder::Background* ElementBackgroundBorder::GetBackground(BackgroundType type)
  89. {
  90. auto it = backgrounds.find(type);
  91. if (it != backgrounds.end())
  92. return &it->second;
  93. return nullptr;
  94. }
  95. ElementBackgroundBorder::Background& ElementBackgroundBorder::GetOrCreateBackground(BackgroundType type)
  96. {
  97. auto it = backgrounds.find(type);
  98. if (it != backgrounds.end())
  99. return it->second;
  100. Background& background = backgrounds[type];
  101. return background;
  102. }
  103. void ElementBackgroundBorder::GenerateGeometry(Element* element)
  104. {
  105. RenderManager* render_manager = element->GetRenderManager();
  106. if (!render_manager)
  107. return;
  108. const ComputedValues& computed = element->GetComputedValues();
  109. const bool has_box_shadow = computed.has_box_shadow();
  110. const float opacity = computed.opacity();
  111. // Apply opacity except if we have a box shadow. In the latter case the background is rendered opaquely into the box-shadow texture, while
  112. // opacity is applied to the entire box-shadow texture when that is rendered.
  113. bool apply_opacity = (!has_box_shadow && opacity < 1.f);
  114. auto ConvertColor = [=](Colourb color) {
  115. if (apply_opacity)
  116. return color.ToPremultiplied(opacity);
  117. else
  118. return color.ToPremultiplied();
  119. };
  120. ColourbPremultiplied background_color = ConvertColor(computed.background_color());
  121. ColourbPremultiplied border_colors[4] = {
  122. ConvertColor(computed.border_top_color()),
  123. ConvertColor(computed.border_right_color()),
  124. ConvertColor(computed.border_bottom_color()),
  125. ConvertColor(computed.border_left_color()),
  126. };
  127. const Vector4f border_radius = computed.border_radius();
  128. Geometry& geometry = GetOrCreateBackground(BackgroundType::BackgroundBorder).geometry;
  129. Mesh mesh = geometry.Release(Geometry::ReleaseMode::ClearMesh);
  130. for (int i = 0; i < element->GetNumBoxes(); i++)
  131. {
  132. Vector2f offset;
  133. const Box& box = element->GetBox(i, offset);
  134. MeshUtilities::GenerateBackgroundBorder(mesh, box, offset, border_radius, background_color, border_colors);
  135. }
  136. geometry = render_manager->MakeGeometry(std::move(mesh));
  137. if (has_box_shadow)
  138. {
  139. Geometry& background_border_geometry = geometry;
  140. const Property* p_box_shadow = element->GetLocalProperty(PropertyId::BoxShadow);
  141. RMLUI_ASSERT(p_box_shadow->value.GetType() == Variant::BOXSHADOWLIST);
  142. BoxShadowList shadow_list = p_box_shadow->value.Get<BoxShadowList>();
  143. // Generate the geometry for the box-shadow texture.
  144. Background& shadow_background = GetOrCreateBackground(BackgroundType::BoxShadow);
  145. Geometry& shadow_geometry = shadow_background.geometry;
  146. CallbackTexture& shadow_texture = shadow_background.texture;
  147. GeometryBoxShadow::Generate(shadow_geometry, shadow_texture, *render_manager, element, background_border_geometry, std::move(shadow_list),
  148. border_radius, computed.opacity());
  149. }
  150. }
  151. } // namespace Rml