diff --git a/nim/src/ui/mainmenu.nim b/nim/src/ui/mainmenu.nim index 025ff7912..401f355ce 100644 --- a/nim/src/ui/mainmenu.nim +++ b/nim/src/ui/mainmenu.nim @@ -15,12 +15,102 @@ # You should have received a copy of the GNU General Public License # along with Steam Sky. If not, see . -import std/[os, strutils] +import std/[algorithm, os, strutils, times] import ../[config, game, tk] -import mainmenucommands +import mainmenucommands, table, utilsui var mainMenuFrame = "" +proc showMainMenu*() + +type SaveSortOrders = enum + playerAsc, playerDesc, shipAsc, shipDesc, timeAsc, timeDesc + +var + loadTable: TableWidget + saveSortOrder = timeDesc + +proc showLoadGameCommand(clientData: cint; interp: PInterp; argc: cint; + argv: openArray[cstring]): TclResults = + if loadTable.rowHeight == 1: + loadTable = createTable(parent = ".loadmenu.list", headers = @[ + "Player name", "Ship name", "Last saved"], command = "SortSaves", + tooltipText = "Press mouse button to sort the saved games.") + else: + clearTable(table = loadTable) + type SaveRecord = object + playerName, shipName, saveTime, fileName: string + var saves: seq[SaveRecord] + for file in walkFiles(saveDirectory & "*.sav"): + let + (_, name, _) = splitFile(path = file) + parts = name.split('_') + if parts.len == 3: + saves.add(SaveRecord(playerName: parts[0], shipName: parts[1], + saveTime: file.getLastModificationTime.format("yyyy-MM-dd hh:mm:ss"), + fileName: file)) + else: + saves.add(SaveRecord(playerName: "Unknown", shipName: "Unknown", + saveTime: file.getLastModificationTime.format("yyyy-MM-dd hh:mm:ss"), + fileName: file)) + + proc sortSaves(x, y: SaveRecord): int = + case saveSortOrder + of playerAsc: + if x.playerName < y.playerName: + return 1 + else: + return -1 + of playerDesc: + if x.playerName > y.playerName: + return 1 + else: + return -1 + of shipAsc: + if x.shipName < y.shipName: + return 1 + else: + return -1 + of shipDesc: + if x.shipName > y.shipName: + return 1 + else: + return -1 + of timeAsc: + if x.saveTime < y.saveTime: + return 1 + else: + return -1 + of timeDesc: + if x.saveTime > y.saveTime: + return 1 + else: + return -1 + saves.sort(cmp = sortSaves) + for save in saves: + addButton(table = loadTable, text = save.playerName, + tooltip = "Press mouse " & ( + if gameSettings.rightButton: "right" else: "left") & + " button to show available option", command = "ShowLoadGameMenu " & + save.fileName, column = 1) + addButton(table = loadTable, text = save.shipName, + tooltip = "Press mouse " & ( + if gameSettings.rightButton: "right" else: "left") & + " button to show available option", command = "ShowLoadGameMenu " & + save.fileName, column = 2) + addButton(table = loadTable, text = save.saveTime, + tooltip = "Press mouse " & ( + if gameSettings.rightButton: "right" else: "left") & + " button to show available option", command = "ShowLoadGameMenu " & + save.fileName, column = 3, newRow = true) + updateTable(table = loadTable) + if loadTable.row == 1: + tclEval(script = "bind . {}") + tclEval(script = "bind . {}") + tclEval(script = "pack forget .loadmenu") + showMainMenu() + return tclOk + proc createMainMenu*() = let uiDirectory = dataDirectory & "ui" & DirSep @@ -28,17 +118,21 @@ proc createMainMenu*() = mainWindow = "." if not dirExists(iconPath): tclEval(script = "wm withdraw " & mainWindow) - tclEval(script = "tk_messageBox -message {Couldn't not find the game data files and the game have to stop. Are you sure that directory \"" & dataDirectory & "\" is the proper place where the game data files exists?} -icon error -type ok") + tclEval(script = "tk_messageBox -message {Couldn't not find the game data files and the game have to stop. Are you sure that directory \"" & + dataDirectory & "\" is the proper place where the game data files exists?} -icon error -type ok") tclEval(script = "exit 1") return let icon = tclEval2(script = "image create photo logo -file {" & iconPath & "}") mainmenucommands.addCommands() + addCommand("ShowLoadGame", showLoadGameCommand) -proc showMainMenu*() = +proc showMainMenu() = let mainWindow = "." var - x: int = ((tclEval2(script = "winfo vrootwidth " & mainWindow).parseInt - 600) / 2).int - y: int = ((tclEval2(script = "winfo vrootheight " & mainWindow).parseInt - 400) / 2).int + x: int = ((tclEval2(script = "winfo vrootwidth " & mainWindow).parseInt - + 600) / 2).int + y: int = ((tclEval2(script = "winfo vrootheight " & mainWindow).parseInt - + 400) / 2).int if x < 0: x = 0 if y < 0: diff --git a/nim/src/ui/mainmenucommands.nim b/nim/src/ui/mainmenucommands.nim index 1e7800759..77ec51149 100644 --- a/nim/src/ui/mainmenucommands.nim +++ b/nim/src/ui/mainmenucommands.nim @@ -15,9 +15,9 @@ # You should have received a copy of the GNU General Public License # along with Steam Sky. If not, see . -import std/[os, osproc, strutils, times] +import std/[os, osproc] import ../[game, halloffame, tk] -import dialogs, table +import dialogs proc openLinkCommand*(clientData: cint; interp: PInterp; argc: cint; argv: openArray[cstring]): TclResults {.sideEffect, raises: [], tags: [ @@ -158,75 +158,7 @@ proc showHallOfFameCommand(clientData: cint; interp: PInterp; argc: cint; entry.name & " " & $entry.points & " " & entry.deathReason & "]") return tclOk -type SaveSortOrders = enum - playerAsc, playerDesc, shipAsc, shipDesc, timeAsc, timeDesc - -var - loadTable: TableWidget - saveSortOrder = timeDesc - -proc showLoadGameCommand(clientData: cint; interp: PInterp; argc: cint; - argv: openArray[cstring]): TclResults = - if loadTable.rowHeight == 1: - loadTable = createTable(parent = ".loadmenu.list", headers = @[ - "Player name", "Ship name", "Last saved"], command = "SortSaves", - tooltipText = "Press mouse button to sort the saved games.") - else: - clearTable(table = loadTable) - type SaveRecord = object - playerName, shipName, saveTime, fileName: string - var saves: seq[SaveRecord] - for file in walkFiles(saveDirectory & "*.sav"): - let - (_, name, _) = splitFile(path = file) - parts = name.split('_') - if parts.len == 3: - saves.add(SaveRecord(playerName: parts[0], shipName: parts[1], - saveTime: file.getLastModificationTime.format("yyyy-MM-dd hh:mm:ss"), - fileName: file)) - else: - saves.add(SaveRecord(playerName: "Unknown", shipName: "Unknown", - saveTime: file.getLastModificationTime.format("yyyy-MM-dd hh:mm:ss"), - fileName: file)) - - proc sortSaves(x, y: SaveRecord): int = - case saveSortOrder - of playerAsc: - if x.playerName < y.playerName: - return 1 - else: - return -1 - of playerDesc: - if x.playerName > y.playerName: - return 1 - else: - return -1 - of shipAsc: - if x.shipName < y.shipName: - return 1 - else: - return -1 - of shipDesc: - if x.shipName > y.shipName: - return 1 - else: - return -1 - of timeAsc: - if x.saveTime < y.saveTime: - return 1 - else: - return -1 - of timeDesc: - if x.saveTime > y.saveTime: - return 1 - else: - return -1 - - echo saves - return tclOk - proc addCommands*() = - discard showLoadGameCommand(0, getInterp(), 0, @[]) addCommand("OpenLink", openLinkCommand) addCommand("ShowFile", showFileCommand) addCommand("ShowNews", showNewsCommand)