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 "sample_ui.h"
17
18 #include "animator/animator.h"
19 #include "common/graphic_startup.h"
20 #include "common/task_manager.h"
21 #include "components/root_view.h"
22 #include "components/ui_image_view.h"
23 #include "components/ui_canvas.h"
24 #include "components/ui_button.h"
25 #include "components/ui_label_button.h"
26 #include "gfx_utils/heap_base.h"
27 #include "gfx_utils/geometry2d.h"
28 #include "gfx_utils/graphic_math.h"
29 #include "gfx_utils/image_info.h"
30 #include "common/screen.h"
31 #include "hal_tick.h"
32 #include "hilog/log.h"
33
34 #include <stdio.h>
35
36 using namespace OHOS;
37
38 class UIViewScaleRotate : public AnimatorCallback
39 {
40 public:
UIViewScaleRotate()41 UIViewScaleRotate() : animator_(this, nullptr, 50, true) {}
42
~UIViewScaleRotate()43 ~UIViewScaleRotate()
44 {
45 if (label_ != nullptr) {
46 delete label_;
47 label_ = nullptr;
48 }
49 }
50
SetUp()51 void SetUp()
52 {
53 rootView_ = RootView::GetInstance();
54 rootView_->SetPosition(0, 0);
55 rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
56 rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Green().full);
57 label_ = new UILabel();
58 label_->SetText("animator");
59 label_->SetPosition(100, 100, 100, 50);
60 label_->SetStyle(STYLE_TEXT_COLOR, Color::Black().full);
61 label_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
62 rootView_->Add(label_);
63
64 animator_.Start();
65 lastRun_ = HALTick::GetInstance().GetTime();
66 }
67
68 void Callback(UIView *view) override
69 {
70 angleValue_++;
71 if (scaleValue_.x_ < 0.5f) {
72 scaleStep_ = 0.01f;
73 } else if (scaleValue_.x_ > 1.5f) {
74 scaleStep_ = -0.01f;
75 }
76 scaleValue_.x_ += scaleStep_;
77 scaleValue_.y_ += scaleStep_;
78 label_->Rotate(angleValue_, VIEW_CENTER);
79 label_->Scale(scaleValue_, VIEW_CENTER);
80
81 frame_cnt_++;
82 if (HALTick::GetInstance().GetElapseTime(lastRun_) >= 1000) {
83 lastRun_ = HALTick::GetInstance().GetTime();
84 frame_cnt_ = 0;
85 }
86 }
87
88 private:
89 RootView *rootView_ = nullptr;
90 UILabel *label_ = nullptr;
91 const Vector2<float> VIEW_CENTER = {100.0f, 100.0f};
92 Animator animator_;
93 int16_t angleValue_ = 0;
94 Vector2<float> scaleValue_ = {1.0f, 1.0f};
95 float scaleStep_ = 0.01f;
96 uint32_t lastRun_ = 0;
97 uint32_t frame_cnt_ = 0;
98 };
99
100 class UiDemo : public UIView::OnClickListener
101 {
102 public:
103 static UiDemo *GetInstance();
104 void Start();
105
106 private:
UiDemo()107 UiDemo() { srand(HALTick::GetInstance().GetTime()); }
108 ~UiDemo();
109
random(int min, int max)110 int random(int min, int max)
111 {
112 return rand() % (max - min) + min;
113 }
114
115 bool OnClick(UIView &view, const ClickEvent &event) override
116 {
117 Point pos = event.GetCurrentPos();
118 int16_t x = random(view.GetWidth(), Screen::GetInstance().GetWidth() - view.GetWidth());
119 int16_t y = random(view.GetHeight(), Screen::GetInstance().GetHeight() - view.GetHeight());
120 view.SetPosition(x, y);
121 RootView::GetInstance()->Invalidate();
122 printf("click at (%d,%d), move to (%d,%d)\n", pos.x, pos.y, x, y);
123 return true;
124 }
125
126 RootView *rootView_ = nullptr;
127 UILabelButton *btn_ = nullptr;
128 UILabel *label_ = nullptr;
129 UIViewScaleRotate *viewScaleRotate_ = nullptr;
130 };
131
132
GetInstance()133 UiDemo *UiDemo::GetInstance()
134 {
135 static UiDemo instance;
136 return &instance;
137 }
138
~UiDemo()139 UiDemo::~UiDemo()
140 {
141 if (btn_ != nullptr) {
142 delete btn_;
143 btn_ = nullptr;
144 }
145 if (label_ != nullptr) {
146 delete label_;
147 label_ = nullptr;
148 }
149 if (viewScaleRotate_ != nullptr) {
150 delete viewScaleRotate_;
151 viewScaleRotate_ = nullptr;
152 }
153 }
154
Start()155 void UiDemo::Start()
156 {
157 if (rootView_ != nullptr) {
158 return;
159 }
160 rootView_ = RootView::GetInstance();
161 rootView_->SetPosition(0, 0);
162 rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
163 rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Green().full);
164 if (viewScaleRotate_ == nullptr) {
165 viewScaleRotate_ = new UIViewScaleRotate();
166 viewScaleRotate_->SetUp();
167 }
168
169 btn_ = new UILabelButton();
170 btn_->SetPosition(250, 200, 150, 64);
171 btn_->SetStyle(STYLE_BORDER_COLOR, Color::Red().full);
172 btn_->SetTextColor(Color::Red());
173 btn_->SetText("Don't click me");
174 rootView_->Add(btn_);
175 btn_->SetOnClickListener(this);
176
177 rootView_->Invalidate();
178 }
179
UiDemoStart(void)180 void UiDemoStart(void)
181 {
182 UiDemo::GetInstance()->Start();
183 }