Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Improve Camera Controls #4908

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ class Body : public Object, public PropertiedObject {

virtual void SetLabel(const std::string &label);
const std::string &GetLabel() const { return m_label; }

unsigned int GetFlags() const { return m_flags; }
// TODO(sturnclaw) use this sparingly, the flags interface is rather fragile and needs work
void SetFlag(unsigned int flag, bool enable)
{
if (enable)
m_flags |= flag;
else
m_flags &= ~flag;
}

// Only Space::KillBody() should call this method.
void MarkDead() { m_dead = true; }
bool IsDead() const { return m_dead; }
Expand Down Expand Up @@ -108,9 +118,12 @@ class Body : public Object, public PropertiedObject {
// Usually equal to the center of the body == vector3d(0, 0, 0)
virtual vector3d GetTargetIndicatorPosition() const;

enum { FLAG_CAN_MOVE_FRAME = (1 << 0),
enum {
FLAG_CAN_MOVE_FRAME = (1 << 0),
FLAG_LABEL_HIDDEN = (1 << 1),
FLAG_DRAW_LAST = (1 << 2) }; // causes the body drawn after other bodies in the z-sort
FLAG_DRAW_LAST = (1 << 2), // causes the body drawn after other bodies in the z-sort
FLAG_DRAW_EXCLUDE = (1 << 3) // do not draw this body, intended for e.g. when camera is inside
};

protected:
virtual void SaveToJson(Json &jsonObj, Space *space);
Expand Down
4 changes: 4 additions & 0 deletions src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ void Camera::Update()
attrs.body = b;
attrs.billboard = false; // false by default

// If the body wishes to be excluded from the draw, skip it.
if (b->GetFlags() & Body::FLAG_DRAW_EXCLUDE)
continue;

// determine position and transform for draw
// Frame::GetFrameTransform(b->GetFrame(), camFrame, attrs.viewTransform); // doesn't use interp coords, so breaks in some cases
Frame *f = Frame::GetFrame(b->GetFrame());
Expand Down
2 changes: 1 addition & 1 deletion src/ShipCockpit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include "ShipCockpit.h"

#include "CameraController.h"
#include "Easing.h"
#include "Game.h"
#include "Pi.h"
#include "Player.h"
#include "WorldView.h"
#include "graphics/Renderer.h"
#include "ship/CameraController.h"

ShipCockpit::ShipCockpit(const std::string &modelName) :
m_shipDir(0.0),
Expand Down
28 changes: 28 additions & 0 deletions src/ViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

#pragma once

class Camera;
class WorldView;

class ViewController {
public:
ViewController(WorldView *v) :
m_parentView(v) {}

// Called when the view owning this controller becomes active
virtual void Activated() = 0;

// Called when the view owning this controller becomes inactive
virtual void Deactivated() = 0;

// Called every frame to update the controller before the camera is updated
virtual void Update() = 0;

// Do view-specific drawing here, called after the camera has drawn the world
virtual void Draw(Camera *camera) = 0;

protected:
WorldView *m_parentView;
};
29 changes: 13 additions & 16 deletions src/WorldView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void WorldView::InitObject()

shipView.reset(new ShipViewController(this));
shipView->Init();
SetViewController(shipView.get());

m_onToggleHudModeCon = InputBindings.toggleHudMode->onPress.connect(sigc::mem_fun(this, &WorldView::OnToggleLabels));
m_onIncTimeAccelCon = InputBindings.increaseTimeAcceleration->onPress.connect(sigc::mem_fun(this, &WorldView::OnRequestTimeAccelInc));
Expand Down Expand Up @@ -140,6 +141,11 @@ void WorldView::OnRequestTimeAccelDec()
Pi::game->RequestTimeAccelDec();
}

void WorldView::SetViewController(ViewController *newView)
{
m_viewController = newView;
}

void WorldView::Draw3D()
{
PROFILE_SCOPED()
Expand All @@ -149,22 +155,11 @@ void WorldView::Draw3D()

m_cameraContext->ApplyDrawTransforms(m_renderer);

Body *excludeBody = nullptr;
ShipCockpit *cockpit = nullptr;
if (shipView->GetCamType() == ShipViewController::CAM_INTERNAL) {
excludeBody = Pi::player;
if (shipView->m_internalCameraController->GetMode() == InternalCameraController::MODE_FRONT)
cockpit = Pi::player->GetCockpit();
}
m_camera->Draw(excludeBody);
m_camera->Draw();

// NB: Do any screen space rendering after here:
// Things like the cockpit and AR features like hudtrails, space dust etc.

// Render cockpit
// XXX camera should rotate inside cockpit, not rotate the cockpit around in the world
if (cockpit)
cockpit->RenderCockpit(m_renderer, m_camera.get(), m_camera->GetContext()->GetTempFrame());
m_viewController->Draw(m_camera.get());

// Draw 3D HUD
// Speed lines
Expand Down Expand Up @@ -201,7 +196,7 @@ void WorldView::Update()
assert(Pi::player);
assert(!Pi::player->IsDead());

shipView->Update();
m_viewController->Update();

m_cameraContext->BeginFrame();
m_camera->Update();
Expand Down Expand Up @@ -242,12 +237,14 @@ void WorldView::Update()

void WorldView::OnSwitchTo()
{
shipView->Activated();
if (m_viewController)
m_viewController->Activated();
}

void WorldView::OnSwitchFrom()
{
shipView->Deactivated();
if (m_viewController)
m_viewController->Deactivated();
Pi::DrawGUI = true;
}

Expand Down
4 changes: 4 additions & 0 deletions src/WorldView.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class WorldView : public PiGuiView {

RefCountedPtr<CameraContext> GetCameraContext() const { return m_cameraContext; }

ViewController *GetViewController() const { return m_viewController; }
void SetViewController(ViewController *newView);

std::unique_ptr<ShipViewController> shipView;

int GetActiveWeapon() const;
Expand Down Expand Up @@ -118,6 +121,7 @@ class WorldView : public PiGuiView {
void SelectBody(Body *, bool reselectIsDeselect);

Game *m_game;
ViewController *m_viewController;

NavTunnelWidget *m_navTunnel;
std::unique_ptr<SpeedLines> m_speedLines;
Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 0 additions & 18 deletions src/ship/InteractionController.h

This file was deleted.

13 changes: 12 additions & 1 deletion src/ship/ShipViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void ShipViewController::SaveToJson(Json &jsonObj)

void ShipViewController::Init()
{
RefCountedPtr<CameraContext> m_cameraContext = parentView->GetCameraContext();
RefCountedPtr<CameraContext> m_cameraContext = m_parentView->GetCameraContext();
m_internalCameraController.reset(new InternalCameraController(m_cameraContext, Pi::player));
m_externalCameraController.reset(new ExternalCameraController(m_cameraContext, Pi::player));
m_siderealCameraController.reset(new SiderealCameraController(m_cameraContext, Pi::player));
Expand All @@ -101,10 +101,12 @@ void ShipViewController::Activated()
Pi::input->onMouseWheel.connect(sigc::mem_fun(this, &ShipViewController::MouseWheel));

Pi::player->GetPlayerController()->SetMouseForRearView(GetCamType() == CAM_INTERNAL && m_internalCameraController->GetMode() == InternalCameraController::MODE_REAR);
Pi::player->SetFlag(Body::FLAG_DRAW_EXCLUDE, !IsExteriorView());
}

void ShipViewController::Deactivated()
{
Pi::player->SetFlag(Body::FLAG_DRAW_EXCLUDE, false);
Pi::input->RemoveInputFrame(&InputBindings);

m_onMouseWheelCon.disconnect();
Expand Down Expand Up @@ -137,6 +139,7 @@ void ShipViewController::SetCamType(enum CamType c)
headtracker_input_priority = false;
}

Pi::player->SetFlag(Body::FLAG_DRAW_EXCLUDE, !IsExteriorView());
Pi::player->GetPlayerController()->SetMouseForRearView(m_camType == CAM_INTERNAL && m_internalCameraController->GetMode() == InternalCameraController::MODE_REAR);

m_activeCameraController->Reset();
Expand Down Expand Up @@ -238,6 +241,14 @@ void ShipViewController::Update()
m_activeCameraController->Update();
}

void ShipViewController::Draw(Camera *camera)
{
// Render cockpit
// XXX camera should rotate inside cockpit, not rotate the cockpit around in the world
if (!IsExteriorView() && Pi::player->GetCockpit() && m_internalCameraController->GetMode() == InternalCameraController::MODE_FRONT)
Pi::player->GetCockpit()->RenderCockpit(Pi::renderer, camera, camera->GetContext()->GetTempFrame());
}

void ShipViewController::MouseWheel(bool up)
{
if (m_activeCameraController->IsExternal()) {
Expand Down
7 changes: 4 additions & 3 deletions src/ship/ShipViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@

#include "CameraController.h"
#include "Input.h"
#include "InteractionController.h"
#include "KeyBindings.h"
#include "ViewController.h"
#include "utils.h"

class ShipViewController : public InteractionController {
class ShipViewController : public ViewController {
public:
ShipViewController(WorldView *v) :
InteractionController(v),
ViewController(v),
m_camType(CAM_INTERNAL),
headtracker_input_priority(false) {}

void Update() override;
void Activated() override;
void Deactivated() override;
void Draw(Camera *camera) override;

enum CamType {
CAM_INTERNAL,
Expand Down