Mouse picking system now uses cursor returned from editor

- This new cursor position is relative to where the viewport is rendering
- Window resize callback in Graphics system now checks if editor is enabled before signaling for resize. This is because editor will handle the signaling for resizing when its enabled. When its disabled, the graphics system will signal itself to resize when the window resize callback is called.
This commit is contained in:
Brandon Mak 2022-10-18 17:41:14 +08:00
parent 033ba48304
commit 9ce5a4a47b
9 changed files with 70 additions and 27 deletions

View File

@ -1,16 +1,16 @@
[Window][MainStatusBar]
Pos=0,1167
Size=2314,20
Pos=0,1060
Size=1920,20
Collapsed=0
[Window][SHEditorMenuBar]
Pos=0,48
Size=2314,1119
Size=1920,1012
Collapsed=0
[Window][Hierarchy Panel]
Pos=0,152
Size=208,1015
Pos=0,142
Size=321,918
Collapsed=0
DockId=0x00000004,0
@ -20,29 +20,29 @@ Size=400,400
Collapsed=0
[Window][Inspector]
Pos=2044,48
Size=270,1119
Pos=1649,48
Size=271,1012
Collapsed=0
DockId=0x00000006,0
[Window][Profiler]
Pos=0,48
Size=208,102
Size=321,92
Collapsed=0
DockId=0x00000003,0
[Window][Viewport]
Pos=210,48
Size=1832,1119
Pos=323,48
Size=1324,1012
Collapsed=0
DockId=0x00000002,0
[Docking][Data]
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=-7,358 Size=2314,1119 Split=X
DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=2042,1036 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=208,1036 Split=Y Selected=0x1E6EB881
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=617,298 Size=1920,1012 Split=X
DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1992,1036 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=321,1036 Split=Y Selected=0x1E6EB881
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=225,940 Selected=0xE096E5AE
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1832,1036 CentralNode=1 Selected=0x13926F0B
DockNode ID=0x00000006 Parent=0xC5C9B8AB SizeRef=270,1036 Selected=0xE7039252
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1324,1036 CentralNode=1 Selected=0x13926F0B
DockNode ID=0x00000006 Parent=0xC5C9B8AB SizeRef=271,1036 Selected=0xE7039252

View File

@ -34,5 +34,6 @@ namespace SHADE
ImGuiIO& io;
SHVec2 windowSize;
SHVec2 windowPos;
SHVec2 viewportMousePos;
};//class SHEditorWindow
}//namespace SHADE

View File

@ -4,6 +4,7 @@
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h"
#include "Graphics/Descriptors/SHVkDescriptorSetGroup.h"
#include "Graphics/MiddleEnd/Interface/SHMousePickSystem.h"
namespace SHADE
{
@ -26,6 +27,14 @@ namespace SHADE
DrawMenuBar();
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
auto const& descriptorSet = gfxSystem->GetPostOffscreenRenderSystem()->GetDescriptorSetGroup()->GetVkHandle()[0];
if (ImGui::IsWindowHovered() && ImGui::IsMouseReleased(ImGuiMouseButton_Left))
{
auto mousePos = ImGui::GetMousePos();
auto cursorPos = ImGui::GetCursorScreenPos();
viewportMousePos = {mousePos.x - cursorPos.x, mousePos.y - cursorPos.y};
gfxSystem->GetMousePickSystem ()->SetViewportMousePos (viewportMousePos);
}
ImGui::Image((ImTextureID)descriptorSet, ImGui::GetWindowSize());
}
ImGui::End();
@ -42,6 +51,8 @@ namespace SHADE
//Get graphics system to resize swapchain image
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
//auto pos = ImGui::GetCursorPos();
//windowCursorPos = {}
gfxSystem->PrepareResize(static_cast<uint32_t>(windowSize.x), static_cast<uint32_t>(windowSize.y));
}

View File

@ -69,7 +69,15 @@ namespace SHADE
if (width == 0 || height == 0)
return;
PrepareResize(resizeWidth, resizeHeight);
#ifdef SHEDITOR
//PrepareResize(1, 1, SHVec2(0, 0));
#else
PrepareResize(resizeWidth, resizeHeight, SHVec2(0, 0));
#endif
});
window->RegisterWindowCloseCallback([&](void)
@ -130,7 +138,7 @@ namespace SHADE
worldCamera->SetPerspective(90.0f, static_cast<float>(windowDims.first), static_cast<float>(windowDims.second), 0.0f, 100.0f);
// Create Default Viewport
defaultViewport = AddViewport(vk::Viewport(0.0f, 0.0f, static_cast<float>(window->GetWindowSize().first), static_cast<float>(window->GetWindowSize().second), 0.0f, 1.0f));
worldViewport = AddViewport(vk::Viewport(0.0f, 0.0f, static_cast<float>(window->GetWindowSize().first), static_cast<float>(window->GetWindowSize().second), 0.0f, 1.0f));
// Get render graph from default viewport world renderer
worldRenderGraph = resourceManager.Create<SHRenderGraph>();
@ -163,7 +171,7 @@ namespace SHADE
worldRenderGraph->Generate();
// Add world renderer to default viewport
worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph);
worldRenderer = worldViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph);
worldRenderer->SetCamera(worldCamera);
@ -691,8 +699,8 @@ namespace SHADE
mousePickSystem->HandleResize();
postOffscreenRender->HandleResize();
defaultViewport->SetWidth(static_cast<float>(resizeWidth));
defaultViewport->SetHeight(static_cast<float>(resizeHeight));
worldViewport->SetWidth(static_cast<float>(resizeWidth));
worldViewport->SetHeight(static_cast<float>(resizeHeight));
worldCamera->SetPerspective(90.0f, static_cast<float>(resizeWidth), static_cast<float>(resizeHeight), 0.0f, 100.0f);

