ElementDecoration.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. 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. decorators_dirty = false;
  52. active_decorators_dirty = true;
  53. return true;
  54. }
  55. // Loads a single decorator and adds it to the list of loaded decorators for this element.
  56. int ElementDecoration::LoadDecorator(Decorator* decorator)
  57. {
  58. DecoratorHandle element_decorator;
  59. element_decorator.decorator = decorator;
  60. element_decorator.decorator->AddReference();
  61. element_decorator.decorator_data = decorator->GenerateElementData(element);
  62. decorators.push_back(element_decorator);
  63. return (int) (decorators.size() - 1);
  64. }
  65. // Releases all existing decorators and frees their data.
  66. void ElementDecoration::ReleaseDecorators()
  67. {
  68. for (size_t i = 0; i < decorators.size(); i++)
  69. {
  70. if (decorators[i].decorator_data)
  71. decorators[i].decorator->ReleaseElementData(decorators[i].decorator_data);
  72. decorators[i].decorator->RemoveReference();
  73. }
  74. decorators.clear();
  75. active_decorators.clear();
  76. decorator_index.clear();
  77. }
  78. // Updates the list of active decorators (if necessary).
  79. void ElementDecoration::UpdateActiveDecorators()
  80. {
  81. if (active_decorators_dirty)
  82. {
  83. active_decorators.clear();
  84. for (DecoratorIndex::iterator i = decorator_index.begin(); i != decorator_index.end(); ++i)
  85. {
  86. PseudoClassDecoratorIndexList& indices = (*i).second;
  87. for (size_t j = 0; j < indices.size(); ++j)
  88. {
  89. if (element->ArePseudoClassesSet(indices[j].first))
  90. {
  91. // Insert the new index into the list of active decorators, ordered by z-index.
  92. float z_index = decorators[indices[j].second].decorator->GetZIndex();
  93. std::vector< int >::iterator insert_iterator = active_decorators.begin();
  94. while (insert_iterator != active_decorators.end() &&
  95. z_index > decorators[(*insert_iterator)].decorator->GetZIndex())
  96. ++insert_iterator;
  97. active_decorators.insert(insert_iterator, indices[j].second);
  98. break;
  99. }
  100. }
  101. }
  102. active_decorators_dirty = false;
  103. }
  104. }
  105. void ElementDecoration::RenderDecorators()
  106. {
  107. if (decorators_dirty)
  108. {
  109. decorators_dirty = false;
  110. ReloadDecorators();
  111. }
  112. UpdateActiveDecorators();
  113. // Render the decorators attached to this element in its current state.
  114. for (size_t i = 0; i < active_decorators.size(); i++)
  115. {
  116. DecoratorHandle& decorator = decorators[active_decorators[i]];
  117. decorator.decorator->RenderElement(element, decorator.decorator_data);
  118. }
  119. }
  120. void ElementDecoration::DirtyDecorators(bool full_reload)
  121. {
  122. if (full_reload)
  123. decorators_dirty = true;
  124. active_decorators_dirty = true;
  125. }
  126. // Iterates over all active decorators attached to the decoration's element.
  127. bool ElementDecoration::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data) const
  128. {
  129. if (index < 0)
  130. return false;
  131. size_t count = 0;
  132. for (DecoratorIndex::const_iterator index_iterator = decorator_index.begin(); index_iterator != decorator_index.end(); ++index_iterator)
  133. {
  134. // This is the list of all pseudo-classes that have a decorator under this name.
  135. const PseudoClassDecoratorIndexList& decorator_index_list = index_iterator->second;
  136. if (count + decorator_index_list.size() <= (size_t) index)
  137. {
  138. count += decorator_index_list.size();
  139. continue;
  140. }
  141. // This is the one we're looking for.
  142. name = index_iterator->first;
  143. int relative_index = index - (int)count;
  144. pseudo_classes = decorator_index_list[relative_index].first;
  145. const DecoratorHandle& decorator_handle = decorators[decorator_index_list[relative_index].second];
  146. decorator = decorator_handle.decorator;
  147. decorator_data = decorator_handle.decorator_data;
  148. index += 1;
  149. return true;
  150. }
  151. return false;
  152. }
  153. }
  154. }