diff --git a/code/render/renderutil/freecamerautil.cc b/code/render/renderutil/freecamerautil.cc index 3dbe94165..0b4cf7572 100644 --- a/code/render/renderutil/freecamerautil.cc +++ b/code/render/renderutil/freecamerautil.cc @@ -2,7 +2,6 @@ // freecamerautil.cc // (C) 2012-2020 Individual contributors, see AUTHORS file //------------------------------------------------------------------------------ - #include "renderutil/freecamerautil.h" namespace RenderUtil @@ -40,8 +39,9 @@ FreeCameraUtil::Setup( const Math::point& defaultEyePos, const Math::vector& def this->defaultEyePos = defaultEyePos; this->defaultEyeVec = defaultEyeVec; this->position = this->defaultEyePos; + this->targetPosition = this->defaultEyePos; this->viewAngles.set(this->defaultEyeVec); - this->Update(); + this->Update(0.01667f); } //------------------------------------------------------------------------------ @@ -52,14 +52,15 @@ FreeCameraUtil::Reset() { this->viewAngles.set(this->defaultEyeVec); this->position = this->defaultEyePos; - this->Update(); + this->targetPosition = this->defaultEyePos; + this->Update(0.01667f); } //------------------------------------------------------------------------------ /** */ void -FreeCameraUtil::Update() +FreeCameraUtil::Update(float deltaTime) { if (this->rotateButton) { @@ -103,7 +104,9 @@ FreeCameraUtil::Update() } translation = this->cameraTransform * translation; - this->position += xyz(translation); + this->targetPosition += xyz(translation); + + this->position = Math::lerp(this->position, this->targetPosition, deltaTime * 10.0f); this->cameraTransform.position = point(this->position); } diff --git a/code/render/renderutil/freecamerautil.h b/code/render/renderutil/freecamerautil.h index 70eb554e4..a82ed484f 100644 --- a/code/render/renderutil/freecamerautil.h +++ b/code/render/renderutil/freecamerautil.h @@ -26,10 +26,13 @@ class FreeCameraUtil /// resets free camera to default values void Reset(); /// updates camera matrix - void Update(); + void Update(float deltaTime); /// gets camera transform const Math::mat4& GetTransform() const; + /// sets the target position of the camera + void SetTargetPosition(Math::vec3 const& point); + /// sets the state of the rotate button void SetRotateButton(bool state); /// sets the state of the accelerate button @@ -61,6 +64,7 @@ class FreeCameraUtil Math::polar viewAngles; Math::point position; + Math::point targetPosition; Math::mat4 cameraTransform; float rotationSpeed; @@ -87,6 +91,15 @@ FreeCameraUtil::GetTransform() const return this->cameraTransform; } +//------------------------------------------------------------------------------ +/** +*/ +inline void +FreeCameraUtil::SetTargetPosition(Math::vec3 const& point) +{ + this->targetPosition = point; +} + //------------------------------------------------------------------------------ /** */ diff --git a/code/render/renderutil/mayacamerautil.cc b/code/render/renderutil/mayacamerautil.cc index dc3374d31..a9f8ed167 100644 --- a/code/render/renderutil/mayacamerautil.cc +++ b/code/render/renderutil/mayacamerautil.cc @@ -57,14 +57,14 @@ MayaCameraUtil::Reset() this->viewAngles.set(normalize(viewVec)); this->centerOfInterest = this->defaultCenterOfInterest; this->cameraTransform = mat4(); - this->Update(); + this->Update(0.01667f); } //------------------------------------------------------------------------------ /** */ void -MayaCameraUtil::Update() +MayaCameraUtil::Update(float deltaTime) { const scalar defOrbitVelocity = 0.015f; const scalar defPanVelocity = 0.01f; // before 0.008f diff --git a/code/render/renderutil/mayacamerautil.h b/code/render/renderutil/mayacamerautil.h index 395650516..54dfba463 100644 --- a/code/render/renderutil/mayacamerautil.h +++ b/code/render/renderutil/mayacamerautil.h @@ -32,7 +32,7 @@ class MayaCameraUtil /// reset the object to its default settings void Reset(); /// update the view matrix - void Update(); + void Update(float deltaTime); /// get the current camera transform const Math::mat4& GetCameraTransform() const; /// get view distance diff --git a/toolkit/editor/editor/editor.cc b/toolkit/editor/editor/editor.cc index 208994568..e0b5b7636 100644 --- a/toolkit/editor/editor/editor.cc +++ b/toolkit/editor/editor/editor.cc @@ -46,7 +46,12 @@ Create() /// Import reload to be able to reload modules. Scripting::ScriptServer::Instance()->Eval("from importlib import reload"); - Game::TimeManager::SetGlobalTimeFactor(0.0f); + Game::TimeSourceCreateInfo editorTimeSourceInfo; + editorTimeSourceInfo.hash = TIMESOURCE_EDITOR; + Game::TimeManager::CreateTimeSource(editorTimeSourceInfo); + + Game::TimeSource* gameTimeSource = Game::TimeManager::GetTimeSource(TIMESOURCE_GAMEPLAY); + gameTimeSource->timeFactor = 0.0f; state.editorWorld = Game::GameServer::Instance()->CreateWorld(WORLD_EDITOR); state.editorWorld->componentInitializationEnabled = false; @@ -78,7 +83,8 @@ void PlayGame() { Game::EditorState::Instance()->isPlaying = true; - Game::TimeManager::SetGlobalTimeFactor(1.0f); + Game::TimeSource* gameTimeSource = Game::TimeManager::GetTimeSource(TIMESOURCE_GAMEPLAY); + gameTimeSource->timeFactor = 1.0f; } //------------------------------------------------------------------------------ @@ -88,7 +94,8 @@ void PauseGame() { Game::EditorState::Instance()->isPlaying = false; - Game::TimeManager::SetGlobalTimeFactor(0.0f); + Game::TimeSource* gameTimeSource = Game::TimeManager::GetTimeSource(TIMESOURCE_GAMEPLAY); + gameTimeSource->timeFactor = 0.0f; } //------------------------------------------------------------------------------ @@ -128,7 +135,8 @@ StopGame() Game::DestroyFilter(filter); - Game::TimeManager::SetGlobalTimeFactor(0.0f); + Game::TimeSource* gameTimeSource = Game::TimeManager::GetTimeSource(TIMESOURCE_GAMEPLAY); + gameTimeSource->timeFactor = 0.0f; } } // namespace Editor diff --git a/toolkit/editor/editor/editor.h b/toolkit/editor/editor/editor.h index 8e1c08c95..e4cb35ec9 100644 --- a/toolkit/editor/editor/editor.h +++ b/toolkit/editor/editor/editor.h @@ -17,6 +17,7 @@ namespace Editor { constexpr uint32_t WORLD_EDITOR = uint32_t('EWLD'); +constexpr uint32_t TIMESOURCE_EDITOR = uint32_t('TsEd'); typedef Game::Entity Entity; diff --git a/toolkit/editor/editor/tools/camera.cc b/toolkit/editor/editor/tools/camera.cc index 87bea394e..1eb8c070d 100644 --- a/toolkit/editor/editor/tools/camera.cc +++ b/toolkit/editor/editor/tools/camera.cc @@ -14,6 +14,8 @@ #include "input/mouse.h" #include "audio/audiodevice.h" +#include "basegamefeature/managers/timemanager.h" +#include "editor/editor.h" #include "imgui.h" @@ -79,13 +81,15 @@ Camera::Update() { auto& io = ImGui::GetIO(); + Game::TimeSource* timeSource = Game::TimeManager::GetTimeSource(TIMESOURCE_EDITOR); + this->mayaCameraUtil.SetOrbitButton(io.MouseDown[Input::MouseButton::LeftButton]); this->mayaCameraUtil.SetPanButton(io.MouseDown[Input::MouseButton::MiddleButton]); this->mayaCameraUtil.SetZoomButton(io.MouseDown[Input::MouseButton::RightButton]); this->mayaCameraUtil.SetZoomInButton(io.MouseWheel > 0); this->mayaCameraUtil.SetZoomOutButton(io.MouseWheel < 0); this->mayaCameraUtil.SetMouseMovement({ -io.MouseDelta.x, -io.MouseDelta.y }); - this->mayaCameraUtil.Update(); + this->mayaCameraUtil.Update(timeSource->frameTime); this->freeCamUtil.SetForwardsKey(io.KeysDown[Input::Key::W]); this->freeCamUtil.SetBackwardsKey(io.KeysDown[Input::Key::S]); @@ -98,7 +102,7 @@ Camera::Update() this->freeCamUtil.SetAccelerateButton(io.KeyShift); this->freeCamUtil.SetRotateButton(io.MouseDown[Input::MouseButton::RightButton]); - this->freeCamUtil.Update(); + this->freeCamUtil.Update(timeSource->frameTime); switch (this->cameraMode) { @@ -123,8 +127,7 @@ Camera::Update() void Camera::Reset() { - this->freeCamUtil.Setup(this->defaultViewPoint, Math::normalize(this->defaultViewPoint)); - this->freeCamUtil.Update(); + this->freeCamUtil.Setup(this->defaultViewPoint, Math::normalize(this->defaultViewPoint)); this->mayaCameraUtil.Setup(Math::point(0.0f, 0.0f, 0.0f), this->defaultViewPoint, Math::vector(0.0f, 1.0f, 0.0f)); } @@ -198,6 +201,16 @@ Camera::SetTransform(Math::mat4 const& val) CameraContext::SetView(this->cameraEntityId, val); } +//------------------------------------------------------------------------------ +/** +*/ +void +Camera::SetTargetPosition(Math::vec3 const& point) +{ + this->freeCamUtil.SetTargetPosition(point); + //this->mayaCameraUtil.SetTargetPosition(point); +} + //------------------------------------------------------------------------------ /** */ diff --git a/toolkit/editor/editor/tools/camera.h b/toolkit/editor/editor/tools/camera.h index ca2af1fc3..34d950fe6 100644 --- a/toolkit/editor/editor/tools/camera.h +++ b/toolkit/editor/editor/tools/camera.h @@ -49,6 +49,8 @@ class Camera void SetTransform(Math::mat4 const& val); + void SetTargetPosition(Math::vec3 const& point); + Math::mat4 GetViewTransform() const; Math::mat4 GetProjectionTransform() const; diff --git a/toolkit/editor/editor/ui/windows/scene.cc b/toolkit/editor/editor/ui/windows/scene.cc index 0822c7646..bf71ac884 100644 --- a/toolkit/editor/editor/ui/windows/scene.cc +++ b/toolkit/editor/editor/ui/windows/scene.cc @@ -9,6 +9,9 @@ #include "editor/ui/uimanager.h" #include "graphicsfeature/graphicsfeatureunit.h" #include "editor/tools/selectiontool.h" +#include "editor/ui/windowserver.h" + +#include "basegamefeature/components/position.h" using namespace Editor; @@ -27,6 +30,10 @@ Scene::Scene() this->SetWindowPadding({0, 0}); + Util::Delegate focusDelegate = Util::Delegate::FromMethod(this); + + WindowServer::Instance()->RegisterCommand(focusDelegate, "Focus camera on current selection", "F", "Camera"); + this->additionalFlags = ImGuiWindowFlags_MenuBar; } @@ -65,4 +72,26 @@ Scene::Run(SaveMode save) } } +//------------------------------------------------------------------------------ +/** +*/ +void +Scene::FocusCamera() +{ + auto selection = Tools::SelectionTool::Selection(); + + //TODO: Use bboxes to more accurately calculate the distance offset to the object after moving the camera + + Math::vec3 centerPoint = Math::vec3(0); + for (auto const& entity : selection) + { + Game::Position pos = Editor::state.editorWorld->GetComponent(entity); + centerPoint += pos; + } + + centerPoint = centerPoint * (1.0f / (float)selection.Size()); + + viewPort.camera.SetTargetPosition(centerPoint + Math::xyz(Math::inverse(viewPort.camera.GetViewTransform()).get_z() * 5.0f)); +} + } // namespace Presentation diff --git a/toolkit/editor/editor/ui/windows/scene.h b/toolkit/editor/editor/ui/windows/scene.h index ef84a7b08..3434012c4 100644 --- a/toolkit/editor/editor/ui/windows/scene.h +++ b/toolkit/editor/editor/ui/windows/scene.h @@ -24,6 +24,8 @@ class Scene : public BaseWindow void Update(); void Run(SaveMode save) override; + void FocusCamera(); + Modules::Viewport viewPort; }; __RegisterClass(Scene) diff --git a/toolkit/editor/editorfeature/editorfeatureunit.cc b/toolkit/editor/editorfeature/editorfeatureunit.cc index 211bb1444..97b30fb21 100644 --- a/toolkit/editor/editorfeature/editorfeatureunit.cc +++ b/toolkit/editor/editorfeature/editorfeatureunit.cc @@ -60,10 +60,10 @@ EditorFeatureUnit::OnActivate() FeatureUnit::OnActivate(); if (this->args.GetBoolFlag("-editor")) { - this->AttachManager(Editor::UIManager::Create()); - Editor::Create(); + this->AttachManager(Editor::UIManager::Create()); + // TODO: move this to a game manager that is created by the editor Game::World* world = Game::GetWorld(WORLD_DEFAULT);