Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw committed Nov 13, 2023
1 parent 35eae2e commit 41656c1
Show file tree
Hide file tree
Showing 16 changed files with 686 additions and 517 deletions.
125 changes: 70 additions & 55 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Display/CameraController.h"
#include "ECWorld/SceneWorld.h"
#include "ImGui/ImGuiContextInstance.h"
#include "ImGui/ImGuiContextManager.h"
#include "ImGui/Localization.h"
#include "ImGui/UILayers/DebugPanel.h"
#include "ImGui/UILayers/Profiler.h"
Expand Down Expand Up @@ -80,7 +81,9 @@ void EditorApp::Init(engine::EngineInitArgs initArgs)
CD_ERROR("Failed to open CSV file");
}

m_pWindowManager = std::make_unique<engine::WindowManager>();
// Init Systems
InitWindowManager();
m_pImGuiContextManager = std::make_unique<engine::ImGuiContextManager>();

// Phase 1 - Splash
// * Compile uber shader permutations automatically when initialization or detect changes
Expand Down Expand Up @@ -160,23 +163,16 @@ void EditorApp::InitEditorImGuiContext(engine::Language language)
{
assert(GetMainWindow() && "Init window before imgui context");

const bool enableDock = true;
const bool enableViewport = true;
m_pEditorImGuiContext = std::make_unique<engine::ImGuiContextInstance>(GetMainWindow()->GetWidth(), GetMainWindow()->GetHeight(), enableDock, enableViewport);
RegisterImGuiUserData(m_pEditorImGuiContext.get());

// TODO : more font files to load and switch dynamically.
std::vector<std::string> ttfFileNames = { "FanWunMing-SB.ttf" };
m_pEditorImGuiContext->LoadFontFiles(ttfFileNames, language);

// Bind event callbacks from current available input devices.
GetMainWindow()->OnResize.Bind<engine::ImGuiContextInstance, &engine::ImGuiContextInstance::OnResize>(m_pEditorImGuiContext.get());

// Set style settings.
m_pEditorImGuiContext = m_pImGuiContextManager->AddImGuiContext(engine::StringCrc("Editor"));
m_pEditorImGuiContext->InitBackendUserData(GetMainWindow(), m_pRenderContext.get());
m_pEditorImGuiContext->SetDisplaySize(GetMainWindow()->GetWidth(), GetMainWindow()->GetHeight());
m_pEditorImGuiContext->LoadFontFiles({ "FanWunMing-SB.ttf" }, language);
m_pEditorImGuiContext->SetImGuiThemeColor(engine::ThemeColor::Dark);
m_pEditorImGuiContext->EnableDock();
m_pEditorImGuiContext->EnableViewport();

// Init viewport settings.
if (enableViewport)
if (m_pEditorImGuiContext->IsViewportEnable())
{
m_pEditorImGuiContext->InitViewport(m_pWindowManager.get(), m_pRenderContext.get());
ImGuiViewport* pMainViewport = ImGui::GetMainViewport();
Expand All @@ -185,6 +181,8 @@ void EditorApp::InitEditorImGuiContext(engine::Language language)
pMainViewport->PlatformHandleRaw = GetMainWindow()->GetHandle();
m_pEditorImGuiContext->UpdateViewport();
}

GetMainWindow()->OnResize.Bind<engine::ImGuiContextInstance, &engine::ImGuiContextInstance::OnResize>(m_pEditorImGuiContext);
}

void EditorApp::InitEditorUILayers()
Expand Down Expand Up @@ -228,15 +226,13 @@ void EditorApp::InitEngineImGuiContext(engine::Language language)
constexpr engine::StringCrc sceneRenderTarget("SceneRenderTarget");
engine::RenderTarget* pSceneRenderTarget = m_pRenderContext->GetRenderTarget(sceneRenderTarget);

