Line data Source code
1 : /**
2 : Copyright (c) 2023 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 "XL2dLayerRounded.h"
24 :
25 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d {
26 :
27 144 : bool LayerRounded::init(const Color4F &color, float borderRadius) {
28 144 : if (!VectorSprite::init(Size2(8.0f, 8.0f))) {
29 0 : return false;
30 : }
31 :
32 144 : setColor(color, true);
33 144 : _borderRadius = borderRadius;
34 144 : return true;
35 : }
36 :
37 2606 : void LayerRounded::onContentSizeDirty() {
38 2606 : auto radius = std::min(std::min(_contentSize.width / 2.0f, _contentSize.height / 2.0f), _borderRadius);
39 :
40 2606 : if (radius != _realBorderRadius || _contentSize != _image->getImageSize()) {
41 1104 : if (radius <= 0.0f) {
42 846 : if (_realBorderRadius != 0.0f) {
43 0 : setImage(Rc<VectorImage>::create(_contentSize));
44 0 : return;
45 : }
46 :
47 846 : _realBorderRadius = 0.0f;
48 : }
49 :
50 1104 : auto img = Rc<VectorImage>::create(_contentSize);
51 1104 : auto path = img->addPath();
52 2208 : path->openForWriting([&] (vg::PathWriter &writer) {
53 1104 : writer.moveTo(0.0f, radius)
54 1104 : .arcTo(radius, radius, 0.0f, false, true, radius, 0.0f)
55 1104 : .lineTo(_contentSize.width - radius, 0.0f)
56 1104 : .arcTo(radius, radius, 0.0f, false, true, _contentSize.width, radius)
57 1104 : .lineTo(_contentSize.width, _contentSize.height - radius)
58 1104 : .arcTo(radius, radius, 0.0f, false, true, _contentSize.width - radius, _contentSize.height)
59 1104 : .lineTo(radius, _contentSize.height)
60 1104 : .arcTo(radius, radius, 0.0f, false, true, 0.0f, _contentSize.height - radius)
61 1104 : .closePath();
62 1104 : })
63 1104 : .setAntialiased(false)
64 1104 : .setFillColor(_pathColor)
65 1104 : .setStyle(vg::DrawStyle::Fill);
66 :
67 1104 : setImage(move(img));
68 :
69 1104 : _realBorderRadius = radius;
70 1104 : }
71 :
72 2606 : VectorSprite::onContentSizeDirty();
73 : }
74 :
75 10 : void LayerRounded::setPathColor(const Color4B &color, bool withOpaity) {
76 10 : if (withOpaity) {
77 0 : _pathColor = color;
78 : } else {
79 10 : _pathColor = Color4B(color.r, color.g, color.b, _pathColor.a);
80 : }
81 :
82 10 : if (!_image->getPaths().empty()) {
83 0 : for (auto &it : _image->getPaths()) {
84 0 : it.second->setFillColor(color);
85 : }
86 : }
87 10 : }
88 :
89 0 : const Color4B &LayerRounded::getPathColor() const {
90 0 : return _pathColor;
91 : }
92 :
93 72 : void LayerRounded::setBorderRadius(float radius) {
94 72 : if (_borderRadius != radius) {
95 72 : _borderRadius = radius;
96 72 : _contentSizeDirty = true;
97 : }
98 72 : }
99 :
100 : }
|