View File

@ -280,7 +280,7 @@ namespace SHADE
Handle<SHVkPhysicalDevice> GetPhysicalDevice() const { return physicalDevice; }
Handle<SHVkQueue> GetQueue() const { return graphicsQueue; }
Handle<SHVkDescriptorPool> GetDescriptorPool() const { return descPool; }
Handle<SHViewport> GetDefaultViewport() const {return defaultViewport;}
Handle<SHViewport> GetDefaultViewport() const {return worldViewport;}
#ifdef SHEDITOR
Handle<SHViewport> GetEditorViewport () const {return editorViewport;};
#endif
@ -325,7 +325,7 @@ namespace SHADE
Handle<SHRenderGraph> editorRenderGraph;
#endif
Handle<SHViewport> defaultViewport; // Whole screen
Handle<SHViewport> worldViewport; // Whole screen
std::vector<Handle<SHViewport>> viewports; // Additional viewports
// Debug Renderers

View File

@ -6,6 +6,7 @@
#include "Graphics/Synchronization/SHVkFence.h"
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/SHVkUtil.h"
#include "Graphics/MiddleEnd/Interface/SHViewport.h"
namespace SHADE
{
@ -13,7 +14,7 @@ namespace SHADE
{
logicalDevice = device;
pickedEID = 0;
pickedEID = MAX_EID;
// Create command buffers
for (auto& pool : cmdPools)
@ -30,7 +31,7 @@ namespace SHADE
void SHMousePickSystem::Run(Handle<SHVkQueue> queue, uint32_t frameIndex) noexcept
{
// if input detected
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::LEFT_CTRL) && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB))
{
afterCopyFence->Reset();
@ -52,10 +53,8 @@ namespace SHADE
// wait for the copy to be done
afterCopyFence->Wait(true, std::numeric_limits<uint64_t>::max());
int mouseX = 0, mouseY = 0;
SHInputManager::GetMouseWindowPosition(&mouseX, &mouseY);
pickedEID = imageDataDstBuffer->GetDataFromMappedPointer<uint32_t>(static_cast<uint32_t>(viewportMousePos.y) * entityIDAttachment->GetWidth() + static_cast<uint32_t>(viewportMousePos.x));
pickedEID = imageDataDstBuffer->GetDataFromMappedPointer<uint32_t>(mouseY * entityIDAttachment->GetWidth() + mouseX);
}
}
@ -79,6 +78,11 @@ namespace SHADE
imageDataDstBuffer = logicalDevice->CreateBuffer(bufferSize, nullptr, bufferSize, vk::BufferUsageFlagBits::eTransferDst, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
}
void SHMousePickSystem::SetViewportMousePos(SHVec2 vpMousePos) noexcept
{
viewportMousePos = vpMousePos;
}
EntityID SHMousePickSystem::GetPickedEntity(void) const noexcept
{
return pickedEID;

View File

@ -13,6 +13,7 @@ namespace SHADE
class SHVkFence;
class SHVkQueue;
class SHVkBuffer;
class SHViewport;
class SHMousePickSystem
{
@ -33,6 +34,10 @@ namespace SHADE
//! eid picked from screen
EntityID pickedEID;
//! mouse position relative to the viewport window displaying the world
SHVec2 viewportMousePos;
public:
/*-----------------------------------------------------------------------*/
/* PUBLIC MEMBER FUNCTIONS */
@ -44,6 +49,8 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */
/*-----------------------------------------------------------------------*/
void SetViewportMousePos (SHVec2 vpMousePos) noexcept;
EntityID GetPickedEntity (void) const noexcept;
};

View File

@ -85,4 +85,14 @@ namespace SHADE
}
void SHViewport::SetX(float x) noexcept
{
viewport.x = x;
}
void SHViewport::SetY(float y) noexcept
{
viewport.y = y;
}
}

View File

@ -67,6 +67,8 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
void SetWidth(float w) noexcept;
void SetHeight (float h) noexcept;
void SetX (float x) noexcept;
void SetY (float y) noexcept;
/*-----------------------------------------------------------------------------*/
/* Getters */