1
0
Fork 0
mirror of https://github.com/ctruLua/ctruLua.git synced 2025-10-27 16:39:29 +00:00

Tabulation correction

This commit is contained in:
Neil Zeke Cecchini 2016-02-24 17:55:12 +01:00
parent 3eb41b5062
commit b6beaddf66
2 changed files with 173 additions and 173 deletions

View file

@ -5,121 +5,121 @@ local ctr = require("ctr")
local gfx = require("ctr.gfx") local gfx = require("ctr.gfx")
local function saveGraphicsState() local function saveGraphicsState()
local old = {gfx.get3D(), gfx.color.getDefault(), gfx.color.getBackground(), local old = {gfx.get3D(), gfx.color.getDefault(), gfx.color.getBackground(),
gfx.font.getDefault()} gfx.font.getDefault()}
local mono = gfx.font.load(ctruLua.root .. "resources/VeraMono.ttf") local mono = gfx.font.load(ctruLua.root .. "resources/VeraMono.ttf")
gfx.set3D(false) gfx.set3D(false)
gfx.color.setDefault(0xFFFDFDFD) gfx.color.setDefault(0xFFFDFDFD)
gfx.color.setBackground(0xFF333333) gfx.color.setBackground(0xFF333333)
gfx.font.setDefault(mono) gfx.font.setDefault(mono)
return old return old
end end
local function restoreGraphicsState(state) local function restoreGraphicsState(state)
gfx.set3D(state[1]) gfx.set3D(state[1])
gfx.color.setDefault(state[2]) gfx.color.setDefault(state[2])
gfx.color.setBackground(state[3]) gfx.color.setBackground(state[3])
gfx.font.setDefault(state[4]) gfx.font.setDefault(state[4])
end end
local function getExtension(sel, bindings) local function getExtension(sel, bindings)
for _, ext in ipairs(bindings) do for _, ext in ipairs(bindings) do
if ext.ext == sel:match("%..+$") then if ext.ext == sel:match("%..+$") then
return ext return ext
end end
end end
end end
local function getFilelist(cur) local function getFilelist(cur)
local files = ctr.fs.list(cur) local files = ctr.fs.list(cur)
if cur ~= "/" and cur ~= "sdmc:/" then if cur ~= "/" and cur ~= "sdmc:/" then
table.insert(files, {name = "..", isDirectory = true}) table.insert(files, {name = "..", isDirectory = true})
end end
-- Stealy stealing code from original openfile.lua -- Stealy stealing code from original openfile.lua
table.sort(files, function(i, j) table.sort(files, function(i, j)
if i.isDirectory and not j.isDirectory then if i.isDirectory and not j.isDirectory then
return true return true
elseif i.isDirectory == j.isDirectory then elseif i.isDirectory == j.isDirectory then
return string.lower(i.name) < string.lower(j.name) return string.lower(i.name) < string.lower(j.name)
end end
end) end)
return files return files
end end
local function drawBottom(cur, selFile, bindings) local function drawBottom(cur, selFile, bindings)
local ext = getExtension(selFile.name, bindings) local ext = getExtension(selFile.name, bindings)
gfx.start(gfx.BOTTOM) gfx.start(gfx.BOTTOM)
gfx.rectangle(0, 0, gfx.BOTTOM_WIDTH, 16, 0, 0xFF0000B3) gfx.rectangle(0, 0, gfx.BOTTOM_WIDTH, 16, 0, 0xFF0000B3)
gfx.text(1, 0, cur, 12) gfx.text(1, 0, cur, 12)
gfx.text(1, 15, selFile.name, 12) gfx.text(1, 15, selFile.name, 12)
if not selFile.isDirectory then if not selFile.isDirectory then
gfx.text(1, 45, selFile.fileSize, 12) gfx.text(1, 45, selFile.fileSize, 12)
end end
local keys = {"X: Quit/Cancel"} local keys = {"X: Quit/Cancel"}
if selFile.isDirectory then if selFile.isDirectory then
gfx.text(1, 30, "Directory", 12, 0xFF727272) gfx.text(1, 30, "Directory", 12, 0xFF727272)
gfx.text(1, gfx.BOTTOM_HEIGHT - 30, "A: Open", 12) gfx.text(1, gfx.BOTTOM_HEIGHT - 30, "A: Open", 12)
gfx.text(1, gfx.BOTTOM_HEIGHT - 15, keys[1], 12) gfx.text(1, gfx.BOTTOM_HEIGHT - 15, keys[1], 12)
elseif ext then elseif ext then
local lines = 1 local lines = 1
-- Keys -- Keys
if ext.y then if ext.y then
lines = lines + 1 lines = lines + 1
table.insert(keys, "Y: " .. ext.y) table.insert(keys, "Y: " .. ext.y)
end end
if ext.a then if ext.a then
lines = lines + 1 lines = lines + 1
table.insert(keys, "A: " .. ext.a) table.insert(keys, "A: " .. ext.a)
end end
-- Drawing -- Drawing
for i=lines, 1, -1 do for i=lines, 1, -1 do
gfx.text(1, gfx.BOTTOM_HEIGHT - 15*i, keys[i], 12) gfx.text(1, gfx.BOTTOM_HEIGHT - 15*i, keys[i], 12)
end end
gfx.text(1, 30, ext.name, 12, 0xFF727272) gfx.text(1, 30, ext.name, 12, 0xFF727272)
gfx.text(1, 45, tostring(selFile.fileSize) .. "B", 12, 0xFF727272) gfx.text(1, 45, tostring(selFile.fileSize) .. "B", 12, 0xFF727272)
else else
gfx.text(1, 30, "File", 12, 0xFF727272) gfx.text(1, 30, "File", 12, 0xFF727272)
gfx.text(1, 45, tostring(selFile.fileSize) .. "B", 12, 0xFF727272) gfx.text(1, 45, tostring(selFile.fileSize) .. "B", 12, 0xFF727272)
gfx.text(1, gfx.BOTTOM_HEIGHT - 15, keys[1], 12) gfx.text(1, gfx.BOTTOM_HEIGHT - 15, keys[1], 12)
end end
gfx.stop() gfx.stop()
end end
local function drawTop(files, sel, scr) local function drawTop(files, sel, scr)
gfx.start(gfx.TOP) gfx.start(gfx.TOP)
gfx.rectangle(0, (sel-scr-1)*15, gfx.TOP_WIDTH, 16, 0, 0xFF0000B3) gfx.rectangle(0, (sel-scr-1)*15, gfx.TOP_WIDTH, 16, 0, 0xFF0000B3)
local over = #files - scr >= 16 and 16 or #files - scr local over = #files - scr >= 16 and 16 or #files - scr
for i=scr+1, scr+over do for i=scr+1, scr+over do
local color = files[i].isDirectory and 0xFF727272 or 0xFFFDFDFD local color = files[i].isDirectory and 0xFF727272 or 0xFFFDFDFD
gfx.text(1, (i-scr-1)*15+1, files[i].name or "", 12, color) gfx.text(1, (i-scr-1)*15+1, files[i].name or "", 12, color)
end end
gfx.stop() gfx.stop()
end end
local function runA(cur, selFile, bindings) local function runA(cur, selFile, bindings)
if not selFile.isDirectory then if not selFile.isDirectory then
local ext = getExtension(selFile.name, bindings) local ext = getExtension(selFile.name, bindings)
if not ext then return end if not ext then return end
if ext.a then return cur .. selFile.name, ext.ext end if ext.a then return cur .. selFile.name, ext.ext end
end end
end end
local function runY(cur, selFile, bindings) local function runY(cur, selFile, bindings)
if not selFile.isDirectory then if not selFile.isDirectory then
local ext = getExtension(selFile.name, bindings) local ext = getExtension(selFile.name, bindings)
if not ext then return end if not ext then return end
if ext.y then return cur .. selFile.name, ext.ext end if ext.y then return cur .. selFile.name, ext.ext end
end end
end end
--- Open a file browser to allow the user to select a file. --- Open a file browser to allow the user to select a file.
@ -131,69 +131,69 @@ end
-- @returns The extension of the file, this might be helpful in cases were multiple file types could be expected, nil in case no file was picked. -- @returns The extension of the file, this might be helpful in cases were multiple file types could be expected, nil in case no file was picked.
-- @returns The "mode", which indicates which key was used to select the file, "A" or "Y". "X" in case no file was picked. -- @returns The "mode", which indicates which key was used to select the file, "A" or "Y". "X" in case no file was picked.
return function(bindings, workdir) return function(bindings, workdir)
-- Initialization -- Initialization
local old = saveGraphicsState() local old = saveGraphicsState()
local cur = workdir or ctr.fs.getDirectory() local cur = workdir or ctr.fs.getDirectory()
if cur:sub(-1) ~= "/" then if cur:sub(-1) ~= "/" then
cur = cur .. "/" cur = cur .. "/"
end end
local bindings = bindings or {} local bindings = bindings or {}
local files = getFilelist(cur) or {{name = "- Empty -"}} local files = getFilelist(cur) or {{name = "- Empty -"}}
local sel = 1 local sel = 1
local scr = 0 local scr = 0
while ctr.run() do while ctr.run() do
drawBottom(cur, files[sel], bindings) drawBottom(cur, files[sel], bindings)
drawTop(files, sel, scr) drawTop(files, sel, scr)
gfx.render() gfx.render()
ctr.hid.read() ctr.hid.read()
local state = ctr.hid.keys() local state = ctr.hid.keys()
if (state.down.dDown or state.down.cpadDown) and sel < #files then if (state.down.dDown or state.down.cpadDown) and sel < #files then
sel = sel + 1 sel = sel + 1
if sel - scr >= 16 then if sel - scr >= 16 then
scr = scr + 1 scr = scr + 1
end end
elseif (state.down.dUp or state.down.cpadUp) and sel > 1 then elseif (state.down.dUp or state.down.cpadUp) and sel > 1 then
sel = sel - 1 sel = sel - 1
if sel == scr then if sel == scr then
scr = scr - 1 scr = scr - 1
end end
elseif state.down.dLeft or state.down.cpadLeft then elseif state.down.dLeft or state.down.cpadLeft then
sel = 1 sel = 1
scr = 0 scr = 0
elseif state.down.dRight or state.down.cpadRight then elseif state.down.dRight or state.down.cpadRight then
sel = #files sel = #files
if #files > 15 then if #files > 15 then
scr = #files - 16 scr = #files - 16
end end
elseif state.down.a then elseif state.down.a then
local selFile = files[sel] local selFile = files[sel]
if selFile.isDirectory then if selFile.isDirectory then
if selFile.name == ".." then if selFile.name == ".." then
cur = cur:gsub("[^/]+/$", "") cur = cur:gsub("[^/]+/$", "")
else else
cur = cur .. selFile.name .. "/" cur = cur .. selFile.name .. "/"
end end
files, sel, scr = getFilelist(cur), 1, 0 files, sel, scr = getFilelist(cur), 1, 0
else else
local file, ext = runA(cur, selFile, bindings) local file, ext = runA(cur, selFile, bindings)
if file then if file then
restoreGraphicsState(old) restoreGraphicsState(old)
return file, ext, "A" return file, ext, "A"
end end
end end
elseif state.down.y then elseif state.down.y then
local file, ext = runY(cur, files[sel], bindings) local file, ext = runY(cur, files[sel], bindings)
if file then if file then
restoreGraphicsState(old) restoreGraphicsState(old)
return file, ext, "Y" return file, ext, "Y"
end end
elseif state.down.x then elseif state.down.x then
restoreGraphicsState(old) restoreGraphicsState(old)
return nil, nil, "X" return nil, nil, "X"
end end
end end
end end

