DecoratorGradient.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifndef RMLUI_CORE_DECORATORGRADIENT_H
  29. #define RMLUI_CORE_DECORATORGRADIENT_H
  30. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  31. #include "../../Include/RmlUi/Core/Decorator.h"
  32. #include "../../Include/RmlUi/Core/Geometry.h"
  33. #include "../../Include/RmlUi/Core/ID.h"
  34. #include "DecoratorUtilities.h"
  35. namespace Rml {
  36. /**
  37. Straight gradient.
  38. CSS usage:
  39. decorator: horizontal-gradient( <start-color> <stop-color> );
  40. decorator: vertical-gradient( <start-color> <stop-color> );
  41. */
  42. class DecoratorStraightGradient : public Decorator {
  43. public:
  44. enum class Direction { Horizontal, Vertical };
  45. DecoratorStraightGradient();
  46. virtual ~DecoratorStraightGradient();
  47. bool Initialise(Direction direction, Colourb start, Colourb stop);
  48. DecoratorDataHandle GenerateElementData(Element* element, BoxArea paint_area) const override;
  49. void ReleaseElementData(DecoratorDataHandle element_data) const override;
  50. void RenderElement(Element* element, DecoratorDataHandle element_data) const override;
  51. private:
  52. Direction direction = {};
  53. Colourb start, stop;
  54. };
  55. class DecoratorStraightGradientInstancer : public DecoratorInstancer {
  56. public:
  57. DecoratorStraightGradientInstancer();
  58. virtual ~DecoratorStraightGradientInstancer();
  59. SharedPtr<Decorator> InstanceDecorator(const String& name, const PropertyDictionary& properties,
  60. const DecoratorInstancerInterface& instancer_interface) override;
  61. private:
  62. struct PropertyIds {
  63. PropertyId direction, start, stop;
  64. };
  65. PropertyIds ids;
  66. };
  67. /**
  68. Linear gradient.
  69. */
  70. class DecoratorLinearGradient : public Decorator {
  71. public:
  72. enum class Corner { TopRight, BottomRight, BottomLeft, TopLeft, None, Count = None };
  73. DecoratorLinearGradient();
  74. virtual ~DecoratorLinearGradient();
  75. bool Initialise(bool repeating, Corner corner, float angle, const ColorStopList& color_stops);
  76. DecoratorDataHandle GenerateElementData(Element* element, BoxArea paint_area) const override;
  77. void ReleaseElementData(DecoratorDataHandle element_data) const override;
  78. void RenderElement(Element* element, DecoratorDataHandle element_data) const override;
  79. private:
  80. struct LinearGradientShape {
  81. // Gradient line starting and ending points.
  82. Vector2f p0, p1;
  83. float length;
  84. };
  85. LinearGradientShape CalculateShape(Vector2f box_dimensions) const;
  86. bool repeating = false;
  87. Corner corner = Corner::None;
  88. float angle = 0.f;
  89. ColorStopList color_stops;
  90. };
  91. class DecoratorLinearGradientInstancer : public DecoratorInstancer {
  92. public:
  93. DecoratorLinearGradientInstancer();
  94. virtual ~DecoratorLinearGradientInstancer();
  95. SharedPtr<Decorator> InstanceDecorator(const String& name, const PropertyDictionary& properties,
  96. const DecoratorInstancerInterface& instancer_interface) override;
  97. private:
  98. enum class Direction {
  99. None = 0,
  100. Top = 1,
  101. Right = 2,
  102. Bottom = 4,
  103. Left = 8,
  104. TopLeft = Top | Left,
  105. TopRight = Top | Right,
  106. BottomRight = Bottom | Right,
  107. BottomLeft = Bottom | Left,
  108. };
  109. struct PropertyIds {
  110. PropertyId angle;
  111. PropertyId direction_to, direction_x, direction_y;
  112. PropertyId color_stop_list;
  113. };
  114. PropertyIds ids;
  115. };
  116. /**
  117. Radial gradient.
  118. */
  119. class DecoratorRadialGradient : public Decorator {
  120. public:
  121. enum class Shape { Circle, Ellipse, Unspecified };
  122. enum class SizeType { ClosestSide, FarthestSide, ClosestCorner, FarthestCorner, LengthPercentage };
  123. DecoratorRadialGradient();
  124. virtual ~DecoratorRadialGradient();
  125. bool Initialise(bool repeating, Shape shape, SizeType size_type, Vector2Numeric size, Vector2Numeric position, const ColorStopList& color_stops);
  126. DecoratorDataHandle GenerateElementData(Element* element, BoxArea paint_area) const override;
  127. void ReleaseElementData(DecoratorDataHandle element_data) const override;
  128. void RenderElement(Element* element, DecoratorDataHandle element_data) const override;
  129. private:
  130. struct RadialGradientShape {
  131. Vector2f center, radius;
  132. };
  133. RadialGradientShape CalculateRadialGradientShape(Element* element, Vector2f dimensions) const;
  134. bool repeating = false;
  135. Shape shape = {};
  136. SizeType size_type = {};
  137. Vector2Numeric size;
  138. Vector2Numeric position;
  139. ColorStopList color_stops;
  140. };
  141. class DecoratorRadialGradientInstancer : public DecoratorInstancer {
  142. public:
  143. DecoratorRadialGradientInstancer();
  144. virtual ~DecoratorRadialGradientInstancer();
  145. SharedPtr<Decorator> InstanceDecorator(const String& name, const PropertyDictionary& properties,
  146. const DecoratorInstancerInterface& instancer_interface) override;
  147. private:
  148. struct GradientPropertyIds {
  149. PropertyId ending_shape;
  150. PropertyId size_x, size_y;
  151. PropertyId position_x, position_y;
  152. PropertyId color_stop_list;
  153. };
  154. GradientPropertyIds ids;
  155. };
  156. /**
  157. Conic gradient.
  158. */
  159. class DecoratorConicGradient : public Decorator {
  160. public:
  161. DecoratorConicGradient();
  162. virtual ~DecoratorConicGradient();
  163. bool Initialise(bool repeating, float angle, Vector2Numeric position, const ColorStopList& color_stops);
  164. DecoratorDataHandle GenerateElementData(Element* element, BoxArea paint_area) const override;
  165. void ReleaseElementData(DecoratorDataHandle element_data) const override;
  166. void RenderElement(Element* element, DecoratorDataHandle element_data) const override;
  167. private:
  168. bool repeating = false;
  169. float angle = {};
  170. Vector2Numeric position;
  171. ColorStopList color_stops;
  172. };
  173. class DecoratorConicGradientInstancer : public DecoratorInstancer {
  174. public:
  175. DecoratorConicGradientInstancer();
  176. virtual ~DecoratorConicGradientInstancer();
  177. SharedPtr<Decorator> InstanceDecorator(const String& name, const PropertyDictionary& properties,
  178. const DecoratorInstancerInterface& instancer_interface) override;
  179. private:
  180. struct GradientPropertyIds {
  181. PropertyId angle;
  182. PropertyId position_x, position_y;
  183. PropertyId color_stop_list;
  184. };
  185. GradientPropertyIds ids;
  186. };
  187. } // namespace Rml
  188. #endif