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
Evaluate()35 void PropertyAnimation::Evaluate()
36 {
37 const PropertyAnimationState::EvaluationData data { currentValue_, from_, to_, META_ACCESS_PROPERTY_VALUE(Progress),
38 META_ACCESS_PROPERTY_VALUE(Curve) };
39 if (GetState().EvaluateValue(data) == AnyReturn::SUCCESS) {
40 NotifyChanged();
41 }
42 }
43
OnPropertyChanged(const TargetProperty & property,const IStackProperty::Ptr & previous)44 void PropertyAnimation::OnPropertyChanged(const TargetProperty& property, const IStackProperty::Ptr& previous)
45 {
46 auto me = GetSelf<IModifier>();
47 if (previous) {
48 previous->RemoveModifier(me);
49 }
50 if (property) {
51 property.stack->AddModifier(me);
52 }
53 }
54
ProcessOnGet(IAny & value)55 EvaluationResult PropertyAnimation::ProcessOnGet(IAny& value)
56 {
57 if (currentValue_ && GetState().IsRunning()) {
58 if (auto result = value.CopyFrom(*currentValue_)) {
59 return result == AnyReturn::NOTHING_TO_DO ? EvaluationResult::EVAL_CONTINUE
60 : EvaluationResult::EVAL_VALUE_CHANGED;
61 }
62 }
63
64 return EvaluationResult::EVAL_CONTINUE;
65 }
66
ProcessOnSet(IAny & value,const IAny & current)67 EvaluationResult PropertyAnimation::ProcessOnSet(IAny& value, const IAny& current)
68 {
69 if (META_ACCESS_PROPERTY_VALUE(Valid) || META_ACCESS_PROPERTY_VALUE(Enabled)) {
70 from_ = current.Clone();
71 to_ = value.Clone();
72 currentValue_ = current.Clone();
73 auto& state = GetState();
74 // Start animating
75 state.Start();
76 // Temp shared_ptr<IAny> which does not delete our source IAny
77 auto tmp = IAny::Ptr(&value, [](auto*) {});
78 // Evaluate the animation with progress=1.f and pass the value down the property stack
79 PropertyAnimationState::EvaluationData data { tmp, from_, to_, 1.f, META_ACCESS_PROPERTY_VALUE(Curve) };
80 state.EvaluateValue(data);
81 }
82 return EvaluationResult::EVAL_CONTINUE;
83 }
84
GetParams()85 AnimationState::AnimationStateParams PropertyAnimation::GetParams()
86 {
87 AnimationState::AnimationStateParams params;
88 params.owner = GetSelf<IAnimation>();
89 params.runningProperty = META_ACCESS_PROPERTY(Running);
90 params.progressProperty = META_ACCESS_PROPERTY(Progress);
91 params.totalDuration = META_ACCESS_PROPERTY(TotalDuration);
92 return params;
93 }
94
95 } // namespace Internal
96 META_END_NAMESPACE()
97