ElementDecoration.cpp 4.5 KB

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