diff --git a/src/engine/editor/Editor.cpp b/src/engine/editor/Editor.cpp index bad5b3965..1de6eaf2d 100644 --- a/src/engine/editor/Editor.cpp +++ b/src/engine/editor/Editor.cpp @@ -102,4 +102,10 @@ void Editor::AddObject(FVector* pos, Gfx* model, float scale, CollisionType coll void Editor::ClearObjects() { eGameObjects.clear(); -} \ No newline at end of file +} + +void Editor::SelectObjectFromSceneExplorer(GameObject* object) { + eObjectPicker._selected = object; + eObjectPicker.eGizmo.Enabled = true; + eObjectPicker.eGizmo.EnableNoCursor(object); +} diff --git a/src/engine/editor/Editor.h b/src/engine/editor/Editor.h index c2ae4dcaf..5d0e803f1 100644 --- a/src/engine/editor/Editor.h +++ b/src/engine/editor/Editor.h @@ -22,6 +22,7 @@ public: void AddObject(FVector* pos, Gfx* model, float scale, CollisionType collision, float boundingBoxSize, int32_t* despawnFlag, int32_t despawnValue); void ClearObjects(); void RemoveObject(); + void SelectObjectFromSceneExplorer(GameObject* object); private: bool _draw = false; Vec3f _ray; diff --git a/src/engine/editor/EditorMath.cpp b/src/engine/editor/EditorMath.cpp index 040816fd5..7313c0a1a 100644 --- a/src/engine/editor/EditorMath.cpp +++ b/src/engine/editor/EditorMath.cpp @@ -18,6 +18,20 @@ extern "C" { #include "camera.h" } +bool IsInGameScreen() { + auto wnd = GameEngine::Instance->context->GetWindow(); + Ship::Coords mouse = wnd->GetMousePos(); + + // Define viewport boundaries + int left = gfx_current_game_window_viewport.x; + int right = left + OTRGetGameRenderWidth(); + int top = gfx_current_game_window_viewport.y; + int bottom = top + OTRGetGameRenderHeight(); + + // Check if the mouse is within the game render area + return (mouse.x >= left && mouse.x < right) && (mouse.y >= top && mouse.y < bottom); +} + FVector ScreenRayTrace() { auto wnd = GameEngine::Instance->context->GetWindow(); Camera* camera = &cameras[0]; diff --git a/src/engine/editor/EditorMath.h b/src/engine/editor/EditorMath.h index 7aafd3f3a..2a9890191 100644 --- a/src/engine/editor/EditorMath.h +++ b/src/engine/editor/EditorMath.h @@ -41,6 +41,7 @@ struct GameObject { * ray.y = camera->pos[1] + direction.y * length; * ray.z = camera->pos[2] + direction.z * length; */ +bool IsInGameScreen(); FVector ScreenRayTrace(); bool QueryCollisionRayActor(Vec3f rayOrigin, Vec3f rayDir, Vec3f actorMin, Vec3f actorMax, float* t); FVector4 MultiplyMatrixVector(float matrix[4][4], float vector[4]); diff --git a/src/engine/editor/Gizmo.cpp b/src/engine/editor/Gizmo.cpp index 71e011166..a366fd7e8 100644 --- a/src/engine/editor/Gizmo.cpp +++ b/src/engine/editor/Gizmo.cpp @@ -50,8 +50,6 @@ void Gizmo::Tick() { // Makes the gizmo visible void Gizmo::Enable(GameObject* object, Ray ray) { - static float length = 180.0f; // Default value - _selected = object; _ray = ray.Direction; Pos = FVector( @@ -61,6 +59,15 @@ void Gizmo::Enable(GameObject* object, Ray ray) { ); } +void Gizmo::EnableNoCursor(GameObject* object) { + _selected = object; + Pos = FVector( + object->Pos->x, + object->Pos->y, + object->Pos->z + ); +} + void Gizmo::Translate() { static float length = 180.0f; // Default value @@ -70,7 +77,6 @@ void Gizmo::Translate() { } if (Enabled) { - //FVector ray = ScreenRayTrace(); length = sqrt( pow(_selected->Pos->x - cameras[0].pos[0], 2) + pow(_selected->Pos->y - cameras[0].pos[1], 2) + diff --git a/src/engine/editor/Gizmo.h b/src/engine/editor/Gizmo.h index 2dde2d4cc..e3f4d335b 100644 --- a/src/engine/editor/Gizmo.h +++ b/src/engine/editor/Gizmo.h @@ -23,6 +23,7 @@ public: void Load(); void Enable(GameObject* object, Ray ray); + void EnableNoCursor(GameObject* object); // Used for scene explorer selection void Translate(); void DrawHandles(); f32 SnapToSurface(FVector* pos); diff --git a/src/engine/editor/ObjectPicker.cpp b/src/engine/editor/ObjectPicker.cpp index fdd08089f..f0cb3e1a4 100644 --- a/src/engine/editor/ObjectPicker.cpp +++ b/src/engine/editor/ObjectPicker.cpp @@ -40,17 +40,22 @@ void ObjectPicker::Tick() { void ObjectPicker::SelectObject(std::vector& objects) { Ray ray; ray.Origin = FVector(cameras[0].pos[0], cameras[0].pos[1], cameras[0].pos[2]); - ray.Direction = ScreenRayTrace(); - ObjectPicker::FindObject(ray, objects); + // This allows selection of objects in the scene explorer. + // Otherwise this would still run when selecting buttons in editor windows. + if (IsInGameScreen()) { + ray.Direction = ScreenRayTrace(); - if (_selected != nullptr) { - eGizmo.Enable(_selected, ray); - eGizmo.Enabled = true; - } else { - //eGizmo.Disable(); - eGizmo.Enabled = false; - eGizmo._selected = nullptr; + ObjectPicker::FindObject(ray, objects); + + if (_selected != nullptr) { + eGizmo.Enable(_selected, ray); + eGizmo.Enabled = true; + } else { + //eGizmo.Disable(); + eGizmo.Enabled = false; + eGizmo._selected = nullptr; + } } } diff --git a/src/engine/editor/ObjectPicker.h b/src/engine/editor/ObjectPicker.h index 8af5a8bd8..e81f6480e 100644 --- a/src/engine/editor/ObjectPicker.h +++ b/src/engine/editor/ObjectPicker.h @@ -17,10 +17,10 @@ class ObjectPicker { void Load(); void Tick(); Gizmo eGizmo; + GameObject* _selected; private: bool _draw = false; Vec3f _ray; - GameObject* _selected; GameObject* _lastSelected; s32 Inverse(MtxF* src, MtxF* dest); void Copy(MtxF* src, MtxF* dest); diff --git a/src/port/ui/ContentBrowser.cpp b/src/port/ui/ContentBrowser.cpp index 6c3ab0de1..eb84899a6 100644 --- a/src/port/ui/ContentBrowser.cpp +++ b/src/port/ui/ContentBrowser.cpp @@ -11,7 +11,7 @@ #include #include -namespace Editor { +namespace EditorNamespace { ContentBrowserWindow::~ContentBrowserWindow() { SPDLOG_TRACE("destruct editor window"); diff --git a/src/port/ui/ContentBrowser.h b/src/port/ui/ContentBrowser.h index d986aa0e3..185e51c4f 100644 --- a/src/port/ui/ContentBrowser.h +++ b/src/port/ui/ContentBrowser.h @@ -2,7 +2,7 @@ #include -namespace Editor { +namespace EditorNamespace { class ContentBrowserWindow : public Ship::GuiWindow { public: using Ship::GuiWindow::GuiWindow; diff --git a/src/port/ui/ImguiUI.cpp b/src/port/ui/ImguiUI.cpp index 6c31ad087..e841f1581 100644 --- a/src/port/ui/ImguiUI.cpp +++ b/src/port/ui/ImguiUI.cpp @@ -79,13 +79,15 @@ void SetupGuiElements() { SPDLOG_ERROR("Could not find input GfxDebuggerWindow"); } - mToolsWindow = std::make_shared("gEditorEnabled", true, "Tools", ImVec2(100, 100), (ImGuiWindowFlags_NoTitleBar)); + mToolsWindow = std::make_shared("gEditorEnabled", true, "Tools", ImVec2(100, 100), + (ImGuiWindowFlags_NoTitleBar)); gui->AddGuiWindow(mToolsWindow); - mSceneExplorerWindow = std::make_shared("gEditorEnabled", "Scene Explorer"); + mSceneExplorerWindow = std::make_shared("gEditorEnabled", "Scene Explorer"); gui->AddGuiWindow(mSceneExplorerWindow); - mContentBrowserWindow = std::make_shared("gEditorEnabled", "Content Browser"); + mContentBrowserWindow = + std::make_shared("gEditorEnabled", "Content Browser"); gui->AddGuiWindow(mContentBrowserWindow); mGameInfoWindow = std::make_shared("gGameInfoEnabled", "Game Info"); diff --git a/src/port/ui/SceneExplorer.cpp b/src/port/ui/SceneExplorer.cpp index 372cb55d5..d927a0840 100644 --- a/src/port/ui/SceneExplorer.cpp +++ b/src/port/ui/SceneExplorer.cpp @@ -11,16 +11,24 @@ #include #include -namespace Editor { +#include "engine/editor/Editor.h" +#include "port/Game.h" + +namespace EditorNamespace { SceneExplorerWindow::~SceneExplorerWindow() { SPDLOG_TRACE("destruct editor window"); } void SceneExplorerWindow::DrawElement() { - ImGui::Text("This is your Scene Explorer window!"); - if (ImGui::Button("Click Me")) { - // Handle button click (example) + ImGui::Text("Scene"); + + int id = 0; // id for now because we don't have unique names atm + for (auto& object : gEditor.eGameObjects) { + std::string label = fmt::format("Object {}", id++); + if (ImGui::Button(label.c_str())) { + gEditor.SelectObjectFromSceneExplorer(&object); + } } } } diff --git a/src/port/ui/SceneExplorer.h b/src/port/ui/SceneExplorer.h index 7e6e1af10..85dee05bb 100644 --- a/src/port/ui/SceneExplorer.h +++ b/src/port/ui/SceneExplorer.h @@ -1,8 +1,10 @@ #pragma once #include +#include "port/Game.h" -namespace Editor { + +namespace EditorNamespace { class SceneExplorerWindow : public Ship::GuiWindow { public: using Ship::GuiWindow::GuiWindow; diff --git a/src/port/ui/Tools.cpp b/src/port/ui/Tools.cpp index 7cd18d481..416c58259 100644 --- a/src/port/ui/Tools.cpp +++ b/src/port/ui/Tools.cpp @@ -11,7 +11,7 @@ #include #include -namespace Editor { +namespace EditorNamespace { ToolsWindow::~ToolsWindow() { SPDLOG_TRACE("destruct editor window"); diff --git a/src/port/ui/Tools.h b/src/port/ui/Tools.h index b1afccf18..ae52ff795 100644 --- a/src/port/ui/Tools.h +++ b/src/port/ui/Tools.h @@ -2,7 +2,7 @@ #include -namespace Editor { +namespace EditorNamespace { class ToolsWindow : public Ship::GuiWindow { public: using Ship::GuiWindow::GuiWindow;