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