• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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/ecs/intf_ecs.h>
27 #include <core/namespace.h>
28 
29 #include "property/property_handle.h"
30 
31 CORE3D_BEGIN_NAMESPACE()
32 class INameComponentManager;
33 class INodeComponentManager;
34 class ILocalMatrixComponentManager;
35 class IWorldMatrixComponentManager;
36 class ITransformComponentManager;
37 
38 class NodeSystem final : public INodeSystem, CORE_NS::IEcs::ComponentListener {
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     // IEcs::ComponentListener
74     void OnComponentEvent(CORE_NS::IEcs::ComponentListener::EventType type,
75         const CORE_NS::IComponentManager& componentManager,
76         BASE_NS::array_view<const CORE_NS::Entity> entities) override;
77 
78 private:
79     class NodeAccess;
80     class SceneNode;
81     class NodeCache;
82     struct State;
83     struct NodeInfo;
84 
85     BASE_NS::vector<ISceneNode*> CollectChangedNodes();
86     NodeInfo ProcessNode(SceneNode* node, const bool parentEnabled, const CORE_NS::ComponentQuery::ResultRow* row);
87     void UpdateTransformations(ISceneNode& node, BASE_NS::Math::Mat4X4 const& matrix, bool enabled);
88     void GatherNodeEntities(const ISceneNode& node, BASE_NS::vector<CORE_NS::Entity>& entities) const;
89     void UpdatePreviousWorldMatrices();
90 
91     CORE_NS::IEcs& ecs_;
92     bool active_ = true;
93 
94     INameComponentManager& nameManager_;
95     INodeComponentManager& nodeManager_;
96     ITransformComponentManager& transformManager_;
97     ILocalMatrixComponentManager& localMatrixManager_;
98     IWorldMatrixComponentManager& worldMatrixManager_;
99 
100     BASE_NS::unique_ptr<NodeCache> cache_;
101 
102     CORE_NS::ComponentQuery nodeQuery_;
103 
104     uint32_t localMatrixGeneration_ = 0;
105     uint32_t worldMatrixGeneration_ = 0;
106     uint32_t nodeGeneration_ = 0;
107 
108     BASE_NS::vector<CORE_NS::Entity> modifiedEntities_;
109     BASE_NS::vector<State> stack_;
110 };
111 CORE3D_END_NAMESPACE()
112 
113 #endif // CORE_ECS_NODESYSTEM_H
114