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 #ifndef SCENE_SRC_CORE_ECS_H
16 #define SCENE_SRC_CORE_ECS_H
17
18 #include <ComponentTools/component_query.h>
19 #include <scene/base/types.h>
20 #include <scene/ext/intf_ecs_context.h>
21 #include <scene/interface/scene_options.h>
22 #include <set>
23 #include <text_3d/ecs/components/text_component.h>
24
25 #include <3d/ecs/components/animation_component.h>
26 #include <3d/ecs/components/animation_state_component.h>
27 #include <3d/ecs/components/camera_component.h>
28 #include <3d/ecs/components/environment_component.h>
29 #include <3d/ecs/components/layer_component.h>
30 #include <3d/ecs/components/light_component.h>
31 #include <3d/ecs/components/local_matrix_component.h>
32 #include <3d/ecs/components/material_component.h>
33 #include <3d/ecs/components/mesh_component.h>
34 #include <3d/ecs/components/morph_component.h>
35 #include <3d/ecs/components/name_component.h>
36 #include <3d/ecs/components/node_component.h>
37 #include <3d/ecs/components/post_process_component.h>
38 #include <3d/ecs/components/render_configuration_component.h>
39 #include <3d/ecs/components/render_handle_component.h>
40 #include <3d/ecs/components/render_mesh_component.h>
41 #include <3d/ecs/components/transform_component.h>
42 #include <3d/ecs/components/uri_component.h>
43 #include <3d/ecs/components/world_matrix_component.h>
44 #include <3d/ecs/systems/intf_node_system.h>
45 #include <3d/util/intf_picking.h>
46 #include <core/ecs/intf_ecs.h>
47 #include <core/intf_engine.h>
48 #include <render/intf_render_context.h>
49
50 #include "ecs_component/entity_owner_component_info.h"
51 #include "ecs_listener.h"
52
SCENE_BEGIN_NAMESPACE()53 SCENE_BEGIN_NAMESPACE()
54
55 class Ecs : public META_NS::IntroduceInterfaces<IEcsContext>,
56 private EcsListener,
57 private CORE3D_NS::INodeSystem::SceneNodeListener {
58 public:
59 bool Initialize(const BASE_NS::shared_ptr<IInternalScene>& scene, const SceneOptions& opts);
60 void Uninitialize();
61
62 CORE3D_NS::ISceneNode* GetNode(CORE_NS::Entity ent);
63 const CORE3D_NS::ISceneNode* GetNode(CORE_NS::Entity ent) const;
64
65 CORE3D_NS::ISceneNode* FindNode(BASE_NS::string_view path);
66 CORE3D_NS::ISceneNode* FindNodeParent(BASE_NS::string_view path);
67
68 bool SetNodeName(CORE_NS::Entity ent, BASE_NS::string_view name); // fix
69 bool SetNodeParentAndName(CORE_NS::Entity ent, BASE_NS::string_view name, CORE3D_NS::ISceneNode* parent);
70
71 BASE_NS::string GetPath(const CORE3D_NS::ISceneNode* node) const;
72 bool IsNodeEntity(CORE_NS::Entity ent) const override;
73 bool RemoveEntity(CORE_NS::Entity ent) override;
74
75 void ListenNodeChanges(bool enabled);
76
77 void SetNodesActive(CORE_NS::Entity ent, bool enabled);
78
79 BASE_NS::vector<CORE_NS::Entity> GetNodeDescendants(CORE_NS::Entity) const;
80
81 public:
82 bool CreateUnnamedRootNode() override;
83
84 CORE_NS::IComponentManager* FindComponent(BASE_NS::string_view name) const override;
85 CORE_NS::IEcs::Ptr GetNativeEcs() const override
86 {
87 return ecs;
88 }
89
90 void AddDefaultComponents(CORE_NS::Entity ent) const override;
91 BASE_NS::string GetPath(CORE_NS::Entity ent) const override;
92 IEcsObject::Ptr GetEcsObject(CORE_NS::Entity) override;
93 void RemoveEcsObject(const IEcsObject::ConstPtr&) override;
94 CORE_NS::Entity GetRootEntity() const override;
95 CORE_NS::Entity GetParent(CORE_NS::Entity ent) const override;
96 CORE_NS::EntityReference GetRenderHandleEntity(const RENDER_NS::RenderHandleReference& handle) override;
97
98 public:
99 CORE_NS::IEcs::Ptr ecs;
100
101 CORE3D_NS::IAnimationComponentManager* animationComponentManager {};
102 CORE3D_NS::IAnimationStateComponentManager* animationStateComponentManager {};
103 CORE3D_NS::ICameraComponentManager* cameraComponentManager {};
104
105 CORE3D_NS::IEnvironmentComponentManager* envComponentManager {};
106 CORE3D_NS::ILayerComponentManager* layerComponentManager {};
107 CORE3D_NS::ILightComponentManager* lightComponentManager {};
108 CORE3D_NS::IMaterialComponentManager* materialComponentManager {};
109 CORE3D_NS::IMeshComponentManager* meshComponentManager {};
110 CORE3D_NS::INameComponentManager* nameComponentManager {};
111 CORE3D_NS::INodeComponentManager* nodeComponentManager {};
112 CORE3D_NS::IRenderMeshComponentManager* renderMeshComponentManager {};
113 CORE3D_NS::IRenderHandleComponentManager* rhComponentManager {};
114 CORE3D_NS::ITransformComponentManager* transformComponentManager {};
115 CORE3D_NS::IUriComponentManager* uriComponentManager {};
116 CORE3D_NS::IRenderConfigurationComponentManager* renderConfigComponentManager {};
117 CORE3D_NS::IPostProcessComponentManager* postProcessComponentManager {};
118 CORE3D_NS::ILocalMatrixComponentManager* localMatrixComponentManager {};
119 CORE3D_NS::IWorldMatrixComponentManager* worldMatrixComponentManager {};
120 CORE3D_NS::IMorphComponentManager* morphComponentManager {};
121
122 SCENE_NS::IEntityOwnerComponentManager* entityOwnerComponentManager {};
123
124 // optional
125 TEXT3D_NS::ITextComponentManager* textComponentManager {};
126
127 BASE_NS::unique_ptr<CORE_NS::ComponentQuery> meshQuery {};
128 BASE_NS::unique_ptr<CORE_NS::ComponentQuery> materialQuery {};
129 BASE_NS::unique_ptr<CORE_NS::ComponentQuery> animationQuery {};
130
131 CORE3D_NS::INodeSystem* nodeSystem {};
132 CORE3D_NS::IPicking* picking {};
133
134 private:
135 template<typename Manager>
136 Manager* GetCoreManager();
137
138 void OnChildChanged(const CORE3D_NS::ISceneNode& parent, CORE3D_NS::INodeSystem::SceneNodeListener::EventType type,
139 const CORE3D_NS::ISceneNode& child, size_t index) override;
140
141 void ReactivateNodes(CORE_NS::Entity ent);
142
143 bool CheckDeactivatedAncestry(
144 CORE_NS::Entity start, CORE_NS::Entity node, std::set<CORE_NS::Entity>& reactivate) const;
145 std::set<CORE_NS::Entity> GetDeactivatedChildren(CORE_NS::Entity ent) const;
146
147 void GetNodeDescendants(CORE_NS::Entity, BASE_NS::vector<CORE_NS::Entity>&) const;
148
149 private:
150 BASE_NS::weak_ptr<IInternalScene> scene_;
151 CORE_NS::Entity rootEntity_ {};
152 BASE_NS::unordered_map<BASE_NS::string, CORE_NS::IComponentManager*> components_;
153 BASE_NS::vector<CORE_NS::Entity> imported_;
154 };
155
156 CORE_NS::Entity CopyExternalAsChild(const IEcsObject& parent, const IEcsObject& extChild);
157 CORE_NS::Entity CopyExternalAsChild(
158 const IEcsObject& parent, const IScene& extScene, BASE_NS::vector<CORE_NS::Entity>& imported);
159
160 SCENE_END_NAMESPACE()
161
162 #endif
163