ElementBackgroundBorder.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "ElementBackgroundBorder.h"
  2. #include "../../Include/RmlUi/Core/Box.h"
  3. #include "../../Include/RmlUi/Core/ComputedValues.h"
  4. #include "../../Include/RmlUi/Core/Context.h"
  5. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  6. #include "../../Include/RmlUi/Core/Element.h"
  7. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  8. #include "../../Include/RmlUi/Core/Profiling.h"
  9. #include "../../Include/RmlUi/Core/RenderManager.h"
  10. #include "BoxShadowCache.h"
  11. #include "GeometryBoxShadow.h"
  12. namespace Rml {
  13. ElementBackgroundBorder::ElementBackgroundBorder() {}
  14. void ElementBackgroundBorder::Render(Element* element)
  15. {
  16. if (background_dirty || border_dirty)
  17. {
  18. for (auto& background : backgrounds)
  19. {
  20. if (background.first != BackgroundType::BackgroundBorder)
  21. background.second.geometry.Release();
  22. }
  23. GenerateGeometry(element);
  24. background_dirty = false;
  25. border_dirty = false;
  26. }
  27. if (Background* shadow = GetBackground(BackgroundType::BoxShadowAndBackgroundBorder))
  28. {
  29. const Vector2f offset = element->GetAbsoluteOffset(BoxArea::Border);
  30. shadow->box_shadow_and_background_border->geometry.Render(offset, shadow->box_shadow_and_background_border->texture);
  31. }
  32. else if (Background* background = GetBackground(BackgroundType::BackgroundBorder))
  33. {
  34. const Vector2f offset = element->GetAbsoluteOffset(BoxArea::Border);
  35. background->geometry.Render(offset);
  36. }
  37. }
  38. void ElementBackgroundBorder::DirtyBackground()
  39. {
  40. background_dirty = true;
  41. }
  42. void ElementBackgroundBorder::DirtyBorder()
  43. {
  44. border_dirty = true;
  45. }
  46. Geometry* ElementBackgroundBorder::GetClipGeometry(Element* element, BoxArea clip_area)
  47. {
  48. BackgroundType type = {};
  49. switch (clip_area)
  50. {
  51. case Rml::BoxArea::Border: type = BackgroundType::ClipBorder; break;
  52. case Rml::BoxArea::Padding: type = BackgroundType::ClipPadding; break;
  53. case Rml::BoxArea::Content: type = BackgroundType::ClipContent; break;
  54. default: RMLUI_ERROR; return nullptr;
  55. }
  56. RenderManager* render_manager = element->GetRenderManager();
  57. Geometry& geometry = GetOrCreateBackground(type).geometry;
  58. if (render_manager && !geometry)
  59. {
  60. Mesh mesh = geometry.Release(Geometry::ReleaseMode::ClearMesh);
  61. MeshUtilities::GenerateBackground(mesh, element->GetRenderBox(clip_area), ColourbPremultiplied(255));
  62. geometry = render_manager->MakeGeometry(std::move(mesh));
  63. }
  64. return &geometry;
  65. }
  66. ElementBackgroundBorder::Background* ElementBackgroundBorder::GetBackground(BackgroundType type)
  67. {
  68. auto it = backgrounds.find(type);
  69. if (it != backgrounds.end())
  70. return &it->second;
  71. return nullptr;
  72. }
  73. ElementBackgroundBorder::Background& ElementBackgroundBorder::GetOrCreateBackground(BackgroundType type)
  74. {
  75. auto it = backgrounds.find(type);
  76. if (it != backgrounds.end())
  77. return it->second;
  78. Background& background = backgrounds[type];
  79. return background;
  80. }
  81. void ElementBackgroundBorder::EraseBackground(BackgroundType type)
  82. {
  83. backgrounds.erase(type);
  84. }
  85. void ElementBackgroundBorder::GenerateGeometry(Element* element)
  86. {
  87. RMLUI_ZoneScoped;
  88. RenderManager* render_manager = element->GetRenderManager();
  89. if (!render_manager)
  90. return;
  91. const ComputedValues& computed = element->GetComputedValues();
  92. const bool has_box_shadow = computed.has_box_shadow();
  93. if (has_box_shadow)
  94. {
  95. // The box shadow geometry also includes the element's background and border, thus we can skip the normal background generation.
  96. EraseBackground(BackgroundType::BackgroundBorder);
  97. Background& shadow_background = GetOrCreateBackground(BackgroundType::BoxShadowAndBackgroundBorder);
  98. shadow_background.box_shadow_and_background_border = BoxShadowCache::GetHandle(element, computed);
  99. return;
  100. }
  101. EraseBackground(BackgroundType::BoxShadowAndBackgroundBorder);
  102. const float opacity = computed.opacity();
  103. ColourbPremultiplied background_color = computed.background_color().ToPremultiplied(opacity);
  104. Array<ColourbPremultiplied, 4> border_colors = {
  105. computed.border_top_color().ToPremultiplied(opacity),
  106. computed.border_right_color().ToPremultiplied(opacity),
  107. computed.border_bottom_color().ToPremultiplied(opacity),
  108. computed.border_left_color().ToPremultiplied(opacity),
  109. };
  110. Geometry& geometry = GetOrCreateBackground(BackgroundType::BackgroundBorder).geometry;
  111. Mesh mesh = geometry.Release(Geometry::ReleaseMode::ClearMesh);
  112. for (int i = 0; i < element->GetNumBoxes(); i++)
  113. MeshUtilities::GenerateBackgroundBorder(mesh, element->GetRenderBox(BoxArea::Padding, i), background_color, border_colors.data());
  114. geometry = render_manager->MakeGeometry(std::move(mesh));
  115. }
  116. } // namespace Rml