/
openfile.lua
142 lines (118 loc) · 3.83 KB
/
openfile.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
--- Open a file explorer to select a file.
-- string title: title of the file explorer.
-- string curdir: the directory to initially open the file explorer in.
-- string exts: the file extensions the user can select, separated by ";". If nil, all extensions are accepted.
-- string type: "exist" to select an existing file, "new" to select an non-existing file or "any" to select a existing
-- or non-existing file name. If nil, defaults to "exist".
-- returns string: the file the user has selected, or nil if the explorer was closed without selecting a file.
-- string: "exist" if the file exist or "new" if it doesn't
return function(title, curdir, exts, type)
-- Open libs
local ctr = require("ctr")
local gfx = require("ctr.gfx")
local keyboard = dofile("sdmc:/3ds/ctruLua/keyboard.lua")
-- Arguments
local type = type or "exist"
-- Variables
local sel = 1
local scroll = 0
local files = ctr.fs.list(curdir)
if curdir ~= "/" then table.insert(files, 1, { name = "..", isDirectory = true }) end
local newFileName = ""
local ret = nil
-- Remember and set defaults
local was3D = gfx.get3D()
local wasDefault = gfx.color.getDefault()
local wasBackground = gfx.color.getBackground()
local wasFont = gfx.font.getDefault()
gfx.set3D(false)
gfx.color.setDefault(0xFFFFFFFF)
gfx.color.setBackground(0x000000FF)
gfx.font.setDefault()
while ctr.run() do
ctr.hid.read()
local keys = ctr.hid.keys()
if keys.down.start then break end
-- Keys input
if keys.down.down and sel < #files then
sel = sel + 1
if sel > scroll + 14 then scroll = scroll + 1 end
elseif keys.down.up and sel > 1 then
sel = sel - 1
if sel <= scroll then scroll = scroll - 1 end
end
if keys.down.a then
local f = files[sel]
if f.isDirectory then
if f.name == ".." then curdir = curdir:gsub("[^/]+/$", "")
else curdir = curdir..f.name.."/" end
sel = 1
scroll = 0
files = ctr.fs.list(curdir)
if curdir ~= "/" then
table.insert(files, 1, { name = "..", isDirectory = true })
end
elseif type == "exist" or type == "any" then
if exts then
for ext in (exts..";"):gmatch("[^;]+") do
if f.name:match("%..+$") == ext then
ret = { curdir..f.name, "exist" }
break
end
end
else
ret = { curdir..f.name, "exist" }
end
if ret then break end
end
end
-- Keyboard input
if type == "new" or type == "any" then
local input = keyboard.read()
if input then
if input == "BACK" then
newFileName = newFileName:sub(1, (utf8.offset(newFileName, -1) or 0)-1)
elseif input == "\n" then
local fileStatus = "new"
local f = io.open(curdir..newFileName)
if f then fileStatus = "exist" f:close() end
ret = { curdir..newFileName, fileStatus }
break
else
newFileName = newFileName..input
end
end
end
-- Draw
gfx.startFrame(gfx.GFX_TOP)
gfx.rectangle(0, 10+(sel-scroll)*15, gfx.TOP_WIDTH, 15, 0, gfx.color.RGBA8(0, 0, 200))
for i = scroll+1, scroll+14, 1 do
local f = files[i]
if not f then break end
local name = f.isDirectory and "["..f.name.."]" or f.name.." ("..f.fileSize.."b)"
if not f.isHidden then gfx.text(5, 12+(i-scroll)*15, name) end
end
gfx.rectangle(0, 0, gfx.TOP_WIDTH, 25, 0, gfx.color.RGBA8(200, 200, 200))
gfx.text(3, 3, curdir, 13, gfx.color.RGBA8(0, 0, 0))
gfx.endFrame()
gfx.startFrame(gfx.GFX_BOTTOM)
gfx.text(5, 5, title)
gfx.text(5, 20, "Accepted file extensions: "..(exts or "all"))
if type == "new" or type == "any" then
gfx.text(5, 90, newFileName)
keyboard.draw(5, 115)
end
gfx.endFrame()
gfx.render()
end
-- Reset defaults
gfx.set3D(was3D)
gfx.color.setDefault(wasDefault)
gfx.color.setBackground(wasBackground)
gfx.font.setDefault(wasFont)
if ret then
return table.unpack(ret)
else
return ret
end
end