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