m_pEngineImGuiContext = std::make_unique<engine::ImGuiContextInstance>(pSceneRenderTarget->GetWidth(), pSceneRenderTarget->GetHeight());
RegisterImGuiUserData(m_pEngineImGuiContext.get());

std::vector<std::string> ttfFileNames = { "FanWunMing-SB.ttf" };
m_pEngineImGuiContext->LoadFontFiles(ttfFileNames, language);

m_pEngineImGuiContext = m_pImGuiContextManager->AddImGuiContext(engine::StringCrc("Engine"));
m_pEngineImGuiContext->InitBackendUserData(GetMainWindow(), 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.get());
pSceneRenderTarget->OnResize.Bind<engine::ImGuiContextInstance, &engine::ImGuiContextInstance::OnResize>(m_pEngineImGuiContext);
}

void EditorApp::InitEngineUILayers()
Expand All @@ -249,17 +245,6 @@ void EditorApp::InitEngineUILayers()
//m_pEngineImGuiContext->AddDynamicLayer(std::make_unique<TestNodeEditor>("TestNodeEditor"));
}

void EditorApp::RegisterImGuiUserData(engine::ImGuiContextInstance* pImGuiContext)
{
assert(GetMainWindow() && m_pRenderContext);

ImGuiIO& io = ImGui::GetIO();
assert(io.UserData == pImGuiContext);

io.BackendPlatformUserData = GetMainWindow();
io.BackendRendererUserData = m_pRenderContext.get();
}

void EditorApp::InitECWorld()
{
m_pSceneWorld = std::make_unique<engine::SceneWorld>();
Expand Down Expand Up @@ -617,6 +602,7 @@ bool EditorApp::Update(float deltaTime)
}

engine::Input::Get().Update();
m_pEditorImGuiContext->BeginFrame();
m_pEditorImGuiContext->Update(deltaTime);
m_pWindowManager->Update();
m_pSceneWorld->Update();
Expand All @@ -638,11 +624,7 @@ bool EditorApp::Update(float deltaTime)
}
}

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
m_pEditorImGuiContext->EndFrame();

if (m_pEngineImGuiContext)
{
Expand All @@ -651,22 +633,25 @@ bool EditorApp::Update(float deltaTime)
{
m_pViewportCameraController->Update(deltaTime);
}
// Do Screen Space Smoothing
if (pTerrainComponent && m_pSceneView->IsTerrainEditMode() && engine::Input::Get().IsMouseLBPressed())
{
float screenSpaceX = 2.0f * static_cast<float>(engine::Input::Get().GetMousePositionX() - m_pSceneView->GetWindowPosX()) /
m_pSceneView->GetRenderTarget()->GetWidth() - 1.0f;
float screenSpaceY = 1.0f - 2.0f * static_cast<float>(engine::Input::Get().GetMousePositionY() - m_pSceneView->GetWindowPosY()) /
m_pSceneView->GetRenderTarget()->GetHeight();

engine::TransformComponent* pCameraTransformComponent = m_pSceneWorld->GetTransformComponent(m_pSceneWorld->GetMainCameraEntity());
cd::Vec3f camPos = pCameraTransformComponent->GetTransform().GetTranslation();

pTerrainComponent->ScreenSpaceSmooth(screenSpaceX, screenSpaceY, pMainCameraComponent->GetProjectionMatrix().Inverse(),
pMainCameraComponent->GetViewMatrix().Inverse(), camPos);
}

m_pEngineImGuiContext->SetWindowPosOffset(m_pSceneView->GetWindowPosX(), m_pSceneView->GetWindowPosY());
// Do Screen Space Smoothing
//if (pTerrainComponent && m_pSceneView->IsTerrainEditMode() && engine::Input::Get().IsMouseLBPressed())
//{
// float screenSpaceX = 2.0f * static_cast<float>(engine::Input::Get().GetMousePositionX() - m_pSceneView->GetWindowPosX()) /
// m_pSceneView->GetRenderTarget()->GetWidth() - 1.0f;
// float screenSpaceY = 1.0f - 2.0f * static_cast<float>(engine::Input::Get().GetMousePositionY() - m_pSceneView->GetWindowPosY()) /
// m_pSceneView->GetRenderTarget()->GetHeight();
//
// engine::TransformComponent* pCameraTransformComponent = m_pSceneWorld->GetTransformComponent(m_pSceneWorld->GetMainCameraEntity());
// cd::Vec3f camPos = pCameraTransformComponent->GetTransform().GetTranslation();
//
// pTerrainComponent->ScreenSpaceSmooth(screenSpaceX, screenSpaceY, pMainCameraComponent->GetProjectionMatrix().Inverse(),
// pMainCameraComponent->GetViewMatrix().Inverse(), camPos);
//}

m_pEngineImGuiContext->BeginFrame();
auto [rectX, rectY] = m_pSceneView->GetRectPosition();
m_pEngineImGuiContext->SetRectPosition(rectX, rectY);
m_pEngineImGuiContext->Update(deltaTime);

UpdateMaterials();
Expand All @@ -682,13 +667,43 @@ bool EditorApp::Update(float deltaTime)
pRenderer->Render(deltaTime);
}
}
}

