Skip to content

Commit

Permalink
Cleanup WorldView, inherit from PiGuiView
Browse files Browse the repository at this point in the history
- Move some less-used signals to PlayerShipController rather than Pi
- Remove WorldView's dependency on newUI
- Remove unused signal connections in WorldView
  • Loading branch information
sturnclaw committed Jul 10, 2020
1 parent 53b40a0 commit 67540d4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 65 deletions.
2 changes: 0 additions & 2 deletions src/Pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@
*/

float Pi::gameTickAlpha;
sigc::signal<void> Pi::onPlayerChangeTarget;
sigc::signal<void> Pi::onPlayerChangeFlightControlState;
LuaSerializer *Pi::luaSerializer;
LuaTimer *Pi::luaTimer;
LuaNameGen *Pi::luaNameGen;
Expand Down
3 changes: 0 additions & 3 deletions src/Pi.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ class Pi {

static const char SAVE_DIR_NAME[];

static sigc::signal<void> onPlayerChangeTarget; // navigation or combat
static sigc::signal<void> onPlayerChangeFlightControlState;

static LuaSerializer *luaSerializer;
static LuaTimer *luaTimer;

Expand Down
4 changes: 0 additions & 4 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,16 @@ Body *Player::GetSetSpeedTarget() const
void Player::SetCombatTarget(Body *const target, bool setSpeedTo)
{
static_cast<PlayerShipController *>(m_controller)->SetCombatTarget(target, setSpeedTo);
Pi::onPlayerChangeTarget.emit();
}

void Player::SetNavTarget(Body *const target)
{
static_cast<PlayerShipController *>(m_controller)->SetNavTarget(target);
Pi::onPlayerChangeTarget.emit();
}

void Player::SetSetSpeedTarget(Body *const target)
{
static_cast<PlayerShipController *>(m_controller)->SetSetSpeedTarget(target);
// TODO: not sure, do we actually need this? we are only changing the set speed target
Pi::onPlayerChangeTarget.emit();
}

void Player::ChangeSetSpeed(double delta)
Expand Down
24 changes: 3 additions & 21 deletions src/WorldView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "sound/Sound.h"
#include "ui/Widget.h"

const double WorldView::PICK_OBJECT_RECT_SIZE = 20.0;
namespace {
static const Color s_hudTextColor(0, 255, 0, 230);
static const float HUD_CROSSHAIR_SIZE = 8.0f;
Expand All @@ -37,14 +36,14 @@ namespace {
} // namespace

WorldView::WorldView(Game *game) :
UIView(),
PiGuiView("WorldView"),
m_game(game)
{
InitObject();
}

WorldView::WorldView(const Json &jsonObj, Game *game) :
UIView(),
PiGuiView("WorldView"),
m_game(game)
{
if (!jsonObj["world_view"].is_object()) throw SavedGameCorruptException();
Expand Down Expand Up @@ -77,9 +76,6 @@ void WorldView::RegisterInputBindings()

void WorldView::InitObject()
{
float size[2];
GetSizeRequested(size);

m_labelsOn = true;
SetTransparency(true);

Expand Down Expand Up @@ -112,7 +108,7 @@ void WorldView::InitObject()
shipView->Init();

m_onPlayerChangeTargetCon =
Pi::onPlayerChangeTarget.connect(sigc::mem_fun(this, &WorldView::OnPlayerChangeTarget));
Pi::player->GetPlayerController()->onChangeTarget.connect(sigc::mem_fun(this, &WorldView::OnPlayerChangeTarget));

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 @@ -186,8 +182,6 @@ void WorldView::Draw3D()
}

m_cameraContext->EndFrame();

UIView::Draw3D();
}

void WorldView::OnToggleLabels()
Expand All @@ -204,11 +198,6 @@ void WorldView::OnToggleLabels()
}
}

void WorldView::ShowAll()
{
View::ShowAll(); // by default, just delegate back to View
}

void WorldView::Update()
{
PROFILE_SCOPED()
Expand Down Expand Up @@ -256,17 +245,10 @@ void WorldView::Update()
for (auto it = Pi::player->GetSensors()->GetContacts().begin(); it != Pi::player->GetSensors()->GetContacts().end(); ++it)
it->trail->Reset(playerFrameId);
}

UIView::Update();
}

void WorldView::BuildUI(UI::Single *container)
{
}

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

Expand Down
31 changes: 12 additions & 19 deletions src/WorldView.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#ifndef _WORLDVIEW_H
#define _WORLDVIEW_H

#include "UIView.h"
#include "gui/GuiWidget.h"
#include "pigui/View.h"
#include "ship/ShipViewController.h"

class Body;
Expand Down Expand Up @@ -33,28 +33,27 @@ namespace Gui {
namespace KeyBindings {
struct ActionBinding;
struct AxisBinding;
}
} // namespace KeyBindings

namespace UI {
class Widget;
class Single;
class Label;
} // namespace UI

