ElementBackgroundBorder.cpp 6.1 KB

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