| 1 |
--[[ Copyright (c) 2015 Stephen E. Baker et al. |
| 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 Map Window |
| 22 |
class "UISaveMap" (UIFileBrowser) |
| 23 |
|
| 24 |
---@type UISaveMap |
| 25 |
local UISaveMap = _G["UISaveMap"] |
| 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 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, |
| 49 |
treenode, true, _S.save_map_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 save name |
| 55 |
self.new_map_textbox = self:addBevelPanel(5, 310, self.width - 10, 17, col_textbox, col_highlight, col_shadow) |
| 56 |
:setLabel(_S.save_map_window.new_map, nil, "left"):setTooltip(_S.tooltip.save_map_window.new_map) |
| 57 |
:makeTextbox(--[[persistable:save_map_new_map_textbox_confirm_callback]] function() self:confirmName() end, |
| 58 |
--[[persistable:save_map_new_map_textbox_abort_callback]] function() self:abortName() end) |
| 59 |
|
| 60 |
local message = ui.app.world:validateMap() |
| 61 |
if message then |
| 62 |
ui:addWindow(UIInformation(ui, {message})) |
| 63 |
end |
| 64 |
end |
| 65 |
|
| 66 |
--! Function called when textbox is aborted (e.g. by pressing escape) |
| 67 |
function UISaveMap:abortName() |
| 68 |
self.new_map_textbox.text = "" |
| 69 |
self.new_map_textbox.panel:setLabel(_S.save_map_window.new_map) |
| 70 |
end |
| 71 |
|
| 72 |
--! Updates the textbox to selected file |
| 73 |
--!param label (string) Selected file name |
| 74 |
function UISaveMap:setInputValue(label) |
| 75 |
local name = string.gsub(label, "%.map$", "") |
| 76 |
self.new_map_textbox:setText(name) |
| 77 |
end |
| 78 |
|
| 79 |
--! Function called when textbox is confirmed (e.g. by pressing enter) |
| 80 |
function UISaveMap:confirmName() |
| 81 |
local filename = self.new_map_textbox.text |
| 82 |
local app = self.ui.app |
| 83 |
if filename == "" then |
| 84 |
self.ui:addWindow(UIInformation(self.ui, { _S.save_map_window.missing_filename })) |
| 85 |
self:abortName() |
| 86 |
return |
| 87 |
end |
| 88 |
self:trySave(app.user_level_dir .. filename .. ".map") |
| 89 |
end |
| 90 |
|
| 91 |
--! Function called by clicking button of existing save #num |
| 92 |
function UISaveMap:choiceMade(name) |
| 93 |
self:trySave(name) |
| 94 |
end |
| 95 |
|
| 96 |
--! Try to save the game with given filename; if already exists, create confirmation window first. |
| 97 |
function UISaveMap:trySave(filename) |
| 98 |
if lfs.attributes(filename, "size") ~= nil then |
| 99 |
self.ui:addWindow(UIConfirmDialog(self.ui, false, _S.confirmation.overwrite_save, --[[persistable:save_map_confirmation]] function() self:doSave(filename) end)) |
| 100 |
else |
| 101 |
self:doSave(filename) |
| 102 |
end |
| 103 |
end |
| 104 |
|
| 105 |
--! Actually do save the map with given filename. |
| 106 |
function UISaveMap:doSave(filename) |
| 107 |
filename = filename |
| 108 |
local ui = self.ui |
| 109 |
local map = ui.app.map |
| 110 |
self:close() |
| 111 |
|
| 112 |
local status, err = pcall(map.save, map, filename) |
| 113 |
if not status then |
| 114 |
err = _S.errors.save_prefix .. err |
| 115 |
print(err) |
| 116 |
ui:addWindow(UIInformation(ui, {err})) |
| 117 |
end |
| 118 |
end |