• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_PROPERTY_ANIMATION_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_PROPERTY_ANIMATION_H
18 
19 #include <memory>
20 
21 #include "animation/rs_animation.h"
22 #include "animation/rs_property_accessors.h"
23 #include "common/rs_macros.h"
24 #include "ui/rs_node.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 template<typename T>
29 class RS_EXPORT RSPropertyAnimation : public RSAnimation {
30 public:
31     RSPropertyAnimation() = delete;
32     virtual ~RSPropertyAnimation() = default;
33 
34 protected:
RSPropertyAnimation(const RSAnimatableProperty & property)35     RSPropertyAnimation(const RSAnimatableProperty& property) : property_(property)
36     {
37         propertyAccess_ = std::static_pointer_cast<RSPropertyAccessors<T>>(
38             RSBasePropertyAccessors::GetAccessor(property));
39         InitAdditiveMode();
40     }
41 
SetAdditive(bool isAdditive)42     void SetAdditive(bool isAdditive)
43     {
44         isAdditive_ = true;
45     }
46 
GetAdditive()47     bool GetAdditive() const
48     {
49         return isAdditive_;
50     }
51 
GetOriginValue()52     auto GetOriginValue() const
53     {
54         return originValue_;
55     }
56 
SetPropertyValue(const T & value)57     void SetPropertyValue(const T& value)
58     {
59         auto target = GetTarget().lock();
60         if (target == nullptr || propertyAccess_->setter_ == nullptr) {
61             return;
62         }
63 
64         (target->stagingProperties_.*propertyAccess_->setter_)(value);
65     }
66 
GetPropertyValue()67     auto GetPropertyValue() const
68     {
69         auto target = GetTarget().lock();
70         if (target == nullptr || propertyAccess_->getter_ == nullptr) {
71             return startValue_;
72         }
73 
74         return (target->stagingProperties_.*propertyAccess_->getter_)();
75     }
76 
GetProperty()77     RSAnimatableProperty GetProperty() const override
78     {
79         return property_;
80     }
81 
OnStart()82     void OnStart() override
83     {
84         if (!hasOriginValue_) {
85             originValue_ = GetPropertyValue();
86             hasOriginValue_ = true;
87         }
88         InitInterpolationValue();
89     }
90 
SetOriginValue(const T & originValue)91     void SetOriginValue(const T& originValue)
92     {
93         if (!hasOriginValue_) {
94             originValue_ = originValue;
95             hasOriginValue_ = true;
96         }
97     }
98 
InitInterpolationValue()99     virtual void InitInterpolationValue()
100     {
101         if (isDelta_) {
102             startValue_ = originValue_;
103             endValue_ = originValue_ + byValue_;
104         } else {
105             byValue_ = endValue_ - startValue_;
106         }
107     }
108 
OnUpdateStagingValue(bool isFirstStart)109     void OnUpdateStagingValue(bool isFirstStart) override
110     {
111         auto startValue = startValue_;
112         auto endValue = endValue_;
113         if (!GetDirection()) {
114             std::swap(startValue, endValue);
115         }
116         auto byValue = endValue - startValue;
117         auto targetValue = endValue;
118         if (isFirstStart) {
119             if (GetAutoReverse() && GetRepeatCount() % 2 == 0) {
120                 targetValue = startValue;
121             } else {
122                 targetValue = endValue;
123             }
124         } else {
125             auto currentValue = GetPropertyValue();
126             if (GetAutoReverse() && GetRepeatCount() % 2 == 0) {
127                 targetValue = IsReversed() ? currentValue + byValue : currentValue - byValue;
128             } else {
129                 targetValue = IsReversed() ? currentValue - byValue : currentValue + byValue;
130             }
131         }
132 
133         SetPropertyValue(targetValue);
134     }
135 
136     T byValue_ {};
137     T startValue_ {};
138     T endValue_ {};
139     T originValue_ {};
140     bool isAdditive_ { true };
141     bool isDelta_ { false };
142     bool hasOriginValue_ { false };
143 
144 private:
InitAdditiveMode()145     void InitAdditiveMode()
146     {
147         switch (GetProperty()) {
148             case RSAnimatableProperty::ROTATION_3D:
149                 SetAdditive(false);
150                 break;
151             default:
152                 break;
153         }
154     }
155 
156     RSAnimatableProperty property_ { RSAnimatableProperty::INVALID };
157     std::shared_ptr<RSPropertyAccessors<T>> propertyAccess_;
158 };
159 } // namespace Rosen
160 } // namespace OHOS
161 
162 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_PROPERTY_ANIMATION_H
163