• 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_NAMED_SCENE_OBJECT_H
17 #define SCENE_EXT_NAMED_SCENE_OBJECT_H
18 
19 #include <scene/base/namespace.h>
20 #include <scene/ext/ecs_lazy_property_fwd.h>
21 
22 #include <meta/api/make_callback.h>
23 
SCENE_BEGIN_NAMESPACE()24 SCENE_BEGIN_NAMESPACE()
25 
26 class NamedSceneObject : public META_NS::IntroduceInterfaces<EcsLazyPropertyFwd, META_NS::INamed> {
27     META_OBJECT_NO_CLASSINFO(NamedSceneObject, IntroduceInterfaces)
28 
29 public:
30     META_BEGIN_STATIC_DATA()
31     META_STATIC_PROPERTY_DATA(META_NS::INamed, BASE_NS::string, Name)
32     META_END_STATIC_DATA()
33 
34     META_IMPLEMENT_PROPERTY(BASE_NS::string, Name)
35 
36     bool SetEcsObject(const IEcsObject::Ptr& obj) override
37     {
38         auto ret = Super::SetEcsObject(obj);
39         if (ret) {
40             Name()->SetValue(GetName());
41             Name()->OnChanged()->AddHandler(META_NS::MakeCallback<META_NS::IOnChanged>([&] {
42                 if (this->object_) {
43                     this->object_->SetName(Name()->GetValue());
44                 }
45             }));
46         }
47         return ret;
48     }
49 
50     BASE_NS::string GetName() const override
51     {
52         return this->object_ ? this->object_->GetName() : "";
53     }
54 };
55 
56 SCENE_END_NAMESPACE()
57 
58 #endif