Skip to content

Commit

Permalink
refactor: continue work on move showLoadGameCommand to Nim
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 95550c6d578a7c1f50869bb5e9e2cbef8a8ca983ec66ace7fad06be6205b283c
  • Loading branch information
thindil committed Jan 4, 2024
1 parent e2f542d commit 8b5fcf5
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 76 deletions.
106 changes: 100 additions & 6 deletions nim/src/ui/mainmenu.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,124 @@
# You should have received a copy of the GNU General Public License
# along with Steam Sky. If not, see <http://www.gnu.org/licenses/>.

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 . <Alt-b> {}")
tclEval(script = "bind . <Escape> {}")
tclEval(script = "pack forget .loadmenu")
showMainMenu()
return tclOk

proc createMainMenu*() =
let
uiDirectory = dataDirectory & "ui" & DirSep
iconPath = uiDirectory & "images" & DirSep & "icon.png"
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:
Expand Down
72 changes: 2 additions & 70 deletions nim/src/ui/mainmenucommands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
# You should have received a copy of the GNU General Public License
# along with Steam Sky. If not, see <http://www.gnu.org/licenses/>.

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: [
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8b5fcf5

Please sign in to comment.