• 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_EXT_ECS_LAZY_PROPERTY_H
17 #define SCENE_EXT_ECS_LAZY_PROPERTY_H
18 
19 #include <scene/base/namespace.h>
20 #include <scene/ext/intf_ecs_object.h>
21 #include <scene/ext/intf_ecs_object_access.h>
22 #include <scene/ext/intf_engine_property_init.h>
23 #include <scene/ext/util.h>
24 
25 #include <meta/ext/object.h>
26 
SCENE_BEGIN_NAMESPACE()27 SCENE_BEGIN_NAMESPACE()
28 
29 class EcsLazyProperty
30     : public META_NS::IntroduceInterfaces<META_NS::MetaObject, IEcsObjectAccess, IEnginePropertyInit> {
31 public:
32     bool SetEcsObject(const IEcsObject::Ptr& obj) override
33     {
34         object_ = obj;
35         return object_ != nullptr;
36     }
37     IEcsObject::Ptr GetEcsObject() const override
38     {
39         return object_;
40     }
41     IInternalScene::Ptr GetInternalScene() const
42     {
43         return object_ ? object_->GetScene() : nullptr;
44     }
45     CORE_NS::Entity GetEntity() const
46     {
47         return object_ ? object_->GetEntity() : CORE_NS::Entity {};
48     }
49 
50 protected:
51     bool AttachEngineProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path) override
52     {
53         return object_->AttachProperty(p, path).GetResult();
54     }
55     bool InitDynamicProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path) override
56     {
57         return AttachEngineProperty(p, path);
58     }
59 
60 protected:
61     IEcsObject::Ptr object_;
62 };
63 
64 class NamedSceneObject : public META_NS::IntroduceInterfaces<EcsLazyProperty, META_NS::INamed> {
META_OBJECT_NO_CLASSINFO(NamedSceneObject,IntroduceInterfaces)65     META_OBJECT_NO_CLASSINFO(NamedSceneObject, IntroduceInterfaces)
66 
67     META_BEGIN_STATIC_DATA()
68     META_STATIC_FORWARDED_PROPERTY_DATA(META_NS::INamed, BASE_NS::string, Name)
69     META_END_STATIC_DATA()
70 
71     META_FORWARD_PROPERTY(BASE_NS::string, Name, EcsNameProperty())
72 
73 public:
74     META_NS::IProperty::Ptr EcsNameProperty() const
75     {
76         auto i = interface_cast<META_NS::INamed>(object_);
77         return i ? i->Name() : nullptr;
78     }
79 
GetName()80     BASE_NS::string GetName() const override
81     {
82         return META_NS::GetValue(Name());
83     }
84 };
85 
86 SCENE_END_NAMESPACE()
87 
88 #endif
89