18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#ifndef HIERARCHY_CONTROLLER_H
178bf80f4bSopenharmony_ci#define HIERARCHY_CONTROLLER_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include <scene_plugin/api/node.h>
208bf80f4bSopenharmony_ci#include <scene_plugin/interface/intf_hierarchy_controller.h>
218bf80f4bSopenharmony_ci#include <shared_mutex>
228bf80f4bSopenharmony_ci
238bf80f4bSopenharmony_ci#include <meta/ext/attachment/attachment.h>
248bf80f4bSopenharmony_ci#include <meta/interface/builtin_objects.h>
258bf80f4bSopenharmony_ci#include <meta/interface/intf_object_hierarchy_observer.h>
268bf80f4bSopenharmony_ci#include <meta/interface/intf_task_queue.h>
278bf80f4bSopenharmony_ci
288bf80f4bSopenharmony_ciSCENE_BEGIN_NAMESPACE()
298bf80f4bSopenharmony_ci
308bf80f4bSopenharmony_ciclass NodeHierarchyController
318bf80f4bSopenharmony_ci    : public META_NS::MetaObjectFwd<NodeHierarchyController, SCENE_NS::ClassId::NodeHierarchyController,
328bf80f4bSopenharmony_ci          META_NS::ClassId::MetaObject, INodeHierarchyController, META_NS::IObjectHierarchyObserver> {
338bf80f4bSopenharmony_cipublic: // ILifeCycle
348bf80f4bSopenharmony_ci    bool Build(const META_NS::IMetadata::Ptr& data) override;
358bf80f4bSopenharmony_ci    void Destroy() override;
368bf80f4bSopenharmony_ci
378bf80f4bSopenharmony_cipublic: // INodeHierarchyController
388bf80f4bSopenharmony_ci    bool AttachAll() override;
398bf80f4bSopenharmony_ci    bool DetachAll() override;
408bf80f4bSopenharmony_ci    BASE_NS::vector<INode::Ptr> GetAllNodes() const override;
418bf80f4bSopenharmony_ci
428bf80f4bSopenharmony_cipublic: // IObjectHierarchyObserver
438bf80f4bSopenharmony_ci    void SetTarget(const META_NS::IObject::Ptr& root, META_NS::HierarchyChangeModeValue mode) override;
448bf80f4bSopenharmony_ci    META_NS::IObject::Ptr GetTarget() const override;
458bf80f4bSopenharmony_ci    BASE_NS::vector<IObject::Ptr> GetAllObserved() const override;
468bf80f4bSopenharmony_ci    META_FORWARD_EVENT(META_NS::IOnHierarchyChanged, OnHierarchyChanged, observer_->OnHierarchyChanged())
478bf80f4bSopenharmony_ci
488bf80f4bSopenharmony_ciprivate:
498bf80f4bSopenharmony_ci    void AttachHierarchy(const IObject::Ptr& root);
508bf80f4bSopenharmony_ci    void DetachHierarchy(const IObject::Ptr& root);
518bf80f4bSopenharmony_ci    void AttachNode(INode* const startable);
528bf80f4bSopenharmony_ci    void DetachNode(INode* const startable);
538bf80f4bSopenharmony_ci    void HierarchyChanged(const META_NS::HierarchyChangedInfo& info);
548bf80f4bSopenharmony_ci
558bf80f4bSopenharmony_ci    META_NS::IObject::WeakPtr target_;
568bf80f4bSopenharmony_ci    META_NS::IObjectHierarchyObserver::Ptr observer_;
578bf80f4bSopenharmony_ci
588bf80f4bSopenharmony_ciprivate: // Task queue handling
598bf80f4bSopenharmony_ci    struct NodeOperation {
608bf80f4bSopenharmony_ci        enum Operation {
618bf80f4bSopenharmony_ci            /** Run AttachHierarchy() for root_ */
628bf80f4bSopenharmony_ci            ATTACH,
638bf80f4bSopenharmony_ci            /** Run DetachHierarchy() for root_ */
648bf80f4bSopenharmony_ci            DETACH,
658bf80f4bSopenharmony_ci        };
668bf80f4bSopenharmony_ci
678bf80f4bSopenharmony_ci        Operation operation_;
688bf80f4bSopenharmony_ci        META_NS::IObject::WeakPtr root_;
698bf80f4bSopenharmony_ci    };
708bf80f4bSopenharmony_ci
718bf80f4bSopenharmony_ci    bool RunOperation(NodeOperation operation);
728bf80f4bSopenharmony_ci};
738bf80f4bSopenharmony_ci
748bf80f4bSopenharmony_ciSCENE_END_NAMESPACE()
758bf80f4bSopenharmony_ci
768bf80f4bSopenharmony_ci#endif // HIERARCHY_CONTROLLER_H
77