class WorldView : public UIView {
class WorldView : public PiGuiView {
public:
static void RegisterInputBindings();
friend class NavTunnelWidget;
WorldView(Game *game);
WorldView(const Json &jsonObj, Game *game);
virtual ~WorldView();
virtual void ShowAll();
virtual void Update();
virtual void Draw3D();
virtual void Draw();
static const double PICK_OBJECT_RECT_SIZE;
virtual void SaveToJson(Json &jsonObj);
virtual void HandleSDLEvent(SDL_Event &event);

void Update() override;
void Draw3D() override;
void Draw() override;
void SaveToJson(Json &jsonObj) override;
void HandleSDLEvent(SDL_Event &event) override;

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

Expand All @@ -77,9 +76,8 @@ class WorldView : public UIView {
bool ShouldShowLabels() { return m_labelsOn; }

protected:
virtual void BuildUI(UI::Single *container);
virtual void OnSwitchTo();
virtual void OnSwitchFrom();
void OnSwitchTo() override;
void OnSwitchFrom() override;

private:
void InitObject();
Expand All @@ -101,7 +99,7 @@ class WorldView : public UIView {
pos(0.0f, 0.0f),
realpos(0.0f, 0.0f),
side(INDICATOR_HIDDEN)
{}
{}
};

void UpdateProjectedObjects();
Expand All @@ -127,12 +125,7 @@ class WorldView : public UIView {

bool m_labelsOn;

// useful docking locations for new-ui widgets in the HUD
RefCountedPtr<UI::Widget> m_hudRoot;

sigc::connection m_onHyperspaceTargetChangedCon;
sigc::connection m_onPlayerChangeTargetCon;
sigc::connection m_onChangeFlightControlStateCon;
sigc::connection m_onToggleHudModeCon;
sigc::connection m_onIncTimeAccelCon;
sigc::connection m_onDecTimeAccelCon;
Expand Down
1 change: 1 addition & 0 deletions src/pigui/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PiGuiView : public View {

virtual void Update() {}
virtual void Draw3D() {}
virtual void DrawPiGui() {}

private:
virtual void OnSwitchTo(){};
Expand Down
36 changes: 20 additions & 16 deletions src/ship/PlayerShipController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,26 @@ bool PlayerShipController::IsAnyLinearThrusterKeyDown()

void PlayerShipController::SetFlightControlState(FlightControlState s)
{
if (m_flightControlState != s) {
m_flightControlState = s;
m_ship->AIClearInstructions();
//set desired velocity to current actual
if (m_flightControlState == CONTROL_FIXSPEED) {
// Speed is set to the projection of the velocity onto the target.
if (m_flightControlState == s)
return;

vector3d shipVel = m_setSpeedTarget ?
// Ship's velocity with respect to the target, in current frame's coordinates
-m_setSpeedTarget->GetVelocityRelTo(m_ship) :
// Ship's velocity with respect to current frame
m_ship->GetVelocity();
m_flightControlState = s;
m_ship->AIClearInstructions();
//set desired velocity to current actual
if (m_flightControlState == CONTROL_FIXSPEED) {
// Speed is set to the projection of the velocity onto the target.

// A change from Manual to Set Speed never sets a negative speed.
m_setSpeed = std::max(shipVel.Dot(-m_ship->GetOrient().VectorZ()), 0.0);
}
vector3d shipVel = m_setSpeedTarget ?
// Ship's velocity with respect to the target, in current frame's coordinates
-m_setSpeedTarget->GetVelocityRelTo(m_ship) :
// Ship's velocity with respect to current frame
m_ship->GetVelocity();

//XXX global stuff
Pi::onPlayerChangeFlightControlState.emit();
// A change from Manual to Set Speed never sets a negative speed.
m_setSpeed = std::max(shipVel.Dot(-m_ship->GetOrient().VectorZ()), 0.0);
}

onChangeFlightControlState.emit();
}

void PlayerShipController::SetLowThrustPower(float power)
Expand Down Expand Up @@ -438,14 +438,18 @@ void PlayerShipController::SetCombatTarget(Body *const target, bool setSpeedTo)
if (setSpeedTo)
m_setSpeedTarget = target;
m_combatTarget = target;
onChangeTarget.emit();
}

void PlayerShipController::SetNavTarget(Body *const target)
{
m_navTarget = target;
onChangeTarget.emit();
}

void PlayerShipController::SetSetSpeedTarget(Body *const target)
{
m_setSpeedTarget = target;
// TODO: not sure, do we actually need this? we are only changing the set speed target
onChangeTarget.emit();
}
2 changes: 2 additions & 0 deletions src/ship/PlayerShipController.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class PlayerShipController : public ShipController {
void SetSetSpeedTarget(Body *const target);

sigc::signal<void> onRotationDampingChanged;
sigc::signal<void> onChangeTarget;
sigc::signal<void> onChangeFlightControlState;

private:
static struct InputBinding : public Input::InputFrame {
Expand Down

0 comments on commit 67540d4

Please sign in to comment.