ElementDecoration.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementDecoration.h"
  29. #include "ElementDefinition.h"
  30. #include "../../Include/Rocket/Core/Decorator.h"
  31. #include "../../Include/Rocket/Core/Element.h"
  32. namespace Rocket {
  33. namespace Core {
  34. ElementDecoration::ElementDecoration(Element* _element)
  35. {
  36. element = _element;
  37. active_decorators_dirty = false;
  38. }
  39. ElementDecoration::~ElementDecoration()
  40. {
  41. ReleaseDecorators();
  42. }
  43. // Releases existing decorators and loads all decorators required by the element's definition.
  44. bool ElementDecoration::ReloadDecorators()
  45. {
  46. ReleaseDecorators();
  47. const ElementDefinition* definition = element->GetDefinition();
  48. if (definition == NULL)
  49. return true;
  50. // Generate the decorator sets for pseudo-classes with overrides.
  51. const PseudoClassDecoratorMap& pseudo_class_decorators = definition->GetPseudoClassDecorators();
  52. for (PseudoClassDecoratorMap::const_iterator i = pseudo_class_decorators.begin(); i != pseudo_class_decorators.end(); ++i)
  53. {
  54. for (DecoratorMap::const_iterator j = (*i).second.begin(); j != (*i).second.end(); ++j)
  55. {
  56. int index = LoadDecorator((*j).second);
  57. // Add it into the index. If a decorator with the same name already exists for this element, then we add it
  58. // into the list at the right position (sorted by specificity, descending).
  59. PseudoClassDecoratorIndexList* pseudo_class_decorator_index = NULL;
  60. DecoratorIndex::iterator index_iterator = decorator_index.find((*j).first);
  61. if (index_iterator == decorator_index.end())
  62. pseudo_class_decorator_index = &(*decorator_index.insert(DecoratorIndex::value_type((*j).first, PseudoClassDecoratorIndexList())).first).second;
  63. else
  64. pseudo_class_decorator_index = &(*index_iterator).second;
  65. // Add the decorator index at the right point to maintain the order of the list.
  66. PseudoClassDecoratorIndexList::iterator k = pseudo_class_decorator_index->begin();
  67. for (; k != pseudo_class_decorator_index->end(); ++k)
  68. {
  69. if (decorators[(*k).second].decorator->GetSpecificity() < decorators[index].decorator->GetSpecificity())
  70. break;
  71. }
  72. pseudo_class_decorator_index->insert(k, PseudoClassDecoratorIndex(PseudoClassList((*i).first.begin(), (*i).first.end()), index));
  73. }
  74. }
  75. // Put the decorators for the element's default state at the end of any index lists.
  76. const DecoratorMap& default_decorators = definition->GetDecorators();
  77. for (DecoratorMap::const_iterator i = default_decorators.begin(); i != default_decorators.end(); ++i)
  78. {
  79. int index = LoadDecorator((*i).second);
  80. DecoratorIndex::iterator index_iterator = decorator_index.find((*i).first);
  81. if (index_iterator == decorator_index.end())
  82. decorator_index.insert(DecoratorIndex::value_type((*i).first, PseudoClassDecoratorIndexList(1, PseudoClassDecoratorIndex(PseudoClassList(), index))));
  83. else
  84. (*index_iterator).second.push_back(PseudoClassDecoratorIndex(PseudoClassList(), index));
  85. }
  86. active_decorators_dirty = true;
  87. return true;
  88. }
  89. // Loads a single decorator and adds it to the list of loaded decorators for this element.
  90. int ElementDecoration::LoadDecorator(Decorator* decorator)
  91. {
  92. DecoratorHandle element_decorator;
  93. element_decorator.decorator = decorator;
  94. element_decorator.decorator->AddReference();
  95. element_decorator.decorator_data = decorator->GenerateElementData(element);
  96. decorators.push_back(element_decorator);
  97. return (int) (decorators.size() - 1);
  98. }
  99. // Releases all existing decorators and frees their data.
  100. void ElementDecoration::ReleaseDecorators()
  101. {
  102. for (size_t i = 0; i < decorators.size(); i++)
  103. {
  104. if (decorators[i].decorator_data)
  105. decorators[i].decorator->ReleaseElementData(decorators[i].decorator_data);
  106. decorators[i].decorator->RemoveReference();
  107. }
  108. decorators.clear();
  109. active_decorators.clear();
  110. decorator_index.clear();
  111. }
  112. // Updates the list of active decorators (if necessary).
  113. void ElementDecoration::UpdateActiveDecorators()
  114. {
  115. if (active_decorators_dirty)
  116. {
  117. active_decorators.clear();
  118. for (DecoratorIndex::iterator i = decorator_index.begin(); i != decorator_index.end(); ++i)
  119. {
  120. PseudoClassDecoratorIndexList& indices = (*i).second;
  121. for (size_t j = 0; j < indices.size(); ++j)
  122. {
  123. if (element->ArePseudoClassesSet(indices[j].first))
  124. {
  125. // Insert the new index into the list of active decorators, ordered by z-index.
  126. float z_index = decorators[indices[j].second].decorator->GetZIndex();
  127. std::vector< int >::iterator insert_iterator = active_decorators.begin();
  128. while (insert_iterator != active_decorators.end() &&
  129. z_index > decorators[(*insert_iterator)].decorator->GetZIndex())
  130. ++insert_iterator;
  131. active_decorators.insert(insert_iterator, indices[j].second);
  132. break;
  133. }
  134. }
  135. }
  136. active_decorators_dirty = false;
  137. }
  138. }
  139. void ElementDecoration::RenderDecorators()
  140. {
  141. UpdateActiveDecorators();
  142. // Render the decorators attached to this element in its current state.
  143. for (size_t i = 0; i < active_decorators.size(); i++)
  144. {
  145. DecoratorHandle& decorator = decorators[active_decorators[i]];
  146. decorator.decorator->RenderElement(element, decorator.decorator_data);
  147. }
  148. }
  149. void ElementDecoration::DirtyDecorators()
  150. {
  151. active_decorators_dirty = true;
  152. }
  153. // Iterates over all active decorators attached to the decoration's element.
  154. bool ElementDecoration::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data) const
  155. {
  156. if (index < 0)
  157. return false;
  158. size_t count = 0;
  159. for (DecoratorIndex::const_iterator index_iterator = decorator_index.begin(); index_iterator != decorator_index.end(); ++index_iterator)
  160. {
  161. // This is the list of all pseudo-classes that have a decorator under this name.
  162. const PseudoClassDecoratorIndexList& decorator_index_list = index_iterator->second;
  163. if (count + decorator_index_list.size() <= (size_t) index)
  164. {
  165. count += decorator_index_list.size();
  166. continue;
  167. }
  168. // This is the one we're looking for.
  169. name = index_iterator->first;
  170. int relative_index = index - (int)count;
  171. pseudo_classes = decorator_index_list[relative_index].first;
  172. const DecoratorHandle& decorator_handle = decorators[decorator_index_list[relative_index].second];
  173. decorator = decorator_handle.decorator;
  174. decorator_data = decorator_handle.decorator_data;
  175. index += 1;
  176. return true;
  177. }
  178. return false;
  179. }
  180. }
  181. }