1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COMMON_PROPERTIES_RADIUS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COMMON_PROPERTIES_RADIUS_H
18 
19 #include "base/geometry/animatable_dimension.h"
20 #include "base/utils/utils.h"
21 
22 namespace OHOS::Ace {
23 
24 // A radius for shapes like rect and etc.
25 class Radius final {
26 public:
27     Radius() = default;
Radius(double value)28     explicit Radius(double value) : Radius(value, value) {}
Radius(const Dimension & value)29     explicit Radius(const Dimension& value) : Radius(value, value) {}
Radius(const AnimatableDimension & value)30     explicit Radius(const AnimatableDimension& value) : Radius(value, value) {}
Radius(double x,double y)31     Radius(double x, double y) : x_(Dimension(x)), y_(Dimension(y)) {}
Radius(const Dimension & x,const Dimension & y)32     Radius(const Dimension& x, const Dimension& y) : x_(x), y_(y) {}
Radius(const AnimatableDimension & x,const AnimatableDimension & y)33     Radius(const AnimatableDimension& x, const AnimatableDimension& y) : x_(x), y_(y) {}
34     ~Radius() = default;
35 
SetContextAndCallback(const WeakPtr<PipelineBase> & context,const RenderNodeAnimationCallback & callback)36     void SetContextAndCallback(const WeakPtr<PipelineBase>& context, const RenderNodeAnimationCallback& callback)
37     {
38         x_.SetContextAndCallback(context, callback);
39         y_.SetContextAndCallback(context, callback);
40     }
41 
ApplyScaleAndRound(double scale)42     void ApplyScaleAndRound(double scale)
43     {
44         x_.SetValue(round(x_.Value() * scale));
45         y_.SetValue(round(y_.Value() * scale));
46     }
47 
IsValid()48     bool IsValid() const
49     {
50         return x_.IsValid() && y_.IsValid();
51     }
52 
HasValue()53     bool HasValue() const
54     {
55         return x_.IsValid() || y_.IsValid();
56     }
57 
GetX()58     const AnimatableDimension& GetX() const
59     {
60         return x_;
61     }
62 
GetY()63     const AnimatableDimension& GetY() const
64     {
65         return y_;
66     }
67 
68     void SetX(const Dimension& x, const AnimationOption& option = AnimationOption())
69     {
70         x_ = AnimatableDimension(x, option);
71     }
72 
73     void SetY(const Dimension& y, const AnimationOption& option = AnimationOption())
74     {
75         y_ = AnimatableDimension(y, option);
76     }
77 
SetX(const AnimatableDimension & x)78     void SetX(const AnimatableDimension& x)
79     {
80         x_ = x;
81     }
82 
SetY(const AnimatableDimension & y)83     void SetY(const AnimatableDimension& y)
84     {
85         y_ = y;
86     }
87 
88     Radius operator+(const Radius& radius) const
89     {
90         return Radius(x_ + radius.GetX(), y_ + radius.GetY());
91     }
92 
93     Radius operator-(const Radius& radius) const
94     {
95         return Radius(x_ - radius.GetX(), y_ - radius.GetY());
96     }
97 
98     Radius operator*(double scale) const
99     {
100         return Radius(x_ * scale, y_ * scale);
101     }
102 
103     bool operator==(const Radius& radius) const
104     {
105         return (radius.GetX() == x_) && (radius.GetY() == y_);
106     }
107 
ToString()108     std::string ToString() const
109     {
110         return std::string("x:").append(x_.ToString()).append(", y:").append(y_.ToString());
111     }
112 
113 private:
114     AnimatableDimension x_;
115     AnimatableDimension y_;
116 };
117 
118 } // namespace OHOS::Ace
119 
120 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COMMON_PROPERTIES_RADIUS_H
121