1 /*
2  * Copyright (c) 2024 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 #include "keyframe_animation.h"
17 
18 #include <meta/api/util.h>
19 
20 META_BEGIN_NAMESPACE()
21 
22 namespace Internal {
23 
GetParams()24 AnimationState::AnimationStateParams KeyframeAnimation::GetParams()
25 {
26     AnimationState::AnimationStateParams params;
27     params.owner = GetSelf<IAnimation>();
28     params.runningProperty = META_ACCESS_PROPERTY(Running);
29     params.progressProperty = META_ACCESS_PROPERTY(Progress);
30     params.totalDuration = META_ACCESS_PROPERTY(TotalDuration);
31     return params;
32 }
33 
Initialize()34 void KeyframeAnimation::Initialize()
35 {
36     GetState().ResetClock();
37     currentValue_.reset();
38 }
39 
OnAnimationStateChanged(const AnimationStateChangedInfo & info)40 void KeyframeAnimation::OnAnimationStateChanged(const AnimationStateChangedInfo& info)
41 {
42     using AnimationTargetState = IAnimationInternal::AnimationTargetState;
43     if (auto p = GetTargetProperty()) {
44         switch (info.state) {
45             case AnimationTargetState::FINISHED:
46                 [[fallthrough]];
47             case AnimationTargetState::STOPPED:
48                 if (currentValue_) {
49                     // Evaluate current value
50                     Evaluate();
51                     // Then set the correct keyframe value to the underlying property
52                     p.property->SetValue(*currentValue_);
53                 }
54                 break;
55             case AnimationTargetState::RUNNING:
56                 currentValue_ = p.property->GetValue().Clone(false);
57                 // Evaluate current value
58                 Evaluate();
59                 break;
60             default:
61                 break;
62         }
63     }
64 }
65 
Start()66 void KeyframeAnimation::Start()
67 {
68     GetState().Start();
69 }
70 
Evaluate()71 void KeyframeAnimation::Evaluate()
72 {
73     const PropertyAnimationState::EvaluationData data { currentValue_, META_ACCESS_PROPERTY_VALUE(From),
74         META_ACCESS_PROPERTY_VALUE(To), META_ACCESS_PROPERTY_VALUE(Progress), META_ACCESS_PROPERTY_VALUE(Curve) };
75     if (GetState().EvaluateValue(data) == AnyReturn::SUCCESS) {
76         NotifyChanged();
77     }
78 }
79 
Stop()80 void KeyframeAnimation::Stop()
81 {
82     GetState().Stop();
83 }
84 
Pause()85 void KeyframeAnimation::Pause()
86 {
87     GetState().Pause();
88 }
89 
Restart()90 void KeyframeAnimation::Restart()
91 {
92     GetState().Restart();
93 }
94 
Finish()95 void KeyframeAnimation::Finish()
96 {
97     GetState().Finish();
98 }
99 
Seek(float position)100 void KeyframeAnimation::Seek(float position)
101 {
102     GetState().Seek(position);
103 }
104 
OnPropertyChanged(const TargetProperty & property,const IStackProperty::Ptr & previous)105 void KeyframeAnimation::OnPropertyChanged(const TargetProperty& property, const IStackProperty::Ptr& previous)
106 {
107     auto me = GetSelf<IModifier>();
108     if (previous) {
109         previous->RemoveModifier(me);
110         if (auto p = interface_cast<IProperty>(previous)) {
111             p->SetValue(*currentValue_);
112         }
113         Stop();
114     }
115     if (property) {
116         property.stack->AddModifier(me);
117     }
118     Initialize();
119 }
120 
ProcessOnGet(IAny & value)121 EvaluationResult KeyframeAnimation::ProcessOnGet(IAny& value)
122 {
123     if (currentValue_ && (GetState().IsRunning() || GetState().IsPaused())) {
124         if (auto result = value.CopyFrom(*currentValue_)) {
125             return result == AnyReturn::NOTHING_TO_DO ? EvaluationResult::EVAL_CONTINUE
126                                                       : EvaluationResult::EVAL_VALUE_CHANGED;
127         }
128     }
129     return EvaluationResult::EVAL_CONTINUE;
130 }
131 
132 } // namespace Internal
133 META_END_NAMESPACE()
134