Skip to content

Commit

Permalink
Add on-screen view control buttons in sector map.
Browse files Browse the repository at this point in the history
- add a functions to calculate the window size by the number of elements
in the utils
- redo the hyperjump planner from a separate module to a part of the
system map module, so that its position and size could be controlled by
system map module.
- add on-screen buttons "ResetView", "Rotate", "Zoom" to the panel on
the right-center.
- add handler functions to these buttons to SectorView class, and
LuaSectorView.cpp
  • Loading branch information
Gliese852 committed May 9, 2020
1 parent 0c07390 commit c5ee0ba
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 63 deletions.
20 changes: 20 additions & 0 deletions data/libs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,24 @@ utils.reverse = function(t)
end
return res
end

utils.newWindowCalculator = function(buttonSize, buttonFramePadding, itemSpacing, textHeight)
return {
buttonSize = buttonSize,
buttonFramePadding = buttonFramePadding,
itemSpacing = itemSpacing,
width = function(buttons)
return (buttonSize.y + buttonFramePadding * 2) * buttons + itemSpacing.x * (buttons + 1)
end,
height = function(buttons, separators, texts)
return
(buttonSize.y + buttonFramePadding * 2) * buttons
+ separators * itemSpacing.y
+ texts * textHeight
+ (buttons + texts - 1) * itemSpacing.y
+ itemSpacing.x * 2
end
}
end

return utils
58 changes: 27 additions & 31 deletions data/pigui/modules/hyperjump-planner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ local lc = Lang.GetResource("core")
local lui = Lang.GetResource("ui-core");

local ui = require 'pigui'
local utils = require 'utils'

local player = nil
local colors = ui.theme.colors
local icons = ui.theme.icons

local sectorView

local mainButtonSize = Vector2(24,24) * (ui.screenHeight / 1200)
local mainButtonSize = ui.rescaleUI(Vector2(24,24), Vector2(1600, 900))
local mainButtonFramePadding = 3
local itemSpacing = Vector2(8, 4) -- couldn't get default from ui

local hyperJumpPlanner = {} -- for export

local calcWindow = utils.newWindowCalculator(mainButtonSize, mainButtonFramePadding, itemSpacing, ui.calcTextSize("H").y)
hyperJumpPlanner.windowSize = Vector2(calcWindow.width(7), calcWindow.height(1, 6, 19))

-- hyperjump route stuff
local hyperjump_route = {}
Expand Down Expand Up @@ -212,8 +219,6 @@ end -- showJumpPlan


