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 #include "animator/animator.h"
17 #include "common/graphic_startup.h"
18 #include "common/task_manager.h"
19 #include "components/root_view.h"
20 #include "components/ui_image_view.h"
21 #include "components/ui_canvas.h"
22 #include "components/ui_button.h"
23 #include "components/ui_label_button.h"
24 #include "gfx_utils/heap_base.h"
25 #include "gfx_utils/geometry2d.h"
26 #include "gfx_utils/graphic_math.h"
27 #include "gfx_utils/image_info.h"
28 #include "gfx_utils/graphic_types.h"
29 #include "font/ui_font_header.h"
30 #include "window/window.h"
31 #include "common/screen.h"
32 #include "engines/gfx/soft_engine.h"
33 #include "hal_tick.h"
34 #include "hilog/log.h"
35
36 #include <stdio.h>
37 #include <unistd.h>
38
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <sys/ioctl.h>
45 #include <sys/mman.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <unistd.h>
49
50 #undef LOG_TAG
51 #define LOG_TAG "UiDemo"
52
53
54 using namespace OHOS;
55 static uint32_t g_fontMemBaseAddr[OHOS::MIN_FONT_PSRAM_LENGTH / 4];
56
GUIInit()57 void GUIInit()
58 {
59 OHOS::GraphicStartUp::Init();
60 static OHOS::SoftEngine localGfxEngine;
61 OHOS::SoftEngine::InitGfxEngine(&localGfxEngine);
62
63 OHOS::GraphicStartUp::InitFontEngine(
64 reinterpret_cast<uintptr_t>(g_fontMemBaseAddr),
65 OHOS::MIN_FONT_PSRAM_LENGTH,
66 VECTOR_FONT_DIR,
67 DEFAULT_VECTOR_FONT_FILENAME);
68 }
69
GUITaskHandler()70 void GUITaskHandler()
71 {
72 OHOS::TaskManager::GetInstance()->TaskHandler();
73 }
74
75 class UIViewScaleRotate : public AnimatorCallback
76 {
77 public:
UIViewScaleRotate()78 UIViewScaleRotate() : animator_(this, nullptr, 50, true) {}
79
~UIViewScaleRotate()80 ~UIViewScaleRotate()
81 {
82 if (label_ != nullptr) {
83 delete label_;
84 label_ = nullptr;
85 }
86 }
87
SetUp()88 void SetUp()
89 {
90 rootView_ = RootView::GetInstance();
91 rootView_->SetPosition(0, 0);
92 rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
93 rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Green().full);
94
95 label_ = new UILabel();
96 label_->SetText("旋转动画");
97 label_->SetPosition(100, 100, 100, 50);
98 label_->SetStyle(STYLE_TEXT_COLOR, Color::Black().full);
99 rootView_->Add(label_);
100
101 animator_.Start();
102 lastRun_ = HALTick::GetInstance().GetTime();
103 }
104
105 void Callback(UIView *view) override
106 {
107 angleValue_++;
108
109 if (scaleValue_.x_ < 0.5f) {
110 scaleStep_ = 0.01f;
111 } else if (scaleValue_.x_ > 1.5f) {
112 scaleStep_ = -0.01f;
113 }
114 scaleValue_.x_ += scaleStep_;
115 scaleValue_.y_ += scaleStep_;
116 label_->Rotate(angleValue_, VIEW_CENTER);
117 label_->Scale(scaleValue_, VIEW_CENTER);
118
119 frame_cnt_++;
120 if (HALTick::GetInstance().GetElapseTime(lastRun_) >= 1000) {
121 HILOG_DEBUG(HILOG_MODULE_APP, "%u fps\n", frame_cnt_);
122 lastRun_ = HALTick::GetInstance().GetTime();
123 frame_cnt_ = 0;
124 }
125 }
126
127 private:
128 RootView *rootView_ = nullptr;
129 UILabel *label_ = nullptr;
130 const Vector2<float> VIEW_CENTER = {100.0f, 100.0f};
131 Animator animator_;
132 int16_t angleValue_ = 0;
133 Vector2<float> scaleValue_ = {1.0f, 1.0f};
134 float scaleStep_ = 0.01f;
135 uint32_t lastRun_ = 0;
136 uint32_t frame_cnt_ = 0;
137 };
138
139 class UiDemo : public UIView::OnClickListener
140 {
141 public:
142 static UiDemo *GetInstance();
143 void Start();
144
145 private:
UiDemo()146 UiDemo() { srand(HALTick::GetInstance().GetTime()); }
147 ~UiDemo();
148
random(int min, int max)149 int random(int min, int max)
150 {
151 return rand() % (max - min) + min;
152 }
153
154 bool OnClick(UIView &view, const ClickEvent &event) override
155 {
156 Point pos = event.GetCurrentPos();
157 int16_t x = random(view.GetWidth(), Screen::GetInstance().GetWidth() - view.GetWidth());
158 int16_t y = random(view.GetHeight(), Screen::GetInstance().GetHeight() - view.GetHeight());
159 view.SetPosition(x, y);
160 RootView::GetInstance()->Invalidate();
161 HILOG_DEBUG(HILOG_MODULE_APP, "click at (%d,%d), move to (%d,%d)\n", pos.x, pos.y, x, y);
162 return true;
163 }
164
165 RootView *rootView_ = nullptr;
166 UILabelButton *btn_ = nullptr;
167 UILabel *label_ = nullptr;
168 UIViewScaleRotate *viewScaleRotate_ = nullptr;
169 };
170
171
GetInstance()172 UiDemo *UiDemo::GetInstance()
173 {
174 static UiDemo instance;
175 return &instance;
176 }
177
~UiDemo()178 UiDemo::~UiDemo()
179 {
180 if (btn_ != nullptr) {
181 delete btn_;
182 btn_ = nullptr;
183 }
184 if (label_ != nullptr) {
185 delete label_;
186 label_ = nullptr;
187 }
188 if (viewScaleRotate_ != nullptr) {
189 delete viewScaleRotate_;
190 viewScaleRotate_ = nullptr;
191 }
192 }
193
Start()194 void UiDemo::Start()
195 {
196 if (rootView_ != nullptr) {
197 return;
198 }
199 rootView_ = RootView::GetInstance();
200 rootView_->SetPosition(0, 0);
201 rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
202 HILOG_DEBUG(HILOG_MODULE_APP, "rootView %d-%d\n", rootView_->GetWidth(), rootView_->GetHeight());
203 if (viewScaleRotate_ == nullptr) {
204 viewScaleRotate_ = new UIViewScaleRotate();
205 viewScaleRotate_->SetUp();
206 }
207
208 btn_ = new UILabelButton();
209 btn_->SetPosition(150, 200, 150, 64);
210 btn_->SetText("点不到我!");
211 rootView_->Add(btn_);
212 btn_->SetOnClickListener(this);
213
214 rootView_->Invalidate();
215 }
216
UiDemoStart(void)217 void UiDemoStart(void)
218 {
219 GUIInit();
220 UiDemo::GetInstance()->Start();
221 OHOS::WindowConfig config = {};
222 config.rect.SetRect(0, 0, OHOS::Screen::GetInstance().GetWidth() - 1,
223 OHOS::Screen::GetInstance().GetHeight() - 1);
224 OHOS::Window* window = OHOS::Window::CreateWindow(config);
225 if (window == nullptr) {
226 GRAPHIC_LOGE("Create window false!");
227 return;
228 }
229 window->BindRootView(OHOS::RootView::GetInstance());
230 window->Show();
231
232 while (1) {
233 GUITaskHandler();
234 usleep(16); /* 16 ms*/
235 }
236 }
237
main(int argc, char* argv[])238 int main(int argc, char* argv[])
239 {
240 UiDemoStart();
241 return 0;
242 }
243
244