Compare commits

...

5 Commits

Author SHA1 Message Date
37c17bdb5c 使用lambda函数和模板函数重构Copy组件功能 2025-07-06 12:58:48 +08:00
fadb73243d use KeyCode MouseButton replace int 2025-07-06 12:57:00 +08:00
ab4ca6285e use KeyCode MouseButton replace int 2025-07-06 12:56:22 +08:00
52463322dd 简单解决距离小于1 缩放bug 2025-07-06 12:53:53 +08:00
0a24a2f3c7 add new clas 2025-07-06 12:51:23 +08:00
9 changed files with 89 additions and 28 deletions

View File

@ -7,6 +7,7 @@
#include <utility>
#include "Core.h"
#include "KeyCodes.h"
namespace Hazel
{
@ -14,9 +15,9 @@ namespace Hazel
public:
virtual ~Input() = default;
static bool IsKeyPressed(int key);
static bool IsKeyPressed(KeyCode key);
static bool IsMouseButtonPressed(int button);
static bool IsMouseButtonPressed(MouseButton button);
static std::pair<float, float> GetMousePosition();
static float GetMouseX();

View File

@ -0,0 +1,5 @@
//
// Created by sfd on 25-6-27.
//
#include "KeyCodes.h"

View File

@ -0,0 +1,28 @@
//
// Created by sfd on 25-6-27.
//
#ifndef KEYCODES_H
#define KEYCODES_H
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_mouse.h>
namespace Hazel
{
using KeyCode = SDL_Scancode;
typedef enum MouseButton
{
BUTTON_LEFT = SDL_BUTTON_LEFT,
BUTTON_MIDDLE = SDL_BUTTON_MIDDLE,
BUTTON_RIGHT = SDL_BUTTON_RIGHT,
BUTTON_X1 = SDL_BUTTON_X1,
BUTTON_X2 = SDL_BUTTON_X2,
}MouseButton;
//TODO: use self keycode, not use sdl directly
}
#endif //KEYCODES_H

View File

@ -70,11 +70,11 @@ namespace Hazel
const glm::vec2 delta = (mouse - m_InitialMousePosition) * 0.003f;
m_InitialMousePosition = mouse;
if (Input::IsMouseButtonPressed(SDL_BUTTON_MIDDLE))
if (Input::IsMouseButtonPressed(BUTTON_MIDDLE))
MousePan(delta);
else if (Input::IsMouseButtonPressed(SDL_BUTTON_LEFT))
else if (Input::IsMouseButtonPressed(BUTTON_LEFT))
MouseRotate(delta);
else if (Input::IsMouseButtonPressed(SDL_BUTTON_RIGHT))
else if (Input::IsMouseButtonPressed(BUTTON_RIGHT))
MouseZoom(-delta.y);
}
@ -118,7 +118,7 @@ namespace Hazel
m_Distance -= delta * ZoomSpeed();
if (m_Distance < 1.0f)
{
m_FocalPoint += GetForwardDirection();
// m_FocalPoint += GetForwardDirection();
m_Distance = 1.0f;
}
}

View File

@ -159,6 +159,16 @@ namespace Hazel
CircleCollider2DComponent() = default;
CircleCollider2DComponent(const CircleCollider2DComponent&) = default;
};
template<typename... Components>
struct ComponentGroup
{
};
using AllComponents =
ComponentGroup<TransformComponent, SpriteRendererComponent,
CircleRendererComponent, BoxCollider2DComponent,
CircleCollider2DComponent, CameraComponent,
NativeScriptComponent, RigidBody2DComponent>;
}

View File