local function showHyperJumpPlannerWindow()
ui.setNextWindowSize(Vector2(ui.screenWidth / 5, (ui.screenHeight / 5) * 2), "Always")
ui.setNextWindowPos(Vector2(ui.screenWidth - ui.screenWidth / 5 - 10, ui.screenHeight - ((ui.screenHeight / 5) * 2) - 10), "Always")
ui.withStyleColors({["WindowBg"] = colors.lightBlackBackground}, function()
ui.window("MapSectorViewHyperJumpPlanner", {"NoTitleBar", "NoResize", "NoFocusOnAppearing", "NoBringToFrontOnFocus"},
function()
Expand All @@ -228,7 +233,7 @@ local function showHyperJumpPlannerWindow()
end)
end -- showHyperJumpPlannerWindow

local function displayHyperJumpPlanner()
function hyperJumpPlanner.display()
player = Game.player
local current_view = Game.CurrentView()

Expand All @@ -248,31 +253,22 @@ local function displayHyperJumpPlanner()
showHyperJumpPlannerWindow()
end
end
end -- displayHyperJumpPlanner

ui.registerModule("game", displayHyperJumpPlanner)

Event.Register("onEnterSystem",
function(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
sectorView:RemoveRouteItem(1)
end
end -- hyperJumpPlanner.display


function hyperJumpPlanner.setSectorView(sv)
sectorView = sv
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
sectorView:RemoveRouteItem(1)
end
end)

Event.Register("onGameStart",
function()
--connect to the class SectorView
sectorView = Game.sectorView
end)

Event.Register("onGameEnd",
function(ship)
-- clear the route out so it doesn't show up if the user starts a new game
sectorView:ClearRoute()
end)
return {}
end
end

return hyperJumpPlanner
63 changes: 51 additions & 12 deletions data/pigui/modules/map-sector-view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local Game = require 'Game'
local utils = require 'utils'
local Event = require 'Event'
local SystemPath = require 'SystemPath'
local hyperJumpPlanner = require 'pigui.modules.hyperjump-planner'

local Lang = require 'Lang'
local lc = Lang.GetResource("core");
Expand All @@ -18,18 +19,33 @@ local colors = ui.theme.colors
local icons = ui.theme.icons
local hideSectorViewWindows = false

local mainButtonSize = Vector2(32,32) * (ui.screenHeight / 1200)
local mainButtonSize = ui.rescaleUI(Vector2(32,32), Vector2(1600, 900))
local mainButtonFramePadding = 3
local itemSpacing = Vector2(8, 4) -- couldn't get default from ui

-- all colors, used in this module
local svColor = {
BUTTON_ACTIVE = colors.buttonBlue,
BUTTON_INACTIVE = Color(150, 150, 200, 0),
BUTTON_SEMIACTIVE = Color(150, 150, 200, 80),
BUTTON_INK = colors.white,
UNKNOWN = Color(255,0,255)
}

local function mainMenuButton(icon, selected, tooltip, color)
color = color or colors.white
return ui.coloredSelectedIconButton(icon, mainButtonSize, selected, mainButtonFramePadding, colors.buttonBlue, color, tooltip)
end

local sectorView

local calcWindow = utils.newWindowCalculator(mainButtonSize, mainButtonFramePadding, itemSpacing, ui.calcTextSize("H").y)

local onGameStart = function ()
--connect to class SectorView
sectorView = Game.sectorView
--connect hyper jump planner to class SectorView
hyperJumpPlanner.setSectorView(sectorView)
end

local function showSystemInfo(label, current_systempath, systempath, othersystempath)
Expand Down Expand Up @@ -68,7 +84,9 @@ local function showSystemInfo(label, current_systempath, systempath, othersystem
if next(starsystem.other_names) ~= nil then
ui.text(table.concat(starsystem.other_names, ", "))
end
ui.text(starsystem.shortDescription)
ui.pushTextWrapPos(ui.getContentRegion().x)
ui.textWrapped(starsystem.shortDescription)
ui.popTextWrapPos()
if othersystempath and not othersystempath:IsSameSystem(systempath) then
local otherstarsystem = othersystempath:GetStarSystem()
local jumpStatus, distance, fuelRequired, duration = player:GetHyperspaceDetails(systempath, othersystempath)
Expand All @@ -79,6 +97,26 @@ local function showSystemInfo(label, current_systempath, systempath, othersystem
end
end

local edgeButtonsWindowSize = Vector2(calcWindow.width(1), calcWindow.height(3, 0, 0))
local edgeButtonsWindowPos = Vector2(ui.screenWidth - edgeButtonsWindowSize.x, ui.screenHeight / 2 - edgeButtonsWindowSize.y / 2) -- center-right

local function showEdgeButtons()
ui.setNextWindowPos(edgeButtonsWindowPos, "Always")
ui.withStyleColors({["WindowBg"] = svColor.WINDOW_BACK}, function()
ui.window("SectorMapEdgeButtons", {"NoTitleBar", "NoResize", "NoFocusOnAppearing", "NoBringToFrontOnFocus", "NoSavedSettings"},
function()
-- view control buttons
if ui.coloredSelectedIconButton(icons.reset_view, mainButtonSize, showShips, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, lc.RESET_ORIENTATION_AND_ZOOM) then
sectorView:ResetView()
end
ui.coloredSelectedIconButton(icons.rotate_view, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, lui.ROTATE_VIEW)
sectorView:SetRotateMode(ui.isItemActive())
ui.coloredSelectedIconButton(icons.search_lens,mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, lui.ZOOM)
sectorView:SetZoomMode(ui.isItemActive())
end)
end)
end

local search_text = ""
local draw_vertical_lines = false
local draw_out_range_labels = false
Expand Down Expand Up @@ -176,16 +214,6 @@ local function showInfoWindow()
ui.window("MapSectorViewInfo", {"NoTitleBar", "NoResize", "NoFocusOnAppearing", "NoBringToFrontOnFocus", "HorizontalScrollbar"},
function()
ui.text(string.format("%.2f", sectorView:GetZoomLevel()) .. lc.UNIT_LY)
ui.sameLine()
local zoom_in = mainMenuButton(icons.zoom_in, false, "Zoom in")
if zoom_in or ui.isItemActive() then
sectorView:ZoomIn()
end
ui.sameLine()
local zoom_out = mainMenuButton(icons.zoom_out, false, "Zoom out")
if zoom_out or ui.isItemActive() then
sectorView:ZoomOut()
end
ui.separator()
local sector = sectorView:GetCenterSector()
local distance = sectorView:GetCenterDistance()
Expand Down Expand Up @@ -252,6 +280,10 @@ local function displaySectorViewWindow()
showInfoWindow()
showFactionLegendWindow()
end
ui.setNextWindowSize(hyperJumpPlanner.windowSize, "Always")
ui.setNextWindowPos(Vector2(ui.screenWidth - calcWindow.width(1), ui.screenHeight) - hyperJumpPlanner.windowSize, "Always")
hyperJumpPlanner.display()
showEdgeButtons()
end
end

Expand All @@ -260,5 +292,12 @@ Event.Register("onGameStart", onGameStart)
Event.Register("onLeaveSystem", function()
hyperspaceDetailsCache = {}
end)
-- events moved from hyperJumpPlanner
Event.Register("onGameEnd",
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", hyperJumpPlanner.onEnterSystem(ship))

return {}
25 changes: 7 additions & 18 deletions data/pigui/modules/system-view-ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local Lang = require 'Lang'
local ui = require 'pigui'
local Format = require 'Format'
local Input = require 'Input'
local utils = require 'utils'

local Vector2 = _G.Vector2
local lc = Lang.GetResource("core")
Expand Down Expand Up @@ -140,31 +141,19 @@ local nextShipDrawings = loop3items("SHIPS_OFF", "SHIPS_ON", "SHIPS_ORBITS")
local nextShowLagrange = loop3items("LAG_OFF", "LAG_ICON", "LAG_ICONTEXT")
local nextShowGrid = loop3items("GRID_OFF", "GRID_ON", "GRID_AND_LEGS")

local function calcWindowWidth(buttons)
return (mainButtonSize.y + mainButtonFramePadding * 2) * buttons
+ itemSpacing.x * (buttons + 1)
end

local function calcWindowHeight(buttons, separators, texts)
return
(mainButtonSize.y + mainButtonFramePadding * 2) * buttons
+ separators * itemSpacing.y
+ texts * ui.calcTextSize("H").y
+ (buttons + texts - 1) * itemSpacing.y
+ itemSpacing.x * 2
end
local calcWindow = utils.newWindowCalculator(mainButtonSize, mainButtonFramePadding, itemSpacing, ui.calcTextSize("H").y)

-- calculate fixed windows positions and sizes (if need)
local edgeButtonsWindowSize = Vector2(calcWindowWidth(1), calcWindowHeight(8, 0, 2))
local edgeButtonsWindowSize = Vector2(calcWindow.width(1), calcWindow.height(8, 0, 2))
local edgeButtonsWindowPos = Vector2(ui.screenWidth - edgeButtonsWindowSize.x, ui.screenHeight / 2 - edgeButtonsWindowSize.y / 2) -- center-right
local timeButtonsWindowPos = Vector2(ui.screenWidth - calcWindowWidth(7), ui.screenHeight - calcWindowHeight(1, 0, 1))
local orbitPlannerWindowSize = Vector2(edgeButtonsWindowPos.x - timeButtonsWindowPos.x, calcWindowHeight(5, 1, 1))
local timeButtonsWindowPos = Vector2(ui.screenWidth - calcWindow.width(7), ui.screenHeight - calcWindow.height(1, 0, 1))
local orbitPlannerWindowSize = Vector2(edgeButtonsWindowPos.x - timeButtonsWindowPos.x, calcWindow.height(5, 1, 1))
local orbitPlannerWindowPos = Vector2(edgeButtonsWindowPos.x, timeButtonsWindowPos.y) - orbitPlannerWindowSize

local function showEdgeButtons()
ui.setNextWindowPos(edgeButtonsWindowPos, "Always")
ui.withStyleColors({["WindowBg"] = svColor.WINDOW_BACK}, function()
ui.window("EdgeButtons", {"NoTitleBar", "NoResize", "NoFocusOnAppearing", "NoBringToFrontOnFocus", "NoSavedSettings"},
ui.window("SystemMapEdgeButtons", {"NoTitleBar", "NoResize", "NoFocusOnAppearing", "NoBringToFrontOnFocus", "NoSavedSettings"},
function()
-- view control buttons
if ui.coloredSelectedIconButton(icons.reset_view, mainButtonSize, showShips, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, lc.RESET_ORIENTATION_AND_ZOOM) then
Expand Down Expand Up @@ -542,7 +531,7 @@ local function showTargetInfoWindow(obj)
end
end
tableWidth = tableWidth + itemSpacing.x * 3 -- total needed window width
local ws = Vector2(tableWidth, calcWindowHeight(0, 1, math.max(rowsCount, MINIMUM_ROWS) + 1)) -- at least 5 rows
local ws = Vector2(tableWidth, calcWindow.height(0, 1, math.max(rowsCount, MINIMUM_ROWS) + 1)) -- at least 5 rows
-- force align orbitplanner, if less
ws.x = math.max(ws.x, edgeButtonsWindowPos.x - orbitPlannerWindowPos.x)
-- no more than a third of a screen
Expand Down
39 changes: 37 additions & 2 deletions src/SectorView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,13 +1094,24 @@ void SectorView::Update()
if (InputBindings.mapViewPitch->IsActive()) m_rotXMovingTo += 0.5f * moveSpeed * InputBindings.mapViewPitch->GetValue();
}

if (Pi::input->MouseButtonState(SDL_BUTTON_MIDDLE)) {
// to capture mouse when button was pressed and release when released
if (Pi::input->MouseButtonState(SDL_BUTTON_MIDDLE) != m_rotateWithMouseButton) {
m_rotateWithMouseButton = !m_rotateWithMouseButton;
Pi::input->SetCapturingMouse(m_rotateWithMouseButton);
}

if (m_rotateWithMouseButton || m_rotateView) {
int motion[2];
Pi::input->GetMouseMotion(motion);

m_rotXMovingTo += 0.2f * float(motion[1]);
m_rotZMovingTo += 0.2f * float(motion[0]);
}
else if (m_zoomView) {
Pi::input->SetCapturingMouse(true);
int motion[2];
Pi::input->GetMouseMotion(motion);
m_zoomMovingTo += ZOOM_SPEED * 0.002f * motion[1];
}

m_rotXMovingTo = Clamp(m_rotXMovingTo, -170.0f, -10.0f);

Expand Down Expand Up @@ -1355,3 +1366,27 @@ void SectorView::SetFactionVisible(const Faction *faction, bool visible)
m_hiddenFactions.insert(faction);
m_toggledFaction = true;
}

void SectorView::SetZoomMode(bool enable)
{
if (enable != m_zoomView) {
Pi::input->SetCapturingMouse(enable);
m_zoomView = enable;
if (m_zoomView) m_rotateView = false;
}
}

void SectorView::SetRotateMode(bool enable) {
if (enable != m_rotateView) {
Pi::input->SetCapturingMouse(enable);
m_rotateView = enable;
if (m_rotateView) m_zoomView = false;
}
}

void SectorView::ResetView() {
GotoSystem(m_current);
m_rotXMovingTo = m_rotXDefault;
m_rotZMovingTo = m_rotZDefault;
m_zoomMovingTo = m_zoomDefault;
}
7 changes: 7 additions & 0 deletions src/SectorView.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class SectorView : public UIView, public DeleteEmitter {
const std::set<const Faction *> &GetVisibleFactions() { return m_visibleFactions; }
const std::set<const Faction *> &GetHiddenFactions() { return m_hiddenFactions; }
void SetFactionVisible(const Faction *faction, bool visible);
void SetZoomMode(bool enable);
void SetRotateMode(bool enable);
void ResetView();

// HyperJump Route Planner
bool MoveRouteItemUp(const std::vector<SystemPath>::size_type element);
Expand Down Expand Up @@ -156,6 +159,10 @@ class SectorView : public UIView, public DeleteEmitter {
float m_zoomClamped;
float m_zoomMovingTo;

bool m_rotateWithMouseButton = false;
bool m_rotateView = false;
bool m_zoomView = false;

SystemPath m_hyperspaceTarget;
bool m_matchTargetToSelection;
bool m_automaticSystemSelection;
Expand Down
Loading

0 comments on commit c5ee0ba

Please sign in to comment.