m_pEngineImGuiContext->EndFrame();
}
m_pRenderContext->EndFrame();

engine::Input::Get().FlushInputs();

return !GetMainWindow()->ShouldClose();
}

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)
{
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);
}
}
}
}

}
25 changes: 16 additions & 9 deletions Engine/Source/Editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CameraController;
class FlybyCamera;
class ImGuiBaseLayer;
class ImGuiContextInstance;
class ImGuiContextManager;
class Window;
class WindowManager;
class RenderContext;
Expand Down Expand Up @@ -46,30 +47,35 @@ class EditorApp final : public engine::IApplication
virtual bool Update(float deltaTime) override;
virtual void Shutdown() override;

// Window Management
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);
void InitShaderPrograms(bool compileAllShaders = false) const;

void InitEditorRenderers();
void InitEngineRenderers();

void AddEditorRenderer(std::unique_ptr<engine::Renderer> pRenderer);
void EditorRenderersWarmup();
void EngineRenderersWarmup();

void InitShaderPrograms(bool compileAllShaders = false) const;
void AddEditorRenderer(std::unique_ptr<engine::Renderer> pRenderer);
void InitEngineRenderers();
void AddEngineRenderer(std::unique_ptr<engine::Renderer> pRenderer);
void EngineRenderersWarmup();

// ImGui Management
void InitEditorImGuiContext(engine::Language language);
void InitEditorUILayers();
void InitEngineImGuiContext(engine::Language language);
void InitEngineUILayers();
void RegisterImGuiUserData(engine::ImGuiContextInstance* pImGuiContext);

// Scene World
void InitECWorld();

// Misc
void InitEditorController();

bool IsAtmosphericScatteringEnable() const;

private:
Expand All @@ -91,8 +97,9 @@ class EditorApp final : public engine::IApplication
std::unique_ptr<engine::WindowManager> m_pWindowManager;

// ImGui
std::unique_ptr<engine::ImGuiContextInstance> m_pEditorImGuiContext;
std::unique_ptr<engine::ImGuiContextInstance> m_pEngineImGuiContext;
engine::ImGuiContextInstance* m_pEditorImGuiContext = nullptr;
engine::ImGuiContextInstance* m_pEngineImGuiContext = nullptr;
std::unique_ptr<engine::ImGuiContextManager> m_pImGuiContextManager;

// Scene
std::unique_ptr<engine::SceneWorld> m_pSceneWorld;
Expand Down
6 changes: 3 additions & 3 deletions Engine/Source/Editor/UILayers/SceneView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ void SceneView::PickSceneMesh(float regionWidth, float regionHeight)
return;
}

