Skip to content

Commit

Permalink
switch mouse cursor in/out SceneView
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw committed Nov 15, 2023
1 parent 40fe355 commit e7b4260
Show file tree
Hide file tree
Showing 26 changed files with 810 additions and 606 deletions.
414 changes: 0 additions & 414 deletions Engine/Source/Editor/Camera/EditorCameraController.cpp

This file was deleted.

521 changes: 521 additions & 0 deletions Engine/Source/Editor/Camera/ViewportCameraController.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "Camera/ICameraController.h"
#include "ECWorld/TransformComponent.h"
#include "Math/Box.hpp"
#include "Math/Transform.hpp"
Expand All @@ -10,20 +11,48 @@ namespace engine
class CameraComponent;
class SceneWorld;

class EditorCameraController final
class ViewportCameraController : public ICameraController
{
public:
EditorCameraController() = delete;
explicit EditorCameraController(const SceneWorld* pSceneWorld, float sensitivity, float movement_speed);
explicit EditorCameraController(const SceneWorld* pSceneWorld, float horizontal_sensitivity, float vertical_sensitivity, float movement_speed);
~EditorCameraController() = default;
ViewportCameraController() = delete;
explicit ViewportCameraController(const SceneWorld* pSceneWorld, float sensitivity, float movement_speed);
explicit ViewportCameraController(const SceneWorld* pSceneWorld, float horizontal_sensitivity, float vertical_sensitivity, float movement_speed);
~ViewportCameraController() = default;

ViewportCameraController(const ViewportCameraController&) = delete;
ViewportCameraController(ViewportCameraController&&) = delete;
ViewportCameraController& operator=(const ViewportCameraController&) = delete;
ViewportCameraController& operator=(ViewportCameraController&&) = delete;

// Operations
virtual bool IsInAnimation() const override;
virtual bool IsZooming() const override;
virtual bool IsPanning() const override;
virtual bool IsTurning() const override;
virtual bool IsTracking() const override;
virtual bool IsInWalkMode() const override;

// Event Handlers
virtual void OnMouseDown() override;
virtual void OnMouseUp() override;
virtual void OnMouseMove(float x, float y) override;
virtual void OnMouseWheel(float y) override;

// TODO : EventDriven, not update.
void Update(float deltaTime);

EditorCameraController(const EditorCameraController&) = delete;
EditorCameraController(EditorCameraController&&) = delete;
EditorCameraController& operator=(const EditorCameraController&) = delete;
EditorCameraController& operator=(EditorCameraController&&) = delete;
// Synchronizes the controller to the transform of camera current state
void CameraToController();

void Update(float deltaTime);
// Synchronizes the transform of camera to the controller's current state
void ControllerToCamera();

// Implement the effect of a translation animation.
void CameraFocus();
void Focusing();

//
void Zoom(float delta);

// Configs
void SetMovementSpeed(float speed);
Expand Down Expand Up @@ -53,16 +82,7 @@ class EditorCameraController final
void AzimuthChanging(float amount);
void ElevationChanging(float amount);

// Double Click entity,camera will focus
void CameraFocus();
// Implement the effect of a translation animation.
void Moving();

// Synchronizes the controller to the transform of camera current state
void CameraToController();

// Synchronizes the transform of camera to the controller's current state
void ControllerToCamera();

// Synchronize view when begin using mayastyle camera or fpscamera
void SynchronizeTrackingCamera();
Expand Down Expand Up @@ -97,8 +117,8 @@ class EditorCameraController final
cd::Vec3f m_lookAtDestination;

bool m_isTracking = false;
bool m_isMoving = false;
bool m_isMouseMovedInView = false;
bool m_isFocusing = false;
bool m_isInWalkMode = false;
};

}
59 changes: 26 additions & 33 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "EditorApp.h"

#include "Application/Engine.h"
#include "Camera/EditorCameraController.h"
#include "Camera/ViewportCameraController.h"
#include "ECWorld/SceneWorld.h"
#include "ImGui/ImGuiContextInstance.h"
#include "ImGui/ImGuiContextManager.h"
Expand Down Expand Up @@ -58,6 +58,23 @@
//#include <format>
#include <thread>

