Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/file_browsers/save_game.lua

raw
1 --[[ Copyright (c) 2010 Manuel "Roujin" Wolf
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Save Game Window
22 class "UISaveGame" (UIFileBrowser)
23
24 ---@type UISaveGame
25 local UISaveGame = _G["UISaveGame"]
26
27 local col_textbox = {
28 red = 0,
29 green = 0,
30 blue = 0,
31 }
32
33 local col_highlight = {
34 red = 174,
35 green = 166,
36 blue = 218,
37 }
38
39 local col_shadow = {
40 red = 134,
41 green = 126,
42 blue = 178,
43 }
44
45 function UISaveGame:UISaveGame(ui)
46 local treenode = FilteredFileTreeNode(ui.app.savegame_dir, ".sav")
47 treenode.label = "Saves"
48 self:UIFileBrowser(ui, "game", _S.save_game_window.caption:format(".sav"), 265,
49 treenode, true, _S.save_game_window.save_button)
50 -- The most probable preference of sorting is by date - what you played last
51 -- is the thing you want to play soon again.
52 self.control:sortByDate()
53
54 -- Textbox for entering new savegame name
55 self.new_savegame_textbox = self:addBevelPanel(5, 310, self.width - 10, 17, col_textbox, col_highlight, col_shadow)
56 :setLabel(_S.save_game_window.new_save_game, nil, "left"):setTooltip(_S.tooltip.save_game_window.new_save_game)
57 :makeTextbox(--[[persistable:save_game_new_savegame_textbox_confirm_callback]] function() self:confirmName() end,
58 --[[persistable:save_game_new_savegame_textbox_abort_callback]] function() self:abortName() end)
59 end
60
61 --! Function called when textbox is aborted (e.g. by pressing escape)
62 function UISaveGame:abortName()
63 self.new_savegame_textbox.text = ""
64 self.new_savegame_textbox.panel:setLabel(_S.save_game_window.new_save_game)
65 end
66
67 --! Updates the textbox to selected file
68 --!param label (string) Selected file name
69 function UISaveGame:setInputValue(label)
70 local name = string.gsub(label, "%.sav$", "")
71 self.new_savegame_textbox:setText(name)
72 end
73
74 --! Function called when textbox is confirmed (e.g. by pressing enter)
75 function UISaveGame:confirmName()
76 local filename = self.new_savegame_textbox.text
77 local app = self.ui.app
78 if filename == "" then
79 self.ui:addWindow(UIInformation(self.ui, { _S.save_game_window.missing_filename }))
80 self:abortName()
81 return
82 end
83 self:trySave(app.savegame_dir .. filename .. ".sav")
84 end
85
86 --! Function called by clicking button of existing save #num
87 function UISaveGame:choiceMade(name)
88 self:trySave(name)
89 end
90
91 --! Try to save the game with given filename; if already exists, create confirmation window first.
92 function UISaveGame:trySave(filename)
93 if lfs.attributes(filename, "size") ~= nil then
94 self.ui:addWindow(UIConfirmDialog(self.ui, false, _S.confirmation.overwrite_save, --[[persistable:save_game_confirmation]] function() self:doSave(filename) end))
95 else
96 self:doSave(filename)
97 end
98 end
99
100 --! Actually do save the game with given filename.
101 function UISaveGame:doSave(filename)
102 filename = filename
103 local ui = self.ui
104 local app = ui.app
105 self:close()
106
107 local status, err = pcall(app.save, app, filename)
108 if not status then
109 err = _S.errors.save_prefix .. err
110 print(err)
111 ui:addWindow(UIInformation(ui, {err}))
112 end
113 end