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 #include "ui_test_texture_mapper.h"
17 #include "common/screen.h"
18 #include "test_resource_config.h"
19 
20 namespace OHOS {
21 namespace {
22 static int16_t g_rotateStart = 0;      // 0: the start angle of rotation
23 static int16_t g_rotateEnd = 360;      // 360: the end angle of rotation
24 static float g_scaleStart = 1;         // 1: the start ratio
25 static float g_scaleEnd = 0.5;         // 0.5: the end ratio
26 static uint16_t g_durationTime = 2000; // 2000: the duration for this animator
27 static uint16_t g_delayTime = 0;
28 static EasingFunc g_easingFunc = EasingEquation::CubicEaseInOut;
29 static Point g_pivot = {58, 58}; // 58: the x-coordinate,; 58: the y-coordinate
30 } // namespace
31 
SetUp()32 void UITestTextureMapper::SetUp()
33 {
34     if (container_ == nullptr) {
35         container_ = new UIScrollView();
36         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
37         UIViewGroup* uiViewGroup = new UIViewGroup();
38         uiViewGroup->SetPosition(0, 0, 320, 390); // 320: width; 390: height
39         container_->Add(uiViewGroup);
40         UILabel* label = new UILabel();
41         uiViewGroup->Add(label);
42         // 288: x-coordinate; 48: y-coordinate
43         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
44         label->SetText("UITextureMapper效果");
45         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
46         UIViewGroup* uiViewGroupFrame = new UIViewGroup();
47         uiViewGroup->Add(uiViewGroupFrame);
48         // 288: x-coordinate; 336: y-coordinate
49         uiViewGroupFrame->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
50         uiViewGroupFrame->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
51         uiViewGroupFrame->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
52         uiViewGroupFrame->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
53         uiViewGroupFrame->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
54         uiViewGroupFrame->SetStyle(STYLE_BACKGROUND_OPA, 0);
55         textureMapper_ = new UITextureMapper();
56         textureMapper_->SetPosition(200, 50, 200, 200); // 200:position x; 50: position y; 200: width; 200: height
57         textureMapper_->SetSrc(RED_IMAGE_PATH);
58         textureMapper_->SetRotateStart(0);
59         textureMapper_->SetRotateEnd(360);     // 360: the end angle of rotation
60         textureMapper_->SetScaleStart(1);      // 1: the start ratio
61         textureMapper_->SetScaleEnd(0.5);      // 0.5: the end ratio
62         textureMapper_->SetPivot(58, 58);      // 58: the x-coordinate,; 58: the y-coordinate
63         textureMapper_->SetDurationTime(2000); // 2000: the duration for this animator
64         listener_ = new TestTextureMapperStopListener(uiViewGroupFrame);
65         uiViewGroupFrame->Add(textureMapper_);
66         textureMapper_->LayoutCenterOfParent();
67     }
68     if (layout_ == nullptr) {
69         layout_ = new GridLayout();
70         layout_->SetPosition(336, 48, 516, 232); // 336: x-coordinate; 48: y-coordinate; 516: y value; 232: height
71         container_->Add(layout_);
72         layout_->SetLayoutDirection(LAYOUT_HOR);
73         layout_->SetRows(4); // 4: the number of rows
74         layout_->SetCols(4); // 4: the number of columns
75     }
76     resetBtn_ = new UILabelButton();
77     SetUpButton(resetBtn_, "重置");
78 }
79 
TearDown()80 void UITestTextureMapper::TearDown()
81 {
82     delete listener_;
83     listener_ = nullptr;
84     DeleteChildren(container_);
85     container_ = nullptr;
86     textureMapper_ = nullptr;
87     layout_ = nullptr;
88 }
89 
GetTestView()90 const UIView* UITestTextureMapper::GetTestView()
91 {
92     UIKitTextureMapperTestSetRotateEnd001();
93     UIKitTextureMapperTestSetRotateStart002();
94     UIKitTextureMapperTestSetScaleEnd003();
95     UIKitTextureMapperTestSetScaleStart004();
96     UIKitTextureMapperTestSetPivot005();
97     UIKitTextureMapperTestStart006();
98     UIKitTextureMapperTestReset007();
99     UIKitTextureMapperTestCancelAnimation008();
100     UIKitTextureMapperTestSetAnimatorStopListener009();
101 
102     layout_->LayoutChildren();
103     return container_;
104 }
105 
SetUpButton(UILabelButton * btn,const char * title)106 void UITestTextureMapper::SetUpButton(UILabelButton* btn, const char* title)
107 {
108     if (btn == nullptr) {
109         return;
110     }
111     layout_->Add(btn);
112     btn->Resize(BUTTON_WIDHT2, BUTTON_HEIGHT2);
113     btn->SetText(title);
114     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
115     btn->SetOnClickListener(this);
116     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
117     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
118     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
119     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
120     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
121     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
122 }
123 
UIKitTextureMapperTestSetRotateEnd001()124 void UITestTextureMapper::UIKitTextureMapperTestSetRotateEnd001()
125 {
126     incRotateEndBtn_ = new UILabelButton();
127     SetUpButton(incRotateEndBtn_, "旋转结束角度+");
128 
129     decRotateEndBtn_ = new UILabelButton();
130     SetUpButton(decRotateEndBtn_, "旋转结束角度-");
131 }
132 
UIKitTextureMapperTestSetRotateStart002()133 void UITestTextureMapper::UIKitTextureMapperTestSetRotateStart002()
134 {
135     startAngleBtn_ = new UILabelButton();
136     SetUpButton(startAngleBtn_, "旋转起始角度+");
137 }
138 
UIKitTextureMapperTestSetScaleEnd003()139 void UITestTextureMapper::UIKitTextureMapperTestSetScaleEnd003()
140 {
141     incScaleEndBtn_ = new UILabelButton();
142     SetUpButton(incScaleEndBtn_, "缩放结束比例+");
143 
144     decScaleEndBtn_ = new UILabelButton();
145     SetUpButton(decScaleEndBtn_, "缩放结束比例-");
146 }
147 
UIKitTextureMapperTestSetScaleStart004()148 void UITestTextureMapper::UIKitTextureMapperTestSetScaleStart004()
149 {
150     startScaleBtn_ = new UILabelButton();
151     SetUpButton(startScaleBtn_, "缩放起始比例+");
152 }
153 
UIKitTextureMapperTestSetPivot005()154 void UITestTextureMapper::UIKitTextureMapperTestSetPivot005()
155 {
156     incPivotBtn_ = new UILabelButton();
157     SetUpButton(incPivotBtn_, "中心+");
158 
159     decPivotBtn_ = new UILabelButton();
160     SetUpButton(decPivotBtn_, "中心-");
161 
162     durationTimeBtn_ = new UILabelButton();
163     SetUpButton(durationTimeBtn_, "持续时间+");
164 
165     delayTimeBtn_ = new UILabelButton();
166     SetUpButton(delayTimeBtn_, "延时时间+");
167 
168     easingBtn_ = new UILabelButton();
169     SetUpButton(easingBtn_, "动效");
170 }
171 
UIKitTextureMapperTestStart006()172 void UITestTextureMapper::UIKitTextureMapperTestStart006()
173 {
174     startBtn_ = new UILabelButton();
175     SetUpButton(startBtn_, "启动动画");
176 }
177 
UIKitTextureMapperTestReset007()178 void UITestTextureMapper::UIKitTextureMapperTestReset007()
179 {
180     resetImageBtn_ = new UILabelButton();
181     SetUpButton(resetImageBtn_, "显示原始图片");
182 }
183 
UIKitTextureMapperTestCancelAnimation008()184 void UITestTextureMapper::UIKitTextureMapperTestCancelAnimation008()
185 {
186     cancelBtn_ = new UILabelButton();
187     SetUpButton(cancelBtn_, "取消动画 ");
188 }
189 
UIKitTextureMapperTestSetAnimatorStopListener009()190 void UITestTextureMapper::UIKitTextureMapperTestSetAnimatorStopListener009()
191 {
192     listenerBtn_ = new UILabelButton();
193     SetUpButton(listenerBtn_, "动画结束监听");
194 }
195 
OnClick(UIView & view,const ClickEvent & event)196 bool UITestTextureMapper::OnClick(UIView& view, const ClickEvent& event)
197 {
198     g_rotateStart = 0;     // 0: the start angle of rotation
199     g_rotateEnd = 360;     // 360: the end angle of rotation
200     g_scaleStart = 1;      // 1: the start ratio
201     g_scaleEnd = 0.5;      // 0.5: the end ratio
202     g_durationTime = 2000; // 2000: the duration for this animator
203     g_delayTime = 0;
204     g_easingFunc = EasingEquation::CubicEaseInOut;
205     if (&view == resetBtn_) {
206         g_rotateStart = 0; // 0: the start angle of rotation
207         g_rotateEnd = 360; // 360: the end angle of rotation
208         g_scaleStart = 1;  // 1: the start ratio
209         g_scaleEnd = 0.5;  // 0.5: the end ratio
210 
211         g_durationTime = 2000; // 2000: the duration for this animator
212         g_delayTime = 0;
213         g_easingFunc = EasingEquation::CubicEaseInOut;
214         g_pivot = {58, 58}; // 58: the x-coordinate,; 58: the y-coordinate
215         textureMapper_->SetRotateStart(g_rotateStart);
216         textureMapper_->SetScaleStart(g_scaleStart);
217         textureMapper_->SetPivot(g_pivot.x, g_pivot.y);
218         textureMapper_->Reset();
219     } else if (&view == incRotateEndBtn_) {
220         g_rotateEnd += 90; // 90: the end angle of rotation augmentation
221         textureMapper_->SetRotateEnd(g_rotateEnd);
222     } else if (&view == decRotateEndBtn_) {
223         g_rotateEnd -= 90; // 90: the end angle of rotation decrease
224         textureMapper_->SetRotateEnd(g_rotateEnd);
225     } else if (&view == durationTimeBtn_) {
226         g_durationTime += 200; // 200: the duration for this animator augmentation
227         textureMapper_->SetDurationTime(g_durationTime);
228     } else if (&view == delayTimeBtn_) {
229         g_delayTime += 200; // 200: the delay for this animator augmentation
230         textureMapper_->SetDelayTime(g_delayTime);
231     } else if (&view == easingBtn_) {
232         g_easingFunc = EasingEquation::SineEaseIn;
233         textureMapper_->SetEasingFunc(g_easingFunc);
234     } else if (&view == startAngleBtn_) {
235         g_rotateStart += 90; // 90: the start angle of rotation augmentation
236         textureMapper_->SetRotateStart(g_rotateStart);
237     } else {
238         ExpandClick(view, event);
239     }
240     return true;
241 }
242 
ExpandClick(UIView & view,const ClickEvent & event)243 bool UITestTextureMapper::ExpandClick(UIView& view, const ClickEvent& event)
244 {
245     if (&view == incScaleEndBtn_) {
246         g_scaleEnd += 0.2; // 0.2: the end ratio augmentation
247         textureMapper_->SetScaleEnd(g_scaleEnd);
248     } else if (&view == decScaleEndBtn_) {
249         g_scaleEnd -= 0.2; // 0.2: the end ratio decrease
250         textureMapper_->SetScaleEnd(g_scaleEnd);
251     } else if (&view == startScaleBtn_) {
252         g_scaleStart += 0.2; // 0.2: the start ratio augmentation
253         textureMapper_->SetScaleStart(g_scaleStart);
254     } else if (&view == incPivotBtn_) {
255         g_pivot.x += 5; // 5: pivot offset pixel
256         g_pivot.y += 5; // 5: pivot offset pixel
257         textureMapper_->SetPivot(g_pivot.x, g_pivot.y);
258     } else if (&view == decPivotBtn_) {
259         g_pivot.x -= 5; // 5: pivot offset pixel
260         g_pivot.y -= 5; // 5: pivot offset pixel
261         textureMapper_->SetPivot(g_pivot.x, g_pivot.y);
262     } else if (&view == startBtn_) {
263         textureMapper_->Start();
264     } else if (&view == resetImageBtn_) {
265         textureMapper_->Reset();
266     } else if (&view == cancelBtn_) {
267         textureMapper_->Cancel();
268     } else if (&view == listenerBtn_) {
269         textureMapper_->SetAnimatorStopListener(listener_);
270     }
271     return true;
272 }
273 } // namespace OHOS
274