1 /*
2  * Copyright (c) 2020-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 OHOS_ACELITE_TRANSITION_IMPL_H
17 #define OHOS_ACELITE_TRANSITION_IMPL_H
18 #include <ctime>
19 
20 #include "animator.h"
21 #include "js_fwk_common.h"
22 #include "non_copyable.h"
23 
24 namespace OHOS {
25 namespace ACELite {
26 enum EasingType : uint8_t { LINEAR, EASE_IN, EASE_OUT, EASE_IN_OUT, EEND };
27 
28 enum OptionsFill : uint8_t { FNONE, FORWARDS };
29 
30 struct TransitionParams {
31     /* animation execution time (ms), default is 0, means no animation effect. */
32     int32_t during;
33     /* the time interval (ms) between requesting an animation operation and executing it. */
34     int32_t delay;
35     /* animation execution times, default is 1. */
36     int8_t iterations;
37     /* specify whether to return to the initial state after the animation finishes,
38     default is 'none', means to return to the initial state; 'forwards' means not return to. */
39     OptionsFill fill;
40     /* specify the speed curve of the animation */
41     EasingType easing;
42 
43     /* transformType include "translateX/translateY", you can only specify one of these once a time. */
44     char* transformType;
45     /* transform from source (px) to target (px) */
46     int16_t transform_from;
47     int16_t transform_to;
48 
49     int16_t height_from;
50     int16_t height_to;
51     int16_t width_from;
52     int16_t width_to;
53     int16_t opacity_from;
54     int16_t opacity_to;
55     uint32_t background_color_from;
56     uint32_t background_color_to;
57 
TransitionParamsTransitionParams58     TransitionParams()
59         : during(0),
60           delay(0),
61           iterations(1),
62           fill(OptionsFill::FNONE),
63           easing(EasingType::LINEAR),
64           transformType(nullptr),
65           transform_from(0),
66           transform_to(0),
67           height_from(-1),
68           height_to(-1),
69           width_from(-1),
70           width_to(-1),
71           opacity_from(-1),
72           opacity_to(-1),
73           background_color_from(RGB_COLOR_VALUE_MAX),
74           background_color_to(RGB_COLOR_VALUE_MAX) {}
75 };
76 
77 struct ViewStatus {
78     int16_t x;
79     int16_t y;
80     int16_t height;
81     int16_t width;
82     int16_t rectOpacity;
83     int16_t imageOpacity;
84     int16_t lineOpacity;
85     int16_t textOpacity;
86     Rect oriRect;
87     ColorType background_color;
88 
89     ACE_DISALLOW_COPY_AND_MOVE(ViewStatus);
ViewStatusViewStatus90     ViewStatus() : x(0), y(0), height(0), width(0), rectOpacity(0), imageOpacity(0), lineOpacity(0), textOpacity(0),
91                    oriRect(), background_color() {}
92 };
93 
94 enum TransformType : uint8_t {
95     TRANSLATE_X,
96     TRANSLATE_Y,
97     ROTATE, // rotate only support image
98     NONE
99 };
100 
101 enum GeneralType : uint8_t {
102     IS_HEIGHT_TRANSITION_SET,
103     IS_WIDTH_TRANSITION_SET,
104     IS_BACKGROUND_COLOR_TRANSITION_SET,
105     IS_OPACITY_TRANSITION_SET,
106     END
107 };
108 
109 enum TransitionType : uint8_t {
110     TTRANSLATE_X,
111     TTRANSLATE_Y,
112     TROTATE,
113     HEIGHT,
114     WIDTH,
115     BACKGROUND_COLOR,
116     OPACITY
117 };
118 
119 /**
120  * @brief: animation callback implement.
121  *         supported parameters can refer to struct TransitionParams
122  */
123 class TransitionImpl final : public AnimatorCallback {
124 public:
125     /**
126      * @brief: Construct function
127      *
128      * @param: params animation transition effect
129      * @param: view target view which run the animation
130      */
131     ACE_DISALLOW_COPY_AND_MOVE(TransitionImpl);
TransitionImpl(TransitionParams & params,UIView * view)132     TransitionImpl(TransitionParams& params, UIView* view)
133         : view_(view),
134           params_(params),
135           oriIteration_(1),
136           animator_(nullptr),
137           xSrc_(0), ySrc_(0), rotateSrc_(0), widthSrc_(0), heightSrc_(0), opacitySrc_(0), bgcolorTimeSrc_(0)
138     {
139     }
140     TransitionImpl() = delete;
~TransitionImpl()141     ~TransitionImpl()
142     {
143         if (animator_ != nullptr) {
144             delete (animator_);
145             animator_ = nullptr;
146         }
147     }
148 
149     /**
150      * @brief: must call this to do initialization after create TransitionImpl instance
151      */
152     void Init();
153     /**
154      * brief: start the animation
155      */
156     void Start();
157     static int8_t GetNumIterations(const char* iterations);
158     static bool IsEndWith(const char* src, const char *end);
159     void Stop() const;
160 
161 private:
162     void Callback(UIView* view) override;
163     bool RepeatAnimator();
164     void ResetRepeatParam();
165     void RecordViewStatus();
166     void RecoveryViewStatus(Rect invalidatedAreaBefore) const;
167     void InitTransitionParams();
168     void InitTransitionParamsStyle();
169     void InitTransitionParamsTransform();
170     void InitTransitionParamsEasing();
171     void GetRGB(const uint32_t color, uint8_t& r, uint8_t& g, uint8_t& b) const;
172     int16_t GetNextFrameValue(int16_t from, int16_t to, int32_t elapsedTime) const;
173     void SetTransformSrcPosition();
174     void RotateAroundCenterPoint(int16_t angle);
175     void Perform(int32_t elapsedTime);
176     void PerformTransitionBgColorLinear(int32_t elapsedTime);
177     void PerformTransition(int16_t from,
178                            int16_t to,
179                            TransitionType transitionType,
180                            int16_t& updateAttrValue,
181                            int32_t elapsedTime);
182 
183     UIView* view_;
184     TransitionParams& params_;
185     int8_t oriIteration_;
186     Animator* animator_;
187     Vector2<float> pivot_;
188     int16_t xSrc_;
189     int16_t ySrc_;
190     int16_t rotateSrc_;
191     int16_t widthSrc_;
192     int16_t heightSrc_;
193     int16_t opacitySrc_;
194     ViewStatus viewStatus_;
195     bool isTransformSrcSet_ = false;
196     bool timeArrivaled_ = false;
197     bool easingType_[EasingType::EEND] = {0};
198     bool isTransitionSet_[GeneralType::END] = {0};
199     OptionsFill fill_ = OptionsFill::FNONE;
200     TransformType transformType_ = TransformType::NONE;
201 
202     /* used for background-color */
203     uint8_t rSrc_ = 0; // used to record the last time red value
204     uint8_t gSrc_ = 0;
205     uint8_t bSrc_ = 0;
206     uint8_t rTo_ = 0;
207     uint8_t gTo_ = 0;
208     uint8_t bTo_ = 0;
209     int8_t count_ = 1;             // used to record the current number of times of updating the bg-color
210     int8_t steps_ = 1;             // total target number of times of updating the bg-color (during_/INTERVAL)
211     int32_t bgcolorTimeSrc_ = 0;       // used to record the last time bg-color
212     const static int8_t ITERATIONS_INFINITY = -1;
213     const static int16_t INTERVAL = 150; // update the bg-color every INTERVAL (ms)
214 };
215 } // namespace ACELite
216 } // namespace OHOS
217 
218 #endif // OHOS_ACELITE_TRANSITION_IMPL_H
219