Skip to content

Commit

Permalink
#123 Refactor viewpointController, update some code on Handles
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed May 11, 2021
1 parent eaed344 commit f712dcf
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 88 deletions.
93 changes: 18 additions & 75 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This file is part of the "Skylicht Engine".
#include "GridPlane/CGridPlane.h"

#include "Editor/SpaceController/CSceneController.h"
#include "Handles/CHandles.h"

using namespace std::placeholders;

Expand All @@ -50,6 +51,7 @@ namespace Skylicht
m_viewpointZone(NULL),
m_viewpoint(NULL),
m_viewpointCamera(NULL),
m_viewpointController(NULL),
m_3dPanel(NULL),
m_handlesRenderer(NULL)
{
Expand Down Expand Up @@ -83,6 +85,9 @@ namespace Skylicht
if (m_viewpointRP != NULL)
delete m_viewpointRP;

if (m_viewpointController != NULL)
delete m_viewpointController;

CSceneController* sceneController = CSceneController::getInstance();
sceneController->setScene(NULL);
sceneController->setSpaceScene(NULL);
Expand Down Expand Up @@ -146,6 +151,13 @@ namespace Skylicht
viewpoint->setName(L"Viewpoint");
m_viewpoint = viewpoint->addComponent<CViewpoint>();

// delete old viewpoint
if (m_viewpointController != NULL)
delete m_viewpointController;

m_viewpointController = new CViewpointController();
m_viewpointController->setCamera(m_editorCamera, m_viewpointCamera);

// handles
m_handlesRenderer = m_scene->getEntityManager()->addRenderSystem<CHandlesRenderer>();

Expand Down Expand Up @@ -189,83 +201,14 @@ namespace Skylicht

void CSpaceScene::update()
{
updateViewpoint();
// test handles
static core::vector3df drag;
CHandles::getInstance()->positionHandle(drag);

m_viewpointController->update();
m_scene->update();
}

void CSpaceScene::updateViewpoint()
{
core::vector3df look = m_editorCamera->getLookVector();
core::vector3df up = m_editorCamera->getUpVector();

float distance = 2.2f;
core::vector3df pos = -look * distance;

m_viewpointCamera->setPosition(pos);
m_viewpointCamera->lookAt(core::vector3df(0.0f, 0.0f, 0.0f), up);
}

void CSpaceScene::setupCameraTween(CTweenVector3df* look, CTweenVector3df* up)
{
look->OnUpdate = [camera = m_editorCamera, up, look](CTween* t)
{
camera->setUpVector(up->getValue());
camera->setLookVector(look->getValue());
};

up->setEase(EEasingFunctions::EaseLinear);
look->setEase(EEasingFunctions::EaseLinear);

CTweenManager::getInstance()->addTween(up);
CTweenManager::getInstance()->addTween(look);
}

void CSpaceScene::setCameraLook(CViewpointData::EAxis axis)
{
float time = 350.0f;

core::vector3df look = m_editorCamera->getLookVector();
core::vector3df up = m_editorCamera->getUpVector();

if (axis == CViewpointData::X)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(-1.0f, 0.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::XNeg)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(1.0f, 0.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::Z)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, 0.0f, -1.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::ZNeg)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, 0.0f, 1.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::Y)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, -1.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 0.0f, 1.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::YNeg)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, 1.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 0.0f, 1.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
}

