DecoratorGradient.cpp 5.1 KB

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