ElementDecoration.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "precompiled.h"
  29. #include "ElementDecoration.h"
  30. #include "ElementDefinition.h"
  31. #include "../../Include/RmlUi/Core/Decorator.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. namespace Rml {
  34. namespace Core {
  35. ElementDecoration::ElementDecoration(Element* _element)
  36. {
  37. element = _element;
  38. active_decorators_dirty = false;
  39. }
  40. ElementDecoration::~ElementDecoration()
  41. {
  42. ReleaseDecorators();
  43. }
  44. // Releases existing decorators and loads all decorators required by the element's definition.
  45. bool ElementDecoration::ReloadDecorators()
  46. {
  47. ReleaseDecorators();
  48. const ElementDefinition* definition = element->GetDefinition();
  49. if (definition == NULL)
  50. return true;
  51. // Generate the decorator sets for pseudo-classes with overrides.
  52. const PseudoClassDecoratorMap& pseudo_class_decorators = definition->GetPseudoClassDecorators();
  53. for (PseudoClassDecoratorMap::const_iterator i = pseudo_class_decorators.begin(); i != pseudo_class_decorators.end(); ++i)
  54. {
  55. for (DecoratorMap::const_iterator j = (*i).second.begin(); j != (*i).second.end(); ++j)
  56. {
  57. int index = LoadDecorator((*j).second);
  58. // Add it into the index. If a decorator with the same name already exists for this element, then we add it
  59. // into the list at the right position (sorted by specificity, descending).
  60. PseudoClassDecoratorIndexList* pseudo_class_decorator_index = NULL;
  61. DecoratorIndex::iterator index_iterator = decorator_index.find((*j).first);
  62. if (index_iterator == decorator_index.end())
  63. pseudo_class_decorator_index = &(*decorator_index.insert(DecoratorIndex::value_type((*j).first, PseudoClassDecoratorIndexList())).first).second;
  64. else
  65. pseudo_class_decorator_index = &(*index_iterator).second;
  66. // Add the decorator index at the right point to maintain the order of the list.
  67. PseudoClassDecoratorIndexList::iterator k = pseudo_class_decorator_index->begin();
  68. for (; k != pseudo_class_decorator_index->end(); ++k)
  69. {
  70. if (decorators[(*k).second].decorator->GetSpecificity() < decorators[index].decorator->GetSpecificity())
  71. break;
  72. }
  73. pseudo_class_decorator_index->insert(k, PseudoClassDecoratorIndex(PseudoClassList((*i).first.begin(), (*i).first.end()), index));
  74. }
  75. }
  76. // Put the decorators for the element's default state at the end of any index lists.
  77. const DecoratorMap& default_decorators = definition->GetDecorators();
  78. for (DecoratorMap::const_iterator i = default_decorators.begin(); i != default_decorators.end(); ++i)
  79. {
  80. int index = LoadDecorator((*i).second);
  81. DecoratorIndex::iterator index_iterator = decorator_index.find((*i).first);
  82. if (index_iterator == decorator_index.end())
  83. decorator_index.insert(DecoratorIndex::value_type((*i).first, PseudoClassDecoratorIndexList(1, PseudoClassDecoratorIndex(PseudoClassList(), index))));
  84. else
  85. (*index_iterator).second.push_back(PseudoClassDecoratorIndex(PseudoClassList(), index));
  86. }
  87. active_decorators_dirty = true;
  88. return true;
  89. }
  90. // Loads a single decorator and adds it to the list of loaded decorators for this element.
  91. int ElementDecoration::LoadDecorator(Decorator* decorator)
  92. {
  93. DecoratorHandle element_decorator;
  94. element_decorator.decorator = decorator;
  95. element_decorator.decorator->AddReference();
  96. element_decorator.decorator_data = decorator->GenerateElementData(element);
  97. decorators.push_back(element_decorator);
  98. return (int) (decorators.size() - 1);
  99. }
  100. // Releases all existing decorators and frees their data.
  101. void ElementDecoration::ReleaseDecorators()
  102. {
  103. for (size_t i = 0; i < decorators.size(); i++)
  104. {
  105. if (decorators[i].decorator_data)
  106. decorators[i].decorator->ReleaseElementData(decorators[i].decorator_data);
  107. decorators[i].decorator->RemoveReference();
  108. }
  109. decorators.clear();
  110. active_decorators.clear();
  111. decorator_index.clear();
  112. }
  113. // Updates the list of active decorators (if necessary).
  114. void ElementDecoration::UpdateActiveDecorators()
  115. {
  116. if (active_decorators_dirty)
  117. {
  118. active_decorators.clear();
  119. for (DecoratorIndex::iterator i = decorator_index.begin(); i != decorator_index.end(); ++i)
  120. {
  121. PseudoClassDecoratorIndexList& indices = (*i).second;
  122. for (size_t j = 0; j < indices.size(); ++j)
  123. {
  124. if (element->ArePseudoClassesSet(indices[j].first))
  125. {
  126. // Insert the new index into the list of active decorators, ordered by z-index.
  127. float z_index = decorators[indices[j].second].decorator->GetZIndex();
  128. std::vector< int >::iterator insert_iterator = active_decorators.begin();
  129. while (insert_iterator != active_decorators.end() &&
  130. z_index > decorators[(*insert_iterator)].decorator->GetZIndex())
  131. ++insert_iterator;
  132. active_decorators.insert(insert_iterator, indices[j].second);
  133. break;
  134. }
  135. }
  136. }
  137. active_decorators_dirty = false;
  138. }
  139. }
  140. void ElementDecoration::RenderDecorators()
  141. {
  142. UpdateActiveDecorators();
  143. // Render the decorators attached to this element in its current state.
  144. for (size_t i = 0; i < active_decorators.size(); i++)
  145. {
  146. DecoratorHandle& decorator = decorators[active_decorators[i]];
  147. decorator.decorator->RenderElement(element, decorator.decorator_data);
  148. }
  149. }
  150. void ElementDecoration::DirtyDecorators()
  151. {
  152. active_decorators_dirty = true;
  153. }
  154. // Iterates over all active decorators attached to the decoration's element.
  155. bool ElementDecoration::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data) const
  156. {
  157. if (index < 0)
  158. return false;
  159. size_t count = 0;
  160. for (DecoratorIndex::const_iterator index_iterator = decorator_index.begin(); index_iterator != decorator_index.end(); ++index_iterator)
  161. {
  162. // This is the list of all pseudo-classes that have a decorator under this name.
  163. const PseudoClassDecoratorIndexList& decorator_index_list = index_iterator->second;
  164. if (count + decorator_index_list.size() <= (size_t) index)
  165. {
  166. count += decorator_index_list.size();
  167. continue;
  168. }
  169. // This is the one we're looking for.
  170. name = index_iterator->first;
  171. int relative_index = index - (int)count;
  172. pseudo_classes = decorator_index_list[relative_index].first;
  173. const DecoratorHandle& decorator_handle = decorators[decorator_index_list[relative_index].second];
  174. decorator = decorator_handle.decorator;
  175. decorator_data = decorator_handle.decorator_data;
  176. index += 1;
  177. return true;
  178. }
  179. return false;
  180. }
  181. }
  182. }