123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 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_ANIMATABLE_DIMENSION_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_ANIMATABLE_DIMENSION_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include "base/geometry/dimension.h"
2023b3eb3cSopenharmony_ci#include "base/geometry/calc_dimension.h"
2123b3eb3cSopenharmony_ci#include "core/animation/animator.h"
2223b3eb3cSopenharmony_ci#include "core/animation/curve_animation.h"
2323b3eb3cSopenharmony_ci#include "core/components/common/properties/animation_option.h"
2423b3eb3cSopenharmony_ci
2523b3eb3cSopenharmony_cinamespace OHOS::Ace {
2623b3eb3cSopenharmony_ci
2723b3eb3cSopenharmony_ciusing RenderNodeAnimationCallback = std::function<void()>;
2823b3eb3cSopenharmony_ci
2923b3eb3cSopenharmony_ci/*
3023b3eb3cSopenharmony_ci * AnimatableDimension is a Dimension with AnimationOption and Animator.
3123b3eb3cSopenharmony_ci */
3223b3eb3cSopenharmony_ciclass ACE_FORCE_EXPORT AnimatableDimension : public CalcDimension {
3323b3eb3cSopenharmony_cipublic:
3423b3eb3cSopenharmony_ci    AnimatableDimension() = default;
3523b3eb3cSopenharmony_ci    ~AnimatableDimension() = default;
3623b3eb3cSopenharmony_ci
3723b3eb3cSopenharmony_ci    explicit AnimatableDimension(
3823b3eb3cSopenharmony_ci        double value, DimensionUnit unit = DimensionUnit::PX, const AnimationOption& option = AnimationOption())
3923b3eb3cSopenharmony_ci        : CalcDimension(value, unit), animationOption_(option)
4023b3eb3cSopenharmony_ci    {}
4123b3eb3cSopenharmony_ci
4223b3eb3cSopenharmony_ci    explicit AnimatableDimension(
4323b3eb3cSopenharmony_ci        const std::string& value, DimensionUnit unit = DimensionUnit::CALC,
4423b3eb3cSopenharmony_ci        const AnimationOption& option = AnimationOption())
4523b3eb3cSopenharmony_ci        : CalcDimension(value, unit), animationOption_(option)
4623b3eb3cSopenharmony_ci    {}
4723b3eb3cSopenharmony_ci
4823b3eb3cSopenharmony_ci    explicit AnimatableDimension(const Dimension& dimension, const AnimationOption& option = AnimationOption())
4923b3eb3cSopenharmony_ci        : CalcDimension(dimension), animationOption_(option)
5023b3eb3cSopenharmony_ci    {}
5123b3eb3cSopenharmony_ci
5223b3eb3cSopenharmony_ci    explicit AnimatableDimension(const CalcDimension& dimension, const AnimationOption& option = AnimationOption())
5323b3eb3cSopenharmony_ci        : CalcDimension(dimension), animationOption_(option)
5423b3eb3cSopenharmony_ci    {}
5523b3eb3cSopenharmony_ci
5623b3eb3cSopenharmony_ci    void SetContextAndCallback(const WeakPtr<PipelineBase>& context, const RenderNodeAnimationCallback& callback)
5723b3eb3cSopenharmony_ci    {
5823b3eb3cSopenharmony_ci        context_ = context;
5923b3eb3cSopenharmony_ci        animationCallback_ = callback;
6023b3eb3cSopenharmony_ci    }
6123b3eb3cSopenharmony_ci
6223b3eb3cSopenharmony_ci    void SetContextAndCallbackAfterFirstAssign(
6323b3eb3cSopenharmony_ci        const WeakPtr<PipelineBase>& context, const RenderNodeAnimationCallback& callback)
6423b3eb3cSopenharmony_ci    {
6523b3eb3cSopenharmony_ci        context_ = context;
6623b3eb3cSopenharmony_ci        animationCallback_ = callback;
6723b3eb3cSopenharmony_ci        isFirstAssign_ = false;
6823b3eb3cSopenharmony_ci    }
6923b3eb3cSopenharmony_ci
7023b3eb3cSopenharmony_ci    const AnimationOption& GetAnimationOption() const
7123b3eb3cSopenharmony_ci    {
7223b3eb3cSopenharmony_ci        return animationOption_;
7323b3eb3cSopenharmony_ci    }
7423b3eb3cSopenharmony_ci
7523b3eb3cSopenharmony_ci    void SetAnimationOption(const AnimationOption& option)
7623b3eb3cSopenharmony_ci    {
7723b3eb3cSopenharmony_ci        animationOption_ = option;
7823b3eb3cSopenharmony_ci    }
7923b3eb3cSopenharmony_ci
8023b3eb3cSopenharmony_ci    void SetAnimationStopCallback(const RenderNodeAnimationCallback& callback)
8123b3eb3cSopenharmony_ci    {
8223b3eb3cSopenharmony_ci        stopCallback_ = callback;
8323b3eb3cSopenharmony_ci    }
8423b3eb3cSopenharmony_ci
8523b3eb3cSopenharmony_ci    Animator::Status GetAnimationStatus() const
8623b3eb3cSopenharmony_ci    {
8723b3eb3cSopenharmony_ci        if (!animationController_) {
8823b3eb3cSopenharmony_ci            return Animator::Status::IDLE;
8923b3eb3cSopenharmony_ci        }
9023b3eb3cSopenharmony_ci        return animationController_->GetStatus();
9123b3eb3cSopenharmony_ci    }
9223b3eb3cSopenharmony_ci
9323b3eb3cSopenharmony_ci    void SetEvaluator(const RefPtr<Evaluator<double>>& evaluator)
9423b3eb3cSopenharmony_ci    {
9523b3eb3cSopenharmony_ci        evaluator_ = evaluator;
9623b3eb3cSopenharmony_ci    }
9723b3eb3cSopenharmony_ci
9823b3eb3cSopenharmony_ci    AnimatableDimension& operator=(const Dimension& newDimension);
9923b3eb3cSopenharmony_ci
10023b3eb3cSopenharmony_ci    AnimatableDimension& operator=(const CalcDimension& newDimension);
10123b3eb3cSopenharmony_ci
10223b3eb3cSopenharmony_ci    AnimatableDimension& operator=(const AnimatableDimension& newDimension);
10323b3eb3cSopenharmony_ci
10423b3eb3cSopenharmony_ci    void MoveTo(double target);
10523b3eb3cSopenharmony_ci
10623b3eb3cSopenharmony_ciprivate:
10723b3eb3cSopenharmony_ci    void AnimateTo(double endValue);
10823b3eb3cSopenharmony_ci    void ResetController();
10923b3eb3cSopenharmony_ci    void OnAnimationCallback(double value);
11023b3eb3cSopenharmony_ci    void ResetAnimatableDimension();
11123b3eb3cSopenharmony_ci
11223b3eb3cSopenharmony_ciprivate:
11323b3eb3cSopenharmony_ci    bool isFirstAssign_ = true;
11423b3eb3cSopenharmony_ci    AnimationOption animationOption_;
11523b3eb3cSopenharmony_ci    RefPtr<Animator> animationController_;
11623b3eb3cSopenharmony_ci    WeakPtr<PipelineBase> context_;
11723b3eb3cSopenharmony_ci    RenderNodeAnimationCallback animationCallback_;
11823b3eb3cSopenharmony_ci    RenderNodeAnimationCallback stopCallback_;
11923b3eb3cSopenharmony_ci    RefPtr<Evaluator<double>> evaluator_;
12023b3eb3cSopenharmony_ci};
12123b3eb3cSopenharmony_ci
12223b3eb3cSopenharmony_ci} // namespace OHOS::Ace
12323b3eb3cSopenharmony_ci
12423b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_ANIMATABLE_DIMENSION_H
125