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_ANIMATABLE_DIMENSION_H
17#define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_ANIMATABLE_DIMENSION_H
18
19#include "base/geometry/dimension.h"
20#include "base/geometry/calc_dimension.h"
21#include "core/animation/animator.h"
22#include "core/animation/curve_animation.h"
23#include "core/components/common/properties/animation_option.h"
24
25namespace OHOS::Ace {
26
27using RenderNodeAnimationCallback = std::function<void()>;
28
29/*
30 * AnimatableDimension is a Dimension with AnimationOption and Animator.
31 */
32class ACE_FORCE_EXPORT AnimatableDimension : public CalcDimension {
33public:
34    AnimatableDimension() = default;
35    ~AnimatableDimension() = default;
36
37    explicit AnimatableDimension(
38        double value, DimensionUnit unit = DimensionUnit::PX, const AnimationOption& option = AnimationOption())
39        : CalcDimension(value, unit), animationOption_(option)
40    {}
41
42    explicit AnimatableDimension(
43        const std::string& value, DimensionUnit unit = DimensionUnit::CALC,
44        const AnimationOption& option = AnimationOption())
45        : CalcDimension(value, unit), animationOption_(option)
46    {}
47
48    explicit AnimatableDimension(const Dimension& dimension, const AnimationOption& option = AnimationOption())
49        : CalcDimension(dimension), animationOption_(option)
50    {}
51
52    explicit AnimatableDimension(const CalcDimension& dimension, const AnimationOption& option = AnimationOption())
53        : CalcDimension(dimension), animationOption_(option)
54    {}
55
56    void SetContextAndCallback(const WeakPtr<PipelineBase>& context, const RenderNodeAnimationCallback& callback)
57    {
58        context_ = context;
59        animationCallback_ = callback;
60    }
61
62    void SetContextAndCallbackAfterFirstAssign(
63        const WeakPtr<PipelineBase>& context, const RenderNodeAnimationCallback& callback)
64    {
65        context_ = context;
66        animationCallback_ = callback;
67        isFirstAssign_ = false;
68    }
69
70    const AnimationOption& GetAnimationOption() const
71    {
72        return animationOption_;
73    }
74
75    void SetAnimationOption(const AnimationOption& option)
76    {
77        animationOption_ = option;
78    }
79
80    void SetAnimationStopCallback(const RenderNodeAnimationCallback& callback)
81    {
82        stopCallback_ = callback;
83    }
84
85    Animator::Status GetAnimationStatus() const
86    {
87        if (!animationController_) {
88            return Animator::Status::IDLE;
89        }
90        return animationController_->GetStatus();
91    }
92
93    void SetEvaluator(const RefPtr<Evaluator<double>>& evaluator)
94    {
95        evaluator_ = evaluator;
96    }
97
98    AnimatableDimension& operator=(const Dimension& newDimension);
99
100    AnimatableDimension& operator=(const CalcDimension& newDimension);
101
102    AnimatableDimension& operator=(const AnimatableDimension& newDimension);
103
104    void MoveTo(double target);
105
106private:
107    void AnimateTo(double endValue);
108    void ResetController();
109    void OnAnimationCallback(double value);
110    void ResetAnimatableDimension();
111
112private:
113    bool isFirstAssign_ = true;
114    AnimationOption animationOption_;
115    RefPtr<Animator> animationController_;
116    WeakPtr<PipelineBase> context_;
117    RenderNodeAnimationCallback animationCallback_;
118    RenderNodeAnimationCallback stopCallback_;
119    RefPtr<Evaluator<double>> evaluator_;
120};
121
122} // namespace OHOS::Ace
123
124#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_ANIMATABLE_DIMENSION_H
125