namespace
{

void OnEnterSceneView()
{
auto* pWindowManager = static_cast<engine::WindowManager*>(ImGui::GetIO().BackendPlatformUserData);
pWindowManager->SetCursor(engine::MouseCursorType::Crosshair);
}

void OnLeaveSceneView()
{
auto* pWindowManager = static_cast<engine::WindowManager*>(ImGui::GetIO().BackendPlatformUserData);
pWindowManager->SetCursor(engine::MouseCursorType::Arrow);
}

}

namespace editor
{

Expand Down Expand Up @@ -164,7 +181,7 @@ void EditorApp::InitEditorImGuiContext(engine::Language language)
assert(GetMainWindow() && "Init window before imgui context");

m_pEditorImGuiContext = m_pImGuiContextManager->AddImGuiContext(engine::StringCrc("Editor"));
m_pEditorImGuiContext->InitBackendUserData(GetMainWindow(), m_pRenderContext.get());
m_pEditorImGuiContext->InitBackendUserData(m_pWindowManager.get(), m_pRenderContext.get());
m_pEditorImGuiContext->SetDisplaySize(GetMainWindow()->GetWidth(), GetMainWindow()->GetHeight());
m_pEditorImGuiContext->LoadFontFiles({ "FanWunMing-SB.ttf" }, language);
m_pEditorImGuiContext->SetImGuiThemeColor(engine::ThemeColor::Dark);
Expand All @@ -174,7 +191,7 @@ void EditorApp::InitEditorImGuiContext(engine::Language language)
// Init viewport settings.
if (m_pEditorImGuiContext->IsViewportEnable())
{
m_pEditorImGuiContext->InitViewport(m_pWindowManager.get(), m_pRenderContext.get());
m_pEditorImGuiContext->InitViewport();
ImGuiViewport* pMainViewport = ImGui::GetMainViewport();
assert(pMainViewport);
pMainViewport->PlatformHandle = GetMainWindow();
Expand Down Expand Up @@ -228,12 +245,15 @@ void EditorApp::InitEngineImGuiContext(engine::Language language)
engine::RenderTarget* pSceneRenderTarget = m_pRenderContext->GetRenderTarget(sceneRenderTarget);

m_pEngineImGuiContext = m_pImGuiContextManager->AddImGuiContext(engine::StringCrc("Engine"));
m_pEngineImGuiContext->InitBackendUserData(GetMainWindow(), m_pRenderContext.get());
m_pEngineImGuiContext->InitBackendUserData(m_pWindowManager.get(), m_pRenderContext.get());
m_pEngineImGuiContext->SetDisplaySize(pSceneRenderTarget->GetWidth(), pSceneRenderTarget->GetHeight());
m_pEngineImGuiContext->LoadFontFiles({ "FanWunMing-SB.ttf" }, language);
m_pEngineImGuiContext->SetImGuiThemeColor(engine::ThemeColor::Light);

pSceneRenderTarget->OnResize.Bind<engine::ImGuiContextInstance, &engine::ImGuiContextInstance::OnResize>(m_pEngineImGuiContext);

m_pEngineImGuiContext->OnMouseEnterDisplayRect.Bind<OnEnterSceneView>();
m_pEngineImGuiContext->OnMouseLeaveDisplayRect.Bind<OnLeaveSceneView>();
}

void EditorApp::InitEngineUILayers()
Expand Down Expand Up @@ -545,7 +565,7 @@ void EditorApp::InitShaderPrograms(bool compileAllShaders) const
void EditorApp::InitEditorController()
{
// Controller for Input events.
m_pViewportCameraController = std::make_unique<engine::EditorCameraController>(
m_pViewportCameraController = std::make_unique<engine::ViewportCameraController>(
m_pSceneWorld.get(),
12.0f /* horizontal sensitivity */,
12.0f /* vertical sensitivity */);
Expand Down Expand Up @@ -623,7 +643,7 @@ bool EditorApp::Update(float deltaTime)
}
}

auto [sceneRectX, sceneRectY] = m_pSceneView->GetRectPosition();
auto [sceneRectX, sceneRectY] = m_pSceneView->GetWorkRectPosition();
m_pEditorImGuiContext->EndFrame();

if (m_pEngineImGuiContext)
Expand Down Expand Up @@ -685,33 +705,6 @@ bool EditorApp::Update(float deltaTime)
void EditorApp::InitWindowManager()
{
m_pWindowManager = std::make_unique<engine::WindowManager>();
m_pWindowManager->OnMouseMove.Bind<EditorApp, &EditorApp::HandleMouseMotionEvent>(this);
}

void EditorApp::HandleMouseMotionEvent(uint32_t windowID, int x, int y)
{
engine::Input::Get().SetMousePositionX(x);
engine::Input::Get().SetMousePositionY(y);

//auto [screenX, screenY] = engine::Input::GetGloalMousePosition();
////auto* pWindow = m_pWindowManager->GetWindow(windowID);
//for (auto& [_, pImGuiContext] : m_pImGuiContextManager->GetAllImGuiContexts())
//{
// if (pImGuiContext->IsViewportEnable())
// {
// // In multiple viewport mode, it always use global screen space coordinates.
// engine::Input::Get().SetMousePositionX(screenX);
// engine::Input::Get().SetMousePositionY(screenY);
// }
// else
// {
// if (pImGuiContext->IsInsideDisplayRect())
// {
// engine::Input::Get().SetMousePositionX(x);
// engine::Input::Get().SetMousePositionY(y);
// }
// }
//}
}

}
5 changes: 2 additions & 3 deletions Engine/Source/Editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace engine
{

class AABBRenderer;
class EditorCameraController;
class ViewportCameraController;
class FlybyCamera;
class ImGuiBaseLayer;
class ImGuiContextInstance;
Expand Down Expand Up @@ -51,7 +51,6 @@ class EditorApp final : public engine::IApplication
void InitWindowManager();
engine::Window* GetMainWindow() const { return m_pMainWindow; }
engine::WindowManager* GetWindowManager() const { return m_pWindowManager.get(); }
void HandleMouseMotionEvent(uint32_t windowID, int x, int y);

// Rendering Management
void InitRenderContext(engine::GraphicsBackend backend, void* hwnd = nullptr);
Expand Down Expand Up @@ -119,7 +118,7 @@ class EditorApp final : public engine::IApplication
engine::Renderer* m_pAABBRenderer = nullptr;

// Controllers for processing input events.
std::unique_ptr<engine::EditorCameraController> m_pViewportCameraController;
std::unique_ptr<engine::ViewportCameraController> m_pViewportCameraController;

std::unique_ptr<FileWatcher> m_pFileWatcher;
};
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Editor/UILayers/EntityList.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "EntityList.h"

