1 /*
2  * Copyright (c) 2023 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 <iostream>
17 
18 #include "window.h"
19 
20 #include "transaction/rs_transaction.h"
21 #include "ui/rs_root_node.h"
22 #include "ui/rs_ui_director.h"
23 
24 using namespace OHOS;
25 using namespace OHOS::Rosen;
26 using namespace std;
27 
28 std::shared_ptr<RSNode> rootNode;
29 std::vector<std::shared_ptr<RSCanvasNode>> nodes;
30 
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)31 void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
32 {
33     std::cout << "rs node demo Init Rosen Backend!" << std::endl;
34 
35     rootNode = RSRootNode::Create();
36     rootNode->SetBounds(0, 0, width, height);
37     rootNode->SetFrame(0, 0, width, height);
38     rootNode->SetBackgroundColor(SK_ColorWHITE);
39 
40     nodes.emplace_back(RSCanvasNode::Create());
41     nodes[0]->SetBounds(400, 200, 400, 400);
42     nodes[0]->SetFrame(400, 200, 400, 400);
43     nodes[0]->SetBackgroundColor(SK_ColorBLUE);
44 
45     nodes.emplace_back(RSCanvasNode::Create());
46     nodes[1]->SetBounds(400, 650, 400, 400);
47     nodes[1]->SetFrame(400, 650, 400, 400);
48     nodes[1]->SetBackgroundColor(SK_ColorBLUE);
49 
50     nodes.emplace_back(RSCanvasNode::Create());
51     nodes[2]->SetBounds(200, 1400, 400, 400);
52     nodes[2]->SetFrame(400, 1400, 400, 400);
53     nodes[2]->SetBackgroundColor(SK_ColorBLUE);
54 
55     nodes.emplace_back(RSCanvasNode::Create());
56     nodes[3]->SetBounds(700, 1400, 400, 400);
57     nodes[3]->SetFrame(700, 1400, 400, 400);
58     nodes[3]->SetBackgroundColor(SK_ColorBLUE);
59 
60     rootNode->AddChild(nodes[0], -1);
61     rootNode->AddChild(nodes[1], -1);
62     rootNode->AddChild(nodes[2], -1);
63     rootNode->AddChild(nodes[3], -1);
64     rsUiDirector->SetRoot(rootNode->GetId());
65 }
66 
main()67 int main()
68 {
69     std::cout << "rs node demo start!" << std::endl;
70     sptr<WindowOption> option = new WindowOption();
71     option->SetWindowType(WindowType::WINDOW_TYPE_STATUS_BAR);
72     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
73     option->SetWindowRect({0, 0, 1280, 2560});
74     auto window = Window::Create("node_demo", option);
75     window->Show();
76     auto rect = window->GetRect();
77     while (rect.width_ == 0 && rect.height_ == 0) {
78         std::cout << "rs node demo create window failed: " << rect.width_ << " " << rect.height_ << std::endl;
79         window->Hide();
80         window->Destroy();
81         window = Window::Create("node_demo", option);
82         window->Show();
83         rect = window->GetRect();
84     }
85     std::cout << "rs node demo create window " << rect.width_ << " " << rect.height_ << std::endl;
86     auto surfaceNode = window->GetSurfaceNode();
87 
88     auto rsUiDirector = RSUIDirector::Create();
89     rsUiDirector->Init();
90     RSTransaction::FlushImplicitTransaction();
91     sleep(1);
92 
93     std::cout << "rs node demo stage 1 " << std::endl;
94     rsUiDirector->SetRSSurfaceNode(surfaceNode);
95     Init(rsUiDirector, rect.width_, rect.height_);
96     rsUiDirector->SendMessages();
97     sleep(1);
98 
99     std::cout << "rs node demo stage 2 " << std::endl;
100     nodes[0]->SetRotationY(0);
101     nodes[1]->SetRotationY(0);
102     nodes[1]->SetPivotZ(200);
103     nodes[2]->SetRotationX(0);
104     nodes[3]->SetRotationX(0);
105     nodes[3]->SetPivotZ(200);
106 
107     RSAnimationTimingProtocol protocol;
108     protocol.SetDuration(3000);
109     RSNode::Animate(protocol, RSAnimationTimingCurve::EASE_IN_OUT, [&]() {
110         nodes[0]->SetRotationY(360.f);
111         nodes[1]->SetRotationY(360.f);
112         nodes[2]->SetRotationX(360.f);
113         nodes[3]->SetRotationX(360.f);
114     }, []() {
115         std::cout<<"custom animation 2 finish callback"<<std::endl;
116     });
117     rsUiDirector->SendMessages();
118     sleep(5);
119 
120     std::cout << "rs node demo end!" << std::endl;
121     window->Hide();
122     window->Destroy();
123     return 0;
124 }
125