ElementDecoration.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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. StyleSheet* stylesheet = element->GetStyleSheet();
  48. if (!stylesheet)
  49. return true;
  50. const Property* property = element->GetLocalProperty(PropertyId::Decorator);
  51. if (!property)
  52. return true;
  53. String decorator_value = property->Get<String>();
  54. // @performance: Can optimize for the case of only one decorator
  55. StringList decorator_list;
  56. // We use custom quote characters as we don't want to split on any commas inside parenthesis, which may appear in decorator shorthands.
  57. StringUtilities::ExpandString(decorator_list, decorator_value, ',', '(', ')');
  58. for (const String& name : decorator_list)
  59. {
  60. std::shared_ptr<Decorator> decorator = stylesheet->GetOrInstanceDecorator(name, property->source, property->source_line_number);
  61. if (decorator)
  62. LoadDecorator(std::move(decorator));
  63. }
  64. return true;
  65. }
  66. // Loads a single decorator and adds it to the list of loaded decorators for this element.
  67. int ElementDecoration::LoadDecorator(std::shared_ptr<Decorator> decorator)
  68. {
  69. DecoratorHandle element_decorator;
  70. element_decorator.decorator_data = decorator->GenerateElementData(element);
  71. element_decorator.decorator = std::move(decorator);
  72. decorators.push_back(element_decorator);
  73. return (int) (decorators.size() - 1);
  74. }
  75. // Releases all existing decorators and frees their data.
  76. void ElementDecoration::ReleaseDecorators()
  77. {
  78. for (size_t i = 0; i < decorators.size(); i++)
  79. {
  80. if (decorators[i].decorator_data)
  81. decorators[i].decorator->ReleaseElementData(decorators[i].decorator_data);
  82. }
  83. decorators.clear();
  84. }
  85. void ElementDecoration::RenderDecorators()
  86. {
  87. // @performance: Ignore dirty flag if e.g. pseudo classes do not affect the decorators
  88. if (decorators_dirty)
  89. {
  90. decorators_dirty = false;
  91. ReloadDecorators();
  92. }
  93. // Render the decorators attached to this element in its current state.
  94. // Render from back to front for correct render order.
  95. for (int i = (int)decorators.size() - 1; i >= 0; i--)
  96. {
  97. DecoratorHandle& decorator = decorators[i];
  98. decorator.decorator->RenderElement(element, decorator.decorator_data);
  99. }
  100. }
  101. void ElementDecoration::DirtyDecorators()
  102. {
  103. decorators_dirty = true;
  104. }
  105. }
  106. }