• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                     PropertyLock lock { p.property };
53                     lock->SetValueAny(*currentValue_);
54                 }
55                 break;
56             case AnimationTargetState::RUNNING: {
57                 PropertyLock lock { p.property };
58                 currentValue_ = lock->GetValueAny().Clone(false);
59             }
60                 // Evaluate current value
61                 Evaluate();
62                 break;
63             default:
64                 break;
65         }
66     }
67 }
68 
Start()69 void KeyframeAnimation::Start()
70 {
71     GetState().Start();
72 }
73 
Evaluate()74 void KeyframeAnimation::Evaluate()
75 {
76     const PropertyAnimationState::EvaluationData data { currentValue_, META_ACCESS_PROPERTY_VALUE(From),
77         META_ACCESS_PROPERTY_VALUE(To), META_ACCESS_PROPERTY_VALUE(Progress), META_ACCESS_PROPERTY_VALUE(Curve) };
78     if (GetState().EvaluateValue(data) == AnyReturn::SUCCESS) {
79         NotifyChanged();
80         if (auto prop = GetTargetProperty()) {
81             PropertyLock lock { prop.property };
82             prop.stack->EvaluateAndStore();
83         }
84     }
85 }
86 
Stop()87 void KeyframeAnimation::Stop()
88 {
89     GetState().Stop();
90 }
91 
Pause()92 void KeyframeAnimation::Pause()
93 {
94     GetState().Pause();
95 }
96 
Restart()97 void KeyframeAnimation::Restart()
98 {
99     GetState().Restart();
100 }
101 
Finish()102 void KeyframeAnimation::Finish()
103 {
104     GetState().Finish();
105 }
106 
Seek(float position)107 void KeyframeAnimation::Seek(float position)
108 {
109     GetState().Seek(position);
110 }
111 
OnPropertyChanged(const TargetProperty & property,const IStackProperty::Ptr & previous)112 void KeyframeAnimation::OnPropertyChanged(const TargetProperty& property, const IStackProperty::Ptr& previous)
113 {
114     auto me = GetSelf<IModifier>();
115     if (previous) {
116         previous->RemoveModifier(me);
117         if (auto p = interface_cast<IProperty>(previous)) {
118             p->SetValue(*currentValue_);
119         }
120         Stop();
121     }
122     if (property) {
123         property.stack->AddModifier(me);
124     }
125     Initialize();
126 }
127 
ProcessOnGet(IAny & value)128 EvaluationResult KeyframeAnimation::ProcessOnGet(IAny& value)
129 {
130     if (currentValue_ && (GetState().IsRunning() || GetState().IsPaused())) {
131         if (auto result = value.CopyFrom(*currentValue_)) {
132             return result == AnyReturn::NOTHING_TO_DO ? EvaluationResult::EVAL_CONTINUE
133                                                       : EvaluationResult::EVAL_VALUE_CHANGED;
134         }
135     }
136     return EvaluationResult::EVAL_CONTINUE;
137 }
138 
139 } // namespace Internal
140 META_END_NAMESPACE()
141