Line data Source code
1 : /** 2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #ifndef XENOLITH_RENDERER_BASIC2D_XL2DLINEARGRADIENT_H_ 24 : #define XENOLITH_RENDERER_BASIC2D_XL2DLINEARGRADIENT_H_ 25 : 26 : #include "XL2d.h" 27 : 28 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d { 29 : 30 : struct GradientStep { 31 : float value = 0.0f; 32 : float factor = 0.0f; 33 : Color4F color = Color4F::WHITE; 34 : 35 : GradientStep() = default; 36 : GradientStep(float v, Color4F c) : value(v), color(c) { } 37 600 : GradientStep(float v, float f, Color4F c) : value(v), factor(f), color(c) { } 38 : 39 : GradientStep(const GradientStep &) = default; 40 : GradientStep &operator=(const GradientStep &) = default; 41 : }; 42 : 43 : struct LinearGradientData : public Ref { 44 1500 : virtual ~LinearGradientData() = default; 45 : 46 : Vec2 start; 47 : Vec2 end; 48 : Vector<GradientStep> steps; 49 : 50 750 : LinearGradientData() = default; 51 : LinearGradientData(const LinearGradientData &); 52 : LinearGradientData(LinearGradientData &&); 53 : }; 54 : 55 : class LinearGradient : public Ref { 56 : public: 57 300 : virtual ~LinearGradient() = default; 58 : 59 : // start and end in node space 60 : bool init(const Vec2 &start, const Vec2 &end, Vector<GradientStep> &&); 61 : bool init(const Vec2 &origin, float angle, float distance, Vector<GradientStep> &&); 62 : 63 : bool updateWithData(const Vec2 &start, const Vec2 &end, Vector<GradientStep> &&); 64 : bool updateWithData(const Vec2 &origin, float angle, float distance, Vector<GradientStep> &&); 65 : 66 : const Vec2 &getStart() const; 67 : const Vec2 &getEnd() const; 68 : 69 : const Vector<GradientStep> &getSteps() const; 70 : 71 : // pop data, mark for copy on write 72 : // user should not modify data 73 : Rc<LinearGradientData> pop(); 74 : 75 : // duplicate data, user can modify new data 76 : Rc<LinearGradientData> dup(); 77 : 78 : protected: 79 : void copy(); 80 : 81 : bool _copyOnWrite = false; 82 : Rc<LinearGradientData> _data; 83 : }; 84 : 85 : } 86 : 87 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2DDYNAMICLINEARGRADIENT_H_ */