1 /*
2 * Copyright (c) 2022 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 "animator/easing_equation.h"
17 #include "common/screen.h"
18 #include "components/root_view.h"
19 #include "components/ui_label.h"
20 #include "components/ui_label_button.h"
21 #include "components/ui_scroll_view.h"
22 #include "core/render_manager.h"
23 #include "gfx_utils/sys_info.h"
24 #include "graphic_config.h"
25 #include "hal_tick.h"
26 #include "hilog/log.h"
27 #include "ui_test.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #undef LOG_TAG
32 #define LOG_TAG "AnimatorDemo"
33
34 using namespace OHOS;
35
36 namespace
37 {
38 static constexpr uint16_t TITLE_LABEL_DEFAULT_HEIGHT = 29;
39 static constexpr uint16_t FONT_DEFAULT_SIZE = 20;
40 static constexpr uint16_t VIEW_DISTANCE_TO_LEFT_SIDE = 48;
41 static constexpr uint16_t VIEW_DISTANCE_TO_TOP_SIDE = 48;
42 static constexpr uint16_t VIEW_DISTANCE_TO_LEFT_SIDE2 = 24;
43 static constexpr uint16_t TEXT_DISTANCE_TO_LEFT_SIDE = 48;
44 static constexpr uint16_t TEXT_DISTANCE_TO_TOP_SIDE = 20;
45 static constexpr uint16_t HALF_OPA_OPAQUE = OPA_OPAQUE / 2;
46 static constexpr uint16_t VIEW_STYLE_BORDER_WIDTH = 2;
47 static constexpr uint16_t VIEW_STYLE_BORDER_RADIUS = 8;
48 static constexpr uint16_t BUTTON_LABEL_SIZE = 16;
49 static constexpr uint8_t BUTTON_STYLE_BORDER_RADIUS_VALUE = 20;
50 static constexpr uint32_t BUTTON_STYLE_BACKGROUND_COLOR_VALUE = 0xFF333333;
51 static constexpr uint32_t BUTTON_STYLE_BACKGROUND_COLOR_PRESS = 0xFF2D2D2D;
52 static constexpr int16_t BACK_BUTTON_HEIGHT = 64;
53 static constexpr uint16_t BUTTON_WIDHT1 = 80;
54 static constexpr uint16_t BUTTON_HEIGHT1 = 40;
55 static constexpr uint16_t BUTTON_WIDHT2 = 120;
56 static constexpr uint16_t BUTTON_HEIGHT2 = 40;
57 static constexpr uint16_t BUTTON_WIDHT3 = 150;
58 static constexpr uint16_t BUTTON_HEIGHT3 = 60;
59 const int16_t LABEL_HEIGHT = 29;
60 const int16_t BUTTON_WIDTH = BUTTON_WIDHT3;
61 const int16_t BUTTON_HEIGHT = BUTTON_HEIGHT3;
62 const int16_t DELTA_X_COORDINATE = 5;
63 const int16_t DELTA_Y_COORDINATE = 12;
64 } // namespace
65
DeleteChildren(UIView * view)66 static void DeleteChildren(UIView *view)
67 {
68 if (view == nullptr) {
69 return;
70 }
71 while (view) {
72 UIView *tempView = view;
73 view = view->GetNextSibling();
74 if (tempView->IsViewGroup()) {
75 DeleteChildren(static_cast<UIViewGroup *>(tempView)->GetChildrenHead());
76 }
77 if (tempView->GetParent()) {
78 static_cast<UIViewGroup *>(tempView->GetParent())->Remove(tempView);
79 }
80 delete tempView;
81 }
82 }
83
84 class AnimatorDemoCallback : public AnimatorCallback
85 {
86 public:
AnimatorDemoCallback(UIView * uiView,int16_t startPos,int16_t endPos)87 AnimatorDemoCallback(UIView *uiView, int16_t startPos, int16_t endPos)
88 : startPos_(startPos),
89 endPos_(endPos),
90 easingFunc_(EasingEquation::LinearEaseNone),
91 animator_(new Animator(this, uiView, 1000, false))
92 {
93 }
94
~AnimatorDemoCallback()95 virtual ~AnimatorDemoCallback()
96 {
97 delete animator_;
98 }
99
GetAnimator() const100 Animator *GetAnimator() const
101 {
102 return animator_;
103 }
104
SetEasingFunc(EasingFunc easingFunc)105 void SetEasingFunc(EasingFunc easingFunc)
106 {
107 easingFunc_ = easingFunc;
108 }
109
Callback(UIView * view)110 virtual void Callback(UIView *view)
111 {
112 if (view == nullptr) {
113 return;
114 }
115 int16_t pos = easingFunc_(startPos_, endPos_, animator_->GetRunTime(), animator_->GetTime());
116 Rect invalidatedArea = view->GetRect();
117 view->SetPosition(pos, view->GetY());
118 invalidatedArea.Join(invalidatedArea, view->GetRect());
119 view->InvalidateRect(invalidatedArea);
120 }
121
122 protected:
123 int16_t startPos_;
124 int16_t endPos_;
125 EasingFunc easingFunc_;
126 Animator *animator_;
127 };
128
129 class AnimatorDemo : public UIView::OnClickListener, public SysInfo::OnFPSChangedListener
130 {
131 public:
GetInstance()132 static AnimatorDemo *GetInstance()
133 {
134 static AnimatorDemo instance;
135 return &instance;
136 }
137
138 void SetUp();
139 UIView *GetTestView();
140
141 private:
AnimatorDemo()142 AnimatorDemo() {}
~AnimatorDemo()143 ~AnimatorDemo()
144 {
145 DeleteChildren(container_);
146 container_ = nullptr;
147 label_ = nullptr;
148 button_ = nullptr;
149 scroll_ = nullptr;
150 delete callback_;
151 callback_ = nullptr;
152 uiViewGroupFrame_ = nullptr;
153 };
154
155 void SetUpButton(UILabelButton *btn, const char *title, int16_t x, int16_t y);
156 void SetUpLabel(const char *title, int16_t x, int16_t y);
157
158 bool OnClick(UIView &view, const ClickEvent &event) override;
159 void OnFPSChanged(float newFPS) override;
160
161 void UIKit_Animator_Test_BackEasing_001();
162
GetTitleLabel(const char * titleName)163 UILabel *GetTitleLabel(const char *titleName)
164 {
165 if (titleName == nullptr) {
166 return nullptr;
167 }
168 UILabel *label = new UILabel();
169 if (label == nullptr) {
170 return nullptr;
171 }
172 // 2: half of screen width
173 label->SetPosition(0, 0, Screen::GetInstance().GetWidth() / 2, TITLE_LABEL_DEFAULT_HEIGHT);
174 label->SetText(titleName);
175 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
176 label_->SetStyle(STYLE_TEXT_COLOR, Color::Red().full);
177 return label;
178 }
179
180 UIScrollView *container_ = nullptr;
181 UIScrollView *scroll_ = nullptr;
182 UILabel *label_ = nullptr;
183 UIButton *button_ = nullptr;
184 AnimatorDemoCallback *callback_ = nullptr;
185 Animator *animator_ = nullptr;
186 UIViewGroup *uiViewGroupFrame_ = nullptr;
187
188 UILabelButton *backOvershootBtn_ = nullptr;
189 UILabelButton *backEaseInBtn_ = nullptr;
190 UILabelButton *backEaseOutBtn_ = nullptr;
191 UILabelButton *backEaseInOutBtn_ = nullptr;
192
193 int16_t positionX_ = 0;
194 int16_t positionY_ = 0;
195 };
196
SetUp()197 void AnimatorDemo::SetUp()
198 {
199 if (container_ == nullptr) {
200 container_ = new UIScrollView();
201 container_->SetPosition(25, 25, Screen::GetInstance().GetWidth() - 50, Screen::GetInstance().GetHeight() - 50);
202
203 uiViewGroupFrame_ = new UIViewGroup();
204 uiViewGroupFrame_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
205 uiViewGroupFrame_->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
206 uiViewGroupFrame_->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
207 uiViewGroupFrame_->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
208 uiViewGroupFrame_->SetStyle(STYLE_BACKGROUND_OPA, 0);
209 uiViewGroupFrame_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE);
210 container_->Add(uiViewGroupFrame_);
211
212 label_ = new UILabel();
213 container_->Add(label_);
214 label_->SetPosition(100, 20, 264, 48);
215 label_->SetText("AnimatorDemo");
216 label_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
217
218 button_ = new UIButton();
219 button_->SetPosition(0, 0, 100, 100);
220 button_->SetStyle(STYLE_BORDER_RADIUS, 90);
221
222 callback_ = new AnimatorDemoCallback(button_, 0, 320);
223 animator_ = callback_->GetAnimator();
224 uiViewGroupFrame_->Add(button_);
225 uiViewGroupFrame_->Resize(container_->GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE,
226 button_->GetHeight());
227 }
228 if (scroll_ == nullptr) {
229 scroll_ = new UIScrollView();
230 // 20: increase y-coordinate
231 uint16_t y = uiViewGroupFrame_->GetY() + uiViewGroupFrame_->GetHeight() + 20;
232 scroll_->SetPosition(0, y, Screen::GetInstance().GetWidth(),
233 Screen::GetInstance().GetHeight() - y - 64); // 64: decrease y-coordinate
234 container_->Add(scroll_);
235 positionX_ = 0;
236 }
237 RenderManager::GetInstance().RegisterFPSChangedListener(this);
238 this->SetFPSCalculateType(SysInfo::FPS_CT_FIXED_TIME);
239 }
240
GetTestView()241 UIView *AnimatorDemo::GetTestView()
242 {
243 UIKit_Animator_Test_BackEasing_001();
244 return container_;
245 }
246
SetUpLabel(const char * title,int16_t x,int16_t y)247 void AnimatorDemo::SetUpLabel(const char *title, int16_t x, int16_t y)
248 {
249 UILabel *label = GetTitleLabel(title);
250 scroll_->Add(label);
251 label->SetPosition(x, y, 288, LABEL_HEIGHT); // 288: label width
252 }
253
SetUpButton(UILabelButton * btn,const char * title,int16_t x,int16_t y)254 void AnimatorDemo::SetUpButton(UILabelButton *btn, const char *title, int16_t x, int16_t y)
255 {
256 if (btn == nullptr) {
257 return;
258 }
259 scroll_->Add(btn);
260 btn->SetPosition(x, y, BUTTON_WIDHT3, BUTTON_HEIGHT3);
261 btn->SetText(title);
262 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
263 btn->SetOnClickListener(this);
264 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
265 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
266 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
267 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
268 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
269 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
270 scroll_->Invalidate();
271 }
272
UIKit_Animator_Test_BackEasing_001()273 void AnimatorDemo::UIKit_Animator_Test_BackEasing_001()
274 {
275 backOvershootBtn_ = new UILabelButton();
276 backEaseInBtn_ = new UILabelButton();
277 backEaseOutBtn_ = new UILabelButton();
278 backEaseInOutBtn_ = new UILabelButton();
279 positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
280 positionY_ = 0;
281 SetUpLabel("back动画效果: ", positionX_, positionY_);
282 positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
283 SetUpButton(backOvershootBtn_, "overshoot+", positionX_, positionY_);
284 positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
285 SetUpButton(backEaseInBtn_, "BackEaseIn", positionX_, positionY_);
286 positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
287 positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
288 SetUpButton(backEaseOutBtn_, "BackEaseOut", positionX_, positionY_);
289 positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
290 SetUpButton(backEaseInOutBtn_, "BackEaseInOut", positionX_, positionY_);
291 }
292
OnClick(UIView & view,const ClickEvent & event)293 bool AnimatorDemo::OnClick(UIView &view, const ClickEvent &event)
294 {
295 static double overshoot = 1.7;
296 if (&view == backOvershootBtn_) {
297 overshoot += 1;
298 EasingEquation::SetBackOvershoot(overshoot);
299 } else if (&view == backEaseInBtn_) {
300 callback_->SetEasingFunc(EasingEquation::BackEaseIn);
301 } else if (&view == backEaseOutBtn_) {
302 callback_->SetEasingFunc(EasingEquation::BackEaseOut);
303 } else if (&view == backEaseInOutBtn_) {
304 callback_->SetEasingFunc(EasingEquation::BackEaseInOut);
305 }
306 animator_->Start();
307 container_->Invalidate();
308 return true;
309 }
310
OnFPSChanged(float newFPS)311 void AnimatorDemo::OnFPSChanged(float newFPS)
312 {
313 static float lastFPS = 0.0f;
314 if (abs(newFPS - lastFPS) < 1) {
315 return;
316 }
317 lastFPS = newFPS;
318 char buf[32] = {0};
319 snprintf(buf, sizeof(buf), "动画效果 %.0f fps", newFPS);
320 label_->SetText(reinterpret_cast<char *>(buf));
321 label_->Invalidate();
322 }
323
324 /**
325 * UIScaleRotateDemo
326 */
327 class UIScaleRotateDemo : public AnimatorCallback
328 {
329 public:
GetInstance()330 static UIScaleRotateDemo *GetInstance()
331 {
332 static UIScaleRotateDemo instance;
333 return &instance;
334 }
335
SetUp()336 void SetUp()
337 {
338 container_ = new UIScrollView();
339 container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
340 container_->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
341 label_ = new UILabel();
342 label_->SetText("label");
343 label_->SetPosition(100, 100, 100, 50);
344 container_->Add(label_);
345
346 button_ = new UILabelButton();
347 button_->SetText("button");
348 button_->SetPosition(100, 200, 100, 50);
349 container_->Add(button_);
350
351 animator_ = new Animator(this, nullptr, 50, true);
352 animator_->Start();
353 lastRun_ = HALTick::GetInstance().GetTime();
354 }
355
GetTestView()356 UIView *GetTestView()
357 {
358 return container_;
359 }
360
361 private:
UIScaleRotateDemo()362 UIScaleRotateDemo() {}
~UIScaleRotateDemo()363 ~UIScaleRotateDemo()
364 {
365 DeleteChildren(container_);
366 container_ = nullptr;
367 label_ = nullptr;
368 button_ = nullptr;
369 if (animator_) {
370 delete animator_;
371 animator_ = nullptr;
372 }
373 }
374
Callback(UIView * view)375 void Callback(UIView *view) override
376 {
377 angleValue_++;
378
379 if (scaleValue_.x_ < 0.5f) {
380 scaleStep_ = 0.02f;
381 } else if (scaleValue_.x_ > 1.5f) {
382 scaleStep_ = -0.02f;
383 }
384 scaleValue_.x_ += scaleStep_;
385 scaleValue_.y_ += scaleStep_;
386 label_->Rotate(angleValue_, VIEW_CENTER1);
387 label_->Scale(scaleValue_, VIEW_CENTER1);
388 button_->Rotate(angleValue_, VIEW_CENTER2);
389 button_->Scale(scaleValue_, VIEW_CENTER2);
390
391 frame_cnt_++;
392 if (HALTick::GetInstance().GetElapseTime(lastRun_) >= 1000) {
393 HILOG_DEBUG(HILOG_MODULE_APP, "%u fps\n", frame_cnt_);
394 lastRun_ = HALTick::GetInstance().GetTime();
395 frame_cnt_ = 0;
396 }
397 }
398
399 private:
400 UIViewGroup *container_ = nullptr;
401 UILabel *label_ = nullptr;
402 UILabelButton *button_ = nullptr;
403 Animator *animator_ = nullptr;
404 const Vector2<float> VIEW_CENTER1 = {100.0f, 100.0f};
405 const Vector2<float> VIEW_CENTER2 = {100.0f, 200.0f};
406 int16_t angleValue_ = 0;
407 Vector2<float> scaleValue_ = {1.0f, 1.0f};
408 float scaleStep_ = 0.02f;
409 uint32_t lastRun_ = 0;
410 uint32_t frame_cnt_ = 0;
411 };
412
AnimatorDemoStart(void)413 void AnimatorDemoStart(void)
414 {
415 RootView *rootView_ = RootView::GetInstance();
416 rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
417 rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Green().full);
418 AnimatorDemo *view = AnimatorDemo::GetInstance();
419 view->SetUp();
420 rootView_->Add(view->GetTestView());
421 rootView_->Invalidate();
422 }