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 : #include "XL2dLinearGradient.h"
24 :
25 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d {
26 :
27 0 : LinearGradientData::LinearGradientData(const LinearGradientData &other)
28 0 : : start(other.start), end(other.end), steps(other.steps) { }
29 :
30 :
31 0 : LinearGradientData::LinearGradientData(LinearGradientData &&other)
32 0 : : start(other.start), end(other.end), steps(move(other.steps)) { }
33 :
34 750 : bool LinearGradient::init(const Vec2 &start, const Vec2 &end, Vector<GradientStep> &&steps) {
35 750 : if (!updateWithData(start, end, move(steps))) {
36 0 : return false;
37 : }
38 :
39 750 : return true;
40 : }
41 :
42 0 : bool LinearGradient::init(const Vec2 &origin, float angle, float distance, Vector<GradientStep> &&steps) {
43 0 : return init(origin, origin + Vec2::forAngle(angle) * distance, move(steps));
44 : }
45 :
46 750 : bool LinearGradient::updateWithData(const Vec2 &start, const Vec2 &end, Vector<GradientStep> &&steps) {
47 750 : if (steps.empty()) {
48 0 : return false;
49 : }
50 :
51 750 : if (!_data) {
52 750 : _data = Rc<LinearGradientData>::alloc();
53 750 : _copyOnWrite = false;
54 0 : } else if (_copyOnWrite) {
55 0 : _data = Rc<LinearGradientData>::alloc(*_data);
56 : }
57 :
58 750 : _data->start = start;
59 750 : _data->end = end;
60 750 : _data->steps = move(steps);
61 :
62 750 : std::sort(_data->steps.begin(), _data->steps.end(), [] (const GradientStep &l, const GradientStep &r) {
63 4500 : return l.value < r.value;
64 : });
65 : /*if (_data->steps.front().value > 0.0f) {
66 : _data->steps.insert(_data->steps.begin(), GradientStep{0.0f, _data->steps.front().color});
67 : }
68 : if (_data->steps.back().value < 1.0f) {
69 : _data->steps.emplace_back(GradientStep{1.0f, _data->steps.back().color});
70 : }*/
71 :
72 750 : return true;
73 : }
74 :
75 0 : bool LinearGradient::updateWithData(const Vec2 &origin, float angle, float distance, Vector<GradientStep> &&steps) {
76 0 : return updateWithData(origin, origin + Vec2::forAngle(angle) * distance, move(steps));
77 : }
78 :
79 0 : const Vec2 &LinearGradient::getStart() const {
80 0 : return _data->start;
81 : }
82 0 : const Vec2 &LinearGradient::getEnd() const {
83 0 : return _data->end;
84 : }
85 :
86 0 : const Vector<GradientStep> &LinearGradient::getSteps() const {
87 0 : return _data->steps;
88 : }
89 :
90 370 : Rc<LinearGradientData> LinearGradient::pop() {
91 370 : _copyOnWrite = true;
92 370 : return _data;
93 : }
94 :
95 : // duplicate data, user can modify new data
96 0 : Rc<LinearGradientData> LinearGradient::dup() {
97 0 : return Rc<LinearGradientData>::alloc(*_data);
98 : }
99 :
100 0 : void LinearGradient::copy() {
101 0 : _data = Rc<LinearGradientData>::alloc(*_data);
102 0 : _copyOnWrite = false;
103 0 : }
104 :
105 : }
|