• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 #include "ui_test.h"
16 #include "common/screen.h"
17 #include "components/root_view.h"
18 #include "components/ui_canvas.h"
19 #include "components/ui_label.h"
20 #include "components/ui_label_button.h"
21 #include "graphic_config.h"
22 #include "hal_tick.h"
23 #include "hilog/log.h"
24 #include <stdlib.h>
25 
26 #undef LOG_TAG
27 #define LOG_TAG "UiDemo"
28 
29 using namespace OHOS;
30 
31 class UiDemo : public UIView::OnClickListener
32 {
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() { srand(HALTick::GetInstance().GetTime()); }
~UiDemo()43     ~UiDemo()
44     {
45         if (btn_ != nullptr) {
46             delete btn_;
47             btn_ = nullptr;
48         }
49         if (label_ != nullptr) {
50             delete label_;
51             label_ = nullptr;
52         }
53     }
54 
random(int min,int max)55     int random(int min, int max)
56     {
57         return rand() % (max - min) + min;
58     }
59 
OnClick(UIView & view,const ClickEvent & event)60     bool OnClick(UIView &view, const ClickEvent &event) override
61     {
62         Point pos = event.GetCurrentPos();
63         int16_t x = random(view.GetWidth(), Screen::GetInstance().GetWidth() - view.GetWidth());
64         int16_t y = random(view.GetHeight(), Screen::GetInstance().GetHeight() - view.GetHeight());
65         view.SetPosition(x, y);
66         RootView::GetInstance()->Invalidate();
67         HILOG_DEBUG(HILOG_MODULE_APP, "click at (%d,%d), move to (%d,%d)\n", pos.x, pos.y, x, y);
68         return true;
69     }
70 
71     RootView *rootView_ = nullptr;
72     UILabelButton *btn_ = nullptr;
73     UILabel *label_ = nullptr;
74 };
75 
Start()76 void UiDemo::Start()
77 {
78     if (rootView_ != nullptr) {
79         return;
80     }
81     rootView_ = RootView::GetInstance();
82     rootView_->SetPosition(0, 0);
83     rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
84     HILOG_DEBUG(HILOG_MODULE_APP, "rootView %d-%d\n", rootView_->GetWidth(), rootView_->GetHeight());
85 
86     label_ = new UILabel();
87     label_->SetPosition(150, 50, 150, 64);
88     label_->SetText("鸿蒙图形子系统");
89     rootView_->Add(label_);
90 
91     btn_ = new UILabelButton();
92     btn_->SetPosition(150, 200, 150, 64);
93     btn_->SetText("点不到我!");
94     rootView_->Add(btn_);
95     btn_->SetOnClickListener(this);
96 
97     rootView_->Invalidate();
98 }
99 
UiDemoStart(void)100 void UiDemoStart(void)
101 {
102     UiDemo::GetInstance()->Start();
103 }
104