123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_OFFSET_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_OFFSET_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include <cmath>
2023b3eb3cSopenharmony_ci#include <limits>
2123b3eb3cSopenharmony_ci
2223b3eb3cSopenharmony_ci#include "base/geometry/size.h"
2323b3eb3cSopenharmony_ci#include "base/utils/utils.h"
2423b3eb3cSopenharmony_ci#include "core/components/common/properties/animation_option.h"
2523b3eb3cSopenharmony_ci
2623b3eb3cSopenharmony_cinamespace OHOS::Ace {
2723b3eb3cSopenharmony_ciclass Offset {
2823b3eb3cSopenharmony_cipublic:
2923b3eb3cSopenharmony_ci    Offset() = default;
3023b3eb3cSopenharmony_ci    ~Offset() = default;
3123b3eb3cSopenharmony_ci    Offset(double deltaX, double deltaY) : deltaX_(deltaX), deltaY_(deltaY) {}
3223b3eb3cSopenharmony_ci
3323b3eb3cSopenharmony_ci    void Reset()
3423b3eb3cSopenharmony_ci    {
3523b3eb3cSopenharmony_ci        deltaX_ = 0.0;
3623b3eb3cSopenharmony_ci        deltaY_ = 0.0;
3723b3eb3cSopenharmony_ci        deltaXAnimationOption_ = AnimationOption();
3823b3eb3cSopenharmony_ci        deltaYAnimationOption_ = AnimationOption();
3923b3eb3cSopenharmony_ci    }
4023b3eb3cSopenharmony_ci
4123b3eb3cSopenharmony_ci    static Offset Zero()
4223b3eb3cSopenharmony_ci    {
4323b3eb3cSopenharmony_ci        return Offset();
4423b3eb3cSopenharmony_ci    }
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ci    static Offset ErrorOffset()
4723b3eb3cSopenharmony_ci    {
4823b3eb3cSopenharmony_ci        return Offset((std::numeric_limits<double>::max)(), (std::numeric_limits<double>::max)());
4923b3eb3cSopenharmony_ci    }
5023b3eb3cSopenharmony_ci
5123b3eb3cSopenharmony_ci    bool IsZero() const
5223b3eb3cSopenharmony_ci    {
5323b3eb3cSopenharmony_ci        return operator==(Offset());
5423b3eb3cSopenharmony_ci    }
5523b3eb3cSopenharmony_ci
5623b3eb3cSopenharmony_ci    bool IsErrorOffset() const
5723b3eb3cSopenharmony_ci    {
5823b3eb3cSopenharmony_ci        return operator==(ErrorOffset());
5923b3eb3cSopenharmony_ci    }
6023b3eb3cSopenharmony_ci
6123b3eb3cSopenharmony_ci    double GetX() const
6223b3eb3cSopenharmony_ci    {
6323b3eb3cSopenharmony_ci        return deltaX_;
6423b3eb3cSopenharmony_ci    }
6523b3eb3cSopenharmony_ci
6623b3eb3cSopenharmony_ci    double GetY() const
6723b3eb3cSopenharmony_ci    {
6823b3eb3cSopenharmony_ci        return deltaY_;
6923b3eb3cSopenharmony_ci    }
7023b3eb3cSopenharmony_ci
7123b3eb3cSopenharmony_ci    void SetX(double x, const AnimationOption& option = AnimationOption())
7223b3eb3cSopenharmony_ci    {
7323b3eb3cSopenharmony_ci        deltaX_ = x;
7423b3eb3cSopenharmony_ci        deltaXAnimationOption_ = option;
7523b3eb3cSopenharmony_ci    }
7623b3eb3cSopenharmony_ci
7723b3eb3cSopenharmony_ci    void SetY(double y, const AnimationOption& option = AnimationOption())
7823b3eb3cSopenharmony_ci    {
7923b3eb3cSopenharmony_ci        deltaY_ = y;
8023b3eb3cSopenharmony_ci        deltaYAnimationOption_ = option;
8123b3eb3cSopenharmony_ci    }
8223b3eb3cSopenharmony_ci
8323b3eb3cSopenharmony_ci    AnimationOption GetXAnimationOption() const
8423b3eb3cSopenharmony_ci    {
8523b3eb3cSopenharmony_ci        return deltaXAnimationOption_;
8623b3eb3cSopenharmony_ci    }
8723b3eb3cSopenharmony_ci
8823b3eb3cSopenharmony_ci    AnimationOption GetYAnimationOption() const
8923b3eb3cSopenharmony_ci    {
9023b3eb3cSopenharmony_ci        return deltaYAnimationOption_;
9123b3eb3cSopenharmony_ci    }
9223b3eb3cSopenharmony_ci
9323b3eb3cSopenharmony_ci    double GetDistance() const
9423b3eb3cSopenharmony_ci    {
9523b3eb3cSopenharmony_ci        return std::sqrt((deltaX_ * deltaX_) + (deltaY_ * deltaY_));
9623b3eb3cSopenharmony_ci    }
9723b3eb3cSopenharmony_ci
9823b3eb3cSopenharmony_ci    Offset operator+(const Offset& offset) const
9923b3eb3cSopenharmony_ci    {
10023b3eb3cSopenharmony_ci        return Offset(deltaX_ + offset.deltaX_, deltaY_ + offset.deltaY_);
10123b3eb3cSopenharmony_ci    }
10223b3eb3cSopenharmony_ci
10323b3eb3cSopenharmony_ci    Offset operator+(const Size& size) const
10423b3eb3cSopenharmony_ci    {
10523b3eb3cSopenharmony_ci        return Offset(deltaX_ + size.Width(), deltaY_ + size.Height());
10623b3eb3cSopenharmony_ci    }
10723b3eb3cSopenharmony_ci
10823b3eb3cSopenharmony_ci    Offset operator-(const Offset& offset) const
10923b3eb3cSopenharmony_ci    {
11023b3eb3cSopenharmony_ci        return Offset(deltaX_ - offset.deltaX_, deltaY_ - offset.deltaY_);
11123b3eb3cSopenharmony_ci    }
11223b3eb3cSopenharmony_ci
11323b3eb3cSopenharmony_ci    Offset operator-(const Size& size) const
11423b3eb3cSopenharmony_ci    {
11523b3eb3cSopenharmony_ci        return Offset(deltaX_ - size.Width(), deltaY_ - size.Height());
11623b3eb3cSopenharmony_ci    }
11723b3eb3cSopenharmony_ci
11823b3eb3cSopenharmony_ci    Offset operator*(double value) const
11923b3eb3cSopenharmony_ci    {
12023b3eb3cSopenharmony_ci        return Offset(deltaX_ * value, deltaY_ * value);
12123b3eb3cSopenharmony_ci    }
12223b3eb3cSopenharmony_ci
12323b3eb3cSopenharmony_ci    Offset operator/(double value) const
12423b3eb3cSopenharmony_ci    {
12523b3eb3cSopenharmony_ci        if (NearZero(value)) {
12623b3eb3cSopenharmony_ci            return ErrorOffset();
12723b3eb3cSopenharmony_ci        }
12823b3eb3cSopenharmony_ci        return Offset(deltaX_ / value, deltaY_ / value);
12923b3eb3cSopenharmony_ci    }
13023b3eb3cSopenharmony_ci
13123b3eb3cSopenharmony_ci    Offset& operator+=(const Offset& offset)
13223b3eb3cSopenharmony_ci    {
13323b3eb3cSopenharmony_ci        deltaX_ += offset.deltaX_;
13423b3eb3cSopenharmony_ci        deltaY_ += offset.deltaY_;
13523b3eb3cSopenharmony_ci        return *this;
13623b3eb3cSopenharmony_ci    }
13723b3eb3cSopenharmony_ci
13823b3eb3cSopenharmony_ci    Offset& operator-=(const Offset& offset)
13923b3eb3cSopenharmony_ci    {
14023b3eb3cSopenharmony_ci        deltaX_ -= offset.deltaX_;
14123b3eb3cSopenharmony_ci        deltaY_ -= offset.deltaY_;
14223b3eb3cSopenharmony_ci        return *this;
14323b3eb3cSopenharmony_ci    }
14423b3eb3cSopenharmony_ci
14523b3eb3cSopenharmony_ci    bool operator==(const Offset& offset) const
14623b3eb3cSopenharmony_ci    {
14723b3eb3cSopenharmony_ci        return NearEqual(deltaX_, offset.deltaX_) && NearEqual(deltaY_, offset.deltaY_);
14823b3eb3cSopenharmony_ci    }
14923b3eb3cSopenharmony_ci
15023b3eb3cSopenharmony_ci    bool operator!=(const Offset& offset) const
15123b3eb3cSopenharmony_ci    {
15223b3eb3cSopenharmony_ci        return !operator==(offset);
15323b3eb3cSopenharmony_ci    }
15423b3eb3cSopenharmony_ci
15523b3eb3cSopenharmony_ci    std::string ToString() const
15623b3eb3cSopenharmony_ci    {
15723b3eb3cSopenharmony_ci        std::stringstream ss;
15823b3eb3cSopenharmony_ci        ss << "Offset (" << std::fixed << std::setprecision(2) << deltaX_ << ", " << deltaY_ << ")";
15923b3eb3cSopenharmony_ci        std::string output = ss.str();
16023b3eb3cSopenharmony_ci        return output;
16123b3eb3cSopenharmony_ci    }
16223b3eb3cSopenharmony_ci
16323b3eb3cSopenharmony_ci    bool IsPositiveOffset() const
16423b3eb3cSopenharmony_ci    {
16523b3eb3cSopenharmony_ci        return deltaX_ >= 0 && deltaY_ >= 0;
16623b3eb3cSopenharmony_ci    }
16723b3eb3cSopenharmony_ci
16823b3eb3cSopenharmony_ciprivate:
16923b3eb3cSopenharmony_ci    double deltaX_ = 0.0;
17023b3eb3cSopenharmony_ci    double deltaY_ = 0.0;
17123b3eb3cSopenharmony_ci    AnimationOption deltaXAnimationOption_;
17223b3eb3cSopenharmony_ci    AnimationOption deltaYAnimationOption_;
17323b3eb3cSopenharmony_ci};
17423b3eb3cSopenharmony_ci} // namespace OHOS::Ace
17523b3eb3cSopenharmony_ci
17623b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_OFFSET_H
177