View file

@ -1,4 +1,4 @@
local ctr = require("ctr") ²local ctr = require("ctr")
local fs = require("ctr.fs") local fs = require("ctr.fs")
local gfx = require("ctr.gfx") local gfx = require("ctr.gfx")
@ -14,39 +14,39 @@ package.path = package.path..";".. ldir.."?.lua;".. ldir.."?/init.lua"
-- Erroring -- Erroring
local function displayError(err) local function displayError(err)
gfx.color.setBackground(0xFF0000B3) gfx.color.setBackground(0xFF0000B3)
gfx.color.setDefault(0xFFFDFDFD) gfx.color.setDefault(0xFFFDFDFD)
gfx.font.setDefault(gfx.font.load(ctruLua.root .. "resources/VeraMono.ttf")) gfx.font.setDefault(gfx.font.load(ctruLua.root .. "resources/VeraMono.ttf"))
while ctr.run() do while ctr.run() do
gfx.start(gfx.TOP) gfx.start(gfx.TOP)
gfx.text(1, 1, "An error has occured.", 12) gfx.text(1, 1, "An error has occured.", 12)
gfx.wrappedText(1, 30, err, gfx.TOP_WIDTH-2, 12) gfx.wrappedText(1, 30, err, gfx.TOP_WIDTH-2, 12)
gfx.text(1, gfx.TOP_HEIGHT-15, "Press Start to continue.", 12) gfx.text(1, gfx.TOP_HEIGHT-15, "Press Start to continue.", 12)
gfx.stop() gfx.stop()
gfx.start(gfx.BOTTOM) gfx.start(gfx.BOTTOM)
gfx.stop() gfx.stop()
gfx.render() gfx.render()
ctr.hid.read() ctr.hid.read()
if ctr.hid.keys().down.start then break end if ctr.hid.keys().down.start then break end
end end
end end
-- Main loop -- Main loop
while ctr.run() do while ctr.run() do
gfx.set3D(false) gfx.set3D(false)
gfx.font.setDefault() gfx.font.setDefault()
gfx.color.setDefault(0xFFFDFDFD) gfx.color.setDefault(0xFFFDFDFD)
gfx.color.setBackground(0xFF333333) gfx.color.setBackground(0xFF333333)
local file, ext, mode = require("filepicker")({{name="Lua Script", ext=".lua", a="Execute"}}) local file, ext, mode = require("filepicker")({{name="Lua Script", ext=".lua", a="Execute"}})
if file and mode == "A" then if file and mode == "A" then
fs.setDirectory(file:match("^(.-)[^/]*$")) fs.setDirectory(file:match("^(.-)[^/]*$"))
local ok, err = pcall(dofile, file) local ok, err = pcall(dofile, file)
if not ok then displayError(err) end if not ok then displayError(err) end
else else
break break
end end
end end
error("Main process has exited.\nPlease reboot.\nPressing Start does not work yet.") error("Main process has exited.\nPlease reboot.\nPressing Start does not work yet.")