Skip to content

Commit

Permalink
Additional minor tweaks to the maps
Browse files Browse the repository at this point in the history
- move detailed route calculation to a separate function, now it does
not execute every frame, only when changing the route or when changing
the equipment
- attach lambda functions to the OnPressed callbacks of these input actions
instead of the generic OnKeyPressed callback
- add some comments
  • Loading branch information
Gliese852 committed Jun 30, 2020
1 parent 37d7b45 commit 92be9cd
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 89 deletions.
79 changes: 48 additions & 31 deletions data/pigui/modules/hyperjump-planner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ local function showInfo()
local start = current_path
-- Tally up totals for the entire jump plan
for _,jump in pairs(hyperjump_route) do
local status, distance, fuel, duration = player:GetHyperspaceDetails(start, jump)
local status, distance, fuel, duration = player:GetHyperspaceDetails(start, jump.path)

total_fuel = total_fuel + fuel
total_duration = total_duration + duration
total_distance = total_distance + distance

start = jump
start = jump.path
end

textIcon(icons.display_navtarget, lui.CURRENT_SYSTEM)
Expand All @@ -94,7 +94,7 @@ local function showInfo()
textIcon(icons.route_destination, lui.FINAL_TARGET)

if route_jumps > 0 then
local final_path = hyperjump_route[route_jumps]
local final_path = hyperjump_route[route_jumps].path
local final_sys = final_path:GetStarSystem()
ui.sameLine()
if ui.selectable(final_sys.name .. " (" .. final_path.sectorX .. ", " .. final_path.sectorY .. ", " .. final_path.sectorZ .. ")", false, {}) then
Expand Down Expand Up @@ -137,11 +137,42 @@ local function mainButton(icon, tooltip, callback)
return button
end --mainButton

local function buildJumpRouteList()
hyperjump_route = {}
local start = current_path
local drive = table.unpack(player:GetEquip("engine")) or nil
local fuel_type = drive and drive.fuel or Equipment.cargo.hydrogen
local current_fuel = player:CountEquip(fuel_type,"cargo")
local running_fuel = 0
for jumpIndex, jump in pairs(sectorView:GetRoute()) do
local jump_sys = jump:GetSystemBody()
local status, distance, fuel, duration = player:GetHyperspaceDetails(start, jump)
local color
local remaining_fuel = current_fuel - running_fuel - fuel
if remaining_fuel == 0 then
color = colors.alertYellow
else
if remaining_fuel < 0 then
color = colors.alertRed
else
color = colors.font
end
end
hyperjump_route[jumpIndex] = {
path = jump,
color = color,
textLine = jumpIndex ..": ".. jump_sys.name .. " (" .. string.format("%.2f", distance) .. lc.UNIT_LY .. " - " .. fuel .. lc.UNIT_TONNES..")"
}
running_fuel = fuel + running_fuel
start = jump
end -- for
end

local function updateHyperspaceTarget()
hyperjump_route = sectorView:GetRoute()
buildJumpRouteList()
if #hyperjump_route > 0 then
-- first waypoint is always the hyperspace target
sectorView:SetHyperspaceTarget(hyperjump_route[1])
sectorView:SetHyperspaceTarget(hyperjump_route[1].path)
else
sectorView:ResetHyperspaceTarget()
end
Expand Down Expand Up @@ -208,55 +239,36 @@ local function showJumpRoute()
mainButton(icons.search_lens, lui.CENTER_ON_SYSTEM,
function()
if selected_jump then
sectorView:GotoSystemPath(hyperjump_route[selected_jump])
sectorView:GotoSystemPath(hyperjump_route[selected_jump].path)
end
end)

ui.separator()

