DecoratorGradient.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "DecoratorGradient.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Geometry.h"
  31. #include "../../Include/RmlUi/Core/GeometryUtilities.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  34. /*
  35. Gradient decorator usage in CSS:
  36. decorator: gradient( direction start-color stop-color );
  37. direction: horizontal|vertical;
  38. start-color: #ff00ff;
  39. stop-color: #00ff00;
  40. */
  41. namespace Rml {
  42. namespace Core {
  43. //=======================================================
  44. DecoratorGradient::DecoratorGradient()
  45. {
  46. }
  47. DecoratorGradient::~DecoratorGradient()
  48. {
  49. }
  50. bool DecoratorGradient::Initialise(const Direction &dir_, const Colourb &start_, const Colourb & stop_)
  51. {
  52. dir = dir_;
  53. start = start_;
  54. stop = stop_;
  55. return true;
  56. }
  57. DecoratorDataHandle DecoratorGradient::GenerateElementData(Element* element) const
  58. {
  59. auto *data = new Geometry(element);
  60. Vector2f padded_size = element->GetBox().GetSize(Box::PADDING);
  61. const float opacity = element->GetComputedValues().opacity;
  62. // Apply opacity
  63. Colourb colour_start = start;
  64. colour_start.alpha = (byte)(opacity * (float)colour_start.alpha);
  65. Colourb colour_stop = stop;
  66. colour_stop.alpha = (byte)(opacity * (float)colour_stop.alpha);
  67. auto &vertices = data->GetVertices();
  68. vertices.resize(4);
  69. auto &indices = data->GetIndices();
  70. indices.resize(6);
  71. GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), padded_size, colour_start, 0);
  72. if (dir == Direction::Horizontal) {
  73. vertices[1].colour = vertices[2].colour = colour_stop;
  74. } else if (dir == Direction::Vertical) {
  75. vertices[2].colour = vertices[3].colour = colour_stop;
  76. }
  77. data->SetHostElement(element);
  78. return reinterpret_cast<DecoratorDataHandle>(data);
  79. }
  80. void DecoratorGradient::ReleaseElementData(DecoratorDataHandle element_data) const
  81. {
  82. delete reinterpret_cast<Geometry*>(element_data);
  83. }
  84. void DecoratorGradient::RenderElement(Element* element, DecoratorDataHandle element_data) const
  85. {
  86. auto* data = reinterpret_cast<Geometry*>(element_data);
  87. data->Render(element->GetAbsoluteOffset(Box::PADDING).Round());
  88. }
  89. //=======================================================
  90. DecoratorGradientInstancer::DecoratorGradientInstancer()
  91. {
  92. // register properties for the decorator
  93. ids.direction = RegisterProperty("direction", "horizontal").AddParser("keyword", "horizontal, vertical").GetId();
  94. ids.start = RegisterProperty("start-color", "#ffffff").AddParser("color").GetId();
  95. ids.stop = RegisterProperty("stop-color", "#ffffff").AddParser("color").GetId();
  96. RegisterShorthand("decorator", "direction, start-color, stop-color", ShorthandType::FallThrough);
  97. }
  98. DecoratorGradientInstancer::~DecoratorGradientInstancer()
  99. {
  100. }
  101. SharedPtr<Decorator> DecoratorGradientInstancer::InstanceDecorator(const String & RMLUI_UNUSED_PARAMETER(name), const PropertyDictionary& properties_,
  102. const DecoratorInstancerInterface& RMLUI_UNUSED_PARAMETER(interface_))
  103. {
  104. RMLUI_UNUSED(name);
  105. RMLUI_UNUSED(interface_);
  106. DecoratorGradient::Direction dir = (DecoratorGradient::Direction)properties_.GetProperty(ids.direction)->Get< int >();
  107. Colourb start = properties_.GetProperty(ids.start)->Get<Colourb>();
  108. Colourb stop = properties_.GetProperty(ids.stop)->Get<Colourb>();
  109. auto decorator = std::make_shared<DecoratorGradient>();
  110. if (decorator->Initialise(dir, start, stop)) {
  111. return decorator;
  112. }
  113. return nullptr;
  114. }
  115. }
  116. }