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 HIERARCHY_CONTROLLER_H
17 #define HIERARCHY_CONTROLLER_H
18 
19 #include <scene_plugin/api/node.h>
20 #include <scene_plugin/interface/intf_hierarchy_controller.h>
21 #include <shared_mutex>
22 
23 #include <meta/ext/attachment/attachment.h>
24 #include <meta/interface/builtin_objects.h>
25 #include <meta/interface/intf_object_hierarchy_observer.h>
26 #include <meta/interface/intf_task_queue.h>
27 
SCENE_BEGIN_NAMESPACE()28 SCENE_BEGIN_NAMESPACE()
29 
30 class NodeHierarchyController
31     : public META_NS::MetaObjectFwd<NodeHierarchyController, SCENE_NS::ClassId::NodeHierarchyController,
32           META_NS::ClassId::MetaObject, INodeHierarchyController, META_NS::IObjectHierarchyObserver> {
33 public: // ILifeCycle
34     bool Build(const META_NS::IMetadata::Ptr& data) override;
35     void Destroy() override;
36 
37 public: // INodeHierarchyController
38     bool AttachAll() override;
39     bool DetachAll() override;
40     BASE_NS::vector<INode::Ptr> GetAllNodes() const override;
41 
42 public: // IObjectHierarchyObserver
43     void SetTarget(const META_NS::IObject::Ptr& root, META_NS::HierarchyChangeModeValue mode) override;
44     META_NS::IObject::Ptr GetTarget() const override;
45     BASE_NS::vector<IObject::Ptr> GetAllObserved() const override;
46     META_FORWARD_EVENT(META_NS::IOnHierarchyChanged, OnHierarchyChanged, observer_->OnHierarchyChanged())
47 
48 private:
49     void AttachHierarchy(const IObject::Ptr& root);
50     void DetachHierarchy(const IObject::Ptr& root);
51     void AttachNode(INode* const startable);
52     void DetachNode(INode* const startable);
53     void HierarchyChanged(const META_NS::HierarchyChangedInfo& info);
54 
55     META_NS::IObject::WeakPtr target_;
56     META_NS::IObjectHierarchyObserver::Ptr observer_;
57 
58 private: // Task queue handling
59     struct NodeOperation {
60         enum Operation {
61             /** Run AttachHierarchy() for root_ */
62             ATTACH,
63             /** Run DetachHierarchy() for root_ */
64             DETACH,
65         };
66 
67         Operation operation_;
68         META_NS::IObject::WeakPtr root_;
69     };
70 
71     bool RunOperation(NodeOperation operation);
72 };
73 
74 SCENE_END_NAMESPACE()
75 
76 #endif // HIERARCHY_CONTROLLER_H
77