• 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 public:
GetInstance()33     static UiDemo *GetInstance()
34     {
35         static UiDemo instance;
36         return &instance;
37     }
38     void Start();
39 
40 private:
UiDemo()41     UiDemo()
42     {
43         srand(HALTick::GetInstance().GetTime());
44     }
~UiDemo()45     ~UiDemo()
46     {
47         if (btn_ != nullptr) {
48             delete btn_;
49             btn_ = nullptr;
50         }
51         if (label_ != nullptr) {
52             delete label_;
53             label_ = nullptr;
54         }
55     }
56 
random(int min,int max)57     int random(int min, int max)
58     {
59         return rand() % (max - min) + min;
60     }
61 
OnClick(UIView & view,const ClickEvent & event)62     bool OnClick(UIView &view, const ClickEvent &event) override
63     {
64         Point pos = event.GetCurrentPos();
65         int16_t x = random(view.GetWidth(), Screen::GetInstance().GetWidth() - view.GetWidth());
66         int16_t y = random(view.GetHeight(), Screen::GetInstance().GetHeight() - view.GetHeight());
67         view.SetPosition(x, y);
68         RootView::GetInstance()->Invalidate();
69         HILOG_DEBUG(HILOG_MODULE_APP, "click at (%d,%d), move to (%d,%d)\n", pos.x, pos.y, x, y);
70         return true;
71     }
72 
73     RootView *rootView_ = nullptr;
74     UILabelButton *btn_ = nullptr;
75     UILabel *label_ = nullptr;
76 };
77 
Start()78 void UiDemo::Start()
79 {
80     if (rootView_ != nullptr) {
81         return;
82     }
83     rootView_ = RootView::GetInstance();
84     rootView_->SetPosition(0, 0);
85     rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
86     HILOG_DEBUG(HILOG_MODULE_APP, "rootView %d-%d\n", rootView_->GetWidth(), rootView_->GetHeight());
87 
88     label_ = new UILabel();
89     label_->SetPosition(150, 50, 150, 64);
90     label_->SetText("鸿蒙图形子系统");
91     rootView_->Add(label_);
92 
93     btn_ = new UILabelButton();
94     btn_->SetPosition(150, 200, 150, 64);
95     btn_->SetText("点不到我!");
96     rootView_->Add(btn_);
97     btn_->SetOnClickListener(this);
98 
99     rootView_->Invalidate();
100 }
101 
UiDemoStart(void)102 void UiDemoStart(void)
103 {
104     UiDemo::GetInstance()->Start();
105 }
106