Skip to content

Commit

Permalink
Search bar for save/load window with some rewrite
Browse files Browse the repository at this point in the history
Search bar for save/load window to search saves by name with a checkbox to make search case-sensitive. Some rewrite to make this window more prone to resolution scaling.
  • Loading branch information
Max5377 committed Sep 27, 2023
1 parent 6a5daf2 commit 3064a1f
Showing 1 changed file with 92 additions and 40 deletions.
132 changes: 92 additions & 40 deletions data/pigui/modules/saveloadgame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ local optionButtonSize = ui.rescaleUI(Vector2(100, 32))
local winSize = Vector2(ui.screenWidth * 0.4, ui.screenHeight * 0.6)
local pionillium = ui.fonts.pionillium

local searchText = lc.SEARCH .. ':'
local saveText = lui.SAVE .. ':'
local caseSensitiveText = lui.CASE_SENSITIVE

local saveFileCache = {}
local selectedSave
local saveIsValid = true

local minSearchTextLength = 1
local searchSave = ""
local caseSensitive = false

local function optionTextButton(label, enabled, callback)
local variant = not enabled and ui.theme.buttonColors.disabled or nil
local button
Expand Down Expand Up @@ -56,25 +64,46 @@ local function getSaveTooltip(name)
else
ret = ret .. "\n" .. lc.SHIP .. ": " .. lc.UNKNOWN
end



if stats.flight_state then
ret = ret .. "\n"..lui.FLIGHT_STATE..": "
if stats.flight_state == "docked" then ret = ret .. lc.DOCKED
elseif stats.flight_state == "docking" then ret = ret .. lc.DOCKING
elseif stats.flight_state == "flying" then ret = ret .. lui.FLYING
elseif stats.flight_state == "hyperspace" then ret = ret .. lc.HYPERSPACE
elseif stats.flight_state == "jumping" then ret = ret .. lui.JUMPING
elseif stats.flight_state == "landed" then ret = ret .. lc.LANDED
elseif stats.flight_state == "undocking" then ret = ret .. lc.UNDOCKING
else ret = ret .. lc.UNKNOWN end
ret = ret .. (rawget(lc, string.upper(stats.flight_state)) or
rawget(lui, string.upper(stats.flight_state)) or
lc.UNKNOWN)
end

if stats.docked_at then ret = ret .. "\n"..lui.DOCKED_AT..": " .. stats.docked_at end
if stats.frame then ret = ret .. "\n"..lui.VICINITY_OF..": " .. stats.frame end


saveFileCache[name].ret = ret
return ret
end

local function shouldDisplayThisSave(f)
if(string.len(searchSave) < minSearchTextLength) then
return true
end

return not caseSensitive and string.find(string.lower(f.name), string.lower(searchSave), 1, true) ~= nil or
string.find(f.name, searchSave, 1, true) ~= nil
end

local function displaySave(f)
if ui.selectable(f.name, f.name == selectedSave, {"SpanAllColumns", "DontClosePopups"}) then
selectedSave = f.name
saveIsValid = pcall(Game.SaveGameStats, f.name)
end
if Engine.pigui.IsItemHovered() then
local tooltip = getSaveTooltip(f.name)
Engine.pigui.SetTooltip(tooltip)
end

ui.nextColumn()
ui.text(Format.Date(f.mtime.timestamp))
ui.nextColumn()
end

local function showSaveFiles()
local ok, files, _ = pcall(FileSystem.ReadDirectory, "USER","savefiles")
if not ok then
Expand All @@ -84,18 +113,9 @@ local function showSaveFiles()
table.sort(files, function(a,b) return (a.mtime.timestamp > b.mtime.timestamp) end)
ui.columns(2,"##saved_games",true)
for _,f in pairs(files) do
if ui.selectable(f.name, f.name == selectedSave, {"SpanAllColumns", "DontClosePopups"}) then
selectedSave = f.name
saveIsValid = pcall(Game.SaveGameStats, f.name)
end
if Engine.pigui.IsItemHovered() then
local tooltip = getSaveTooltip(f.name)
Engine.pigui.SetTooltip(tooltip)
if(shouldDisplayThisSave(f)) then
displaySave(f)
end

ui.nextColumn()
ui.text(Format.Date(f.mtime.timestamp))
ui.nextColumn()
end
ui.columns(1,"",false)
end
Expand All @@ -106,6 +126,7 @@ local function closeAndClearCache()
ui.saveLoadWindow.mode = nil
saveFileCache = {}
popupOpened = false
searchSave = ""
end