float screenX = static_cast<float>(m_mouseFixedPositionX - GetWindowPosX());
float screenY = static_cast<float>(m_mouseFixedPositionY - GetWindowPosY());
auto [windowPosX, windowPosY] = GetRectSize();
float screenX = static_cast<float>(m_mouseFixedPositionX - windowPosX);
float screenY = static_cast<float>(m_mouseFixedPositionY - windowPosY);
float screenWidth = static_cast<float>(regionWidth);
float screenHeight = static_cast<float>(regionHeight);
if (screenX < 0.0f || screenX > screenWidth ||
Expand Down Expand Up @@ -336,7 +337,6 @@ void SceneView::Update()
// Check if mouse hover on the area of SceneView so it can control.
ImVec2 cursorPosition = ImGui::GetCursorPos();
ImVec2 sceneViewPosition = ImGui::GetWindowPos() + cursorPosition;
SetWindowPos(sceneViewPosition.x, sceneViewPosition.y);
if (ImGui::IsWindowHovered())
{
isMouseInsideSeneView = true;
Expand Down
30 changes: 30 additions & 0 deletions Engine/Source/Runtime/ImGui/ImGuiBaseLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,40 @@
#include "Rendering/RenderContext.h"

#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>

namespace engine
{

ImGuiBaseLayer::ImGuiBaseLayer(const char* pName)
: m_pName(pName)
{
}

ImGuiWindow* ImGuiBaseLayer::GetRootWindow() const
{
if (!m_nameHash.has_value())
{
auto* pWindow = ImGui::FindWindowByName(m_pName);
m_nameHash = pWindow->ID;
return pWindow;
}

return ImGui::FindWindowByID(m_nameHash.value());
}

std::pair<float, float> ImGuiBaseLayer::GetRectPosition() const
{
auto pos = GetRootWindow()->Pos;
return std::make_pair(pos.x, pos.y);
}

std::pair<float, float> ImGuiBaseLayer::GetRectSize() const
{
auto size = GetRootWindow()->Size;
return std::make_pair(size.x, size.y);
}

ImGuiContextInstance* ImGuiBaseLayer::GetImGuiContextInstance() const
{
return reinterpret_cast<engine::ImGuiContextInstance*>(ImGui::GetIO().UserData);
Expand Down
16 changes: 10 additions & 6 deletions Engine/Source/Runtime/ImGui/ImGuiBaseLayer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

#include <optional>
#include <utility>

struct ImGuiWindow;

namespace engine
{

Expand All @@ -10,7 +15,7 @@ class SceneWorld;
class ImGuiBaseLayer
{
public:
ImGuiBaseLayer(const char* pName) : m_pName(pName) { }
ImGuiBaseLayer(const char* pName);
ImGuiBaseLayer(const ImGuiBaseLayer&) = delete;
ImGuiBaseLayer& operator=(const ImGuiBaseLayer&) = delete;
ImGuiBaseLayer(ImGuiBaseLayer&&) = default;
Expand All @@ -21,9 +26,9 @@ class ImGuiBaseLayer
virtual void Update() = 0;

const char* GetName() const { return m_pName; }
float GetWindowPosX() const { return m_windowPosX; }
float GetWindowPosY() const { return m_windowPosY; }
void SetWindowPos(float x, float y) { m_windowPosX = x; m_windowPosY = y; }
ImGuiWindow* GetRootWindow() const;
std::pair<float, float> GetRectPosition() const;
std::pair<float, float> GetRectSize() const;

void SetEnable(bool enable) { m_isEnable = enable; }
bool IsEnable() const { return m_isEnable; }
Expand All @@ -34,8 +39,7 @@ class ImGuiBaseLayer

protected:
const char* m_pName = nullptr;
float m_windowPosX = 0.0f;
float m_windowPosY = 0.0f;
mutable std::optional<uint32_t> m_nameHash;
bool m_isEnable = true;
};

Expand Down
Loading

0 comments on commit 41656c1

Please sign in to comment.