DecoratorGradient.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. const Vector4f border_radius{
  60. computed.border_top_left_radius(),
  61. computed.border_top_right_radius(),
  62. computed.border_bottom_right_radius(),
  63. computed.border_bottom_left_radius(),
  64. };
  65. GeometryUtilities::GenerateBackgroundBorder(geometry, element->GetBox(), Vector2f(0), border_radius, Colourb());
  66. // Apply opacity
  67. Colourb colour_start = start;
  68. colour_start.alpha = (byte)(opacity * (float)colour_start.alpha);
  69. Colourb colour_stop = stop;
  70. colour_stop.alpha = (byte)(opacity * (float)colour_stop.alpha);
  71. const Vector2f padding_offset = box.GetPosition(Box::PADDING);
  72. const Vector2f padding_size = box.GetSize(Box::PADDING);
  73. Vector<Vertex>& vertices = geometry->GetVertices();
  74. if (dir == Direction::Horizontal)
  75. {
  76. for (int i = 0; i < (int)vertices.size(); i++)
  77. {
  78. const float t = Math::Clamp((vertices[i].position.x - padding_offset.x) / padding_size.x, 0.0f, 1.0f);
  79. vertices[i].colour = Math::RoundedLerp(t, colour_start, colour_stop);
  80. }
  81. }
  82. else if (dir == Direction::Vertical)
  83. {
  84. for (int i = 0; i < (int)vertices.size(); i++)
  85. {
  86. const float t = Math::Clamp((vertices[i].position.y - padding_offset.y) / padding_size.y, 0.0f, 1.0f);
  87. vertices[i].colour = Math::RoundedLerp(t, colour_start, colour_stop);
  88. }
  89. }
  90. return reinterpret_cast<DecoratorDataHandle>(geometry);
  91. }
  92. void DecoratorGradient::ReleaseElementData(DecoratorDataHandle element_data) const
  93. {
  94. delete reinterpret_cast<Geometry*>(element_data);
  95. }
  96. void DecoratorGradient::RenderElement(Element* element, DecoratorDataHandle element_data) const
  97. {
  98. auto* data = reinterpret_cast<Geometry*>(element_data);
  99. data->Render(element->GetAbsoluteOffset(Box::BORDER));
  100. }
  101. DecoratorGradientInstancer::DecoratorGradientInstancer()
  102. {
  103. // register properties for the decorator
  104. ids.direction = RegisterProperty("direction", "horizontal").AddParser("keyword", "horizontal, vertical").GetId();
  105. ids.start = RegisterProperty("start-color", "#ffffff").AddParser("color").GetId();
  106. ids.stop = RegisterProperty("stop-color", "#ffffff").AddParser("color").GetId();
  107. RegisterShorthand("decorator", "direction, start-color, stop-color", ShorthandType::FallThrough);
  108. }
  109. DecoratorGradientInstancer::~DecoratorGradientInstancer() {}
  110. SharedPtr<Decorator> DecoratorGradientInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties_,
  111. const DecoratorInstancerInterface& /*interface_*/)
  112. {
  113. DecoratorGradient::Direction dir = (DecoratorGradient::Direction)properties_.GetProperty(ids.direction)->Get<int>();
  114. Colourb start = properties_.GetProperty(ids.start)->Get<Colourb>();
  115. Colourb stop = properties_.GetProperty(ids.stop)->Get<Colourb>();
  116. auto decorator = MakeShared<DecoratorGradient>();
  117. if (decorator->Initialise(dir, start, stop))
  118. {
  119. return decorator;
  120. }
  121. return nullptr;
  122. }
  123. } // namespace Rml