DecoratorGradient.cpp 5.4 KB

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