• 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_EXT_IINTERNAL_SCENE_H
17 #define SCENE_EXT_IINTERNAL_SCENE_H
18 
19 #include <scene/ext/intf_component.h>
20 #include <scene/ext/intf_internal_camera.h>
21 #include <scene/interface/intf_camera.h>
22 #include <scene/interface/intf_node.h>
23 #include <scene/interface/intf_render_context.h>
24 #include <scene/interface/scene_options.h>
25 
26 #include <3d/intf_graphics_context.h>
27 #include <render/intf_render_context.h>
28 
29 #include <meta/api/task_queue.h>
30 #include <meta/ext/object.h>
31 #include <meta/interface/animation/intf_animation.h>
32 #include <meta/interface/engine/intf_engine_value.h>
33 #include <meta/interface/intf_event.h>
34 #include <meta/interface/intf_startable_controller.h>
35 
36 SCENE_BEGIN_NAMESPACE()
37 
38 class IScene;
39 class IEcsObject;
40 class IComponentFactory;
41 class IEcsContext;
42 
43 class IInternalSceneCore : public CORE_NS::IInterface {
44     META_INTERFACE(CORE_NS::IInterface, IInternalSceneCore, "55523d8b-b1db-45a3-a0fc-64a53551d779")
45 public:
46     virtual bool Initialize() = 0;
47     virtual void Uninitialize() = 0;
48 
49     virtual void RegisterComponent(const BASE_NS::Uid&, const BASE_NS::shared_ptr<IComponentFactory>&) = 0;
50     virtual void UnregisterComponent(const BASE_NS::Uid&) = 0;
51     virtual BASE_NS::shared_ptr<IComponentFactory> FindComponentFactory(const BASE_NS::Uid&) const = 0;
52 
53     virtual IRenderContext::Ptr GetContext() const = 0;
54 
55     virtual IEcsContext& GetEcsContext() = 0;
56     virtual BASE_NS::shared_ptr<IScene> GetScene() const = 0;
57     virtual RENDER_NS::IRenderContext& GetRenderContext() = 0;
58     virtual CORE3D_NS::IGraphicsContext& GetGraphicsContext() = 0;
59 
60     virtual void SchedulePropertyUpdate(const BASE_NS::shared_ptr<IEcsObject>& obj) = 0;
61     virtual void SyncProperties() = 0;
62     struct UpdateInfo {
63         bool syncProperties { true }; /// If true, synchronize properties between ECS
64         META_NS::IClock::Ptr clock;   /// Optional clock to use for ECS update
65     };
66     virtual void Update(const UpdateInfo& info) = 0;
67     /// Updates using an internal clock. Update(const UpdateInfo& info) is preferred.
68     virtual void Update(bool syncProperties = true) = 0;
69 
70     virtual bool SyncProperty(const META_NS::IProperty::ConstPtr& p, META_NS::EngineSyncDirection dir) = 0;
71     virtual void ListenNodeChanges(bool enabled) = 0;
72 
73 public:
74     template<typename Func>
AddTask(Func && func)75     auto AddTask(Func&& func)
76     {
77         return GetContext()->AddTask(BASE_NS::forward<Func>(func));
78     }
79     template<typename Func>
AddTask(Func && func,const META_NS::ITaskQueue::Ptr & queue)80     auto AddTask(Func&& func, const META_NS::ITaskQueue::Ptr& queue)
81     {
82         return GetContext()->AddTask(BASE_NS::forward<Func>(func), queue);
83     }
84     template<typename Func>
PostUserNotification(Func && func)85     auto PostUserNotification(Func&& func)
86     {
87         return GetContext()->PostUserNotification(BASE_NS::forward<Func>(func));
88     }
89     template<typename MyEvent, typename... Args>
InvokeUserNotification(const META_NS::IEvent::Ptr & event,Args...args)90     auto InvokeUserNotification(const META_NS::IEvent::Ptr& event, Args... args)
91     {
92         return GetContext()->InvokeUserNotification<MyEvent>(event, BASE_NS::move(args)...);
93     }
94 };
95 
96 class IInternalScene : public IInternalSceneCore {
97     META_INTERFACE(IInternalSceneCore, IInternalScene, "24c69cfe-1d25-4546-a5cb-b63c2262065e")
98 public:
99     virtual META_NS::IObject::Ptr CreateObject(META_NS::ObjectId id) = 0;
100 
101     //-- node handling
102     virtual INode::Ptr CreateNode(BASE_NS::string_view path, META_NS::ObjectId id) = 0;
103     virtual INode::Ptr FindNode(BASE_NS::string_view path, META_NS::ObjectId id) const = 0;
104     virtual INode::Ptr FindNode(CORE_NS::Entity, META_NS::ObjectId id) const = 0;
105 
106     virtual bool ReleaseNode(INode::Ptr&& node, bool recursive) = 0;
107     virtual bool RemoveNode(const INode::Ptr& node) = 0;
108 
109     virtual BASE_NS::vector<INode::Ptr> GetChildren(const BASE_NS::shared_ptr<IEcsObject>&) const = 0;
110     virtual bool RemoveChild(
111         const BASE_NS::shared_ptr<IEcsObject>& object, const BASE_NS::shared_ptr<IEcsObject>& child) = 0;
112     virtual bool AddChild(const BASE_NS::shared_ptr<IEcsObject>& object, const INode::Ptr& child, size_t index) = 0;
113     //--
114 
115     virtual void SetEntityActive(const BASE_NS::shared_ptr<IEcsObject>&, bool active) = 0;
116 
117     virtual IComponent::Ptr CreateEcsComponent(const INode::Ptr& node, BASE_NS::string_view componentName) = 0;
118 
119     virtual BASE_NS::vector<ICamera::Ptr> GetCameras() const = 0;
120     virtual BASE_NS::vector<META_NS::IAnimation::Ptr> GetAnimations() const = 0;
121 
122     virtual void AddRenderingCamera(const IInternalCamera::Ptr& camera) = 0;
123     virtual void RemoveRenderingCamera(const IInternalCamera::Ptr& camera) = 0;
124 
125     virtual bool SetRenderMode(RenderMode) = 0;
126     virtual RenderMode GetRenderMode() const = 0;
127     virtual void RenderFrame() = 0;
128     virtual bool HasPendingRender() const = 0;
129 
130     virtual SceneOptions GetOptions() const = 0;
131 
132     // Startable handling
133     virtual void StartAllStartables(META_NS::IStartableController::ControlBehavior behavior) = 0;
134     virtual void StopAllStartables(META_NS::IStartableController::ControlBehavior behavior) = 0;
135 };
136 
137 inline Future<bool> SyncProperty(const IInternalScene::Ptr& scene, const META_NS::IProperty::ConstPtr& p,
138     META_NS::EngineSyncDirection dir = META_NS::EngineSyncDirection::AUTO)
139 {
140     return scene->AddTask([=] { return scene->SyncProperty(p, dir); });
141 }
142 
143 SCENE_END_NAMESPACE()
144 
145 META_INTERFACE_TYPE(SCENE_NS::IInternalScene)
146 
147 #endif
148