• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_BASE_GEOMETRY_NG_OFFSET_T_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_NG_OFFSET_T_H
18 
19 #include <cstdint>
20 #include <iomanip>
21 #include <sstream>
22 #include <string>
23 
24 #include "base/geometry/axis.h"
25 #include "base/utils/utils.h"
26 
27 namespace OHOS::Ace::NG {
28 template<typename T>
29 class OffsetT {
30 public:
31     OffsetT() = default;
32     ~OffsetT() = default;
OffsetT(T x,T y)33     OffsetT(T x, T y) : x_(x), y_(y) {}
34 
Reset()35     void Reset()
36     {
37         x_ = 0;
38         y_ = 0;
39     }
40 
GetX()41     T GetX() const
42     {
43         return x_;
44     }
45 
GetY()46     T GetY() const
47     {
48         return y_;
49     }
50 
GetMainOffset(Axis axis)51     T GetMainOffset(Axis axis) const
52     {
53         return axis == Axis::HORIZONTAL ? x_ : y_;
54     }
55 
GetCrossOffset(Axis axis)56     T GetCrossOffset(Axis axis) const
57     {
58         return axis == Axis::HORIZONTAL ? y_ : x_;
59     }
60 
SetX(T x)61     void SetX(T x)
62     {
63         x_ = x;
64     }
65 
SetY(T y)66     void SetY(T y)
67     {
68         y_ = y;
69     }
70 
AddX(T x)71     void AddX(T x)
72     {
73         x_ += x;
74     }
75 
AddY(T y)76     void AddY(T y)
77     {
78         y_ += y;
79     }
80 
81     OffsetT operator+(const OffsetT& offset) const
82     {
83         return OffsetT(x_ + offset.x_, y_ + offset.y_);
84     }
85 
86     OffsetT operator-(const OffsetT& offset) const
87     {
88         return OffsetT(x_ - offset.x_, y_ - offset.y_);
89     }
90 
91     OffsetT operator*(double value) const
92     {
93         return OffsetT(x_ * value, y_ * value);
94     }
95 
96     OffsetT operator/(double value) const
97     {
98         if (NearZero(value)) {
99             return {};
100         }
101         return OffsetT(x_ / value, y_ / value);
102     }
103 
104     OffsetT& operator+=(const OffsetT& offset)
105     {
106         x_ += offset.x_;
107         y_ += offset.y_;
108         return *this;
109     }
110 
111     OffsetT& operator-=(const OffsetT& offset)
112     {
113         x_ -= offset.x_;
114         y_ -= offset.y_;
115         return *this;
116     }
117 
118     bool operator==(const OffsetT& offset) const
119     {
120         return NearEqual(x_, offset.x_) && NearEqual(y_, offset.y_);
121     }
122 
123     bool operator!=(const OffsetT& offset) const
124     {
125         return !operator==(offset);
126     }
127 
NonNegative()128     bool NonNegative() const
129     {
130         return NonNegative(x_) && NonNegative(y_);
131     }
132 
NonOffset()133     bool NonOffset() const
134     {
135         return NearZero(x_) && NearZero(y_);
136     }
137 
ToString()138     std::string ToString() const
139     {
140         static const int32_t precision = 2;
141         std::stringstream ss;
142         ss << "Offset (" << std::fixed << std::setprecision(precision) << x_ << ", " << y_ << ")";
143         std::string output = ss.str();
144         return output;
145     }
146 
147 private:
148     T x_ { 0 };
149     T y_ { 0 };
150 };
151 
152 using OffsetF = OffsetT<float>;
153 } // namespace OHOS::Ace::NG
154 
155 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_NG_OFFSET_T_H
156