1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- return function(BOXUI)
- local utils = BOXUI.utils
- local List = BOXUI.list
- local M = utils.newclass("filelist", List)
- local isAllowedFile = function(filename, allowedExts)
- if not allowedExts then return true end
- local ext = filename:match(".([^.]+)$")
- return ext and allowedExts[ext:lower()]
- end
- local itemComperator = function(item1, item2)
- local type1, type2 = item1.context.type, item2.context.type
- if type1 == type2 then return item1.text < item2.text end
- return type1 < type2
- end
- local createItemsFromDir = function(fl, path)
- local toplevel = fl.toplevel
- local exts = fl.extensions
- if not path or (toplevel and string.find(path, toplevel, 1, true) ~= 1) then
- path = toplevel or ""
- end
-
- local diritems = love.filesystem.getDirectoryItems(path)
- local listitems = {}
- local prefix = (path == "" and "" or path .. "/")
- local ctx
- local info, infotype, filepath
- for i, f in ipairs(diritems) do
- filepath = prefix .. f
- info = love.filesystem.getInfo(filepath)
- if info ~= nil then
- infotype = info.type
- ctx = {type = infotype, name = filepath}
- if infotype == "directory" then
- table.insert(listitems, {text = f, context = ctx, icon = fl.diricon})
- elseif infotype == "file" and isAllowedFile(f, exts) then
- table.insert(listitems, {text = f, context = ctx, icon = fl.fileicon})
- end
- end
- end
- table.sort(listitems, itemComperator)
-
- if path ~= "" and path ~= toplevel then
- ctx = {type = "directory", name = path:gsub("/?[^/]+$","")}
- table.insert(listitems, 1, {text = "..", context = ctx, icon = fl.diricon})
- end
- --table.insert(listitems, 1, {back = fl.headerBack, text = prefix})
- fl.title = (prefix == "" and "[ROOT]" or prefix)
- return listitems, path
- end
- M.init = function(self, x, y, width, height)
- List.init(self, x, y, width, height)
- self.toplevel = nil
- self.extensions = nil
- self.diricon = nil
- self.fileicon = nil
- --self.headerBack = {0.7, 0.7, 0.7}
- self.path = nil
- end
- M.setPath = function(self, path)
- local items, usedpath = createItemsFromDir(self, path)
- self:setItems(items, true)
- self.path = usedpath
- return path == nil or path == usedpath
- end
- BOXUI.add(M)
- end
|