18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#include "keyframe_animation.h"
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <meta/api/util.h>
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
218bf80f4bSopenharmony_ci
228bf80f4bSopenharmony_cinamespace Internal {
238bf80f4bSopenharmony_ci
248bf80f4bSopenharmony_ciAnimationState::AnimationStateParams KeyframeAnimation::GetParams()
258bf80f4bSopenharmony_ci{
268bf80f4bSopenharmony_ci    AnimationState::AnimationStateParams params;
278bf80f4bSopenharmony_ci    params.owner = GetSelf<IAnimation>();
288bf80f4bSopenharmony_ci    params.runningProperty = META_ACCESS_PROPERTY(Running);
298bf80f4bSopenharmony_ci    params.progressProperty = META_ACCESS_PROPERTY(Progress);
308bf80f4bSopenharmony_ci    params.totalDuration = META_ACCESS_PROPERTY(TotalDuration);
318bf80f4bSopenharmony_ci    return params;
328bf80f4bSopenharmony_ci}
338bf80f4bSopenharmony_ci
348bf80f4bSopenharmony_civoid KeyframeAnimation::Initialize()
358bf80f4bSopenharmony_ci{
368bf80f4bSopenharmony_ci    GetState().ResetClock();
378bf80f4bSopenharmony_ci    currentValue_.reset();
388bf80f4bSopenharmony_ci}
398bf80f4bSopenharmony_ci
408bf80f4bSopenharmony_civoid KeyframeAnimation::OnAnimationStateChanged(const AnimationStateChangedInfo& info)
418bf80f4bSopenharmony_ci{
428bf80f4bSopenharmony_ci    using AnimationTargetState = IAnimationInternal::AnimationTargetState;
438bf80f4bSopenharmony_ci    if (auto p = GetTargetProperty()) {
448bf80f4bSopenharmony_ci        switch (info.state) {
458bf80f4bSopenharmony_ci            case AnimationTargetState::FINISHED:
468bf80f4bSopenharmony_ci                [[fallthrough]];
478bf80f4bSopenharmony_ci            case AnimationTargetState::STOPPED:
488bf80f4bSopenharmony_ci                if (currentValue_) {
498bf80f4bSopenharmony_ci                    // Evaluate current value
508bf80f4bSopenharmony_ci                    Evaluate();
518bf80f4bSopenharmony_ci                    // Then set the correct keyframe value to the underlying property
528bf80f4bSopenharmony_ci                    p.property->SetValue(*currentValue_);
538bf80f4bSopenharmony_ci                }
548bf80f4bSopenharmony_ci                break;
558bf80f4bSopenharmony_ci            case AnimationTargetState::RUNNING:
568bf80f4bSopenharmony_ci                currentValue_ = p.property->GetValue().Clone(false);
578bf80f4bSopenharmony_ci                // Evaluate current value
588bf80f4bSopenharmony_ci                Evaluate();
598bf80f4bSopenharmony_ci                break;
608bf80f4bSopenharmony_ci            default:
618bf80f4bSopenharmony_ci                break;
628bf80f4bSopenharmony_ci        }
638bf80f4bSopenharmony_ci    }
648bf80f4bSopenharmony_ci}
658bf80f4bSopenharmony_ci
668bf80f4bSopenharmony_civoid KeyframeAnimation::Start()
678bf80f4bSopenharmony_ci{
688bf80f4bSopenharmony_ci    GetState().Start();
698bf80f4bSopenharmony_ci}
708bf80f4bSopenharmony_ci
718bf80f4bSopenharmony_civoid KeyframeAnimation::Evaluate()
728bf80f4bSopenharmony_ci{
738bf80f4bSopenharmony_ci    const PropertyAnimationState::EvaluationData data { currentValue_, META_ACCESS_PROPERTY_VALUE(From),
748bf80f4bSopenharmony_ci        META_ACCESS_PROPERTY_VALUE(To), META_ACCESS_PROPERTY_VALUE(Progress), META_ACCESS_PROPERTY_VALUE(Curve) };
758bf80f4bSopenharmony_ci    if (GetState().EvaluateValue(data) == AnyReturn::SUCCESS) {
768bf80f4bSopenharmony_ci        NotifyChanged();
778bf80f4bSopenharmony_ci    }
788bf80f4bSopenharmony_ci}
798bf80f4bSopenharmony_ci
808bf80f4bSopenharmony_civoid KeyframeAnimation::Stop()
818bf80f4bSopenharmony_ci{
828bf80f4bSopenharmony_ci    GetState().Stop();
838bf80f4bSopenharmony_ci}
848bf80f4bSopenharmony_ci
858bf80f4bSopenharmony_civoid KeyframeAnimation::Pause()
868bf80f4bSopenharmony_ci{
878bf80f4bSopenharmony_ci    GetState().Pause();
888bf80f4bSopenharmony_ci}
898bf80f4bSopenharmony_ci
908bf80f4bSopenharmony_civoid KeyframeAnimation::Restart()
918bf80f4bSopenharmony_ci{
928bf80f4bSopenharmony_ci    GetState().Restart();
938bf80f4bSopenharmony_ci}
948bf80f4bSopenharmony_ci
958bf80f4bSopenharmony_civoid KeyframeAnimation::Finish()
968bf80f4bSopenharmony_ci{
978bf80f4bSopenharmony_ci    GetState().Finish();
988bf80f4bSopenharmony_ci}
998bf80f4bSopenharmony_ci
1008bf80f4bSopenharmony_civoid KeyframeAnimation::Seek(float position)
1018bf80f4bSopenharmony_ci{
1028bf80f4bSopenharmony_ci    GetState().Seek(position);
1038bf80f4bSopenharmony_ci}
1048bf80f4bSopenharmony_ci
1058bf80f4bSopenharmony_civoid KeyframeAnimation::OnPropertyChanged(const TargetProperty& property, const IStackProperty::Ptr& previous)
1068bf80f4bSopenharmony_ci{
1078bf80f4bSopenharmony_ci    auto me = GetSelf<IModifier>();
1088bf80f4bSopenharmony_ci    if (previous) {
1098bf80f4bSopenharmony_ci        previous->RemoveModifier(me);
1108bf80f4bSopenharmony_ci        if (auto p = interface_cast<IProperty>(previous)) {
1118bf80f4bSopenharmony_ci            p->SetValue(*currentValue_);
1128bf80f4bSopenharmony_ci        }
1138bf80f4bSopenharmony_ci        Stop();
1148bf80f4bSopenharmony_ci    }
1158bf80f4bSopenharmony_ci    if (property) {
1168bf80f4bSopenharmony_ci        property.stack->AddModifier(me);
1178bf80f4bSopenharmony_ci    }
1188bf80f4bSopenharmony_ci    Initialize();
1198bf80f4bSopenharmony_ci}
1208bf80f4bSopenharmony_ci
1218bf80f4bSopenharmony_ciEvaluationResult KeyframeAnimation::ProcessOnGet(IAny& value)
1228bf80f4bSopenharmony_ci{
1238bf80f4bSopenharmony_ci    if (currentValue_ && (GetState().IsRunning() || GetState().IsPaused())) {
1248bf80f4bSopenharmony_ci        if (auto result = value.CopyFrom(*currentValue_)) {
1258bf80f4bSopenharmony_ci            return result == AnyReturn::NOTHING_TO_DO ? EvaluationResult::EVAL_CONTINUE
1268bf80f4bSopenharmony_ci                                                      : EvaluationResult::EVAL_VALUE_CHANGED;
1278bf80f4bSopenharmony_ci        }
1288bf80f4bSopenharmony_ci    }
1298bf80f4bSopenharmony_ci    return EvaluationResult::EVAL_CONTINUE;
1308bf80f4bSopenharmony_ci}
1318bf80f4bSopenharmony_ci
1328bf80f4bSopenharmony_ci} // namespace Internal
1338bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
134