• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gfx_engine_manager.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     OHOS::BaseGfxEngine::InitGfxEngine();
61 
62     OHOS::GraphicStartUp::InitFontEngine(
63             reinterpret_cast<uintptr_t>(g_fontMemBaseAddr),
64             OHOS::MIN_FONT_PSRAM_LENGTH,
65             OHOS::VECTOR_FONT_DIR,
66             DEFAULT_VECTOR_FONT_FILENAME);
67 }
68 
GUITaskHandler()69 void GUITaskHandler()
70 {
71     OHOS::TaskManager::GetInstance()->TaskHandler();
72 }
73 
74 class UIViewScaleRotate : public AnimatorCallback
75 {
76 public:
UIViewScaleRotate()77     UIViewScaleRotate() : animator_(this, nullptr, 50, true) {}
78 
~UIViewScaleRotate()79     ~UIViewScaleRotate()
80     {
81         if (label_ != nullptr) {
82             delete label_;
83             label_ = nullptr;
84         }
85     }
86 
SetUp()87     void SetUp()
88     {
89         rootView_ = RootView::GetInstance();
90         rootView_->SetPosition(0, 0);
91         rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
92         rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Green().full);
93 
94         label_ = new UILabel();
95         label_->SetText("旋转动画");
96         label_->SetPosition(100, 100, 100, 50);
97         label_->SetStyle(STYLE_TEXT_COLOR, Color::Black().full);
98         rootView_->Add(label_);
99 
100         animator_.Start();
101         lastRun_ = HALTick::GetInstance().GetTime();
102     }
103 
Callback(UIView * view)104     void Callback(UIView *view) override
105     {
106         angleValue_++;
107 
108         if (scaleValue_.x_ < 0.5f) {
109             scaleStep_ = 0.01f;
110         } else if (scaleValue_.x_ > 1.5f) {
111             scaleStep_ = -0.01f;
112         }
113         scaleValue_.x_ += scaleStep_;
114         scaleValue_.y_ += scaleStep_;
115         label_->Rotate(angleValue_, VIEW_CENTER);
116         label_->Scale(scaleValue_, VIEW_CENTER);
117 
118         frame_cnt_++;
119         if (HALTick::GetInstance().GetElapseTime(lastRun_) >= 1000) {
120             HILOG_DEBUG(HILOG_MODULE_APP, "%u fps\n", frame_cnt_);
121             lastRun_ = HALTick::GetInstance().GetTime();
122             frame_cnt_ = 0;
123         }
124     }
125 
126 private:
127     RootView *rootView_ = nullptr;
128     UILabel *label_ = nullptr;
129     const Vector2<float> VIEW_CENTER = {100.0f, 100.0f};
130     Animator animator_;
131     int16_t angleValue_ = 0;
132     Vector2<float> scaleValue_ = {1.0f, 1.0f};
133     float scaleStep_ = 0.01f;
134     uint32_t lastRun_ = 0;
135     uint32_t frame_cnt_ = 0;
136 };
137 
138 class UiDemo : public UIView::OnClickListener
139 {
140 public:
141     static UiDemo *GetInstance();
142     void Start();
143 
144 private:
UiDemo()145     UiDemo() { srand(HALTick::GetInstance().GetTime()); }
146     ~UiDemo();
147 
random(int min,int max)148     int random(int min, int max)
149     {
150         return rand() % (max - min) + min;
151     }
152 
OnClick(UIView & view,const ClickEvent & event)153     bool OnClick(UIView &view, const ClickEvent &event) override
154     {
155         Point pos = event.GetCurrentPos();
156         int16_t x = random(view.GetWidth(), Screen::GetInstance().GetWidth() - view.GetWidth());
157         int16_t y = random(view.GetHeight(), Screen::GetInstance().GetHeight() - view.GetHeight());
158         view.SetPosition(x, y);
159         RootView::GetInstance()->Invalidate();
160         HILOG_DEBUG(HILOG_MODULE_APP, "click at (%d,%d), move to (%d,%d)\n", pos.x, pos.y, x, y);
161         return true;
162     }
163 
164     RootView *rootView_ = nullptr;
165     UILabelButton *btn_ = nullptr;
166     UILabel *label_ = nullptr;
167     UIViewScaleRotate *viewScaleRotate_ = nullptr;
168 };
169 
170 
GetInstance()171 UiDemo *UiDemo::GetInstance()
172 {
173     static UiDemo instance;
174     return &instance;
175 }
176 
~UiDemo()177 UiDemo::~UiDemo()
178 {
179     if (btn_ != nullptr) {
180         delete btn_;
181         btn_ = nullptr;
182     }
183     if (label_ != nullptr) {
184         delete label_;
185         label_ = nullptr;
186     }
187     if (viewScaleRotate_ != nullptr) {
188         delete viewScaleRotate_;
189         viewScaleRotate_ = nullptr;
190     }
191 }
192 
Start()193 void UiDemo::Start()
194 {
195     if (rootView_ != nullptr) {
196         return;
197     }
198     rootView_ = RootView::GetInstance();
199     rootView_->SetPosition(0, 0);
200     rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
201     HILOG_DEBUG(HILOG_MODULE_APP, "rootView %d-%d\n", rootView_->GetWidth(), rootView_->GetHeight());
202     if (viewScaleRotate_ == nullptr) {
203         viewScaleRotate_ = new UIViewScaleRotate();
204         viewScaleRotate_->SetUp();
205     }
206 
207     btn_ = new UILabelButton();
208     btn_->SetPosition(150, 200, 150, 64);
209     btn_->SetText("点不到我!");
210     rootView_->Add(btn_);
211     btn_->SetOnClickListener(this);
212 
213     rootView_->Invalidate();
214 }
215 
UiDemoStart(void)216 void UiDemoStart(void)
217 {
218     GUIInit();
219     UiDemo::GetInstance()->Start();
220     OHOS::WindowConfig config = {};
221     config.rect.SetRect(0, 0, OHOS::Screen::GetInstance().GetWidth() - 1,
222             OHOS::Screen::GetInstance().GetHeight() - 1);
223     OHOS::Window* window = OHOS::Window::CreateWindow(config);
224     if (window == nullptr) {
225         GRAPHIC_LOGE("Create window false!");
226         return;
227     }
228     window->BindRootView(OHOS::RootView::GetInstance());
229     window->Show();
230 
231     while (1) {
232         GUITaskHandler();
233         usleep(16); /* 16 ms*/
234     }
235 }
236 
main(int argc,char * argv[])237 int main(int argc, char* argv[])
238 {
239     UiDemoStart();
240     return 0;
241 }
242 
243