From 8d8f31653da6e5f79938dc503c263de315ebe9d5 Mon Sep 17 00:00:00 2001 From: VTui22 Date: Tue, 12 Sep 2023 16:55:40 +0800 Subject: [PATCH 1/5] CameraController Improvement and select entity change --- Engine/Source/Editor/UILayers/SceneView.cpp | 33 ++++++++++----------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Engine/Source/Editor/UILayers/SceneView.cpp b/Engine/Source/Editor/UILayers/SceneView.cpp index 423f6e25..4022f61c 100644 --- a/Engine/Source/Editor/UILayers/SceneView.cpp +++ b/Engine/Source/Editor/UILayers/SceneView.cpp @@ -313,8 +313,11 @@ void SceneView::PickSceneMesh(float regionWidth, float regionHeight) } } } - - pSceneWorld->SetSelectedEntity(nearestEntity); + //If the ray does not hit the AABB, then selectEntity remains unchanged. + if (nearestEntity != engine::INVALID_ENTITY) + { + pSceneWorld->SetSelectedEntity(nearestEntity); + } } void SceneView::Update() @@ -332,21 +335,6 @@ void SceneView::Update() ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); auto flags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse; ImGui::Begin(GetName(), &m_isEnable, flags); - if (engine::Input::Get().IsMouseLBPressed()) - { - ImVec2 windowSize = ImGui::GetWindowSize(); - ImVec2 windowPos = ImGui::GetWindowPos(); - ImVec2 mousePos = ImGui::GetMousePos(); - cd::Vec2f rightDown(windowPos.x + windowSize.x, windowPos.y + windowSize.y); - if (mousePos.x > windowPos.x && mousePos.x < rightDown.x() && mousePos.y > windowPos.y && mousePos.y < rightDown.y() && !m_isTerrainEditMode) - { - m_pCameraController->SetIsInViewScene(true); - } - else - { - m_pCameraController->SetIsInViewScene(false); - } - } // Draw top menu buttons which include ImGuizmo operation modes, ViewCamera settings. UpdateToolMenuButtons(); @@ -366,6 +354,9 @@ void SceneView::Update() pCameraComponent->SetAspect(static_cast(regionWidth) / static_cast(regionHeight)); } } + ImVec2 windowPos = ImGui::GetWindowPos(); + ImVec2 mousePos = ImGui::GetMousePos(); + cd::Vec2f rightDown(windowPos.x + regionSize.x, windowPos.y + regionSize.y); // Check if mouse hover on the area of SceneView so it can control. ImVec2 cursorPosition = ImGui::GetCursorPos(); @@ -399,6 +390,14 @@ void SceneView::Update() m_isMouseDownFirstTime = false; PickSceneMesh(regionWidth, regionHeight); + if (mousePos.x > windowPos.x && mousePos.x < rightDown.x() && mousePos.y > windowPos.y && mousePos.y < rightDown.y() && !m_isTerrainEditMode) + { + m_pCameraController->SetIsInViewScene(true); + } + else + { + m_pCameraController->SetIsInViewScene(false); + } } else { From 30370c05a63514663b7a7c3a6e2e03112d1dc1a4 Mon Sep 17 00:00:00 2001 From: VTui22 Date: Sat, 16 Sep 2023 15:17:50 +0800 Subject: [PATCH 2/5] add capture mouse and optimize select --- Engine/Source/Editor/EditorApp.cpp | 3 +++ Engine/Source/Editor/UILayers/SceneView.cpp | 26 ++++++++++++++------- Engine/Source/Editor/UILayers/SceneView.h | 12 ++++++++++ Engine/Source/Runtime/Window/Window.cpp | 14 +++++++++++ Engine/Source/Runtime/Window/Window.h | 6 +++-- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index bba08d91..b4ab71f2 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -532,6 +532,9 @@ bool EditorApp::Update(float deltaTime) } GetMainWindow()->Update(); + + GetMainWindow()->SetMouseVisible(m_pSceneView->IsShowMouse(), m_pSceneView->GetMouseFixedPositionX(), m_pSceneView->GetMouseFixedPositionY()); + m_pSceneWorld->Update(); m_pEditorImGuiContext->Update(deltaTime); diff --git a/Engine/Source/Editor/UILayers/SceneView.cpp b/Engine/Source/Editor/UILayers/SceneView.cpp index 4022f61c..9d8efa29 100644 --- a/Engine/Source/Editor/UILayers/SceneView.cpp +++ b/Engine/Source/Editor/UILayers/SceneView.cpp @@ -269,8 +269,8 @@ void SceneView::PickSceneMesh(float regionWidth, float regionHeight) return; } - float screenX = static_cast(engine::Input::Get().GetMousePositionX() - GetWindowPosX()); - float screenY = static_cast(engine::Input::Get().GetMousePositionY() - GetWindowPosY()); + float screenX = static_cast(m_mouseFixedPositionX - GetWindowPosX()); + float screenY = static_cast(m_mouseFixedPositionY - GetWindowPosY()); float screenWidth = static_cast(regionWidth); float screenHeight = static_cast(regionHeight); if (screenX < 0.0f || screenX > screenWidth || @@ -313,11 +313,9 @@ void SceneView::PickSceneMesh(float regionWidth, float regionHeight) } } } - //If the ray does not hit the AABB, then selectEntity remains unchanged. - if (nearestEntity != engine::INVALID_ENTITY) - { - pSceneWorld->SetSelectedEntity(nearestEntity); - } + + pSceneWorld->SetSelectedEntity(nearestEntity); + } void SceneView::Update() @@ -385,14 +383,18 @@ void SceneView::Update() { if (!m_isMouseDownFirstTime) { + if (0 != engine::Input::Get().GetMousePositionOffsetX() || 0 != engine::Input::Get().GetMousePositionOffsetY()) + { + m_isUsingCamera = true; + } return; } m_isMouseDownFirstTime = false; - PickSceneMesh(regionWidth, regionHeight); if (mousePos.x > windowPos.x && mousePos.x < rightDown.x() && mousePos.y > windowPos.y && mousePos.y < rightDown.y() && !m_isTerrainEditMode) { m_pCameraController->SetIsInViewScene(true); + m_isMouseShow = false; } else { @@ -401,7 +403,15 @@ void SceneView::Update() } else { + m_mouseFixedPositionX = engine::Input::Get().GetMousePositionX(); + m_mouseFixedPositionY = engine::Input::Get().GetMousePositionY(); + if (!m_isMouseShow && !m_isUsingCamera) + { + PickSceneMesh(regionWidth, regionHeight); + } m_isMouseDownFirstTime = true; + m_isMouseShow = true; + m_isUsingCamera = false; } } diff --git a/Engine/Source/Editor/UILayers/SceneView.h b/Engine/Source/Editor/UILayers/SceneView.h index 689c37af..12f1bc58 100644 --- a/Engine/Source/Editor/UILayers/SceneView.h +++ b/Engine/Source/Editor/UILayers/SceneView.h @@ -80,6 +80,10 @@ class SceneView : public engine::ImGuiBaseLayer void SetAABBRenderer(engine::Renderer* pAABBRenderer) { m_pAABBRenderer = pAABBRenderer; } bool IsTerrainEditMode() const { return m_isTerrainEditMode; } + bool IsFirstClick() const { return m_isMouseDownFirstTime; } + bool IsShowMouse() { return m_isMouseShow; } + uint32_t GetMouseFixedPositionX() const { return m_mouseFixedPositionX; } + uint32_t GetMouseFixedPositionY() const { return m_mouseFixedPositionY; } void SetCameraController(engine::CameraController* pCameraController) { m_pCameraController = pCameraController; } @@ -102,6 +106,8 @@ class SceneView : public engine::ImGuiBaseLayer bool m_is3DMode = true; bool m_isIBLActive = false; bool m_isTerrainEditMode = false; + bool m_isMouseShow = true; + bool m_isUsingCamera = false; DebugModeType m_debugMode = DebugModeType::NoDebug; AABBModeType m_AABBMode = AABBModeType::NoAABB; @@ -112,6 +118,12 @@ class SceneView : public engine::ImGuiBaseLayer engine::RenderTarget* m_pRenderTarget = nullptr; bool m_isMouseDownFirstTime = true; + int32_t m_mouseFixedPositionX ; + int32_t m_mouseFixedPositionY ; + + int32_t m_mouseMovementDuringPressedX; + int32_t m_mouseMovementDuringPressedY; + engine::CameraController* m_pCameraController = nullptr; }; diff --git a/Engine/Source/Runtime/Window/Window.cpp b/Engine/Source/Runtime/Window/Window.cpp index c40569a7..ad1cc71a 100644 --- a/Engine/Source/Runtime/Window/Window.cpp +++ b/Engine/Source/Runtime/Window/Window.cpp @@ -313,4 +313,18 @@ void Window::SetWindowIcon(const char* pFilePath) const stbi_image_free(pImageData); } +void Window::SetMouseVisible(bool isVisible, uint32_t x, uint32_t y) +{ + SDL_ShowCursor(isVisible); + if (!isVisible) + { + SDL_SetRelativeMouseMode(SDL_TRUE); + SDL_WarpMouseInWindow(m_pSDLWindow, x, y); + } + else + { + SDL_SetRelativeMouseMode(SDL_FALSE); + } +} + } \ No newline at end of file diff --git a/Engine/Source/Runtime/Window/Window.h b/Engine/Source/Runtime/Window/Window.h index c2477bf8..89d62fea 100644 --- a/Engine/Source/Runtime/Window/Window.h +++ b/Engine/Source/Runtime/Window/Window.h @@ -28,7 +28,7 @@ class Window uint16_t GetWidth() const { return m_width; } uint16_t GetHeight() const { return m_height; } - + const bool GetInputFocus() const; const bool GetMouseFocus() const; @@ -43,7 +43,9 @@ class Window bool ShouldClose() const { return m_isClosed; } void Close(bool bPushSdlEvent = true); - + + void SetMouseVisible(bool isVisible, uint32_t x, uint32_t y); + public: // Window Delegate OnDropFile; From 1c778aee5ba70fa068f0a10c17f198b3b4d745ba Mon Sep 17 00:00:00 2001 From: VTui22 Date: Sat, 16 Sep 2023 15:56:05 +0800 Subject: [PATCH 3/5] merge --- Engine/Source/Editor/EditorApp.cpp | 4 ++-- Engine/Source/Editor/UILayers/SceneView.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index b4ab71f2..77600ad7 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -533,8 +533,6 @@ bool EditorApp::Update(float deltaTime) GetMainWindow()->Update(); - GetMainWindow()->SetMouseVisible(m_pSceneView->IsShowMouse(), m_pSceneView->GetMouseFixedPositionX(), m_pSceneView->GetMouseFixedPositionY()); - m_pSceneWorld->Update(); m_pEditorImGuiContext->Update(deltaTime); @@ -591,6 +589,8 @@ bool EditorApp::Update(float deltaTime) } } + GetMainWindow()->SetMouseVisible(m_pSceneView->IsShowMouse(), m_pSceneView->GetMouseFixedPositionX(), m_pSceneView->GetMouseFixedPositionY()); + m_pRenderContext->EndFrame(); engine::Input::Get().FlushInputs(); diff --git a/Engine/Source/Editor/UILayers/SceneView.h b/Engine/Source/Editor/UILayers/SceneView.h index 12f1bc58..ede84a86 100644 --- a/Engine/Source/Editor/UILayers/SceneView.h +++ b/Engine/Source/Editor/UILayers/SceneView.h @@ -118,8 +118,8 @@ class SceneView : public engine::ImGuiBaseLayer engine::RenderTarget* m_pRenderTarget = nullptr; bool m_isMouseDownFirstTime = true; - int32_t m_mouseFixedPositionX ; - int32_t m_mouseFixedPositionY ; + int32_t m_mouseFixedPositionX = 0; + int32_t m_mouseFixedPositionY = 0; int32_t m_mouseMovementDuringPressedX; int32_t m_mouseMovementDuringPressedY; From 73dbb2cb6b36b3743bdbed37137794d37561ba6c Mon Sep 17 00:00:00 2001 From: VTui22 Date: Sat, 16 Sep 2023 16:01:09 +0800 Subject: [PATCH 4/5] merge --- Engine/Source/Editor/EditorApp.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index b23ebd04..39cbb23b 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -539,7 +539,6 @@ bool EditorApp::Update(float deltaTime) } GetMainWindow()->Update(); - m_pSceneWorld->Update(); m_pEditorImGuiContext->Update(deltaTime); @@ -562,6 +561,7 @@ bool EditorApp::Update(float deltaTime) if (m_pEngineImGuiContext) { + GetMainWindow()->SetMouseVisible(m_pSceneView->IsShowMouse(), m_pSceneView->GetMouseFixedPositionX(), m_pSceneView->GetMouseFixedPositionY()); if (m_pViewportCameraController) { m_pViewportCameraController->Update(deltaTime); @@ -596,8 +596,6 @@ bool EditorApp::Update(float deltaTime) } } - GetMainWindow()->SetMouseVisible(m_pSceneView->IsShowMouse(), m_pSceneView->GetMouseFixedPositionX(), m_pSceneView->GetMouseFixedPositionY()); - m_pRenderContext->EndFrame(); engine::Input::Get().FlushInputs(); From 21baa2e24ff458044bfde2377e3f83f43f4dba2a Mon Sep 17 00:00:00 2001 From: VTui22 Date: Sat, 16 Sep 2023 17:32:49 +0800 Subject: [PATCH 5/5] add right click and middle click --- Engine/Source/Editor/UILayers/SceneView.cpp | 7 ++++++- Engine/Source/Editor/UILayers/SceneView.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Engine/Source/Editor/UILayers/SceneView.cpp b/Engine/Source/Editor/UILayers/SceneView.cpp index 1cdb8696..02af5e01 100644 --- a/Engine/Source/Editor/UILayers/SceneView.cpp +++ b/Engine/Source/Editor/UILayers/SceneView.cpp @@ -354,7 +354,7 @@ void SceneView::Update() ImGui::End(); - if (engine::Input::Get().IsMouseLBPressed()) + if (engine::Input::Get().IsMouseLBPressed() || engine::Input::Get().IsMouseMBPressed() || engine::Input::Get().IsMouseRBPressed()) { if (!m_isMouseDownFirstTime) { @@ -362,6 +362,10 @@ void SceneView::Update() { m_isUsingCamera = true; } + if (engine::Input::Get().IsMouseLBPressed()) + { + m_isLeftClick = true; + } return; } @@ -387,6 +391,7 @@ void SceneView::Update() m_isMouseDownFirstTime = true; m_isMouseShow = true; m_isUsingCamera = false; + m_isLeftClick = false; } } diff --git a/Engine/Source/Editor/UILayers/SceneView.h b/Engine/Source/Editor/UILayers/SceneView.h index 60824199..80c385b3 100644 --- a/Engine/Source/Editor/UILayers/SceneView.h +++ b/Engine/Source/Editor/UILayers/SceneView.h @@ -90,6 +90,7 @@ class SceneView : public engine::ImGuiBaseLayer bool m_isTerrainEditMode = false; bool m_isMouseShow = true; bool m_isUsingCamera = false; + bool m_isLeftClick = false; RenderModeType m_renderMode = RenderModeType::Rendered; engine::Renderer* m_pSceneRenderer = nullptr;