WIP: add select on click
This commit is contained in:
parent
e79a629257
commit
6487acadca
|
|
@ -28,15 +28,19 @@ namespace Nz
|
||||||
auto& registry = m_world->GetRegistry();
|
auto& registry = m_world->GetRegistry();
|
||||||
for (auto&& entity : registry.storage<entt::entity>().each())
|
for (auto&& entity : registry.storage<entt::entity>().each())
|
||||||
{
|
{
|
||||||
Nz::GraphicsComponent* component = registry.try_get<Nz::GraphicsComponent>(std::get<entt::entity>(entity));
|
Nz::GraphicsComponent* graphics = registry.try_get<Nz::GraphicsComponent>(std::get<entt::entity>(entity));
|
||||||
if (component != nullptr)
|
Nz::NodeComponent* transform = registry.try_get<Nz::NodeComponent>(std::get<entt::entity>(entity));
|
||||||
|
if (transform != nullptr && graphics != nullptr)
|
||||||
{
|
{
|
||||||
|
BoundingVolumef boundingVolume(graphics->GetAABB());
|
||||||
|
boundingVolume.Update(transform->GetTransformMatrix());
|
||||||
|
|
||||||
float distance = 0;
|
float distance = 0;
|
||||||
if (ray.Intersect(component->GetAABB(), &distance))
|
if (ray.Intersect(boundingVolume, &distance))
|
||||||
{
|
{
|
||||||
entities.push_back({
|
entities.push_back({
|
||||||
.entity = entt::handle(registry, std::get<entt::entity>(entity)),
|
.entity = entt::handle(registry, std::get<entt::entity>(entity)),
|
||||||
.position = ray.origin + ray.direction * distance,
|
.position = ray.GetPoint(distance),
|
||||||
.distance = distance
|
.distance = distance
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
#include <NazaraEditor/Core/Components/CameraComponent.hpp>
|
#include <NazaraEditor/Core/Components/CameraComponent.hpp>
|
||||||
|
|
||||||
#include <NazaraEditor/Core/Application/BaseApplication.hpp>
|
#include <NazaraEditor/Core/Application/BaseApplication.hpp>
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/Components/CameraComponent.hpp>
|
||||||
|
#include <Nazara/Math/Ray.hpp>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// Unity SmoothDamp function
|
// Unity SmoothDamp function
|
||||||
|
|
@ -43,8 +45,10 @@ namespace
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
EditorCameraComponent::EditorCameraComponent()
|
EditorCameraComponent::EditorCameraComponent(Nz::Camera& camera, Nz::DebugDrawer& debugDrawer)
|
||||||
: m_targetAngles(Nz::EulerAnglesf::Zero())
|
: m_camera(camera)
|
||||||
|
, m_debugDrawer(debugDrawer)
|
||||||
|
, m_targetAngles(Nz::EulerAnglesf::Zero())
|
||||||
, m_targetPosition(Nz::Vector3f::Zero())
|
, m_targetPosition(Nz::Vector3f::Zero())
|
||||||
, m_currentVelocity(Nz::Vector3f::Zero())
|
, m_currentVelocity(Nz::Vector3f::Zero())
|
||||||
, m_moveSpeed(3.f)
|
, m_moveSpeed(3.f)
|
||||||
|
|
@ -52,6 +56,14 @@ namespace Nz
|
||||||
{
|
{
|
||||||
auto& handler = EditorBaseApplication::Instance()->GetWindow()->GetEventHandler();
|
auto& handler = EditorBaseApplication::Instance()->GetWindow()->GetEventHandler();
|
||||||
|
|
||||||
|
m_onMouseClicked.Connect(handler.OnMouseButtonReleased, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseButtonEvent& event)
|
||||||
|
{
|
||||||
|
if (event.button == Nz::Mouse::Button::Left)
|
||||||
|
{
|
||||||
|
RaycastSelection(event.x, event.y);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
m_onMouseMoved.Connect(handler.OnMouseMoved, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
|
m_onMouseMoved.Connect(handler.OnMouseMoved, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
|
||||||
{
|
{
|
||||||
if (Nz::Mouse::IsButtonPressed(Nz::Mouse::Button::Right))
|
if (Nz::Mouse::IsButtonPressed(Nz::Mouse::Button::Right))
|
||||||
|
|
@ -103,5 +115,31 @@ namespace Nz
|
||||||
|
|
||||||
node.SetPosition(currentPosition);
|
node.SetPosition(currentPosition);
|
||||||
node.SetRotation(currentRotation);
|
node.SetRotation(currentRotation);
|
||||||
|
|
||||||
|
if (!m_debugClock.IsPaused())
|
||||||
|
{
|
||||||
|
if (m_debugClock.GetElapsedTime().AsSeconds() > 100)
|
||||||
|
m_debugClock.Pause();
|
||||||
|
|
||||||
|
m_debugDrawer.DrawLine(m_lastRay.origin, m_lastRay.direction * 500 + m_lastRay.origin, Nz::Color::Green());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EditorCameraComponent::RaycastSelection(int x, int y)
|
||||||
|
{
|
||||||
|
auto near = m_camera.Unproject(Nz::Vector3f(x, y, 0));
|
||||||
|
auto far = m_camera.Unproject(Nz::Vector3f(x, y, 1));
|
||||||
|
|
||||||
|
Nz::Rayf ray(near, (far - near).Normalize());
|
||||||
|
|
||||||
|
auto entities = EditorBaseApplication::Instance()->GetLevel().Raycast(ray);
|
||||||
|
|
||||||
|
if (!entities.empty())
|
||||||
|
{
|
||||||
|
EditorBaseApplication::Instance()->OnEntitySelected(entities.front().entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_lastRay = ray;
|
||||||
|
m_debugClock.Restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue