Skip to content

Commit

Permalink
Merge pull request pioneerspacesim#5898 from Web-eWorks/load-game-window
Browse files Browse the repository at this point in the history
Redesign Save/Load window, add UI Notification system
  • Loading branch information
Webster Sheets authored Aug 23, 2024
2 parents a005ad4 + a2b7923 commit dfb0d47
Show file tree
Hide file tree
Showing 33 changed files with 1,008 additions and 278 deletions.
4 changes: 4 additions & 0 deletions data/lang/core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,10 @@
"description": "Distance unit: an astronomical unit, radius of Earth's orbit",
"message": "AU"
},
"UNIT_CREDITS": {
"description": "Unit of money, used when $ isn't specific enough",
"message": "Cr"
},
"UNIT_CUBIC_METERS": {
"description": "Volume unit: cubic meter",
"message": ""
Expand Down
22 changes: 21 additions & 1 deletion data/lang/ui-core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,17 @@
"message": "Could not find system body for frame"
},
"COULD_NOT_LOAD_GAME": {
"description": "",
"description": "Label indicating a specific file could not be loaded",
"message": "Could not load game: "
},
"COULD_NOT_LOAD_SAVE_FILES": {
"description": "Label indicating the list of save files could not be loaded",
"message": "Could not load save files"
},
"COULD_NOT_SAVE_GAME": {
"description": "Label indicating a specific file could not be saved",
"message": "Could not save game: "
},
"COULD_NOT_READ_THE_SAVE_VERSION": {
"description": "Message when restoring savegame",
"message": "Could not read the save version"
Expand Down Expand Up @@ -2027,6 +2035,14 @@
"description": "",
"message": "Save"
},
"SAVED_GAMES": {
"description": "Header for save/load window",
"message": "Saved Games"
},
"SAVE_AS": {
"description": "",
"message": "Save As"
},
"SAVE_DELETED_SUCCESSFULLY": {
"description": "",
"message": "Save deleted successfully"
Expand Down Expand Up @@ -2155,6 +2171,10 @@
"description": "",
"message": "Ship Type"
},
"SHOW_AUTOSAVES": {
"description": "Button tooltip indicating whether to show autosave files",
"message": "Show Autosaves"
},
"SIMULATING_UNIVERSE_EVOLUTION_N_BYEARS": {
"description": "",
"message": "Simulating evolution of the universe: {age} billion years ;-)"
Expand Down
40 changes: 39 additions & 1 deletion data/libs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ utils.to_array = function(t, predicate)
return out
end

--
-- Function: find_if
--
-- Returns the first value in the passed table which matches the predicate.
--
-- Iteration order is undefined (uses pairs() internally).
--
---@generic K, V
---@param t table<K, V>
---@param predicate fun(k: K, v: V): boolean
---@return V?
utils.find_if = function(t, predicate)
for k, v in pairs(t) do
if predicate(k, v) then
return v
end
end

return nil
end

--
-- Function: stable_sort
--
Expand Down Expand Up @@ -543,7 +564,7 @@ end
--
-- Function: contains
--
-- Return true if the function contains the given value under any key.
-- Return true if the table contains the given value under any key.
--
utils.contains = function(t, val)
for _, v in pairs(t) do
Expand All @@ -553,6 +574,23 @@ utils.contains = function(t, val)
return false
end

--
-- Function: contains
--
-- Return true if the table contains a value that passes the given predicate.
--
---@generic K, V
---@param t table<K, V>
---@param predicate fun(v: V): boolean
---@return boolean
utils.contains_if = function(t, predicate)
for _, v in pairs(t) do
if predicate(v) then return true end
end

return false
end

--
-- Function: utils.indexOf
--
Expand Down
2 changes: 1 addition & 1 deletion data/meta/Color.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
-- Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

-- This file implements type information about C++ classes for Lua static analysis
Expand Down
1 change: 1 addition & 0 deletions data/meta/CoreObject/Game.meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function Game.SaveGame(filename) end

--- Delete savefile with specified filename.
---@param filename string
---@return boolean success
function Game.DeleteSave(filename) end

--- End the current game and return to the main menu.
Expand Down
2 changes: 1 addition & 1 deletion data/meta/Space.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
-- Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

-- This file implements type information about C++ classes for Lua static analysis
Expand Down
2 changes: 1 addition & 1 deletion data/modules/Common/ProximityQuery.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
-- Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

local Engine = require 'Engine'
Expand Down
12 changes: 6 additions & 6 deletions data/modules/FlightLog/FlightLogEntries.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
-- Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

-- Entries for the FlightLog
Expand Down Expand Up @@ -118,7 +118,7 @@ end

--- Based on flight state, compose a reasonable string for location
--- TODO: consider a class to represent, construct, store and format this
---@param location string[] Array of string info, the first one is the
---@param location string[] Array of string info, the first one is the
---@return string The formatted composite location.
function FlightLogEntry.Base.composeLocationString(location)
return string.interp(l["FLIGHTLOG_"..location[1]],
Expand Down Expand Up @@ -166,7 +166,7 @@ end
FlightLogEntry.System = utils.class("FlightLogEntry.System", FlightLogEntry.Base)

---@return string Description of this type
function FlightLogEntry.System:GetType()
function FlightLogEntry.System:GetType()
return "System"
end

Expand Down Expand Up @@ -257,7 +257,7 @@ end
FlightLogEntry.Custom = utils.class("FlightLogEntry.Custom", FlightLogEntry.Base)

---@return string Description of this type
function FlightLogEntry.Custom:GetType()
function FlightLogEntry.Custom:GetType()
return "Custom"
end

Expand Down Expand Up @@ -314,7 +314,7 @@ end
FlightLogEntry.Station = utils.class("FlightLogEntry.Station", FlightLogEntry.Base)

---@return string Description of this type
function FlightLogEntry.Station:GetType()
function FlightLogEntry.Station:GetType()
return "Station"
end

Expand Down Expand Up @@ -359,4 +359,4 @@ function FlightLogEntry.Station:GetDataPairs( earliest_first )
}
end

return FlightLogEntry
return FlightLogEntry
4 changes: 2 additions & 2 deletions data/modules/FlightLog/FlightLogExporter.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
-- Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

local FlightLog = require 'modules.FlightLog.FlightLog'
Expand Down Expand Up @@ -136,4 +136,4 @@ function Exporter.Export( included_types, earliest_first, player_info, export_ht

end

return Exporter
return Exporter
2 changes: 1 addition & 1 deletion data/pigui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ require 'pigui.libs.icons'
require 'pigui.libs.buttons'
require 'pigui.libs.radial-menu'
require 'pigui.libs.gauge'

require 'pigui.libs.notification'

return ui
11 changes: 5 additions & 6 deletions data/pigui/libs/buttons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,18 @@ end
--
-- Parameters:
-- label - string, label of the button to calculate size for
-- font - Font|string, optional font to use when calculating button size
-- size - number|nil, optional font size to use
-- font - Font, optional font to use when calculating button size
--
function ui.calcButtonSize(label, font, size)
return ui.calcTextSize(label, font, size) + ui.theme.styles.ButtonPadding * 2
function ui.calcButtonSize(label, font)
return ui.calcTextSize(label, font) + ui.theme.styles.ButtonPadding * 2
end

function ui.getButtonHeight(font)
return (font and font.size or ui.getTextLineHeight()) + ui.theme.styles.ButtonPadding.y * 2
end

function ui.getButtonHeightWithSpacing()
return ui.getTextLineHeightWithSpacing() + ui.theme.styles.ButtonPadding.y * 2.0
function ui.getButtonHeightWithSpacing(font)
return ui.getButtonHeight(font) + ui.getItemSpacing().y
end

--
Expand Down
4 changes: 3 additions & 1 deletion data/pigui/libs/forwarded.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ ui.pointOnClock = pigui.pointOnClock
ui.screenWidth = pigui.screen_width
ui.screenHeight = pigui.screen_height

ui.bringWindowToDisplayFront = pigui.bringWindowToDisplayFront ---@type fun()

-- Return the size of the specified window's contents from last frame (without padding/decoration)
-- Returns {0,0} if the window hasn't been submitted during the lifetime of the program
ui.getWindowContentSize = pigui.GetWindowContentSize ---@type fun(name: string): Vector2
Expand Down Expand Up @@ -55,7 +57,7 @@ ui.addCircleFilled = pigui.AddCircleFilled
ui.addRect = pigui.AddRect ---@type fun(a: Vector2, b: Vector2, col: Color, rounding: number, edges: integer, thickness: number)
ui.addRectFilled = pigui.AddRectFilled ---@type fun(a: Vector2, b: Vector2, col: Color, rounding: number, edges: integer)
ui.addLine = pigui.AddLine ---@type fun(a: Vector2, b: Vector2, col: Color, thickness: number)
ui.addText = pigui.AddText ---@type fun(pos: Vector2, col: Color, text: string)
ui.addText = pigui.AddText ---@type fun(pos: Vector2, col: Color, text: string, wrapWidth: number?)
ui.pathArcTo = pigui.PathArcTo
ui.pathStroke = pigui.PathStroke
ui.setCursorPos = pigui.SetCursorPos ---@type fun(pos: Vector2)
Expand Down
46 changes: 36 additions & 10 deletions data/pigui/libs/modal-win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,61 @@

local ui = require 'pigui'

local Module = require 'pigui.libs.module'
local utils = require 'utils'

local modalStack = {}

local ModalWindow = {}
---@class UI.ModalWindow : UI.Module
local ModalWindow = utils.class("UI.ModalWindow", Module)

local defaultModalFlags = ui.WindowFlags {"NoTitleBar", "NoResize", "AlwaysAutoResize", "NoMove"}

function ModalWindow.New(name, innerHandler, outerHandler, flags)
function ModalWindow.New(name, render, outerHandler, flags)
local modalWin = {
name = name,
flags = flags or defaultModalFlags,
stackIdx = -1,
isOpen = false,
innerHandler = innerHandler,
outerHandler = outerHandler or function(_, drawPopupFn)
drawPopupFn()
end,
render = render,
outerHandler = outerHandler,
}

setmetatable(modalWin, {
__index = ModalWindow,
class = "UI.ModalWindow",
})

Module.Constructor(modalWin)

return modalWin
end

function ModalWindow:open()
function ModalWindow:open(...)
if self.stackIdx < 0 then
table.insert(modalStack, self)
self.stackIdx = #modalStack
end

self:message("onOpen", ...)
end

function ModalWindow:close()
function ModalWindow:close(...)
for i=#modalStack, self.stackIdx, -1 do
modalStack[i].stackIdx = -1
modalStack[i].isOpen = false
ui.closeCurrentPopup()
table.remove(modalStack, i)
end

self:message("onClose", ...)
end

function ModalWindow:onOpen() end

function ModalWindow:onClose() end

function ModalWindow:outerHandler(innerFn)
innerFn()
end

local function drawModals(idx)
Expand All @@ -53,17 +69,27 @@ local function drawModals(idx)
ui.openPopup(win.name)
end

win:update()

win:outerHandler(function ()
if ui.beginPopupModal(win.name, win.flags) then
win:innerHandler()
win:render()
-- modal could close in handler
if win.isOpen then
drawModals(idx+1)
else
ui.closeCurrentPopup()
end
ui.endPopup()
end
end)
end

if idx == #modalStack + 1 then
for _,v in ipairs(ui.getModules('notification')) do
v.draw()
end
end
end

ui.registerModule('modal', function()
Expand Down
Loading

0 comments on commit dfb0d47

Please sign in to comment.