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 #include "text_node.h" 17 18 #include <scene/interface/intf_material.h> 19 20 #include "core/ecs.h" 21 SCENE_BEGIN_NAMESPACE()22SCENE_BEGIN_NAMESPACE() 23 24 CORE_NS::Entity TextNode::CreateEntity(const IInternalScene::Ptr& scene) 25 { 26 auto ecs = scene->GetEcsContext().GetNativeEcs(); 27 auto textManager = CORE_NS::GetManager<TEXT3D_NS::ITextComponentManager>(*ecs); 28 auto materialManager = CORE_NS::GetManager<CORE3D_NS::IMaterialComponentManager>(*ecs); 29 if (!textManager || !materialManager) { 30 return {}; 31 } 32 auto entity = ecs->GetEntityManager().Create(); 33 scene->GetEcsContext().AddDefaultComponents(entity); 34 textManager->Create(entity); 35 materialManager->Create(entity); 36 return entity; 37 } 38 SetEcsObject(const IEcsObject::Ptr & o)39bool TextNode::SetEcsObject(const IEcsObject::Ptr& o) 40 { 41 if (Super::SetEcsObject(o)) { 42 auto att = GetSelf<META_NS::IAttach>()->GetAttachments<IText>(); 43 if (!att.empty()) { 44 return true; 45 } 46 if (auto att = GetSelf<META_NS::IAttach>()) { 47 if (auto c = att->GetAttachmentContainer(true)->FindByName<IEcsObjectAccess>("MaterialComponent")) { 48 auto mat = META_NS::GetObjectRegistry().Create<IMaterial>(ClassId::Material); 49 auto matatt = interface_cast<META_NS::IAttach>(mat); 50 auto matacc = interface_cast<IEcsObjectAccess>(mat); 51 if (matatt && matacc) { 52 matatt->Attach(c); 53 if (matacc->SetEcsObject(c->GetEcsObject())) { 54 fontColor_ = mat->Textures()->GetValueAt(0); 55 } 56 } 57 } 58 } 59 } 60 return fontColor_ != nullptr; 61 } 62 63 SCENE_END_NAMESPACE()