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

Made the editor usable

It was able to edit file before, but didn't show the changes on the screen, so this was easy to do.

Also renamed some setTextSize->setSize missed in last commit.
This commit is contained in:
Reuh 2016-04-21 22:31:48 +02:00
parent 347e480d5a
commit 2b7d37304d
3 changed files with 46 additions and 10 deletions

View file

@ -38,6 +38,7 @@ local lineHeight = 10
local fontSize = 9
local cursorX, cursorY = 1, 1
local scrollX, scrollY = 0, 0
local fileModified = false
-- Helper functions
local function displayedText(text)
@ -56,7 +57,30 @@ while ctr.run() do
local keys = hid.keys()
-- Keys input
if keys.down.start then return end
if keys.down.start then
local exit = not fileModified
if fileModified then
while ctr.run() do
hid.read()
local keys = hid.keys()
if keys.down.b then
exit = false
break
elseif keys.down.a then
exit = true
break
end
gfx.start(gfx.TOP)
gfx.text(3, 3, "The file was modified but not saved!")
gfx.text(3, 3 + lineHeight, "Are you sure you want to exit without saving?")
gfx.text(3, 3 + lineHeight*2, "Press A to exit and discard the modified file")
gfx.text(3, 3 + lineHeight*3, "Press B to return to the editor")
gfx.stop()
gfx.render()
end
end
if exit then break end
end
if keys.down.dRight then
cursorX = cursorX + 1
@ -101,15 +125,16 @@ while ctr.run() do
for i = 1, #lines, 1 do
file:write(lines[i]..lineEnding)
gfx.start(gfx.TOP)
gfx.rectangle(0, 0, math.ceil(i/#lines*gfx.TOP_WIDTH), gfx.TOP_HEIGHT, 0, 0xFFFFFFFF)
gfx.color.setDefault(color.background)
gfx.text(gfx.TOP_WIDTH/2, gfx.TOP_HEIGHT/2, math.ceil(i/#lines*100).."%")
gfx.color.setDefault(color.default)
gfx.rectangle(0, 0, math.ceil(i/#lines*gfx.TOP_WIDTH), gfx.TOP_HEIGHT, 0, 0xFFFFFFFF)
gfx.color.setDefault(color.background)
gfx.text(gfx.TOP_WIDTH/2, gfx.TOP_HEIGHT/2, math.ceil(i/#lines*100).."%")
gfx.color.setDefault(color.default)
gfx.stop()
gfx.render()
end
file:flush()
file:close()
fileModified = false
end
end
@ -126,21 +151,30 @@ while ctr.run() do
cursorX, cursorY = utf8.len(lines[cursorY-1])+1, cursorY - 1
lines[cursorY] = lines[cursorY]..lines[cursorY+1]
table.remove(lines, cursorY+1)
table.remove(coloredLines, cursorY+1)
end
coloredLines[cursorY] = syntax(lines[cursorY], color)
elseif input == "\n" then
local newline = lines[cursorY]:sub(utf8.offset(lines[cursorY], cursorX), -1)
local whitespace = lines[cursorY]:match("^%s+")
if whitespace then newline = whitespace .. newline end
lines[cursorY] = lines[cursorY]:sub(1, utf8.offset(lines[cursorY], cursorX)-1)
coloredLines[cursorY] = syntax(lines[cursorY], color)
table.insert(lines, cursorY + 1, newline)
table.insert(coloredLines, cursorY + 1, syntax(newline, color))
cursorX, cursorY = whitespace and #whitespace+1 or 1, cursorY + 1
else
lines[cursorY] = lines[cursorY]:sub(1, utf8.offset(lines[cursorY], cursorX)-1)..input..
lines[cursorY]:sub(utf8.offset(lines[cursorY], cursorX), -1)
coloredLines[cursorY] = syntax(lines[cursorY], color)
cursorX = cursorX + 1
end
fileModified = true
end
-- Draw
@ -174,6 +208,8 @@ while ctr.run() do
gfx.start(gfx.BOTTOM)
gfx.text(3, 3, "FPS: "..math.ceil(gfx.getFPS()))
gfx.text(3, 3 + lineHeight, "Press select to save.")
gfx.text(3, 3 + lineHeight*2, "Press start to exit.")
keyboard.draw(4, 115)

View file

@ -50,7 +50,7 @@ local syntax = {
return function(lines, color)
local ret = {}
for _, line in ipairs(lines) do
for _, line in ipairs(type(lines) == "table" and lines or {lines}) do
local colored = { { line, color.default } }
for _, patterns in ipairs(syntax) do
@ -86,5 +86,5 @@ return function(lines, color)
table.insert(ret, colored)
end
return ret
return type(lines) == "table" and ret or ret[1]
end

View file

@ -7,7 +7,7 @@ local externalConfig
local function gfxPrepare()
local old = {gfx.get3D(), gfx.color.getDefault(), gfx.color.getBackground(),
gfx.font.getDefault(), gfx.getTextSize()}
gfx.font.getDefault(), gfx.font.getSize()}
local mono = gfx.font.load(ctr.root .. "resources/VeraMono.ttf")
@ -15,7 +15,7 @@ local function gfxPrepare()
gfx.color.setDefault(0xFFFDFDFD)
gfx.color.setBackground(0xFF333333)
gfx.font.setDefault(mono)
gfx.setTextSize(12)
gfx.font.setSize(12)
return old
end
@ -25,7 +25,7 @@ local function gfxRestore(state)
gfx.color.setDefault(state[2])
gfx.color.setBackground(state[3])
gfx.font.setDefault(state[4])
gfx.setTextSize(state[5])
gfx.font.setSize(state[5])
end
local function systemBindings(bindings)