1 /*
2 * Copyright (c) 2024 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 "scene_manager.h"
17
18 #include <scene/ext/intf_internal_scene.h>
19 #include <scene/ext/util.h>
20
21 #include <render/intf_render_context.h>
22
23 #include <meta/api/task_queue.h>
24
25 #include "asset/asset_object.h"
26 #include "util.h"
27
SCENE_BEGIN_NAMESPACE()28 SCENE_BEGIN_NAMESPACE()
29
30 bool SceneManager::Build(const META_NS::IMetadata::Ptr& d)
31 {
32 bool res = Super::Build(d);
33 if (res) {
34 if (d) {
35 using META_NS::SharedPtrIInterface;
36 BASE_NS::shared_ptr<RENDER_NS::IRenderContext> rc =
37 GetInterfaceBuildArg<RENDER_NS::IRenderContext>(d, "RenderContext");
38 engineQueue_ = GetInterfaceBuildArg<META_NS::ITaskQueue>(d, "EngineQueue");
39 META_NS::ITaskQueue::Ptr app = GetInterfaceBuildArg<META_NS::ITaskQueue>(d, "AppQueue");
40 if (!engineQueue_ || !app || !rc) {
41 CORE_LOG_E("Invalid arguments to construct SceneManager");
42 return false;
43 }
44 } else {
45 CORE_LOG_E("SceneManager requires arguments when constructed");
46 return false;
47 }
48 context_ = d;
49 }
50 return res;
51 }
52
CreateScene()53 Future<IScene::Ptr> SceneManager::CreateScene()
54 {
55 return META_NS::AddFutureTaskOrRunDirectly(engineQueue_, [context = context_] {
56 if (auto scene = META_NS::GetObjectRegistry().Create<IScene>(SCENE_NS::ClassId::Scene, context)) {
57 auto& ecs = scene->GetInternalScene()->GetEcsContext();
58 if (ecs.CreateUnnamedRootNode()) {
59 return scene;
60 }
61 CORE_LOG_E("Failed to create root node");
62 }
63 return SCENE_NS::IScene::Ptr {};
64 });
65 }
66
Load(const IScene::Ptr & scene,BASE_NS::string_view uri)67 static IScene::Ptr Load(const IScene::Ptr& scene, BASE_NS::string_view uri)
68 {
69 CORE_LOG_I("Loading scene: '%s'", BASE_NS::string(uri).c_str());
70 if (uri == "" || uri == "scene://empty") {
71 return scene;
72 }
73
74 if (auto assets = META_NS::GetObjectRegistry().Create<IAssetObject>(ClassId::AssetObject)) {
75 if (assets->Load(scene, uri)) {
76 if (auto att = interface_cast<META_NS::IAttach>(scene)) {
77 att->Attach(assets);
78 }
79
80 scene->GetInternalScene()->Update();
81
82 return scene;
83 }
84 }
85 return {};
86 }
87
CreateScene(BASE_NS::string_view uri)88 Future<IScene::Ptr> SceneManager::CreateScene(BASE_NS::string_view uri)
89 {
90 return META_NS::AddFutureTaskOrRunDirectly(engineQueue_, [path = BASE_NS::string(uri), context = context_] {
91 if (auto s = META_NS::GetObjectRegistry().Create<IScene>(SCENE_NS::ClassId::Scene, context)) {
92 return Load(s, path);
93 }
94 return IScene::Ptr {};
95 });
96 }
97
98 SCENE_END_NAMESPACE()