• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SCENE_SRC_UTIL_H
17 #define SCENE_SRC_UTIL_H
18 
19 #include <scene/ext/intf_ecs_context.h>
20 #include <scene/ext/intf_internal_scene.h>
21 #include <scene/ext/intf_render_resource.h>
22 
23 #include <3d/ecs/components/render_handle_component.h>
24 #include <core/property/scoped_handle.h>
25 
SCENE_BEGIN_NAMESPACE()26 SCENE_BEGIN_NAMESPACE()
27 
28 inline CORE_NS::IEcs* GetNativeEcs(const IEcsObject::Ptr& object)
29 {
30     if (auto s = object->GetScene()) {
31         return s->GetEcsContext().GetNativeEcs().get();
32     }
33     return nullptr;
34 }
35 
GetEcs(const IEcsObject::Ptr & object)36 inline IEcsContext* GetEcs(const IEcsObject::Ptr& object)
37 {
38     if (auto s = object->GetScene()) {
39         return &s->GetEcsContext();
40     }
41     return nullptr;
42 }
43 
44 template<typename ComponentType>
GetScopedHandle(const IEcsObject::Ptr & object)45 CORE_NS::ScopedHandle<ComponentType> GetScopedHandle(const IEcsObject::Ptr& object)
46 {
47     if (auto ecs = GetEcs(object)) {
48         if (auto m = ecs->FindComponent<ComponentType>()) {
49             return CORE_NS::ScopedHandle<ComponentType> { m->GetData(object->GetEntity()) };
50         }
51     }
52     return CORE_NS::ScopedHandle<ComponentType> {};
53 }
54 
55 template<typename Interface>
ObjectWithRenderHandle(const IInternalScene::Ptr & scene,CORE_NS::EntityReference entRef,typename Interface::Ptr p,META_NS::ObjectId id)56 typename Interface::Ptr ObjectWithRenderHandle(
57     const IInternalScene::Ptr& scene, CORE_NS::EntityReference entRef, typename Interface::Ptr p, META_NS::ObjectId id)
58 {
59     if (auto rhman = static_cast<CORE3D_NS::IRenderHandleComponentManager*>(
60             scene->GetEcsContext().FindComponent<CORE3D_NS::RenderHandleComponent>())) {
61         if (!p && id.IsValid()) {
62             p = interface_pointer_cast<Interface>(scene->CreateObject(id));
63         }
64         if (auto i = interface_cast<IRenderResource>(p)) {
65             i->SetRenderHandle(rhman->GetRenderHandleReference(entRef));
66         }
67     }
68     return p;
69 }
70 
HandleFromRenderResource(const IInternalScene::Ptr & scene,const RENDER_NS::RenderHandleReference & handle)71 inline CORE_NS::EntityReference HandleFromRenderResource(
72     const IInternalScene::Ptr& scene, const RENDER_NS::RenderHandleReference& handle)
73 {
74     return scene->GetEcsContext().GetRenderHandleEntity(handle);
75 }
76 
77 template<typename Interface>
HandleFromRenderResource(const IInternalScene::Ptr & scene,const typename Interface::Ptr & p)78 CORE_NS::EntityReference HandleFromRenderResource(const IInternalScene::Ptr& scene, const typename Interface::Ptr& p)
79 {
80     CORE_NS::EntityReference ent;
81     if (auto i = interface_cast<IRenderResource>(p)) {
82         ent = HandleFromRenderResource(scene, i->GetRenderHandle());
83     }
84     return ent;
85 }
86 
87 SCENE_END_NAMESPACE()
88 
89 #endif