DecoratorGradient.cpp 4.8 KB

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