Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/file_browser.lua

raw
1 --[[ Copyright (c) 2011 Edvin "Lego3" Linge
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 local lfs = require("lfs")
22
23 --! A tree node representing a file (or directory) in the physical file-system
24 -- that meets a given file extension criterion.
25 class "FilteredFileTreeNode" (FileTreeNode)
26
27 ---@type FilteredFileTreeNode
28 local FilteredFileTreeNode = _G["FilteredFileTreeNode"]
29
30 function FilteredFileTreeNode:FilteredFileTreeNode(path, filter)
31 self:FileTreeNode(path)
32 self.filter_by = filter
33 end
34
35 function FilteredFileTreeNode:isValidFile(name)
36 -- Directories and files ending with .sav are valid.
37 if FileTreeNode.isValidFile(self, name) then
38 if lfs.attributes(self:childPath(name), "mode") == "directory" then
39 return true
40 end
41 if type(self.filter_by) == "table" then
42 for _, ext in ipairs(self.filter_by) do
43 if string.sub(name:lower(), -string.len(ext)) == ext then
44 return true
45 end
46 end
47 else
48 return string.sub(name:lower(), -string.len(self.filter_by)) == self.filter_by
49 end
50 end
51 return false
52 -- TODO: We don't want to show hidden files on windows. How can we check that?
53 end
54
55 function FilteredFileTreeNode:createNewNode(path)
56 return FilteredFileTreeNode(path, self.filter_by)
57 end
58
59 function FilteredFileTreeNode:getLabel()
60 local label = self.label
61 if not label then
62 label = FileTreeNode.getLabel(self)
63 self.label = label
64 end
65 return label
66 end
67
68 --! A sortable tree control that accommodates a certain file type and also possibly shows
69 -- their last modification dates.
70 class "FilteredTreeControl" (TreeControl)
71
72 ---@type FilteredTreeControl
73 local FilteredTreeControl = _G["FilteredTreeControl"]
74
75 function FilteredTreeControl:FilteredTreeControl(root, x, y, width, height, col_bg, col_fg, has_font, show_dates)
76 self:TreeControl(root, x, y, width, height, col_bg, col_fg, 14, has_font)
77
78 self.num_rows = (self.tree_rect.h - self.y_offset) / self.row_height
79
80 -- Magic numbers used to find a static position across different screen resolutions.
81 local button1x = math.floor(TheApp.ui.app.config.width / 2 - 90)
82 local button2x = button1x + 210
83 local buttony = math.floor(TheApp.ui.app.config.height / 4 - 95)
84 -- Add the two column headers and make buttons on them.
85 if show_dates then
86 self:addBevelPanel(1, 1, width - 170, 13, col_bg):setLabel(_S.menu_list_window.name)
87 :makeButton(0, 0, width - 170, 13, nil, self.sortByName):setTooltip(_S.tooltip.menu_list_window.name, button1x, buttony)
88 self:addBevelPanel(width - 169, 1, 150, 13, col_bg):setLabel(_S.menu_list_window.save_date)
89 :makeButton(0, 0, 150, 13, nil, self.sortByDate):setTooltip(_S.tooltip.menu_list_window.save_date, button2x, buttony)
90 end
91 self.show_dates = show_dates
92 end
93
94 function FilteredTreeControl:sortByName()
95 if self.sort_by == "date" or (self.sort_by == "name" and self.order == "descending") then
96 self:sortBy("name", "ascending")
97 else
98 self:sortBy("name", "descending")
99 end
100 end
101
102 function FilteredTreeControl:sortByDate()
103 if self.sort_by == "name" or (self.sort_by == "date" and self.order == "descending") then
104 self:sortBy("date", "ascending")
105 else
106 self:sortBy("date", "descending")
107 end
108 end
109
110 --! Sorts the list according to the given parameters.
111 --!param sort_by Either "name" or "date".
112 --!param order Either "ascending" or "descending"
113 function FilteredTreeControl:sortBy(sort_by, order)
114 self.sort_by = sort_by
115 self.order = order
116 -- Find how many nodes are above the first visible one in order
117 -- to make the correct new nodes show. (Because of the scrollbar)
118 local number = 0
119 local node = self.first_visible_node
120 while node ~= self.tree_root do
121 number = number + 1
122 node = node:getPrevVisible()
123 end
124 self.tree_root:reSortChildren(sort_by, order)
125 -- Now check which new node will be the first visible one.
126 node = self.tree_root
127 while number > 0 do
128 node = node:getNextVisible()
129 number = number - 1
130 end
131 self.first_visible_node = node
132 end
133
134 function FilteredTreeControl:drawExtraOnRow(canvas, node, x, y)
135 -- We want to show the modification date to the right of each save.
136 if not node:hasChildren() and self.show_dates then
137 local last_mod = node:getLastModification()
138 local daytime = _S.date_format.daymonth:format(os.date("%d", last_mod), tonumber(os.date("%m", last_mod)))
139 local s = TheApp.config.ui_scale
140 self.font:draw(canvas, daytime .. " " .. os.date("%Y %X", last_mod), x + self.tree_rect.w * s - 140 * s, y)
141 end
142 end
143
144 --! A file browser with a scrollbar. Used by load_game and save_game.
145 class "UIFileBrowser" (UIResizable)
146
147 ---@type UIFileBrowser
148 local UIFileBrowser = _G["UIFileBrowser"]
149
150 local col_caption = {
151 red = 174,
152 green = 166,
153 blue = 218,
154 }
155
156 --[[ Constructs the dialog.
157 !param ui (UI) The active ui.
158 !param mode (string) Either "menu" or "game" depending on which mode the game is in right now.
159 !param title (string) The desired title of the dialog.
160 !param vertical_size (number)
161 !param root
162 !param show_dates (boolean) Whether to show date last modified
163 !param submit_text (string) Optional alternative labelling of the OK button
164 ]]
165 function UIFileBrowser:UIFileBrowser(ui, mode, title, vertical_size, root, show_dates, submit_text)
166 self.col_bg = {
167 red = 154,
168 green = 146,
169 blue = 198,
170 }
171 self.col_scrollbar = {
172 red = 164,
173 green = 156,
174 blue = 208,
175 }
176 local h_size = 450
177 self:UIResizable(ui, h_size, 380, self.col_bg)
178
179 self.default_button_sound = "selectx.wav"
180
181 self.mode = mode
182 self.modal_class = mode == "menu" and "main menu" or "saveload"
183 self.on_top = mode == "menu"
184 self.esc_closes = true
185 self.resizable = false
186 self:setDefaultPosition(0.5, 0.25)
187
188 self:addBevelPanel((h_size - 190) / 2, 10, 190, 20, col_caption):setLabel(title)
189 .lowered = true
190
191 -- Initialize the tree control
192 self.control = FilteredTreeControl(root, 5, 35, h_size - 10, vertical_size, self.col_bg, self.col_scrollbar, true, show_dates)
193 :setSelectCallback(--[[persistable:file_browser_select_callback]] function(node)
194 if self:checkChoice(node) then self:choiceMade(node.path) end
195 end)
196 :setValueChangeCallback(--[[persistable:file_browser_textbox_callback]] function(node, label)
197 -- Update any user input
198 if self:checkChoice(node) then self:setInputValue(label) end
199 end)
200 self:addWindow(self.control)
201
202 -- Create the back and ok buttons.
203 local button_size = 135
204 local indent = math.floor((h_size - (2*button_size))/3)
205 self:addBevelPanel(indent, 340, button_size, 30, self.col_bg):setLabel(_S.menu_list_window.back)
206 :makeButton(0, 0, button_size, 40, nil, self.buttonBack):setTooltip(_S.tooltip.menu_list_window.back)
207
208 self:addBevelPanel(h_size - button_size - indent, 340, button_size, 30,
209 self.col_bg):setLabel(submit_text or _S.menu_list_window.ok)
210 :makeButton(0, 0, button_size, 40, nil, (--[[persistable:filebrowser_ok_callback]] function()
211 if self.confirmName then
212 self:confirmName()
213 elseif self.control.selected_node then
214 local sel_node = self.control.selected_node
215 if self:checkChoice(sel_node) then self:choiceMade(sel_node.path) end
216 end
217 end)):setTooltip(_S.tooltip.menu_list_window.ok)
218 end
219
220 --! Function stub for dialogs to override. This function is called each time a file is chosen.
221 --!param name (string) Name of the file chosen.
222 function UIFileBrowser:choiceMade(name)
223 end
224
225 --! Function stub for dialogs with user input option. This will be called for
226 --! updating inputs, override it for a proper implementation in the derived class.
227 --!param label (string) Name of the file chosen
228 function UIFileBrowser:setInputValue(label)
229 end
230
231 --! Check selection is a valid file, and not a directory
232 --!param node (table) user selected element
233 function UIFileBrowser:checkChoice(node)
234 return node.is_valid_file and (lfs.attributes(node.path, "mode") ~= "directory")
235 end
236
237 function UIFileBrowser:buttonBack()
238 self:close()
239 end