local start = current_path
local clicked
local running_fuel = 0
ui.child("routelist", function()
for jumpIndex, jump in pairs(hyperjump_route) do
local jump_sys = jump:GetSystemBody()
local status, distance, fuel, duration = player:GetHyperspaceDetails(start, jump)
local color
local remaining_fuel = current_fuel - running_fuel - fuel

if remaining_fuel == 0 then
color = colors.alertYellow
else
if remaining_fuel < 0 then
color = colors.alertRed
else
color = colors.font
end
end

ui.withStyleColors({["Text"] = color},
ui.withStyleColors({["Text"] = jump.color},
function()
if ui.selectable(jumpIndex..": ".. jump_sys.name .. " (" .. string.format("%.2f", distance) .. lc.UNIT_LY .. " - " .. fuel .. lc.UNIT_TONNES..")", jumpIndex == selected_jump, {}) then
if ui.selectable(jump.textLine, jumpIndex == selected_jump) then
clicked = jumpIndex
end
end)
running_fuel = fuel + running_fuel
start = jump
end -- for
end --function
)

if clicked then
selected_jump = clicked
sectorView:SwitchToPath(hyperjump_route[selected_jump])
sectorView:SwitchToPath(hyperjump_route[selected_jump].path)
end
end
end -- showJumpPlan

-- scan the route and if this system is there, but another star is selected, update it in route
function hyperJumpPlanner.updateInRoute(path)
for jumpIndex, jump in pairs(hyperjump_route) do
if jump:IsSameSystem(path) then
if jump.path:IsSameSystem(path) then
selected_jump = jumpIndex
if jump ~= path then
sectorView:UpdateRouteItem(jumpIndex, path)
Expand Down Expand Up @@ -322,7 +334,6 @@ function hyperJumpPlanner.display()
current_path = current_system.path
current_fuel = player:CountEquip(fuel_type,"cargo")
map_selected_path = sectorView:GetSelectedSystemPath()
hyperjump_route = sectorView:GetRoute()
route_jumps = sectorView:GetRouteSize()
showHyperJumpPlannerWindow()
end -- hyperJumpPlanner.display
Expand All @@ -331,12 +342,18 @@ function hyperJumpPlanner.setSectorView(sv)
sectorView = sv
end

function hyperJumpPlanner.onShipEquipmentChanged(ship, equipment)
if ship:IsPlayer() and equipment and (equipment:GetName() == "Hydrogen" or equipment:IsValidSlot("engine", ship)) then
buildJumpRouteList()
end
end

function hyperJumpPlanner.onEnterSystem(ship)
-- remove the first jump if it's the current system (and enabled to do so)
-- this should be the case if you are following a route and want the route to be
-- updated as you make multiple jumps
if ship:IsPlayer() and remove_first_if_current then
if route_jumps > 0 and hyperjump_route[1]:IsSameSystem(Game.system.path) then
if route_jumps > 0 and hyperjump_route[1].path:IsSameSystem(Game.system.path) then
sectorView:RemoveRouteItem(1)
end
end
Expand Down
6 changes: 2 additions & 4 deletions data/pigui/modules/map-sector-view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ function(ship)
-- clear the route out so it doesn't show up if the user starts a new game
sectorView:ClearRoute()
end)
Event.Register("onEnterSystem",
function(ship)
hyperJumpPlanner.onEnterSystem(ship)
end)
Event.Register("onEnterSystem", hyperJumpPlanner.onEnterSystem)
Event.Register("onShipEquipmentChanged", hyperJumpPlanner.onShipEquipmentChanged)

return {}
2 changes: 2 additions & 0 deletions data/pigui/modules/settings-window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ local ModalWindow = require 'pigui.libs.modal-win'

-- convert an axis binding style ID to a translation resource identifier
local function localize_binding_id(str)
-- TODO: avoid reading lines from the "Core" resource (lc)
-- it's here to reuse old strings (keyboard bindings for maps in KeyBindings.inc.h)
local jsonIndex = str:gsub("([^A-Z0-9_])([A-Z0-9])", "%1_%2"):upper()
return rawget(linput, jsonIndex) or rawget(lc, jsonIndex) or error("NO_JSON: " .. jsonIndex)
end
Expand Down
2 changes: 1 addition & 1 deletion src/Pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void RegisterInputBindings()
ShipViewController::InputBindings.RegisterBindings();

WorldView::RegisterInputBindings();
SectorView::RegisterInputBindings();
SectorView::InputBindings.RegisterBindings();
}

void Pi::App::Startup()
Expand Down
96 changes: 47 additions & 49 deletions src/SectorView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ void SectorView::InitDefaults()

