From 37c17bdb5cc799b134801f4302f27eee08cca8c2 Mon Sep 17 00:00:00 2001 From: Atdunbg <979541498@qq.com> Date: Sun, 6 Jul 2025 12:58:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8lambda=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=92=8C=E6=A8=A1=E6=9D=BF=E5=87=BD=E6=95=B0=E9=87=8D=E6=9E=84?= =?UTF-8?q?Copy=E7=BB=84=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hazel/src/Hazel/Scene/Components.h | 10 ++++++ Hazel/src/Hazel/Scene/Entity.h | 2 +- Hazel/src/Hazel/Scene/Scene.cpp | 50 ++++++++++++++++++++---------- Sandbox/src/Editor/EditorLayer.cpp | 5 ++- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/Hazel/src/Hazel/Scene/Components.h b/Hazel/src/Hazel/Scene/Components.h index 8d21382..5c843bb 100644 --- a/Hazel/src/Hazel/Scene/Components.h +++ b/Hazel/src/Hazel/Scene/Components.h @@ -159,6 +159,16 @@ namespace Hazel CircleCollider2DComponent() = default; CircleCollider2DComponent(const CircleCollider2DComponent&) = default; }; + + template + struct ComponentGroup + { + }; + using AllComponents = + ComponentGroup; } diff --git a/Hazel/src/Hazel/Scene/Entity.h b/Hazel/src/Hazel/Scene/Entity.h index e26fbbd..b043990 100644 --- a/Hazel/src/Hazel/Scene/Entity.h +++ b/Hazel/src/Hazel/Scene/Entity.h @@ -35,7 +35,7 @@ namespace Hazel } template - T& AddOrRelpaceComponent(Args&&... args) + T& AddOrReplaceComponent(Args&&... args) { T& component = m_Scene->m_Registry.emplace_or_replace(m_EntityHandle, std::forward(args)...); m_Scene->OnComponentAdded(*this, component); diff --git a/Hazel/src/Hazel/Scene/Scene.cpp b/Hazel/src/Hazel/Scene/Scene.cpp index ece8b7b..6bc3301 100644 --- a/Hazel/src/Hazel/Scene/Scene.cpp +++ b/Hazel/src/Hazel/Scene/Scene.cpp @@ -40,31 +40,42 @@ namespace Hazel delete m_PhysicsWorld; } - template + template static void CopyComponent(entt::registry& dst, entt::registry& src, const std::unordered_map& enttMap) { - auto view = src.view(); - for (auto e : view) + + ([&]() { - UUID uuid = src.get(e).ID; - if (enttMap.find(uuid) == enttMap.end()) + auto view = src.view(); + for (auto srcEntity : view) { - HZ_CORE_ERROR("ERROR"); - assert(-1); + entt::entity dstEntity = enttMap.at(src.get(srcEntity).ID); + + auto& srcComponent = src.get(srcEntity); + dst.emplace_or_replace(dstEntity, srcComponent); } - - entt::entity entity = enttMap.at(uuid); - auto& component = view.get(e); - - dst.emplace_or_replace(entity, component); - } + }(), ...); } - template + template + static void CopyComponent(ComponentGroup, entt::registry& dst, entt::registry& src, const std::unordered_map& enttMap) + { + CopyComponent(dst, src, enttMap); + } + + template static void CopyComponentIfExists(Entity dst, Entity src) { - if (src.HasComponent()) - dst.AddOrRelpaceComponent(src.GetComponent()); + ([&](){ + if (src.HasComponent()) + dst.AddOrReplaceComponent(src.GetComponent()); + }(), ...); + } + + template + static void CopyComponentIfExists(ComponentGroup, Entity dst, Entity src) + { + CopyComponentIfExists(dst, src); } Ref Scene::Copy(Ref other) @@ -89,6 +100,9 @@ namespace Hazel enttMap[uuid] = (entt::entity)newEntity; } + // Copy all components except IDComponent and TagComponent + CopyComponent(AllComponents{}, dstSceneRegistry, srcSceneRegistry, enttMap); + /* CopyComponent(dstSceneRegistry, srcSceneRegistry, enttMap); CopyComponent(dstSceneRegistry, srcSceneRegistry, enttMap); CopyComponent(dstSceneRegistry, srcSceneRegistry, enttMap); @@ -97,6 +111,7 @@ namespace Hazel CopyComponent(dstSceneRegistry, srcSceneRegistry, enttMap); CopyComponent(dstSceneRegistry, srcSceneRegistry, enttMap); CopyComponent(dstSceneRegistry, srcSceneRegistry, enttMap); + */ return newScene; } @@ -388,6 +403,8 @@ namespace Hazel { Entity newEntity = CreateEntity(entity.GetName() + " copy"); + CopyComponentIfExists(AllComponents{}, newEntity, entity); + /* CopyComponentIfExists(newEntity, entity); CopyComponentIfExists(newEntity, entity); CopyComponentIfExists(newEntity, entity); @@ -396,6 +413,7 @@ namespace Hazel CopyComponentIfExists(newEntity, entity); CopyComponentIfExists(newEntity, entity); CopyComponentIfExists(newEntity, entity); + */ } Entity Scene::GetPrimaryCameraEntity() diff --git a/Sandbox/src/Editor/EditorLayer.cpp b/Sandbox/src/Editor/EditorLayer.cpp index 7e78571..d484384 100644 --- a/Sandbox/src/Editor/EditorLayer.cpp +++ b/Sandbox/src/Editor/EditorLayer.cpp @@ -146,7 +146,6 @@ namespace Hazel if (m_ShowPhysicsColliders) { // box collider - auto bcview = m_ActiveScene->GetAllEntitiesWith(); for (auto entity : bcview) { @@ -169,7 +168,7 @@ namespace Hazel Renderer2D::DrawRect(transform, glm::vec4(0.2f, 1.0f, 0.2f, 1.0f)); } - + // circle collider auto ccview = m_ActiveScene->GetAllEntitiesWith(); for (auto entity : ccview) { @@ -562,7 +561,7 @@ namespace Hazel m_CameraController.OnEvent(e); m_EditorCamera.OnEvent(e); } - if (e.button.clicks && m_ViewportHovered && !ImGuizmo::IsOver() && Input::IsMouseButtonPressed(SDL_BUTTON_LEFT) && !Input::IsKeyPressed(SDL_SCANCODE_LALT)) + if (e.button.clicks && m_ViewportHovered && !ImGuizmo::IsOver() && Input::IsMouseButtonPressed(BUTTON_LEFT) && !Input::IsKeyPressed(SDL_SCANCODE_LALT)) m_SceneHierachyPanel.SetSelectedEntity(m_HoveredEntity); #define SHORTCUT_EXIT (SDL_KMOD_CTRL + SDLK_W)