• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_BASE_GEOMETRY_OFFSET_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_OFFSET_H
18 
19 #include <cmath>
20 #include <limits>
21 
22 #include "base/geometry/size.h"
23 #include "base/utils/utils.h"
24 #include "core/components/common/properties/animation_option.h"
25 
26 namespace OHOS::Ace {
27 
28 class Offset {
29 public:
30     Offset() = default;
31     ~Offset() = default;
Offset(double deltaX,double deltaY)32     Offset(double deltaX, double deltaY) : deltaX_(deltaX), deltaY_(deltaY) {}
33 
Reset()34     void Reset()
35     {
36         deltaX_ = 0.0;
37         deltaY_ = 0.0;
38         deltaXAnimationOption_ = AnimationOption();
39         deltaYAnimationOption_ = AnimationOption();
40     }
41 
Zero()42     static Offset Zero()
43     {
44         return Offset();
45     }
46 
ErrorOffset()47     static Offset ErrorOffset()
48     {
49         return Offset((std::numeric_limits<double>::max)(), (std::numeric_limits<double>::max)());
50     }
51 
IsZero()52     bool IsZero() const
53     {
54         return operator==(Offset());
55     }
56 
IsErrorOffset()57     bool IsErrorOffset() const
58     {
59         return operator==(ErrorOffset());
60     }
61 
GetX()62     double GetX() const
63     {
64         return deltaX_;
65     }
66 
GetY()67     double GetY() const
68     {
69         return deltaY_;
70     }
71 
72     void SetX(double x, const AnimationOption& option = AnimationOption())
73     {
74         deltaX_ = x;
75         deltaXAnimationOption_ = option;
76     }
77 
78     void SetY(double y, const AnimationOption& option = AnimationOption())
79     {
80         deltaY_ = y;
81         deltaYAnimationOption_ = option;
82     }
83 
GetXAnimationOption()84     AnimationOption GetXAnimationOption() const
85     {
86         return deltaXAnimationOption_;
87     }
88 
GetYAnimationOption()89     AnimationOption GetYAnimationOption() const
90     {
91         return deltaYAnimationOption_;
92     }
93 
GetDistance()94     double GetDistance() const
95     {
96         return std::sqrt((deltaX_ * deltaX_) + (deltaY_ * deltaY_));
97     }
98 
99     Offset operator+(const Offset& offset) const
100     {
101         return Offset(deltaX_ + offset.deltaX_, deltaY_ + offset.deltaY_);
102     }
103 
104     Offset operator+(const Size& size) const
105     {
106         return Offset(deltaX_ + size.Width(), deltaY_ + size.Height());
107     }
108 
109     Offset operator-(const Offset& offset) const
110     {
111         return Offset(deltaX_ - offset.deltaX_, deltaY_ - offset.deltaY_);
112     }
113 
114     Offset operator*(double value) const
115     {
116         return Offset(deltaX_ * value, deltaY_ * value);
117     }
118 
119     Offset operator/(double value) const
120     {
121         if (NearZero(value)) {
122             return ErrorOffset();
123         }
124         return Offset(deltaX_ / value, deltaY_ / value);
125     }
126 
127     Offset& operator+=(const Offset& offset)
128     {
129         deltaX_ += offset.deltaX_;
130         deltaY_ += offset.deltaY_;
131         return *this;
132     }
133 
134     bool operator==(const Offset& offset) const
135     {
136         return NearEqual(deltaX_, offset.deltaX_) && NearEqual(deltaY_, offset.deltaY_);
137     }
138 
139     bool operator!=(const Offset& offset) const
140     {
141         return !operator==(offset);
142     }
143 
ToString()144     std::string ToString() const
145     {
146         std::stringstream ss;
147         ss << "Offset (" << std::fixed << std::setprecision(2) << deltaX_ << ", " << deltaY_ << ")";
148         std::string output = ss.str();
149         return output;
150     }
151 
IsPositiveOffset()152     bool IsPositiveOffset() const
153     {
154         return deltaX_ >= 0 && deltaY_ >= 0;
155     }
156 
157 private:
158     double deltaX_ = 0.0;
159     double deltaY_ = 0.0;
160     AnimationOption deltaXAnimationOption_;
161     AnimationOption deltaYAnimationOption_;
162 };
163 
164 } // namespace OHOS::Ace
165 
166 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_OFFSET_H
167