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