ElementDecoration.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "ElementDecoration.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Decorator.h"
  31. #include "../../Include/RmlUi/Core/DecoratorInstancer.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/ElementDocument.h"
  34. #include "../../Include/RmlUi/Core/Profiling.h"
  35. #include "../../Include/RmlUi/Core/StyleSheet.h"
  36. namespace Rml {
  37. ElementDecoration::ElementDecoration(Element* _element) : element(_element) {}
  38. ElementDecoration::~ElementDecoration()
  39. {
  40. ReleaseDecorators();
  41. }
  42. void ElementDecoration::InstanceDecorators()
  43. {
  44. if (decorators_dirty)
  45. {
  46. decorators_dirty = false;
  47. decorators_data_dirty = true;
  48. ReloadDecorators();
  49. }
  50. }
  51. bool ElementDecoration::ReloadDecorators()
  52. {
  53. RMLUI_ZoneScopedC(0xB22222);
  54. ReleaseDecorators();
  55. if (!element->GetComputedValues().has_decorator())
  56. return true;
  57. const Property* property = element->GetLocalProperty(PropertyId::Decorator);
  58. if (!property || property->unit != Unit::DECORATOR)
  59. return false;
  60. DecoratorsPtr decorators_ptr = property->Get<DecoratorsPtr>();
  61. if (!decorators_ptr)
  62. return false;
  63. const StyleSheet* style_sheet = element->GetStyleSheet();
  64. if (!style_sheet)
  65. return false;
  66. PropertySource document_source("", 0, "");
  67. const PropertySource* source = property->source.get();
  68. if (!source)
  69. {
  70. if (ElementDocument* document = element->GetOwnerDocument())
  71. {
  72. document_source.path = document->GetSourceURL();
  73. source = &document_source;
  74. }
  75. }
  76. const DecoratorPtrList& decorator_list = style_sheet->InstanceDecorators(*decorators_ptr, source);
  77. for (const SharedPtr<const Decorator>& decorator : decorator_list)
  78. {
  79. if (decorator)
  80. {
  81. DecoratorHandle decorator_handle;
  82. decorator_handle.decorator_data = 0;
  83. decorator_handle.decorator = decorator;
  84. decorators.push_back(std::move(decorator_handle));
  85. }
  86. }
  87. return true;
  88. }
  89. void ElementDecoration::ReloadDecoratorsData()
  90. {
  91. if (decorators_data_dirty)
  92. {
  93. decorators_data_dirty = false;
  94. for (DecoratorHandle& decorator : decorators)
  95. {
  96. if (decorator.decorator_data)
  97. decorator.decorator->ReleaseElementData(decorator.decorator_data);
  98. decorator.decorator_data = decorator.decorator->GenerateElementData(element);
  99. }
  100. }
  101. }
  102. void ElementDecoration::ReleaseDecorators()
  103. {
  104. for (DecoratorHandle& decorator : decorators)
  105. {
  106. if (decorator.decorator_data)
  107. decorator.decorator->ReleaseElementData(decorator.decorator_data);
  108. }
  109. decorators.clear();
  110. }
  111. void ElementDecoration::RenderDecorators()
  112. {
  113. InstanceDecorators();
  114. ReloadDecoratorsData();
  115. // Render the decorators attached to this element in its current state.
  116. // Render from back to front for correct render order.
  117. for (int i = (int)decorators.size() - 1; i >= 0; i--)
  118. {
  119. DecoratorHandle& decorator = decorators[i];
  120. decorator.decorator->RenderElement(element, decorator.decorator_data);
  121. }
  122. }
  123. void ElementDecoration::DirtyDecorators()
  124. {
  125. decorators_dirty = true;
  126. }
  127. void ElementDecoration::DirtyDecoratorsData()
  128. {
  129. decorators_data_dirty = true;
  130. }
  131. } // namespace Rml