ElementDecoration.cpp 4.5 KB

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