Skip to content

Commit aa7b23a

Browse files
authored
Materials can now be dropped onto an actor (#484)
1 parent 432dfcd commit aa7b23a

File tree

2 files changed

+82
-20
lines changed

2 files changed

+82
-20
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
#pragma once
88

9-
#include "OvEditor/Panels/AViewControllable.h"
10-
#include "OvEditor/Core/GizmoBehaviour.h"
9+
#include <OvEditor/Core/GizmoBehaviour.h>
10+
#include <OvEditor/Panels/AViewControllable.h>
11+
#include <OvEditor/Rendering/PickingRenderPass.h>
1112

1213
namespace OvEditor::Panels
1314
{
@@ -47,6 +48,10 @@ namespace OvEditor::Panels
4748
private:
4849
virtual void DrawFrame() override;
4950
void HandleActorPicking();
51+
OvEditor::Rendering::PickingRenderPass::PickingResult GetPickingResult();
52+
void OnSceneDropped(const std::string& p_path);
53+
void OnModelDropped(const std::string& p_path);
54+
void OnMaterialDropped(const std::string& p_path);
5055

5156
private:
5257
OvCore::SceneSystem::SceneManager& m_sceneManager;

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

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @licence: MIT
55
*/
66

7+
#include <OvCore/ECS/Components/CMaterialRenderer.h>
8+
79
#include <OvEditor/Rendering/DebugSceneRenderer.h>
810
#include <OvEditor/Rendering/PickingRenderPass.h>
911
#include <OvEditor/Core/EditorActions.h>
@@ -13,6 +15,24 @@
1315

1416
#include <OvUI/Plugins/DDTarget.h>
1517

18+
namespace
19+
{
20+
OvTools::Utils::OptRef<OvCore::ECS::Actor> GetActorFromPickingResult(
21+
OvEditor::Rendering::PickingRenderPass::PickingResult p_result
22+
)
23+
{
24+
if (p_result)
25+
{
26+
if (const auto actor = std::get_if<OvTools::Utils::OptRef<OvCore::ECS::Actor>>(&p_result.value()))
27+
{
28+
return *actor;
29+
}
30+
}
31+
32+
return std::nullopt;
33+
}
34+
}
35+
1636
OvEditor::Panels::SceneView::SceneView
1737
(
1838
const std::string& p_title,
@@ -31,12 +51,16 @@ OvEditor::Panels::SceneView::SceneView
3151

3252
m_image->AddPlugin<OvUI::Plugins::DDTarget<std::pair<std::string, OvUI::Widgets::Layout::Group*>>>("File").DataReceivedEvent += [this](auto p_data)
3353
{
34-
std::string path = p_data.first;
54+
const std::string path = p_data.first;
55+
56+
using namespace OvTools::Utils;
57+
using enum PathParser::EFileType;
3558

36-
switch (OvTools::Utils::PathParser::GetFileType(path))
59+
switch (PathParser::GetFileType(path))
3760
{
38-
case OvTools::Utils::PathParser::EFileType::SCENE: EDITOR_EXEC(LoadSceneFromDisk(path)); break;
39-
case OvTools::Utils::PathParser::EFileType::MODEL: EDITOR_EXEC(CreateActorWithModel(path, true)); break;
61+
case SCENE: OnSceneDropped(path); break;
62+
case MODEL: OnModelDropped(path); break;
63+
case MATERIAL: OnMaterialDropped(path); break;
4064
}
4165
};
4266

@@ -150,20 +174,7 @@ void OvEditor::Panels::SceneView::HandleActorPicking()
150174

151175
if (IsHovered() && !IsResizing())
152176
{
153-
auto [mouseX, mouseY] = inputManager.GetMousePosition();
154-
mouseX -= m_position.x;
155-
mouseY -= m_position.y;
156-
mouseY = GetSafeSize().second - mouseY + 25;
157-
158-
auto& scene = *GetScene();
159-
160-
auto& actorPickingFeature = m_renderer->GetPass<Rendering::PickingRenderPass>("Picking");
161-
162-
auto pickingResult = actorPickingFeature.ReadbackPickingResult(
163-
scene,
164-
static_cast<uint32_t>(mouseX),
165-
static_cast<uint32_t>(mouseY)
166-
);
177+
const auto pickingResult = GetPickingResult();
167178

168179
m_highlightedActor = {};
169180
m_highlightedGizmoDirection = {};
@@ -221,3 +232,49 @@ void OvEditor::Panels::SceneView::HandleActorPicking()
221232
m_gizmoOperations.ApplyOperation(m_camera.GetViewMatrix(), m_camera.GetProjectionMatrix(), m_camera.GetPosition(), { static_cast<float>(winWidth), static_cast<float>(winHeight) });
222233
}
223234
}
235+
236+
OvEditor::Rendering::PickingRenderPass::PickingResult OvEditor::Panels::SceneView::GetPickingResult()
237+
{
238+
auto [mouseX, mouseY] = EDITOR_CONTEXT(inputManager)->GetMousePosition();
239+
mouseX -= m_position.x;
240+
mouseY -= m_position.y;
241+
mouseY = GetSafeSize().second - mouseY + 25;
242+
243+
auto& scene = *GetScene();
244+
245+
auto& actorPickingFeature = m_renderer->GetPass<OvEditor::Rendering::PickingRenderPass>("Picking");
246+
247+
return actorPickingFeature.ReadbackPickingResult(
248+
scene,
249+
static_cast<uint32_t>(mouseX),
250+
static_cast<uint32_t>(mouseY)
251+
);
252+
}
253+
254+
void OvEditor::Panels::SceneView::OnSceneDropped(const std::string& p_path)
255+
{
256+
EDITOR_EXEC(LoadSceneFromDisk(p_path));
257+
}
258+
259+
void OvEditor::Panels::SceneView::OnModelDropped(const std::string& p_path)
260+
{
261+
EDITOR_EXEC(CreateActorWithModel(p_path, true));
262+
}
263+
264+
void OvEditor::Panels::SceneView::OnMaterialDropped(const std::string& p_path)
265+
{
266+
const auto pickingResult = GetPickingResult();
267+
268+
if (auto actor = GetActorFromPickingResult(pickingResult))
269+
{
270+
if (auto materialRenderer = actor->GetComponent<OvCore::ECS::Components::CMaterialRenderer>())
271+
{
272+
const auto resourcePath = EDITOR_EXEC(GetResourcePath(p_path));
273+
274+
if (const auto material = EDITOR_CONTEXT(materialManager)[resourcePath])
275+
{
276+
materialRenderer->SetMaterialAtIndex(0, *material);
277+
}
278+
}
279+
}
280+
}

0 commit comments

Comments
 (0)