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 CORE_ECS_NODESYSTEM_H
17 #define CORE_ECS_NODESYSTEM_H
18 
19 #include <ComponentTools/component_query.h>
20 
21 #include <3d/ecs/systems/intf_node_system.h>
22 #include <base/containers/unique_ptr.h>
23 #include <base/containers/unordered_map.h>
24 #include <base/containers/vector.h>
25 #include <base/math/matrix.h>
26 #include <core/namespace.h>
27 
28 #include "property/property_handle.h"
29 
30 CORE3D_BEGIN_NAMESPACE()
31 class INameComponentManager;
32 class INodeComponentManager;
33 class ILocalMatrixComponentManager;
34 class IPreviousWorldMatrixComponentManager;
35 class IWorldMatrixComponentManager;
36 class ITransformComponentManager;
37 
38 class NodeSystem final : public INodeSystem {
39 public:
40     explicit NodeSystem(CORE_NS::IEcs& ecs);
41     ~NodeSystem() override = default;
42 
43     BASE_NS::string_view GetName() const override;
44     BASE_NS::Uid GetUid() const override;
45 
46     CORE_NS::IPropertyHandle* GetProperties() override;
47     const CORE_NS::IPropertyHandle* GetProperties() const override;
48     void SetProperties(const CORE_NS::IPropertyHandle&) override;
49 
50     bool IsActive() const override;
51     void SetActive(bool state) override;
52 
53     void Initialize() override;
54     bool Update(bool frameRenderingQueued, uint64_t time, uint64_t delta) override;
55     void Uninitialize() override;
56 
57     const CORE_NS::IEcs& GetECS() const override;
58 
59     // INodeSystem.
60     ISceneNode& GetRootNode() const override;
61 
62     ISceneNode* GetNode(CORE_NS::Entity entity) const override;
63 
64     ISceneNode* CreateNode() override;
65 
66     ISceneNode* CloneNode(const ISceneNode& node, bool recursive) override;
67 
68     void DestroyNode(ISceneNode& rootNode) override;
69 
70     void AddListener(SceneNodeListener& listener) override;
71     void RemoveListener(SceneNodeListener& listener) override;
72 
73 private:
74     class NodeAccess;
75     class SceneNode;
76     class NodeCache;
77     struct State;
78     struct NodeInfo;
79 
80     void CollectChangedNodes(ISceneNode& node, BASE_NS::vector<ISceneNode*>& result);
81     NodeInfo ProcessNode(SceneNode* node, const bool parentEnabled, const CORE_NS::ComponentQuery::ResultRow* row);
82     void UpdateTransformations(ISceneNode& node, BASE_NS::Math::Mat4X4 const& matrix, bool enabled);
83     void GatherNodeEntities(const ISceneNode& node, BASE_NS::vector<CORE_NS::Entity>& entities) const;
84     bool UpdatePreviousWorldMatrices();
85 
86     CORE_NS::IEcs& ecs_;
87     bool active_ = true;
88 
89     INameComponentManager& nameManager_;
90     INodeComponentManager& nodeManager_;
91     ITransformComponentManager& transformManager_;
92     ILocalMatrixComponentManager& localMatrixManager_;
93     IWorldMatrixComponentManager& worldMatrixManager_;
94     IPreviousWorldMatrixComponentManager& prevWorldMatrixManager_;
95 
96     BASE_NS::unique_ptr<NodeCache> cache_;
97 
98     CORE_NS::ComponentQuery nodeQuery_;
99 
100     uint32_t localMatrixGeneration_ = 0;
101     uint32_t worldMatrixGeneration_ = 0;
102     uint32_t nodeGeneration_ = 0;
103 };
104 CORE3D_END_NAMESPACE()
105 
106 #endif // CORE_ECS_NODESYSTEM_H
107