#include "Camera/EditorCameraController.h"
#include "Camera/ViewportCameraController.h"
#include "ECWorld/SceneWorld.h"
#include "ECWorld/World.h"
#include "ImGui/IconFont/IconsMaterialDesignIcons.h"
Expand Down
6 changes: 3 additions & 3 deletions Engine/Source/Editor/UILayers/EntityList.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace engine
{

class EditorCameraController;
class ViewportCameraController;
class SceneWorld;

}
Expand All @@ -30,11 +30,11 @@ class EntityList : public engine::ImGuiBaseLayer
void AddEntity(engine::SceneWorld* pSceneWorld);
void DrawEntity(engine::SceneWorld* pSceneWorld, engine::Entity entity);

void SetCameraController(engine::EditorCameraController* pCameraController) { m_pCameraController = pCameraController; }
void SetCameraController(engine::ViewportCameraController* pCameraController) { m_pCameraController = pCameraController; }

private:
ImGuiTextFilter m_entityFilter;
engine::EditorCameraController* m_pCameraController = nullptr;
engine::ViewportCameraController* m_pCameraController = nullptr;
};

}
1 change: 0 additions & 1 deletion Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ void UpdateComponentWidget<engine::CameraComponent>(engine::SceneWorld* pSceneWo
ImGuiUtils::ImGuiFloatProperty("NearPlane", pCameraComponent->GetNearPlane()) ||
ImGuiUtils::ImGuiFloatProperty("FarPlane", pCameraComponent->GetFarPlane()))
{
pCameraComponent->Dirty();
pCameraComponent->BuildProjectMatrix();
}

