1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 <stdlib>
17 #include "common/screen.h"
18 #include "components/root_view.h"
19 #include "components/ui_canvas.h"
20 #include "components/ui_label.h"
21 #include "components/ui_label_button.h"
22 #include "graphic_config.h"
23 #include "hal_tick.h"
24 #include "hilog/log.h"
25 #include "ui_test.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "UiDemo"
29
30 using namespace OHOS;
31
32 class UiDemo : public UIView::OnClickListener {
33 public:
GetInstance()34 static UiDemo *GetInstance()
35 {
36 static UiDemo instance;
37 return &instance;
38 }
39 void Start();
40
41 private:
UiDemo()42 UiDemo()
43 {
44 srand(HALTick::GetInstance().GetTime());
45 }
~UiDemo()46 ~UiDemo()
47 {
48 if (btn_ != nullptr) {
49 delete btn_;
50 btn_ = nullptr;
51 }
52 if (label_ != nullptr) {
53 delete label_;
54 label_ = nullptr;
55 }
56 }
57
random(int min,int max)58 int random(int min, int max)
59 {
60 return rand() % (max - min) + min;
61 }
62
OnClick(UIView & view,const ClickEvent & event)63 bool OnClick(UIView &view, const ClickEvent &event) override
64 {
65 Point pos = event.GetCurrentPos();
66 int16_t x = random(view.GetWidth(), Screen::GetInstance().GetWidth() - view.GetWidth());
67 int16_t y = random(view.GetHeight(), Screen::GetInstance().GetHeight() - view.GetHeight());
68 view.SetPosition(x, y);
69 RootView::GetInstance()->Invalidate();
70 HILOG_DEBUG(HILOG_MODULE_APP, "click at (%d,%d), move to (%d,%d)\n", pos.x, pos.y, x, y);
71 return true;
72 }
73
74 RootView *rootView_ = nullptr;
75 UILabelButton *btn_ = nullptr;
76 UILabel *label_ = nullptr;
77 };
78
Start()79 void UiDemo::Start()
80 {
81 if (rootView_ != nullptr) {
82 return;
83 }
84 rootView_ = RootView::GetInstance();
85 rootView_->SetPosition(0, 0);
86 rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
87 HILOG_DEBUG(HILOG_MODULE_APP, "rootView %d-%d\n", rootView_->GetWidth(), rootView_->GetHeight());
88 int16_t labelPositionx = 150;
89 int16_t labelPositiony = 50;
90 int16_t labelWidth = 150;
91 int16_t labelHeight = 64;
92 label_ = new UILabel();
93 label_->SetPosition(labelPositionx, labelPositiony, labelWidth, labelHeight);
94 label_->SetText("鸿蒙图形子系统");
95 rootView_->Add(label_);
96
97 int16_t btnPositionx = 150;
98 int16_t btnPositiony = 200;
99 int16_t btnWidth = 150;
100 int16_t btnHeight = 64;
101 btn_ = new UILabelButton();
102 btn_->SetPosition(btnPositionx, btnPositiony, btnWidth, btnHeight);
103 btn_->SetText("点不到我!");
104 rootView_->Add(btn_);
105 btn_->SetOnClickListener(this);
106
107 rootView_->Invalidate();
108 }
109
UiDemoStart(void)110 void UiDemoStart(void)
111 {
112 UiDemo::GetInstance()->Start();
113 }
114