Skip to content

Commit

Permalink
Improve SceneView mouse interaction (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
VTui22 authored Dec 7, 2023
1 parent a7059dd commit bb2aab4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Engine/Source/Editor/UILayers/SceneView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,12 @@ void SceneView::Update()
if (ImGui::IsWindowHovered())
{
isMouseInsideSeneView = true;
m_pCameraController->SetIsMouseInViewScene(true);
}
else
{
isMouseInsideSeneView = false;
m_pCameraController->SetIsMouseInViewScene(false);
}
// Draw scene.
ImGui::Image(reinterpret_cast<ImTextureID>(m_pRenderTarget->GetTextureHandle(0).idx),
Expand Down Expand Up @@ -382,12 +384,12 @@ void SceneView::Update()
m_isMouseDownFirstTime = false;
if (isMouseInsideSeneView && !m_isTerrainEditMode)
{
m_pCameraController->SetIsInViewScene(true);
m_pCameraController->SetIsFirstClickInViewScene(true);
m_isMouseShow = false;
}
else
{
m_pCameraController->SetIsInViewScene(false);
m_pCameraController->SetIsFirstClickInViewScene(false);
}
}
else
Expand Down
27 changes: 23 additions & 4 deletions Engine/Source/Runtime/Display/CameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,32 +163,39 @@ void CameraController::Update(float deltaTime)
MoveRight(m_movementSpeed * deltaTime);
}

if (Input::Get().IsKeyPressed(KeyCode::e))
if (Input::Get().IsKeyPressed(KeyCode::q))
{
m_isTracking = false;
MoveDown(m_movementSpeed * deltaTime);
}

if (Input::Get().IsKeyPressed(KeyCode::q) && !m_isMoving)
if (Input::Get().IsKeyPressed(KeyCode::e) && !m_isMoving)
{
m_isTracking = false;
MoveUp(m_movementSpeed * deltaTime);
}
}


if (Input::Get().IsMouseRBPressed() && !m_isMoving)
if (Input::Get().IsMouseLBPressed() && !m_isMoving && m_isFirstClickInViewScene && !ImGuizmo::IsUsing())
{
m_isTracking = false;
MoveFront(m_horizontalSensitivity * Input::Get().GetMousePositionOffsetY() * deltaTime);
Yaw(m_horizontalSensitivity * Input::Get().GetMousePositionOffsetX() * deltaTime);
}

if (Input::Get().IsMouseLBPressed() && m_isInViewScene && !ImGuizmo::IsUsing())
if (Input::Get().IsMouseRBPressed() && !m_isMoving && m_isFirstClickInViewScene && !ImGuizmo::IsUsing())
{
m_isTracking = false;
PitchLocal(m_verticalSensitivity * Input::Get().GetMousePositionOffsetY() * deltaTime);
Yaw(m_horizontalSensitivity * Input::Get().GetMousePositionOffsetX() * deltaTime);
}
if (Input::Get().GetMouseScrollOffsetY() && !m_isMoving && m_isMouseInViewScene && !ImGuizmo::IsUsing())
{
m_isTracking = false;
MoveForward(m_movementSpeed * Input::Get().GetMouseScrollOffsetY() * deltaTime * 10.0f);
}

}
}

Expand Down Expand Up @@ -261,6 +268,18 @@ void CameraController::MoveDown(float amount)
MoveUp(-amount);
}

void CameraController::MoveFront(float amount)
{
cd::Vec3f direction = cd::Vec3f(m_lookAt.x(), 0.0f, m_lookAt.z()).Normalize();
m_eye = m_eye - direction * amount;
ControllerToCamera();
}

void CameraController::MoveBack(float amount)
{
MoveFront(-amount);
}

void CameraController::Rotate(const cd::Vec3f& axis, float angleDegrees)
{
cd::Quaternion rotation = cd::Quaternion::FromAxisAngle(axis, cd::Math::DegreeToRadian<float>(angleDegrees));
Expand Down
9 changes: 7 additions & 2 deletions Engine/Source/Runtime/Display/CameraController.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class CameraController final
void MoveRight(float amount);
void MoveUp(float amount);
void MoveDown(float amount);
void MoveFront(float amount);
void MoveBack(float amount);
void Rotate(const cd::Vec3f& axis, float angleDegrees);
void Rotate(float x, float y, float z, float angleDegrees);
void Yaw(float angleDegrees);
Expand Down Expand Up @@ -70,7 +72,9 @@ class CameraController final
void MoveToPosition(cd::Point position, cd::Vec3f lookAt);

// TODO : generic solution to process mouse / key input events for UI panels in different areas.
void SetIsInViewScene(bool isIn) { m_isInViewScene = isIn; }
void SetIsFirstClickInViewScene(bool isIn) { m_isFirstClickInViewScene = isIn; }

void SetIsMouseInViewScene(bool isIn) { m_isMouseInViewScene = isIn; }

bool GetViewIsMoved() { return m_isMouseMovedInView; }

Expand Down Expand Up @@ -103,7 +107,8 @@ class CameraController final

bool m_isTracking = false;
bool m_isMoving = false;
bool m_isInViewScene = false;
bool m_isFirstClickInViewScene = false;
bool m_isMouseInViewScene = true;
bool m_isMouseMovedInView = false;
};

Expand Down

0 comments on commit bb2aab4

Please sign in to comment.