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