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 "property_animation.h"
17
18 #include <meta/api/make_callback.h>
19 #include <meta/interface/intf_any.h>
20
21 META_BEGIN_NAMESPACE()
22
23 namespace Internal {
24
Build(const IMetadata::Ptr & meta)25 bool PropertyAnimation::Build(const IMetadata::Ptr& meta)
26 {
27 return Super::Build(meta);
28 }
29
Step(const IClock::ConstPtr & clock)30 void PropertyAnimation::Step(const IClock::ConstPtr& clock)
31 {
32 Super::Step(clock);
33 }
34
OnAnimationStateChanged(const IAnimationInternal::AnimationStateChangedInfo & info)35 void PropertyAnimation::OnAnimationStateChanged(const IAnimationInternal::AnimationStateChangedInfo& info)
36 {
37 if (auto p = GetTargetProperty()) {
38 switch (info.state) {
39 case AnimationTargetState::FINISHED:
40 [[fallthrough]];
41 case AnimationTargetState::STOPPED:
42 // Evaluate current value
43 Evaluate();
44 break;
45 case AnimationTargetState::RUNNING:
46 break;
47 default:
48 break;
49 }
50 }
51 }
52
Evaluate()53 void PropertyAnimation::Evaluate()
54 {
55 const PropertyAnimationState::EvaluationData data { currentValue_, from_, to_, META_ACCESS_PROPERTY_VALUE(Progress),
56 META_ACCESS_PROPERTY_VALUE(Curve) };
57 if (GetState().EvaluateValue(data) == AnyReturn::SUCCESS) {
58 evalChanged_ = true;
59 NotifyChanged();
60 if (auto prop = GetTargetProperty()) {
61 PropertyLock lock { prop.property };
62 prop.stack->EvaluateAndStore();
63 }
64 }
65 }
66
OnPropertyChanged(const TargetProperty & property,const IStackProperty::Ptr & previous)67 void PropertyAnimation::OnPropertyChanged(const TargetProperty& property, const IStackProperty::Ptr& previous)
68 {
69 auto me = GetSelf<IModifier>();
70 if (previous) {
71 previous->RemoveModifier(me);
72 }
73 if (property) {
74 property.stack->AddModifier(me);
75 }
76 }
77
ProcessOnGet(IAny & value)78 EvaluationResult PropertyAnimation::ProcessOnGet(IAny& value)
79 {
80 if (currentValue_ && (evalChanged_ || GetState().IsRunning())) {
81 evalChanged_ = false;
82 if (auto result = value.CopyFrom(*currentValue_)) {
83 return result == AnyReturn::NOTHING_TO_DO ? EvaluationResult::EVAL_CONTINUE
84 : EvaluationResult::EVAL_VALUE_CHANGED;
85 }
86 }
87
88 return EvaluationResult::EVAL_CONTINUE;
89 }
90
ProcessOnSet(IAny & value,const IAny & current)91 EvaluationResult PropertyAnimation::ProcessOnSet(IAny& value, const IAny& current)
92 {
93 if (META_ACCESS_PROPERTY_VALUE(Valid) || META_ACCESS_PROPERTY_VALUE(Enabled)) {
94 from_ = current.Clone();
95 to_ = value.Clone();
96 currentValue_ = current.Clone();
97 auto& state = GetState();
98 // Start animating
99 state.Start();
100 // Propagate initial value
101 value.CopyFrom(*from_);
102 }
103 return EvaluationResult::EVAL_CONTINUE;
104 }
105
GetParams()106 AnimationState::AnimationStateParams PropertyAnimation::GetParams()
107 {
108 AnimationState::AnimationStateParams params;
109 params.owner = GetSelf<IAnimation>();
110 params.runningProperty = META_ACCESS_PROPERTY(Running);
111 params.progressProperty = META_ACCESS_PROPERTY(Progress);
112 params.totalDuration = META_ACCESS_PROPERTY(TotalDuration);
113 return params;
114 }
115
116 } // namespace Internal
117 META_END_NAMESPACE()
118