Skip to content

Commit

Permalink
Added "focus on object" to editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
fLindahl committed Nov 11, 2024
1 parent cabd9af commit 47c69b8
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 19 deletions.
13 changes: 8 additions & 5 deletions code/render/renderutil/freecamerautil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// freecamerautil.cc
// (C) 2012-2020 Individual contributors, see AUTHORS file
//------------------------------------------------------------------------------

#include "renderutil/freecamerautil.h"

namespace RenderUtil
Expand Down Expand Up @@ -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);
}

//------------------------------------------------------------------------------
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 14 additions & 1 deletion code/render/renderutil/freecamerautil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +64,7 @@ class FreeCameraUtil

Math::polar viewAngles;
Math::point position;
Math::point targetPosition;
Math::mat4 cameraTransform;

float rotationSpeed;
Expand All @@ -87,6 +91,15 @@ FreeCameraUtil::GetTransform() const
return this->cameraTransform;
}

//------------------------------------------------------------------------------
/**
*/
inline void
FreeCameraUtil::SetTargetPosition(Math::vec3 const& point)
{
this->targetPosition = point;
}

//------------------------------------------------------------------------------
/**
*/
Expand Down
4 changes: 2 additions & 2 deletions code/render/renderutil/mayacamerautil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion code/render/renderutil/mayacamerautil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions toolkit/editor/editor/editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

//------------------------------------------------------------------------------
Expand All @@ -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;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions toolkit/editor/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 17 additions & 4 deletions toolkit/editor/editor/tools/camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "input/mouse.h"

#include "audio/audiodevice.h"
#include "basegamefeature/managers/timemanager.h"
#include "editor/editor.h"

#include "imgui.h"

Expand Down Expand Up @@ -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]);
Expand All @@ -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)
{
Expand All @@ -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));
}

Expand Down Expand Up @@ -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);
}

//------------------------------------------------------------------------------
/**
*/
Expand Down
2 changes: 2 additions & 0 deletions toolkit/editor/editor/tools/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions toolkit/editor/editor/ui/windows/scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +30,10 @@ Scene::Scene()

this->SetWindowPadding({0, 0});

Util::Delegate<void()> focusDelegate = Util::Delegate<void()>::FromMethod<Scene, &Scene::FocusCamera>(this);

WindowServer::Instance()->RegisterCommand(focusDelegate, "Focus camera on current selection", "F", "Camera");

this->additionalFlags = ImGuiWindowFlags_MenuBar;
}

Expand Down Expand Up @@ -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<Game::Position>(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
2 changes: 2 additions & 0 deletions toolkit/editor/editor/ui/windows/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Scene : public BaseWindow
void Update();
void Run(SaveMode save) override;

void FocusCamera();

Modules::Viewport viewPort;
};
__RegisterClass(Scene)
Expand Down
4 changes: 2 additions & 2 deletions toolkit/editor/editorfeature/editorfeatureunit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 47c69b8

Please sign in to comment.