From 1e5871436b4e3e2c329ecc15078dcf37edfcd083 Mon Sep 17 00:00:00 2001 From: JonBooth78 Date: Sun, 21 Jan 2024 22:27:44 +1300 Subject: [PATCH 1/4] Save and restore planned hysperspace route and the settings for the map sector view Fixes #5721 --- data/pigui/modules/hyperjump-planner.lua | 3 + data/pigui/views/map-sector-view.lua | 82 +++++++++++++++++++----- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/data/pigui/modules/hyperjump-planner.lua b/data/pigui/modules/hyperjump-planner.lua index 20eef134ef7..6c289702548 100644 --- a/data/pigui/modules/hyperjump-planner.lua +++ b/data/pigui/modules/hyperjump-planner.lua @@ -352,6 +352,9 @@ end function hyperJumpPlanner.onGameStart() -- get notified when the player buys hydrogen Game.player:GetComponent('CargoManager'):AddListener('hyperjump-planner', hyperJumpPlanner.onPlayerCargoChanged) + -- we may have just loaded a jump route list, so lets build it fresh now + buildJumpRouteList() + end function hyperJumpPlanner.onGameEnd() diff --git a/data/pigui/views/map-sector-view.lua b/data/pigui/views/map-sector-view.lua index 574bd30eade..b134e96e469 100644 --- a/data/pigui/views/map-sector-view.lua +++ b/data/pigui/views/map-sector-view.lua @@ -17,6 +17,8 @@ local Color = _G.Color local ui = require 'pigui' local layout = require 'pigui.libs.window-layout' +local Serializer = require 'Serializer' + local player = nil local colors = ui.theme.colors local icons = ui.theme.icons @@ -39,10 +41,16 @@ local buttonState = { [false] = ui.theme.buttonColors.transparent } -local draw_vertical_lines = false -local draw_out_range_labels = false -local draw_uninhabited_labels = true -local automatic_system_selection = true +local settings = +{ + draw_vertical_lines=false, + draw_out_range_labels=false, + draw_uninhabited_labels=true, + automatic_system_selection=true +} + +local loaded_data = nil + local function textIcon(icon, tooltip) ui.icon(icon, Vector2(ui.getTextLineHeight()), svColor.FONT, tooltip) @@ -65,13 +73,29 @@ local onGameStart = function () hyperJumpPlanner.setSectorView(sectorView) -- reset hyperspace details cache on new game hyperspaceDetailsCache = {} + + -- apply any data loaded earlier + if loaded_data then + if loaded_data.jump_targets then + local targets = loaded_data.jump_targets + for _, target in pairs( loaded_data.jump_targets) do + sectorView:AddToRoute(target) + end + end + if loaded_data.settings then + settings = loaded_data.settings + end + loaded_data = nil + end + -- update visibility states - sectorView:SetAutomaticSystemSelection(automatic_system_selection) - sectorView:SetDrawOutRangeLabels(draw_out_range_labels) - sectorView:GetMap():SetDrawUninhabitedLabels(draw_uninhabited_labels) - sectorView:GetMap():SetDrawVerticalLines(draw_vertical_lines) + sectorView:SetAutomaticSystemSelection(settings.automatic_system_selection) + sectorView:SetDrawOutRangeLabels(settings.draw_out_range_labels) + sectorView:GetMap():SetDrawUninhabitedLabels(settings.draw_uninhabited_labels) + sectorView:GetMap():SetDrawVerticalLines(settings.draw_vertical_lines) sectorView:GetMap():SetLabelParams("orbiteer", font.size, 2.0, svColor.LABEL_HIGHLIGHT, svColor.LABEL_SHADE) - -- allow hyperjump planner to register its events + + -- allow hyperjump planner to register its events: hyperJumpPlanner.onGameStart() end @@ -245,21 +269,21 @@ end local function showSettings() local changed - changed, draw_vertical_lines = ui.checkbox(lc.DRAW_VERTICAL_LINES, draw_vertical_lines) + changed, settings.draw_vertical_lines = ui.checkbox(lc.DRAW_VERTICAL_LINES, settings.draw_vertical_lines) if changed then - sectorView:GetMap():SetDrawVerticalLines(draw_vertical_lines) + sectorView:GetMap():SetDrawVerticalLines(settings.draw_vertical_lines) end - changed, draw_out_range_labels = ui.checkbox(lc.DRAW_OUT_RANGE_LABELS, draw_out_range_labels) + changed, settings.draw_out_range_labels = ui.checkbox(lc.DRAW_OUT_RANGE_LABELS, settings.draw_out_range_labels) if changed then - sectorView:SetDrawOutRangeLabels(draw_out_range_labels) + sectorView:SetDrawOutRangeLabels(settings.draw_out_range_labels) end - changed, draw_uninhabited_labels = ui.checkbox(lc.DRAW_UNINHABITED_LABELS, draw_uninhabited_labels) + changed, settings.draw_uninhabited_labels = ui.checkbox(lc.DRAW_UNINHABITED_LABELS, settings.draw_uninhabited_labels) if changed then - sectorView:GetMap():SetDrawUninhabitedLabels(draw_uninhabited_labels) + sectorView:GetMap():SetDrawUninhabitedLabels(settings.draw_uninhabited_labels) end - changed, automatic_system_selection = ui.checkbox(lc.AUTOMATIC_SYSTEM_SELECTION, automatic_system_selection) + changed, settings.automatic_system_selection = ui.checkbox(lc.AUTOMATIC_SYSTEM_SELECTION, settings.automatic_system_selection) if changed then - sectorView:SetAutomaticSystemSelection(automatic_system_selection) + sectorView:SetAutomaticSystemSelection(settings.automatic_system_selection) end -- end end @@ -468,4 +492,28 @@ Event.Register("onShipTypeChanged", function(ship, ...) if ship:IsPlayer() then hyperspaceDetailsCache = {} end end) + +local serialize = function () + + local data = + { + version = 1, + jump_targets = {}, + settings = settings + } + + for jumpIndex, jump_sys in pairs(sectorView:GetRoute()) do + table.insert( data.jump_targets, jump_sys ) + end + + return data +end + +local unserialize = function (data) + loaded_data = data +end + +Serializer:Register("HyperJumpPlanner", serialize, unserialize) + + return {} From 1d1bb7df637c3c4e6bcd6c633bb7c0c198ad67d6 Mon Sep 17 00:00:00 2001 From: JonBooth78 Date: Sun, 28 Jan 2024 22:17:33 +1300 Subject: [PATCH 2/4] Correctly hide or show out of range labels Before if the options to draw out of range labels was ticket, they were hidden and unticled they were shown. --- src/SectorView.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SectorView.cpp b/src/SectorView.cpp index 006c7644c77..4bc29731a55 100644 --- a/src/SectorView.cpp +++ b/src/SectorView.cpp @@ -96,9 +96,9 @@ class SectorView::SectorMapCallbacks : public SectorMapContext::Callbacks { if (dist > sv.m_playerHyperspaceRange) { if (sv.m_drawOutRangeLabels) { - return DisplayModes::HIDE_LABEL; - } else { return DisplayModes::SHADOW_LABEL; + } else { + return DisplayModes::HIDE_LABEL; } } return DisplayModes::DEFAULT; From 7c515ef75e72e10fdb3c56dc5bd5bd1bd5e823b9 Mon Sep 17 00:00:00 2001 From: JonBooth78 Date: Sun, 28 Jan 2024 22:32:44 +1300 Subject: [PATCH 3/4] clang format --- src/SectorView.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/SectorView.cpp b/src/SectorView.cpp index 4bc29731a55..22b0c084de2 100644 --- a/src/SectorView.cpp +++ b/src/SectorView.cpp @@ -3,16 +3,12 @@ #include "SectorView.h" -#include -#include -#include #include "AnimationCurves.h" #include "Game.h" #include "GameSaveError.h" #include "Input.h" #include "Json.h" #include "JsonUtils.h" -#include "lua/LuaObject.h" #include "MathUtil.h" #include "Pi.h" #include "Player.h" @@ -24,9 +20,13 @@ #include "galaxy/Galaxy.h" #include "galaxy/Sector.h" #include "galaxy/StarSystem.h" +#include "lua/LuaObject.h" #include "lua/LuaRef.h" #include "lua/LuaTable.h" #include "matrix4x4.h" +#include +#include +#include #include @@ -73,8 +73,10 @@ void SectorView::InputBinding::RegisterBindings() // callbacks for the SectorMap class SectorView::SectorMapCallbacks : public SectorMapContext::Callbacks { SectorView &sv; + public: - SectorMapCallbacks(SectorView &sv) : sv(sv) {} + SectorMapCallbacks(SectorView &sv) : + sv(sv) {} void OnClickLabel(const SystemPath &clickedLabel) override { @@ -139,7 +141,6 @@ SectorView::SectorView(Game *game) : m_detailBoxVisible = DETAILBOX_INFO; InitObject(); - } SectorView::SectorView(const Json &jsonObj, Game *game) : @@ -195,7 +196,7 @@ void SectorView::InitObject() }); m_onViewReset = InputBindings.mapViewReset->onPressed.connect([&]() { - m_map->ResetView(); + m_map->ResetView(); }); } @@ -242,7 +243,7 @@ void SectorView::Draw3D() // prior to modelview transformation, rotate in the opposite direction so // that the billboard is always facing the camera matrix4x4f rot = modelview; - rot.ClearToRotOnly(); + rot.ClearToRotOnly(); rot = rot.Inverse(); // move this disk 0.03 light years further so that it does not overlap the star, and selected indicator and hyperspace target indicator m_map->AddStarBillboard(trans * rot, vector3f(0.f, 0.f, -0.03f), Color(0, 0, 204), 1.5f); @@ -272,7 +273,6 @@ void SectorView::Draw3D() // actually rendering m_map->Draw3D(); - } void SectorView::DrawPiGui() @@ -641,7 +641,6 @@ void SectorView::Update() } m_playerHyperspaceRange = LuaObject::CallMethod(Pi::player, "GetHyperspaceRange"); - } void SectorView::ResetView() From d018a27a39c7e4b73336cea157333027c9f99bd9 Mon Sep 17 00:00:00 2001 From: JonBooth78 Date: Sun, 28 Jan 2024 22:29:40 +1300 Subject: [PATCH 4/4] Fix being able to jump with multiple destinations planned in the route --- data/pigui/views/map-sector-view.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data/pigui/views/map-sector-view.lua b/data/pigui/views/map-sector-view.lua index b134e96e469..50097305ed8 100644 --- a/data/pigui/views/map-sector-view.lua +++ b/data/pigui/views/map-sector-view.lua @@ -469,9 +469,9 @@ ui.registerModule("game", { id = 'map-sector-view', draw = function() end}) Event.Register("onGameStart", onGameStart) -Event.Register("onEnterSystem", function() - hyperJumpPlanner.onEnterSystem() - hyperspaceDetailsCache = {} +Event.Register("onEnterSystem", function(ship) + hyperJumpPlanner.onEnterSystem(ship) + if ship:IsPlayer() then hyperspaceDetailsCache = {} end end) -- reset cached data