Expand Down
35 changes: 17 additions & 18 deletions Engine/Source/Editor/UILayers/MainMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "MainMenu.h"

#include "Camera/EditorCameraController.h"
#include "Camera/ViewportCameraController.h"
#include "ECWorld/SceneWorld.h"
#include "EditorApp.h"
#include "ImGui/ImGuiContextInstance.h"
Expand Down Expand Up @@ -53,13 +53,13 @@ void MainMenu::FileMenu()

ImGui::Separator();

if (ImGui::MenuItem("Quit", "Ctrl Q"))
{
if (auto* pMainWindow = reinterpret_cast<engine::Window*>(ImGui::GetIO().BackendPlatformUserData))
{
pMainWindow->Close();
}
}
//if (ImGui::MenuItem("Quit", "Ctrl Q"))
//{
// if (auto* pMainWindow = reinterpret_cast<engine::Window*>(ImGui::GetIO().BackendPlatformUserData))
// {
// pMainWindow->Close();
// }
//}

ImGui::EndMenu();
}
Expand Down Expand Up @@ -117,7 +117,7 @@ void MainMenu::EditMenu()

void MainMenu::ViewMenu()
{
auto FrameEntities = [](engine::SceneWorld* pSceneWorld, const std::vector<engine::Entity>& entities, engine::EditorCameraController* pCameraController)
auto FrameEntities = [](engine::SceneWorld* pSceneWorld, const std::vector<engine::Entity>& entities, engine::ViewportCameraController* pCameraController)
{
if (entities.empty())
{
Expand Down Expand Up @@ -164,7 +164,6 @@ void MainMenu::ViewMenu()

pTransformComponent->Dirty();
pTransformComponent->Build();
pCameraComponent->ViewDirty();
pCameraComponent->BuildViewMatrix(pTransformComponent->GetTransform());

// TODO : add event queue to get mouse down and up events.
Expand Down Expand Up @@ -263,14 +262,14 @@ void MainMenu::Update()

m_pCreateProjectDialog->Display();

if ((ImGui::IsKeyPressed(ImGuiKey_LeftCtrl) || ImGui::IsKeyPressed(ImGuiKey_RightCtrl))
&& ImGui::IsKeyPressed(ImGuiKey_Q))
{
if (auto* pMainWindow = reinterpret_cast<engine::Window*>(ImGui::GetIO().BackendPlatformUserData))
{
pMainWindow->Close();
}
}
//if ((ImGui::IsKeyPressed(ImGuiKey_LeftCtrl) || ImGui::IsKeyPressed(ImGuiKey_RightCtrl))
// && ImGui::IsKeyPressed(ImGuiKey_Q))
//{
// if (auto* pMainWindow = reinterpret_cast<engine::Window*>(ImGui::GetIO().BackendPlatformUserData))
// {
// pMainWindow->Close();
// }
//}
}

}
6 changes: 3 additions & 3 deletions Engine/Source/Editor/UILayers/MainMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FileBrowser;
namespace engine
{

class EditorCameraController;
class ViewportCameraController;

}

Expand All @@ -37,11 +37,11 @@ class MainMenu : public engine::ImGuiBaseLayer
void BuildMenu();
void AboutMenu();

void SetCameraController(engine::EditorCameraController* pCameraController) { m_pCameraController = pCameraController; }
void SetCameraController(engine::ViewportCameraController* pCameraController) { m_pCameraController = pCameraController; }

private:
std::unique_ptr<ImGui::FileBrowser> m_pCreateProjectDialog;
engine::EditorCameraController* m_pCameraController = nullptr;
engine::ViewportCameraController* m_pCameraController = nullptr;
};

}
Loading

0 comments on commit e7b4260

Please sign in to comment.