Changeset View
Changeset View
Standalone View
Standalone View
source/tools/atlas/GameInterface/Handlers/CameraCtrlHandlers.cpp
Show All 32 Lines | |||||
#include "graphics/Terrain.h" | #include "graphics/Terrain.h" | ||||
#include <assert.h> | #include <assert.h> | ||||
namespace AtlasMessage { | namespace AtlasMessage { | ||||
MESSAGEHANDLER(CameraReset) | MESSAGEHANDLER(CameraReset) | ||||
{ | { | ||||
if (!g_Game || g_Game->GetView()->GetCinema()->IsEnabled()) | if (!CGame::IsGameInitialised() || CGame::GetGame()->GetView()->GetCinema()->IsEnabled()) | ||||
return; | return; | ||||
CVector3D focus = g_Game->GetView()->GetCamera()->GetFocus(); | CVector3D focus = CGame::GetGame()->GetView()->GetCamera()->GetFocus(); | ||||
CVector3D target; | CVector3D target; | ||||
if (!g_Game->GetWorld()->GetTerrain()->IsOnMap(focus.X, focus.Z)) | if (!CGame::GetGame()->GetWorld()->GetTerrain()->IsOnMap(focus.X, focus.Z)) | ||||
{ | { | ||||
target = CVector3D( | target = CVector3D( | ||||
g_Game->GetWorld()->GetTerrain()->GetMaxX()/2.f, | CGame::GetGame()->GetWorld()->GetTerrain()->GetMaxX()/2.f, | ||||
focus.Y, | focus.Y, | ||||
g_Game->GetWorld()->GetTerrain()->GetMaxZ()/2.f); | CGame::GetGame()->GetWorld()->GetTerrain()->GetMaxZ()/2.f); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
target = focus; | target = focus; | ||||
} | } | ||||
g_Game->GetView()->ResetCameraTarget(target); | CGame::GetGame()->GetView()->ResetCameraTarget(target); | ||||
UNUSED2(msg); | UNUSED2(msg); | ||||
} | } | ||||
MESSAGEHANDLER(ScrollConstant) | MESSAGEHANDLER(ScrollConstant) | ||||
{ | { | ||||
if (!g_Game || g_Game->GetView()->GetCinema()->IsEnabled()) | if (!CGame::IsGameInitialised() || CGame::GetGame()->GetView()->GetCinema()->IsEnabled()) | ||||
return; | return; | ||||
if (msg->dir < 0 || msg->dir > 5) | if (msg->dir < 0 || msg->dir > 5) | ||||
{ | { | ||||
debug_warn(L"ScrollConstant: invalid direction"); | debug_warn(L"ScrollConstant: invalid direction"); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
g_AtlasGameLoop->input.scrollSpeed[msg->dir] = msg->speed; | g_AtlasGameLoop->input.scrollSpeed[msg->dir] = msg->speed; | ||||
} | } | ||||
} | } | ||||
// TODO: change all these g_Game->...GetCamera() bits to use the current AtlasView's | // TODO: change all these CGame::GetGame()->...GetCamera() bits to use the current AtlasView's | ||||
// camera instead. | // camera instead. | ||||
MESSAGEHANDLER(Scroll) | MESSAGEHANDLER(Scroll) | ||||
{ | { | ||||
if (!g_Game || g_Game->GetView()->GetCinema()->IsEnabled()) // TODO: do this better (probably a separate AtlasView class for cinematics) | if (!CGame::IsGameInitialised() || CGame::GetGame()->GetView()->GetCinema()->IsEnabled()) // TODO: do this better (probably a separate AtlasView class for cinematics) | ||||
return; | return; | ||||
static CVector3D targetPos; | static CVector3D targetPos; | ||||
static float targetDistance = 0.f; | static float targetDistance = 0.f; | ||||
CMatrix3D& camera = g_Game->GetView()->GetCamera()->m_Orientation; | CMatrix3D& camera = CGame::GetGame()->GetView()->GetCamera()->m_Orientation; | ||||
static CVector3D lastCameraPos = camera.GetTranslation(); | static CVector3D lastCameraPos = camera.GetTranslation(); | ||||
// Ensure roughly correct motion when dragging is combined with other | // Ensure roughly correct motion when dragging is combined with other | ||||
// movements. | // movements. | ||||
if (lastCameraPos != camera.GetTranslation()) | if (lastCameraPos != camera.GetTranslation()) | ||||
targetPos += camera.GetTranslation() - lastCameraPos; | targetPos += camera.GetTranslation() - lastCameraPos; | ||||
Show All 13 Lines | if (msg->type == eScrollType::FROM) | ||||
targetPos = msg->pos->GetWorldSpace(); | targetPos = msg->pos->GetWorldSpace(); | ||||
targetDistance = (targetPos - camera.GetTranslation()).Length(); | targetDistance = (targetPos - camera.GetTranslation()).Length(); | ||||
} | } | ||||
else if (msg->type == eScrollType::TO) | else if (msg->type == eScrollType::TO) | ||||
{ | { | ||||
CVector3D origin, dir; | CVector3D origin, dir; | ||||
float x, y; | float x, y; | ||||
msg->pos->GetScreenSpace(x, y); | msg->pos->GetScreenSpace(x, y); | ||||
g_Game->GetView()->GetCamera()->BuildCameraRay((int)x, (int)y, origin, dir); | CGame::GetGame()->GetView()->GetCamera()->BuildCameraRay((int)x, (int)y, origin, dir); | ||||
dir *= targetDistance; | dir *= targetDistance; | ||||
camera.Translate(targetPos - dir - origin); | camera.Translate(targetPos - dir - origin); | ||||
g_Game->GetView()->GetCamera()->UpdateFrustum(); | CGame::GetGame()->GetView()->GetCamera()->UpdateFrustum(); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
debug_warn(L"Scroll: Invalid type"); | debug_warn(L"Scroll: Invalid type"); | ||||
} | } | ||||
lastCameraPos = camera.GetTranslation(); | lastCameraPos = camera.GetTranslation(); | ||||
} | } | ||||
MESSAGEHANDLER(SmoothZoom) | MESSAGEHANDLER(SmoothZoom) | ||||
{ | { | ||||
if (!g_Game || g_Game->GetView()->GetCinema()->IsEnabled()) | if (!CGame::IsGameInitialised() || CGame::GetGame()->GetView()->GetCinema()->IsEnabled()) | ||||
return; | return; | ||||
g_AtlasGameLoop->input.zoomDelta += msg->amount; | g_AtlasGameLoop->input.zoomDelta += msg->amount; | ||||
} | } | ||||
MESSAGEHANDLER(RotateAround) | MESSAGEHANDLER(RotateAround) | ||||
{ | { | ||||
if (!g_Game || g_Game->GetView()->GetCinema()->IsEnabled()) | if (!CGame::IsGameInitialised() || CGame::GetGame()->GetView()->GetCinema()->IsEnabled()) | ||||
return; | return; | ||||
static CVector3D focusPos; | static CVector3D focusPos; | ||||
static float lastX = 0.f, lastY = 0.f; | static float lastX = 0.f, lastY = 0.f; | ||||
CMatrix3D& camera = g_Game->GetView()->GetCamera()->m_Orientation; | CMatrix3D& camera = CGame::GetGame()->GetView()->GetCamera()->m_Orientation; | ||||
if (msg->type == eRotateAroundType::FROM) | if (msg->type == eRotateAroundType::FROM) | ||||
{ | { | ||||
msg->pos->GetScreenSpace(lastX, lastY); // get mouse position | msg->pos->GetScreenSpace(lastX, lastY); // get mouse position | ||||
focusPos = msg->pos->GetWorldSpace(); // get point on terrain under mouse | focusPos = msg->pos->GetWorldSpace(); // get point on terrain under mouse | ||||
} | } | ||||
else if (msg->type == eRotateAroundType::TO) | else if (msg->type == eRotateAroundType::TO) | ||||
{ | { | ||||
Show All 17 Lines | else if (msg->type == eRotateAroundType::TO) | ||||
q.ToMatrix(camera); | q.ToMatrix(camera); | ||||
// Make sure up is still pointing up, regardless of any rounding errors. | // Make sure up is still pointing up, regardless of any rounding errors. | ||||
// (Maybe this distorts the camera in other ways, but at least the errors | // (Maybe this distorts the camera in other ways, but at least the errors | ||||
// are far less noticeable to me.) | // are far less noticeable to me.) | ||||
camera._21 = 0.f; // (_21 = Y component returned by GetLeft()) | camera._21 = 0.f; // (_21 = Y component returned by GetLeft()) | ||||
camera.Translate(focusPos + offset); | camera.Translate(focusPos + offset); | ||||
g_Game->GetView()->GetCamera()->UpdateFrustum(); | CGame::GetGame()->GetView()->GetCamera()->UpdateFrustum(); | ||||
lastX = x; | lastX = x; | ||||
lastY = y; | lastY = y; | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
debug_warn(L"RotateAround: Invalid type"); | debug_warn(L"RotateAround: Invalid type"); | ||||
} | } | ||||
Show All 26 Lines | MESSAGEHANDLER(LookAt) | ||||
camera.m_Orientation = M.GetTranspose(); | camera.m_Orientation = M.GetTranspose(); | ||||
camera.m_Orientation.Translate(-eye); | camera.m_Orientation.Translate(-eye); | ||||
camera.UpdateFrustum(); | camera.UpdateFrustum(); | ||||
} | } | ||||
QUERYHANDLER(GetView) | QUERYHANDLER(GetView) | ||||
{ | { | ||||
if (!g_Game) | if (!CGame::IsGameInitialised()) | ||||
return; | return; | ||||
CVector3D focus = g_Game->GetView()->GetCamera()->GetFocus(); | CVector3D focus = CGame::GetGame()->GetView()->GetCamera()->GetFocus(); | ||||
sCameraInfo info; | sCameraInfo info; | ||||
info.pX = focus.X; | info.pX = focus.X; | ||||
info.pY = focus.Y; | info.pY = focus.Y; | ||||
info.pZ = focus.Z; | info.pZ = focus.Z; | ||||
CQuaternion quatRot = g_Game->GetView()->GetCamera()->m_Orientation.GetRotation(); | CQuaternion quatRot = CGame::GetGame()->GetView()->GetCamera()->m_Orientation.GetRotation(); | ||||
quatRot.Normalize(); | quatRot.Normalize(); | ||||
CVector3D rotation = quatRot.ToEulerAngles(); | CVector3D rotation = quatRot.ToEulerAngles(); | ||||
info.rX = RADTODEG(rotation.X); | info.rX = RADTODEG(rotation.X); | ||||
info.rY = RADTODEG(rotation.Y); | info.rY = RADTODEG(rotation.Y); | ||||
info.rZ = RADTODEG(rotation.Z); | info.rZ = RADTODEG(rotation.Z); | ||||
msg->info = info; | msg->info = info; | ||||
} | } | ||||
MESSAGEHANDLER(SetView) | MESSAGEHANDLER(SetView) | ||||
{ | { | ||||
if (!g_Game || g_Game->GetView()->GetCinema()->IsEnabled()) | if (!CGame::IsGameInitialised() || CGame::GetGame()->GetView()->GetCinema()->IsEnabled()) | ||||
return; | return; | ||||
CGameView* view = g_Game->GetView(); | CGameView* view = CGame::GetGame()->GetView(); | ||||
view->ResetCameraTarget(view->GetCamera()->GetFocus()); | view->ResetCameraTarget(view->GetCamera()->GetFocus()); | ||||
sCameraInfo cam = msg->info; | sCameraInfo cam = msg->info; | ||||
view->ResetCameraTarget(CVector3D(cam.pX, cam.pY, cam.pZ)); | view->ResetCameraTarget(CVector3D(cam.pX, cam.pY, cam.pZ)); | ||||
// TODO: Rotation | // TODO: Rotation | ||||
} | } | ||||
} | } |
Wildfire Games · Phabricator