void SectorView::InitObject()
{
// single keystroke handlers
m_onToggleSelectionFollowView =
InputBindings.mapToggleSelectionFollowView->onPress.connect([&]() {
m_automaticSystemSelection = !m_automaticSystemSelection;
});
m_onWarpToCurrent =
InputBindings.mapWarpToCurrent->onPress.connect([&]() {
GotoSystem(m_current);
});
m_onWarpToSelected =
InputBindings.mapWarpToSelected->onPress.connect([&]() {
GotoSystem(m_selected);
});
m_onViewReset =
InputBindings.mapViewReset->onPress.connect([&]() {
while (m_rotZ < -180.0f)
m_rotZ += 360.0f;
while (m_rotZ > 180.0f)
m_rotZ -= 360.0f;
m_rotXMovingTo = m_rotXDefault;
m_rotZMovingTo = m_rotZDefault;
m_zoomMovingTo = m_zoomDefault;
});

m_onMouseWheelCon =
Pi::input->onMouseWheel.connect(sigc::mem_fun(this, &SectorView::MouseWheel));

SetTransparency(true);

m_lineVerts.reset(new Graphics::VertexArray(Graphics::ATTRIB_POSITION, 500));
Expand Down Expand Up @@ -169,14 +196,15 @@ void SectorView::InitObject()

m_disk.reset(new Graphics::Drawables::Disk(m_renderer, m_solidState, Color::WHITE, 0.2f));

m_onMouseWheelCon =
Pi::input->onMouseWheel.connect(sigc::mem_fun(this, &SectorView::MouseWheel));
}

SectorView::~SectorView()
{
m_onMouseWheelCon.disconnect();
if (m_onKeyPressConnection.connected()) m_onKeyPressConnection.disconnect();
m_onToggleSelectionFollowView.disconnect();
m_onWarpToCurrent.disconnect();
m_onWarpToSelected.disconnect();
m_onViewReset.disconnect();
}

