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-(const Size& size) const 115 { 116 return Offset(deltaX_ - size.Width(), deltaY_ - size.Height()); 117 } 118 119 Offset operator*(double value) const 120 { 121 return Offset(deltaX_ * value, deltaY_ * value); 122 } 123 124 Offset operator/(double value) const 125 { 126 if (NearZero(value)) { 127 return ErrorOffset(); 128 } 129 return Offset(deltaX_ / value, deltaY_ / value); 130 } 131 132 Offset& operator+=(const Offset& offset) 133 { 134 deltaX_ += offset.deltaX_; 135 deltaY_ += offset.deltaY_; 136 return *this; 137 } 138 139 Offset& operator-=(const Offset& offset) 140 { 141 deltaX_ -= offset.deltaX_; 142 deltaY_ -= offset.deltaY_; 143 return *this; 144 } 145 146 bool operator==(const Offset& offset) const 147 { 148 return NearEqual(deltaX_, offset.deltaX_) && NearEqual(deltaY_, offset.deltaY_); 149 } 150 151 bool operator!=(const Offset& offset) const 152 { 153 return !operator==(offset); 154 } 155 ToString()156 std::string ToString() const 157 { 158 std::stringstream ss; 159 ss << "Offset (" << std::fixed << std::setprecision(2) << deltaX_ << ", " << deltaY_ << ")"; 160 std::string output = ss.str(); 161 return output; 162 } 163 IsPositiveOffset()164 bool IsPositiveOffset() const 165 { 166 return deltaX_ >= 0 && deltaY_ >= 0; 167 } 168 169 private: 170 double deltaX_ = 0.0; 171 double deltaY_ = 0.0; 172 AnimationOption deltaXAnimationOption_; 173 AnimationOption deltaYAnimationOption_; 174 }; 175 } // namespace OHOS::Ace 176 177 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_OFFSET_H 178