local function closeAndLoadOrSave()
Expand All @@ -120,32 +141,63 @@ local function closeAndLoadOrSave()
end
end

local function drawSearchHeader(txt_width)
ui.withFont(pionillium.medium.name, pionillium.medium.size, function()
ui.text(searchText)
ui.nextItemWidth(txt_width, 0)
searchSave, _ = ui.inputText("##searchSave", searchSave, {})
ui.sameLine()
local ch, value = ui.checkbox(caseSensitiveText, caseSensitive)
if ch then
caseSensitive = value
end
end)
end

local function drawOptionButtons(txt_width, saving)

-- for vertical center alignment
local txt_hshift = math.max(0, (optionButtonSize.y - ui.getFrameHeight()) / 2)
local mode = saving and lui.SAVE or lui.LOAD
ui.sameLine(txt_width + ui.getWindowPadding().x + ui.getItemSpacing().x)
ui.addCursorPos(Vector2(0, saving and -txt_hshift or txt_hshift))
optionTextButton(mode, selectedSave ~= nil and selectedSave ~= '' and saveIsValid, closeAndLoadOrSave)
ui.sameLine()
ui.addCursorPos(Vector2(0, saving and -txt_hshift or txt_hshift))
optionTextButton(lui.CANCEL, true, closeAndClearCache)
end

ui.saveLoadWindow = ModalWindow.New("LoadGame", function()
local saving = ui.saveLoadWindow.mode == "SAVE"
local other_height = optionButtonSize.y + ui.getItemSpacing().y * 2 + ui.getWindowPadding().y * 2
ui.child("savefiles", Vector2(-1, winSize.y - other_height), function()
local searchTextSize = ui.calcTextSize(searchText, pionillium.medium.name, pionillium.medium.size)

local txt_width = winSize.x - (ui.getWindowPadding().x + optionButtonSize.x + ui.getItemSpacing().x) * 2

drawSearchHeader(txt_width)

ui.separator()

local saveFilesSearchHeaderHeight = (searchTextSize.y * 2 + ui.getItemSpacing().y * 2 + ui.getWindowPadding().y * 2)
local saveFilesChildWindowHeight = (optionButtonSize.y + (saving and searchTextSize.y or 0) + ui.getItemSpacing().y * 2 + ui.getWindowPadding().y * 2)

local saveFilesChildWindowSize = Vector2(0, (winSize.y - saveFilesChildWindowHeight) - saveFilesSearchHeaderHeight)

ui.child("savefiles", saveFilesChildWindowSize, function()
showSaveFiles()
end)

ui.separator()

local txt_width = winSize.x - (ui.getWindowPadding().x + optionButtonSize.x + ui.getItemSpacing().x) * 2

-- a little padding just before the window border, so that the cancel button will not be cut out
txt_width = txt_width / 1.03
if saving then
-- for vertical center alignment
local txt_hshift = math.max(0, (optionButtonSize.y - ui.getFrameHeight()) / 2)
ui.nextItemWidth(txt_width, 0)
ui.addCursorPos(Vector2(0, txt_hshift))
selectedSave = ui.inputText("##saveFileName", selectedSave or "", {})
ui.sameLine(txt_width + ui.getWindowPadding().x + ui.getItemSpacing().x)
ui.addCursorPos(Vector2(0, -txt_hshift))
else
ui.addCursorPos(Vector2(txt_width + ui.getItemSpacing().x, 0))
ui.withFont(pionillium.medium.name, pionillium.medium.size, function()
ui.text(saveText)
ui.nextItemWidth(txt_width, 0)
selectedSave = ui.inputText("##saveFileName", selectedSave or "", {})
end)
end

local mode = saving and lui.SAVE or lui.LOAD
optionTextButton(mode, selectedSave ~= nil and selectedSave ~= '' and saveIsValid, closeAndLoadOrSave)
ui.sameLine()
optionTextButton(lui.CANCEL, true, closeAndClearCache)
drawOptionButtons(txt_width, saving)

end, function (_, drawPopupFn)
ui.setNextWindowSize(winSize, "Always")
Expand All @@ -155,4 +207,4 @@ end)

ui.saveLoadWindow.mode = "LOAD"

return {}
return {}

0 comments on commit 3064a1f

Please sign in to comment.