void CSpaceScene::onRender(GUI::CBase* base)
{
if (m_renderRP != NULL)
Expand Down Expand Up @@ -337,15 +280,15 @@ namespace Skylicht

if (m_viewpointRect.isPointInside(core::vector2df(local.X, local.Y)))
{
// viewpoint
if (down)
{
// viewpoint
float x = local.X - m_viewpointRect.UpperLeftCorner.X;
float y = local.Y - m_viewpointRect.UpperLeftCorner.Y;
CViewpointData::EAxis axis = m_viewpoint->getViewpointData()->hit(m_viewpointCamera, x, y, (int)m_viewpointRect.getWidth(), (int)m_viewpointRect.getHeight());

if (axis != CViewpointData::None)
setCameraLook(axis);
m_viewpointController->setCameraLook(axis);
}
}
else
Expand Down
13 changes: 3 additions & 10 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ This file is part of the "Skylicht Engine".

#include "SkylichtEngine.h"
#include "Editor/Space/CSpace.h"
#include "EditorComponents/Viewpoint/CViewpoint.h"
#include "EditorComponents/Handles/CHandlesRenderer.h"
#include "Tween/CTweenManager.h"
#include "CViewpointController.h"

namespace Skylicht
{
Expand All @@ -54,13 +53,13 @@ namespace Skylicht
float m_mouseY;

CViewpoint* m_viewpoint;
core::rectf m_viewpointRect;
CZone* m_viewpointZone;
CCamera* m_viewpointCamera;
CViewpointController* m_viewpointController;

CHandlesRenderer* m_handlesRenderer;

core::rectf m_viewpointRect;

public:
CSpaceScene(GUI::CWindow* window, CEditor* editor);

Expand Down Expand Up @@ -88,12 +87,6 @@ namespace Skylicht

protected:

void updateViewpoint();

void setupCameraTween(CTweenVector3df* look, CTweenVector3df* up);

void setCameraLook(CViewpointData::EAxis axis);

void initRenderPipeline(float w, float h);

void postMouseEventToScene(EMOUSE_INPUT_EVENT eventType, float x, float y);
Expand Down
126 changes: 126 additions & 0 deletions Projects/Editor/Source/Editor/Space/Scene/CViewpointController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
!@
MIT License
CopyRight (c) 2021 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CViewpointController.h"

namespace Skylicht
{
namespace Editor
{
CViewpointController::CViewpointController() :
m_viewpointCamera(NULL),
m_editorCamera(NULL)
{

}

CViewpointController::~CViewpointController()
{
}

void CViewpointController::update()
{
updateViewpoint();
}

void CViewpointController::updateViewpoint()
{
if (m_editorCamera == NULL || m_viewpointCamera == NULL)
return;

core::vector3df look = m_editorCamera->getLookVector();
core::vector3df up = m_editorCamera->getUpVector();

float distance = 2.2f;
core::vector3df pos = -look * distance;

m_viewpointCamera->setPosition(pos);
m_viewpointCamera->lookAt(core::vector3df(0.0f, 0.0f, 0.0f), up);
}

void CViewpointController::setupCameraTween(CTweenVector3df* look, CTweenVector3df* up)
{
look->OnUpdate = [camera = m_editorCamera, up, look](CTween* t)
{
camera->setUpVector(up->getValue());
camera->setLookVector(look->getValue());
};

up->setEase(EEasingFunctions::EaseLinear);
look->setEase(EEasingFunctions::EaseLinear);

CTweenManager::getInstance()->addTween(up);
CTweenManager::getInstance()->addTween(look);
}

void CViewpointController::setCameraLook(CViewpointData::EAxis axis)
{
if (m_editorCamera == NULL)
return;

float time = 350.0f;

core::vector3df look = m_editorCamera->getLookVector();
core::vector3df up = m_editorCamera->getUpVector();

if (axis == CViewpointData::X)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(-1.0f, 0.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::XNeg)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(1.0f, 0.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::Z)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, 0.0f, -1.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::ZNeg)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, 0.0f, 1.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 1.0f, 0.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::Y)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, -1.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 0.0f, 1.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
else if (axis == CViewpointData::YNeg)
{
CTweenVector3df* tweenLook = new CTweenVector3df(look, core::vector3df(0.0f, 1.0f, 0.0f), time);
CTweenVector3df* tweenUp = new CTweenVector3df(up, core::vector3df(0.0f, 0.0f, 1.0f), time);
setupCameraTween(tweenLook, tweenUp);
}
}
}
}
62 changes: 62 additions & 0 deletions Projects/Editor/Source/Editor/Space/Scene/CViewpointController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
!@
MIT License
CopyRight (c) 2021 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "Tween/CTweenManager.h"
#include "EditorComponents/Viewpoint/CViewpoint.h"

namespace Skylicht
{
namespace Editor
{
class CViewpointController
{
protected:
CCamera* m_editorCamera;
CCamera* m_viewpointCamera;

public:
CViewpointController();

virtual ~CViewpointController();

void update();

void setCameraLook(CViewpointData::EAxis axis);

inline void setCamera(CCamera* editorCamera, CCamera* viewpointCamera)
{
m_editorCamera = editorCamera;
m_viewpointCamera = viewpointCamera;
}

protected:

void updateViewpoint();

void setupCameraTween(CTweenVector3df* look, CTweenVector3df* up);
};
}
}
Loading

0 comments on commit f712dcf

Please sign in to comment.