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_BASE_GEOMETRY_DIMENSION_OFFSET_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_DIMENSION_OFFSET_H 18 19 #include <cmath> 20 #include <limits> 21 #include <optional> 22 23 #include "base/geometry/dimension.h" 24 #include "base/geometry/ng/offset_t.h" 25 #include "base/geometry/offset.h" 26 27 namespace OHOS::Ace { 28 29 class DimensionOffset { 30 public: 31 DimensionOffset() = default; 32 ~DimensionOffset() = default; DimensionOffset(const Dimension & deltaX,const Dimension & deltaY)33 DimensionOffset(const Dimension& deltaX, const Dimension& deltaY) : deltaX_(deltaX), deltaY_(deltaY) {} DimensionOffset(const Offset & offset)34 DimensionOffset(const Offset& offset) : deltaX_(Dimension(offset.GetX(), DimensionUnit::PX)), 35 deltaY_(Dimension(offset.GetY(), DimensionUnit::PX)) {} 36 DimensionOffset(const NG::OffsetF & offset)37 explicit DimensionOffset(const NG::OffsetF& offset) 38 : deltaX_(Dimension(offset.GetX())), deltaY_(Dimension(offset.GetY())) 39 {} 40 GetX()41 const Dimension& GetX() const 42 { 43 return deltaX_; 44 } 45 GetY()46 const Dimension& GetY() const 47 { 48 return deltaY_; 49 } 50 GetZ()51 const std::optional<Dimension>& GetZ() const 52 { 53 return deltaZ_; 54 } 55 SetX(Dimension & x)56 void SetX(Dimension& x) 57 { 58 deltaX_ = x; 59 } 60 SetY(Dimension & y)61 void SetY(Dimension& y) 62 { 63 deltaY_ = y; 64 } 65 SetZ(const Dimension & z)66 void SetZ(const Dimension& z) 67 { 68 deltaZ_ = z; 69 } 70 71 DimensionOffset operator+(const DimensionOffset& dimensionOffset) const 72 { 73 return DimensionOffset(deltaX_ + dimensionOffset.deltaX_, deltaY_ + dimensionOffset.deltaY_); 74 } 75 76 DimensionOffset operator-(const DimensionOffset& dimensionOffset) const 77 { 78 return DimensionOffset(deltaX_ - dimensionOffset.deltaX_, deltaY_ - dimensionOffset.deltaY_); 79 } 80 81 DimensionOffset operator*(double value) const 82 { 83 return DimensionOffset(deltaX_ * value, deltaY_ * value); 84 } 85 86 bool operator==(const DimensionOffset& dimensionOffset) const 87 { 88 return deltaX_ == dimensionOffset.deltaX_ && deltaY_ == dimensionOffset.deltaY_ && deltaZ_ == deltaZ_; 89 } 90 91 private: 92 Dimension deltaX_; 93 Dimension deltaY_; 94 std::optional<Dimension> deltaZ_; 95 }; 96 97 } // namespace OHOS::Ace 98 99 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_DIMENSION_OFFSET_H 100