Skip to content

Commit abea5db

Browse files
author
Adrien GIVRY
committed
Debug draw for actor picking added (Hold [G] key)
1 parent cb9dfab commit abea5db

2 files changed

Lines changed: 63 additions & 41 deletions

File tree

Sources/Overload/OvEditor/include/OvEditor/Panels/SceneView.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace OvEditor::Panels
3737
*/
3838
void RenderScene(uint8_t p_defaultRenderState);
3939

40+
/**
41+
* Render the scene for actor picking (Using unlit colors)
42+
*/
43+
void RenderSceneForActorPicking();
44+
4045
/**
4146
* Render the scene for actor picking and handle the logic behind it
4247
*/

Sources/Overload/OvEditor/src/OvEditor/Panels/SceneView.cpp

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -78,51 +78,68 @@ void OvEditor::Panels::SceneView::RenderScene(uint8_t p_defaultRenderState)
7878
m_fbo.Unbind();
7979
}
8080

81+
void OvEditor::Panels::SceneView::RenderSceneForActorPicking()
82+
{
83+
auto& baseRenderer = *EDITOR_CONTEXT(renderer).get();
84+
85+
auto [winWidth, winHeight] = GetSafeSize();
86+
87+
m_actorPickingFramebuffer.Resize(winWidth, winHeight);
88+
m_actorPickingFramebuffer.Bind();
89+
baseRenderer.SetClearColor(1.0f, 1.0f, 1.0f);
90+
baseRenderer.Clear();
91+
m_editorRenderer.RenderSceneForActorPicking();
92+
m_actorPickingFramebuffer.Unbind();
93+
}
94+
8195
void OvEditor::Panels::SceneView::HandleActorPicking()
8296
{
83-
if (IsHovered() && EDITOR_CONTEXT(inputManager)->IsMouseButtonPressed(OvWindowing::Inputs::EMouseButton::MOUSE_BUTTON_LEFT))
97+
using namespace OvWindowing::Inputs;
98+
99+
auto cursor = ImGui::GetMouseCursor();
100+
101+
bool isResizing =
102+
cursor == ImGuiMouseCursor_ResizeEW ||
103+
cursor == ImGuiMouseCursor_ResizeNS ||
104+
cursor == ImGuiMouseCursor_ResizeNWSE ||
105+
cursor == ImGuiMouseCursor_ResizeNESW ||
106+
cursor == ImGuiMouseCursor_ResizeAll;
107+
108+
bool mouseClicked = EDITOR_CONTEXT(inputManager)->IsMouseButtonPressed(EMouseButton::MOUSE_BUTTON_LEFT);
109+
110+
bool enableActorPickingDebugDraw = EDITOR_CONTEXT(inputManager)->GetKeyState(EKey::KEY_G) == EKeyState::KEY_DOWN;
111+
112+
if (mouseClicked || enableActorPickingDebugDraw)
84113
{
85-
/* Prevent losing focus on actor while resizing a window */
86-
if (auto cursor = ImGui::GetMouseCursor();
87-
cursor != ImGuiMouseCursor_ResizeEW &&
88-
cursor != ImGuiMouseCursor_ResizeNS &&
89-
cursor != ImGuiMouseCursor_ResizeNWSE &&
90-
cursor != ImGuiMouseCursor_ResizeNESW &&
91-
cursor != ImGuiMouseCursor_ResizeAll)
92-
{
93-
auto& baseRenderer = *EDITOR_CONTEXT(renderer).get();
94-
95-
auto [winWidth, winHeight] = GetSafeSize();
96-
97-
m_actorPickingFramebuffer.Resize(winWidth, winHeight);
98-
m_actorPickingFramebuffer.Bind();
99-
baseRenderer.SetClearColor(1.0f, 1.0f, 1.0f);
100-
baseRenderer.Clear();
101-
m_editorRenderer.RenderSceneForActorPicking();
102-
103-
// Look actor under mouse
104-
auto mousePosition = EDITOR_CONTEXT(inputManager)->GetMousePosition();
105-
auto mouseX = static_cast<uint16_t>(mousePosition.first);
106-
auto mouseY = static_cast<uint16_t>(mousePosition.second);
107-
mouseX -= static_cast<uint16_t>(m_position.x);
108-
mouseY -= static_cast<uint16_t>(m_position.y);
109-
mouseY = GetSafeSize().second - mouseY + 25;
110-
111-
uint8_t pixels[3];
112-
glReadPixels(static_cast<int>(mouseX), static_cast<int>(mouseY), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels);
113-
m_actorPickingFramebuffer.Unbind();
114-
115-
uint32_t actorID = (0 << 24) | (pixels[2] << 16) | (pixels[1] << 8) | (pixels[0] << 0);
116-
117-
if (auto actor = EDITOR_CONTEXT(sceneManager).GetCurrentScene()->FindActorByID(actorID))
118-
{
119-
EDITOR_EXEC(SelectActor(*actor));
120-
}
121-
else
122-
{
123-
EDITOR_EXEC(UnselectActor());
124-
}
114+
RenderSceneForActorPicking();
115+
}
125116

117+
if (IsHovered() && !isResizing && mouseClicked)
118+
{
119+
// Look actor under mouse
120+
auto mousePosition = EDITOR_CONTEXT(inputManager)->GetMousePosition();
121+
auto mouseX = static_cast<uint16_t>(mousePosition.first);
122+
auto mouseY = static_cast<uint16_t>(mousePosition.second);
123+
mouseX -= static_cast<uint16_t>(m_position.x);
124+
mouseY -= static_cast<uint16_t>(m_position.y);
125+
mouseY = GetSafeSize().second - mouseY + 25;
126+
127+
m_actorPickingFramebuffer.Bind();
128+
uint8_t pixels[3];
129+
glReadPixels(static_cast<int>(mouseX), static_cast<int>(mouseY), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels);
130+
m_actorPickingFramebuffer.Unbind();
131+
132+
uint32_t actorID = (0 << 24) | (pixels[2] << 16) | (pixels[1] << 8) | (pixels[0] << 0);
133+
134+
if (auto actor = EDITOR_CONTEXT(sceneManager).GetCurrentScene()->FindActorByID(actorID))
135+
{
136+
EDITOR_EXEC(SelectActor(*actor));
137+
}
138+
else
139+
{
140+
EDITOR_EXEC(UnselectActor());
126141
}
127142
}
143+
144+
m_image->textureID.d = enableActorPickingDebugDraw ? m_actorPickingFramebuffer.GetTextureID() : m_fbo.GetTextureID();
128145
}

0 commit comments

Comments
 (0)