void SectorView::SaveToJson(Json &jsonObj)
Expand Down Expand Up @@ -1009,54 +1037,14 @@ void SectorView::BuildFarSector(RefCountedPtr<Sector> sec, const vector3f &origi
void SectorView::OnSwitchTo()
{
m_renderer->SetViewport(0, 0, Graphics::GetScreenWidth(), Graphics::GetScreenHeight());

if (!m_onKeyPressConnection.connected())
m_onKeyPressConnection =
Pi::input->onKeyPress.connect(sigc::mem_fun(this, &SectorView::OnKeyPressed));

Pi::input->PushInputFrame(&InputBindings);
UIView::OnSwitchTo();

Update();
}

void SectorView::OnKeyPressed(SDL_Keysym *keysym)
void SectorView::OnSwitchFrom()
{
if (Pi::GetView() != this) {
m_onKeyPressConnection.disconnect();
return;
}

// XXX ugly hack checking for Lua console here
if (Pi::IsConsoleActive())
return;

if (InputBindings.mapToggleSelectionFollowView->Matches(keysym)) {
m_automaticSystemSelection = !m_automaticSystemSelection;
return;
}

bool reset_view = false;

// fast move selection to current player system or hyperspace target
const bool shifted = (Pi::input->KeyState(SDLK_LSHIFT) || Pi::input->KeyState(SDLK_RSHIFT));
if (InputBindings.mapWarpToCurrent->Matches(keysym)) {
GotoSystem(m_current);
reset_view = shifted;
} else if (InputBindings.mapWarpToSelected->Matches(keysym)) {
GotoSystem(m_selected);
reset_view = shifted;
}

// reset rotation and zoom
if (reset_view || InputBindings.mapViewReset->Matches(keysym)) {
while (m_rotZ < -180.0f)
m_rotZ += 360.0f;
while (m_rotZ > 180.0f)
m_rotZ -= 360.0f;
m_rotXMovingTo = m_rotXDefault;
m_rotZMovingTo = m_rotZDefault;
m_zoomMovingTo = m_zoomDefault;
}
Pi::input->RemoveInputFrame(&InputBindings);
}

void SectorView::Update()
Expand Down Expand Up @@ -1329,15 +1317,21 @@ std::vector<SystemPath> SectorView::GetNearbyStarSystemsByName(std::string patte

SectorView::InputBinding SectorView::InputBindings;

void SectorView::RegisterInputBindings()
void SectorView::InputBinding::RegisterBindings()
{
using namespace KeyBindings;
Input::BindingPage *page = Pi::input->GetBindingPage("MapControls");
Input::BindingGroup *group;

#define BINDING_GROUP(n) group = page->GetBindingGroup(#n);
#define KEY_BINDING(n, id, k1, k2) InputBindings.n = Pi::input->AddActionBinding(id, group, ActionBinding(k1, k2));
#define AXIS_BINDING(n, id, k1, k2) InputBindings.n = Pi::input->AddAxisBinding(id, group, AxisBinding(k1, k2));
#define KEY_BINDING(n, id, k1, k2) \
n = \
Pi::input->AddActionBinding(id, group, ActionBinding(k1, k2)); \
actions.push_back(n);
#define AXIS_BINDING(n, id, k1, k2) \
n = \
Pi::input->AddAxisBinding(id, group, AxisBinding(k1, k2)); \
axes.push_back(n);

BINDING_GROUP(GeneralViewControls)
KEY_BINDING(mapViewReset, "ResetOrientationAndZoom", SDLK_t, 0)
Expand All @@ -1352,6 +1346,10 @@ void SectorView::RegisterInputBindings()
KEY_BINDING(mapToggleSelectionFollowView, "MapToggleSelectionFollowView", SDLK_RETURN, SDLK_KP_ENTER)
KEY_BINDING(mapWarpToCurrent, "MapWarpToCurrentSystem", SDLK_c, 0)
KEY_BINDING(mapWarpToSelected, "MapWarpToSelectedSystem", SDLK_g, 0)

#undef BINDING_GROUP
#undef KEY_BINDING
#undef AXIS_BINDING
}

void SectorView::SetFactionVisible(const Faction *faction, bool visible)
Expand Down
9 changes: 6 additions & 3 deletions src/SectorView.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace Graphics {

class SectorView : public UIView, public DeleteEmitter {
public:
static void RegisterInputBindings();
SectorView(Game *game);
SectorView(const Json &jsonObj, Game *game);
virtual ~SectorView();
Expand Down Expand Up @@ -95,11 +94,12 @@ class SectorView : public UIView, public DeleteEmitter {
Axis *mapViewYaw;
Axis *mapViewPitch;
Axis *mapViewZoom;

virtual void RegisterBindings();
} InputBindings;

protected:
virtual void OnSwitchTo();
virtual void OnSwitchFrom();

private:
void InitDefaults();
Expand Down Expand Up @@ -183,7 +183,10 @@ class SectorView : public UIView, public DeleteEmitter {
void OnToggleFaction(Gui::ToggleButton *button, bool pressed, const Faction *faction);

sigc::connection m_onMouseWheelCon;
sigc::connection m_onKeyPressConnection;
sigc::connection m_onToggleSelectionFollowView;
sigc::connection m_onWarpToCurrent;
sigc::connection m_onWarpToSelected;
sigc::connection m_onViewReset;

RefCountedPtr<SectorCache::Slave> m_sectorCache;
std::string m_previousSearch;
Expand Down
16 changes: 15 additions & 1 deletion src/SystemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ void SystemView::CalculateFramePositionAtTime(FrameId frameId, double t, vector3
void SystemView::Draw3D()
{
PROFILE_SCOPED()
m_renderer->SetPerspectiveProjection(CAMERA_FOV, m_renderer->GetDisplayAspect(), 1.f, 1000.f * float(AU) + DEFAULT_VIEW_DISTANCE * 2);
// We need to adjust the "far" cutoff plane, so that at high magnifications you can see
// distant objects in the background.
m_renderer->SetPerspectiveProjection(CAMERA_FOV, m_renderer->GetDisplayAspect(), 1.f, 1000.f * m_zoom * float(AU) + DEFAULT_VIEW_DISTANCE * 2);
m_renderer->ClearScreen();
m_projected.clear();
//TODO add reserve
Expand All @@ -504,6 +506,18 @@ void SystemView::Draw3D()
m_unexplored = m_system->GetUnexplored();
}

// The matrix is shifted from the (0,0,0) by DEFAULT_VIEW_DISTANCE
// and then rotated (around 0,0,0) and scaled by m_zoom, shift doesn't scale.
// m_zoom default value is 1/AU.
// { src/gameconsts.h:static const double AU = 149598000000.0; // m }
// The coordinates of the objects are given in meters; they are multiplied by m_zoom,
// therefore dy default, 1.0 AU (in meters) in the coordinate turns into 1.0 in camera space.
// Since the *shift doesn't scale*, it always equals to DEFAULT_VIEW_DISTANCE,
// in camera space.
// So, the "apparent" view distance, is DEFAULT_VIEW_DISTANCE / m_zoom * AU (AU)
// Therefore the default "apparent" view distance is DEFAULT_VIEW_DISTANCE, in AU
// When we change m_zoom, we actually change the "apparent" view distance, because
// the coordinates of the objects are scaled, but the shift is not.
m_cameraSpace = matrix4x4f::Identity();
m_cameraSpace.Translate(0, 0, -DEFAULT_VIEW_DISTANCE);
m_cameraSpace.Rotate(DEG2RAD(m_rot_x), 1, 0, 0);
Expand Down

0 comments on commit 92be9cd

Please sign in to comment.