DecoratorGradient.cpp 5.5 KB

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