• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPONENT_FACTORIES_H
17 #define SCENE_SRC_COMPONENT_FACTORIES_H
18 
19 #include <scene/ext/intf_internal_scene.h>
20 #include <text_3d/ecs/components/text_component.h>
21 
22 #include "component/animation_component.h"
23 #include "component/camera_component.h"
24 #include "component/environment_component.h"
25 #include "component/layer_component.h"
26 #include "component/light_component.h"
27 #include "component/material_component.h"
28 #include "component/mesh_component.h"
29 #include "component/node_component.h"
30 #include "component/postprocess_component.h"
31 #include "component/text_component.h"
32 #include "component/transform_component.h"
33 
34 SCENE_BEGIN_NAMESPACE()
35 
36 struct ComponentFactory : public META_NS::IntroduceInterfaces<IComponentFactory> {
ComponentFactoryComponentFactory37     ComponentFactory(META_NS::ClassInfo info) : info_(info) {}
38 
CreateComponentComponentFactory39     IComponent::Ptr CreateComponent(const IEcsObject::Ptr& eobj) override
40     {
41         auto p = META_NS::GetObjectRegistry().Create<IComponent>(info_);
42         if (auto acc = interface_cast<IEcsObjectAccess>(p)) {
43             if (!acc->SetEcsObject(eobj)) {
44                 return nullptr;
45             }
46         }
47         return p;
48     }
49 
50 private:
51     META_NS::ClassInfo info_;
52 };
53 
AddBuiltinComponentFactories(IInternalScene::Ptr s)54 inline void AddBuiltinComponentFactories(IInternalScene::Ptr s)
55 {
56     s->RegisterComponent(
57         CORE3D_NS::ITransformComponentManager::UID, CreateShared<ComponentFactory>(ClassId::TransformComponent));
58     s->RegisterComponent(
59         CORE3D_NS::ICameraComponentManager::UID, CreateShared<ComponentFactory>(ClassId::CameraComponent));
60     s->RegisterComponent(
61         CORE3D_NS::ILightComponentManager::UID, CreateShared<ComponentFactory>(ClassId::LightComponent));
62     s->RegisterComponent(
63         CORE3D_NS::IPostProcessComponentManager::UID, CreateShared<ComponentFactory>(ClassId::PostProcessComponent));
64     s->RegisterComponent(
65         CORE3D_NS::IAnimationComponentManager::UID, CreateShared<ComponentFactory>(ClassId::AnimationComponent));
66     s->RegisterComponent(
67         CORE3D_NS::IEnvironmentComponentManager::UID, CreateShared<ComponentFactory>(ClassId::EnvironmentComponent));
68     s->RegisterComponent(
69         CORE3D_NS::IMaterialComponentManager::UID, CreateShared<ComponentFactory>(ClassId::MaterialComponent));
70     s->RegisterComponent(CORE3D_NS::IMeshComponentManager::UID, CreateShared<ComponentFactory>(ClassId::MeshComponent));
71     s->RegisterComponent(
72         CORE3D_NS::IRenderMeshComponentManager::UID, CreateShared<ComponentFactory>(ClassId::RenderMeshComponent));
73     s->RegisterComponent(
74         CORE3D_NS::ILayerComponentManager::UID, CreateShared<ComponentFactory>(ClassId::LayerComponent));
75     s->RegisterComponent(CORE3D_NS::INodeComponentManager::UID, CreateShared<ComponentFactory>(ClassId::NodeComponent));
76     s->RegisterComponent(TEXT3D_NS::ITextComponentManager::UID, CreateShared<ComponentFactory>(ClassId::TextComponent));
77 }
78 
79 SCENE_END_NAMESPACE()
80 
81 #endif