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 SCENE_SRC_NODE_NODE_H 17 #define SCENE_SRC_NODE_NODE_H 18 19 #include <scene/ext/component.h> 20 #include <scene/ext/intf_ecs_object_access.h> 21 #include <scene/ext/intf_node_notify.h> 22 #include <scene/interface/intf_layer.h> 23 #include <scene/interface/intf_node.h> 24 #include <scene/interface/intf_node_import.h> 25 #include <scene/interface/intf_scene.h> 26 27 #include <meta/ext/implementation_macros.h> 28 #include <meta/ext/object.h> 29 #include <meta/interface/intf_containable.h> 30 #include <meta/interface/intf_container.h> 31 #include <meta/interface/intf_iterable.h> 32 33 #include "startable_handler.h" 34 35 SCENE_BEGIN_NAMESPACE() 36 37 struct ContainableTag {}; 38 class NodeContaineable : public META_NS::IContainable { 39 virtual META_NS::IObject::Ptr GetParent(ContainableTag) const = 0; GetParent()40 META_NS::IObject::Ptr GetParent() const override 41 { 42 return GetParent(ContainableTag {}); 43 } 44 }; 45 46 struct NodeTag {}; 47 class NodeInterface : public INode { 48 virtual Future<INode::Ptr> GetParent(NodeTag) const = 0; GetParent()49 Future<INode::Ptr> GetParent() const override 50 { 51 return GetParent(NodeTag {}); 52 } 53 }; 54 55 class Node : public META_NS::IntroduceInterfaces<NamedSceneObject, NodeInterface, INodeImport, META_NS::IIterable, 56 META_NS::IContainer, NodeContaineable, META_NS::IMetadataOwner, INodeNotify> { 57 META_OBJECT(Node, SCENE_NS::ClassId::Node, IntroduceInterfaces) 58 59 META_BEGIN_STATIC_DATA() 60 META_STATIC_FORWARDED_PROPERTY_DATA(ITransform, BASE_NS::Math::Vec3, Position) 61 META_STATIC_FORWARDED_PROPERTY_DATA(ITransform, BASE_NS::Math::Quat, Rotation) 62 META_STATIC_FORWARDED_PROPERTY_DATA(ITransform, BASE_NS::Math::Vec3, Scale) 63 META_STATIC_FORWARDED_PROPERTY_DATA(INode, bool, Enabled) 64 META_STATIC_EVENT_DATA(META_NS::IContainer, META_NS::IOnChildChanged, OnContainerChanged) 65 META_END_STATIC_DATA() 66 67 SCENE_USE_COMPONENT_PROPERTY(BASE_NS::Math::Vec3, Position, "TransformComponent") 68 SCENE_USE_COMPONENT_PROPERTY(BASE_NS::Math::Vec3, Scale, "TransformComponent") 69 SCENE_USE_COMPONENT_PROPERTY(BASE_NS::Math::Quat, Rotation, "TransformComponent") 70 SCENE_USE_COMPONENT_PROPERTY(bool, Enabled, "NodeComponent") 71 72 META_IMPLEMENT_EVENT(META_NS::IOnChildChanged, OnContainerChanged) 73 public: 74 Future<bool> IsEnabledInHierarchy() const override; 75 76 BASE_NS::string GetName() const override; 77 Future<BASE_NS::string> GetPath() const override; 78 using INode::GetParent; 79 Future<INode::Ptr> GetParent(NodeTag) const override; 80 Future<BASE_NS::vector<INode::Ptr>> GetChildren() const override; 81 Future<bool> RemoveChild(const INode::Ptr&) override; 82 Future<bool> AddChild(const INode::Ptr& child, size_t index = -1) override; 83 84 Future<INode::Ptr> ImportChild(const INode::ConstPtr& node) override; 85 Future<INode::Ptr> ImportChildScene(const IScene::ConstPtr& scene, BASE_NS::string_view nodeName) override; 86 Future<INode::Ptr> ImportChildScene(BASE_NS::string_view uri, BASE_NS::string_view nodeName) override; 87 bool SetEcsObject(const IEcsObject::Ptr& obj) override; 88 void TrackImportedEntities(BASE_NS::array_view<const CORE_NS::Entity> entities) override; 89 90 public: 91 bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override; 92 bool Detach(const IObject::Ptr& attachment) override; 93 94 public: 95 bool Build(const META_NS::IMetadata::Ptr&) override; 96 void Destroy() override; 97 IScene::Ptr GetScene() const override; 98 99 // IIterable 100 META_NS::IterationResult Iterate(const META_NS::IterationParameters& params) override; 101 META_NS::IterationResult Iterate(const META_NS::IterationParameters& params) const override; 102 103 // IContainer 104 BASE_NS::vector<IObject::Ptr> GetAll() const override; 105 IObject::Ptr GetAt(SizeType index) const override; 106 SizeType GetSize() const override; 107 BASE_NS::vector<IObject::Ptr> FindAll(const FindOptions& options) const override; 108 IObject::Ptr FindAny(const FindOptions& options) const override; 109 IObject::Ptr FindByName(BASE_NS::string_view name) const override; 110 bool Add(const IObject::Ptr& object) override; 111 bool Insert(SizeType index, const IObject::Ptr& object) override; 112 bool Remove(SizeType index) override; 113 bool Remove(const IObject::Ptr& child) override; 114 bool Move(SizeType fromIndex, SizeType toIndex) override; 115 bool Move(const IObject::Ptr& child, SizeType toIndex) override; 116 bool Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways) override; 117 void RemoveAll() override; 118 bool IsAncestorOf(const IObject::ConstPtr& object) const override; 119 META_NS::IObject::Ptr GetParent(ContainableTag) const override; 120 121 void OnMetadataConstructed(const META_NS::StaticMetadata& m, CORE_NS::IInterface& i) override; 122 void OnChildChanged(META_NS::ContainerChangeType type, const INode::Ptr& child, size_t index) override; 123 bool IsListening() const override; 124 void OnNodeActiveStateChanged(NodeActiteStateInfo state) override; 125 126 private: 127 BASE_NS::unique_ptr<StartableHandler> startableHandler_; 128 BASE_NS::vector<CORE_NS::Entity> imported_; 129 }; 130 131 SCENE_END_NAMESPACE() 132 133 #endif 134