@ -35,7 +35,7 @@ namespace Hazel
}
template<typename T, typename... Args>
T& AddOrRelpaceComponent(Args&&... args)
T& AddOrReplaceComponent(Args&&... args)
{
T& component = m_Scene->m_Registry.emplace_or_replace<T>(m_EntityHandle, std::forward<Args>(args)...);
m_Scene->OnComponentAdded<T>(*this, component);

View File

@ -40,31 +40,42 @@ namespace Hazel
delete m_PhysicsWorld;
}
template<typename Component>
template<typename... Component>
static void CopyComponent(entt::registry& dst, entt::registry& src, const std::unordered_map<UUID, entt::entity>& enttMap)
{
auto view = src.view<Component>();
for (auto e : view)
([&]()
{
UUID uuid = src.get<IDComponent>(e).ID;
if (enttMap.find(uuid) == enttMap.end())
auto view = src.view<Component>();
for (auto srcEntity : view)
{
HZ_CORE_ERROR("ERROR");
assert(-1);
entt::entity dstEntity = enttMap.at(src.get<IDComponent>(srcEntity).ID);
auto& srcComponent = src.get<Component>(srcEntity);
dst.emplace_or_replace<Component>(dstEntity, srcComponent);
}
entt::entity entity = enttMap.at(uuid);
auto& component = view.get<Component>(e);
dst.emplace_or_replace<Component>(entity, component);
}
}(), ...);
}
template<typename Component>
template<typename... Component>
static void CopyComponent(ComponentGroup<Component ...>, entt::registry& dst, entt::registry& src, const std::unordered_map<UUID, entt::entity>& enttMap)
{
CopyComponent<Component ...>(dst, src, enttMap);
}
template<typename... Component>
static void CopyComponentIfExists(Entity dst, Entity src)
{
if (src.HasComponent<Component>())
dst.AddOrRelpaceComponent<Component>(src.GetComponent<Component>());
([&](){
if (src.HasComponent<Component>())
dst.AddOrReplaceComponent<Component>(src.GetComponent<Component>());
}(), ...);
}
template<typename... Component>
static void CopyComponentIfExists(ComponentGroup<Component ...>, Entity dst, Entity src)
{
CopyComponentIfExists<Component ...>(dst, src);
}
Ref<Scene> Scene::Copy(Ref<Scene> 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<TransformComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<SpriteRendererComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<CircleRendererComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
@ -97,6 +111,7 @@ namespace Hazel
CopyComponent<RigidBody2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<BoxCollider2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<CircleCollider2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
*/
return newScene;
}
@ -388,6 +403,8 @@ namespace Hazel
{
Entity newEntity = CreateEntity(entity.GetName() + " copy");
CopyComponentIfExists(AllComponents{}, newEntity, entity);
/*
CopyComponentIfExists<TransformComponent>(newEntity, entity);
CopyComponentIfExists<SpriteRendererComponent>(newEntity, entity);
CopyComponentIfExists<CircleRendererComponent>(newEntity, entity);
@ -396,6 +413,7 @@ namespace Hazel
CopyComponentIfExists<RigidBody2DComponent>(newEntity, entity);
CopyComponentIfExists<BoxCollider2DComponent>(newEntity, entity);
CopyComponentIfExists<CircleCollider2DComponent>(newEntity, entity);
*/
}
Entity Scene::GetPrimaryCameraEntity()

View File

@ -8,14 +8,14 @@
namespace Hazel
{
bool Input::IsKeyPressed(const int key)
bool Input::IsKeyPressed(const KeyCode key)
{
const auto state = SDL_GetKeyboardState(nullptr);
return state[key];
}
bool Input::IsMouseButtonPressed(const int button)
bool Input::IsMouseButtonPressed(const MouseButton button)
{
const auto mod = SDL_GetMouseState(nullptr, nullptr);
return mod & SDL_BUTTON_MASK(button);

View File

@ -146,7 +146,6 @@ namespace Hazel
if (m_ShowPhysicsColliders)
{
// box collider
auto bcview = m_ActiveScene->GetAllEntitiesWith<TransformComponent, BoxCollider2DComponent>();
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<TransformComponent, CircleCollider2DComponent>();
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)