mirror of
https://github.com/ctruLua/ctruLua.git
synced 2025-10-28 00:39:30 +00:00
Updated font and fs lib; updated editor; updated sf2dlib
Libs additions: font.load, font:unload, font:getWidth, fs.getDirectory, fs.setDirectory, fs.exists Editor additions: syntaxic coloring and mono font sf2dlib update: you will need the latest version of ctrulib. Also, because of the lib font needs, the sftdlib was modified.
This commit is contained in:
parent
3f995629c0
commit
45f3216ed8
14 changed files with 382 additions and 28 deletions
BIN
sdcard/3ds/ctruLua/editor/VeraMono.ttf
Normal file
BIN
sdcard/3ds/ctruLua/editor/VeraMono.ttf
Normal file
Binary file not shown.
|
|
@ -1,6 +1,16 @@
|
|||
-- Colors based on the Monokai theme
|
||||
return {
|
||||
background = 0x272822FF,
|
||||
default = 0xF8F8F2FF,
|
||||
cursor = 0xFF0000FF
|
||||
-- General
|
||||
["background"] = 0x272822FF,
|
||||
["cursor"] = 0xF8F8F0FF,
|
||||
["default"] = 0xF8F8F2FF,
|
||||
|
||||
-- Syntax
|
||||
["comment"] = 0x75715EFF,
|
||||
["string"] = 0xE6DB74FF,
|
||||
["constant.numeric"] = 0xAE81FFFF,
|
||||
["constant.language"] = 0xAE81FFFF,
|
||||
["keyword.control"] = 0xF92672FF,
|
||||
["keyword.operator"] = 0xF92672FF,
|
||||
["support.function"] = 0x66D9EFFF
|
||||
}
|
||||
|
|
@ -5,27 +5,45 @@ local gfx = require("ctr.gfx")
|
|||
-- Open libs
|
||||
local keyboard = dofile("sdmc:/3ds/ctruLua/keyboard.lua")
|
||||
local openfile = dofile("sdmc:/3ds/ctruLua/openfile.lua")
|
||||
local color = dofile("sdmc:/3ds/ctruLua/editor/color.lua")
|
||||
local color = dofile("color.lua")
|
||||
local syntax = dofile("syntax.lua")
|
||||
|
||||
-- Load data
|
||||
local font = gfx.font.load("VeraMono.ttf")
|
||||
|
||||
-- Open file
|
||||
local path, status = openfile("Choose a file to edit", "/3ds/ctruLua/", nil, "any")
|
||||
if not path then return end
|
||||
local lineEnding
|
||||
local lines = {}
|
||||
if status == "exist" then
|
||||
for line in io.lines(path) do table.insert(lines, line) end
|
||||
for line in io.lines(path, "L") do
|
||||
if not lineEnding then lineEnding = line:match("([\n\r]+)$") end
|
||||
table.insert(lines, line:match("^(.-)[\n\r]*$"))
|
||||
end
|
||||
else
|
||||
lineEnding = "\n"
|
||||
lines = { "" }
|
||||
end
|
||||
|
||||
-- Syntax coloring
|
||||
local coloredLines = syntax(lines, color)
|
||||
|
||||
-- Variables
|
||||
local lineHeight = 10
|
||||
local cursorX, cursorY = 1, 1
|
||||
local scrollX, scrollY = 0, 0
|
||||
|
||||
-- Helper functions
|
||||
local function displayedText(text)
|
||||
return text:gsub("\t", " ")
|
||||
end
|
||||
|
||||
-- Set defaults
|
||||
gfx.set3D(false)
|
||||
gfx.color.setDefault(color.default)
|
||||
gfx.color.setBackground(color.background)
|
||||
gfx.font.setDefault(font)
|
||||
|
||||
while ctr.run() do
|
||||
hid.read()
|
||||
|
|
@ -75,10 +93,10 @@ while ctr.run() do
|
|||
until t + 5 < os.time()
|
||||
else
|
||||
for i = 1, #lines, 1 do
|
||||
file:write(lines[i].."\n")
|
||||
file:write(lines[i]..lineEnding)
|
||||
gfx.startFrame(gfx.GFX_TOP)
|
||||
gfx.rectangle(0, 0, math.ceil(i/#lines*gfx.TOP_WIDTH), gfx.TOP_HEIGHT, 0, 0xFFFFFFFF)
|
||||
gfx.color.setDefault(0x000000FF)
|
||||
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.endFrame()
|
||||
|
|
@ -122,6 +140,7 @@ while ctr.run() do
|
|||
-- Draw
|
||||
gfx.startFrame(gfx.GFX_TOP)
|
||||
|
||||
-- Lines
|
||||
local sI = math.floor(scrollY / lineHeight)
|
||||
if sI < 1 then sI = 1 end
|
||||
|
||||
|
|
@ -129,21 +148,30 @@ while ctr.run() do
|
|||
if eI > #lines then eI = #lines end
|
||||
|
||||
for i = sI, eI, 1 do
|
||||
local line = lines[i]
|
||||
local x = -scrollX
|
||||
local y = -scrollY+ (i-1)*lineHeight
|
||||
|
||||
if cursorY == i then
|
||||
gfx.color.setDefault(color.cursor)
|
||||
gfx.text(-scrollX, y, line:sub(1, (utf8.offset(line, cursorX) or 0)-1):gsub("\t", " ").."|", nil) -- TODO: color doesn't work
|
||||
|
||||
for _,colored in ipairs(coloredLines[i]) do
|
||||
local str = displayedText(colored[1])
|
||||
|
||||
gfx.color.setDefault(colored[2])
|
||||
gfx.text(x, y, str)
|
||||
gfx.color.setDefault(color.default)
|
||||
|
||||
x = x + font:width(str)
|
||||
end
|
||||
|
||||
gfx.text(-scrollX, y, line:gsub("\t", " "), nil)
|
||||
end
|
||||
|
||||
-- Cursor
|
||||
local curline = lines[cursorY]
|
||||
gfx.rectangle(-scrollX+ font:width(displayedText(curline:sub(1, (utf8.offset(curline, cursorX) or 0)-1))),
|
||||
-scrollY+ (cursorY-1)*lineHeight, 1, lineHeight, 0, color.cursor)
|
||||
|
||||
gfx.endFrame()
|
||||
|
||||
gfx.startFrame(gfx.GFX_BOTTOM)
|
||||
|
||||
gfx.text(3, 3, "FPS: "..math.ceil(gfx.getFPS()))
|
||||
|
||||
keyboard.draw(5, 115)
|
||||
|
||||
|
|
@ -151,3 +179,5 @@ while ctr.run() do
|
|||
|
||||
gfx.render()
|
||||
end
|
||||
|
||||
font:unload()
|
||||
90
sdcard/3ds/ctruLua/editor/syntax.lua
Normal file
90
sdcard/3ds/ctruLua/editor/syntax.lua
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
-- Each pattern should return 3 captures : start position, the string to colorize, and the end position.
|
||||
local syntax = {
|
||||
{ "comment", { "()(%-%-.*)()$" } },
|
||||
|
||||
--["string"] = { "()(%'.*%f[%\\]%')()", "()(%\".*%f[%\\]%\")()" },
|
||||
{ "string", { "()(%'[^%']*%')()", "()(%\"[^%\"]*%\")()" } },
|
||||
|
||||
{ "constant.numeric", {
|
||||
"%f[%d%w%.]()(0x[a-fA-F%d]+)()%f[^%d%w%.]",
|
||||
"%f[%d%w%.]()([%d% ]+%.[%d% ]+)()%f[^%d%w%.]",
|
||||
"%f[%d%w%.]()([%d% ]+)()%f[^%d%w%.]"
|
||||
}
|
||||
},
|
||||
|
||||
{ "constant.language", {
|
||||
"%f[%w]()(false)()%f[%W]", "%f[%w]()(nil)()%f[%W]", "%f[%w]()(true)()%f[%W]", "%f[%w]()(_G)()%f[%W]",
|
||||
"%f[%w]()(_VERSION)()%f[%W]", "%f[%w]()(math.pi)()%f[%W]", "%f[%w]()(math.huge)()%f[%W]", "%f[%w]()(%.%.%.)()%f[%W]"
|
||||
}
|
||||
},
|
||||
|
||||
{ "keyword.control", {
|
||||
"%f[%w]()(break)()%f[%W]", "%f[%w]()(goto)()%f[%W]", "%f[%w]()(do)()%f[%W]", "%f[%w]()(else)()%f[%W]",
|
||||
"%f[%w]()(for)()%f[%W]", "%f[%w]()(if)()%f[%W]", "%f[%w]()(elseif)()%f[%W]", "%f[%w]()(return)()%f[%W]",
|
||||
"%f[%w]()(then)()%f[%W]", "%f[%w]()(repeat)()%f[%W]", "%f[%w]()(while)()%f[%W]", "%f[%w]()(until)()%f[%W]",
|
||||
"%f[%w]()(end)()%f[%W]", "%f[%w]()(function)()%f[%W]", "%f[%w]()(local)()%f[%W]", "%f[%w]()(in)()%f[%W]"
|
||||
}
|
||||
},
|
||||
|
||||
{ "keyword.operator", {
|
||||
"%f[%w]()(and)()%f[%W]", "%f[%w]()(or)()%f[%W]", "%f[%w]()(not)()%f[%W]",
|
||||
"()(%+)()", "()(%-)()", "()(%%)()", "()(%#)()", "()(%*)()", "()(%/%/?)()", "()(%^)()", "()(%=%=?)()", "()(%~%=?)()",
|
||||
"()(%.%.)()", "()(%<%=?)()", "()(%>%=?)()", "()(%&)()", "()(%|)()", "()(%<%<)()", "()(%>%>)()",
|
||||
}
|
||||
},
|
||||
|
||||
{ "support.function", {
|
||||
"[^%.%:]()(assert)()[%( %{]", "[^%.%:]()(collectgarbage)()[%( %{]", "[^%.%:]()(dofile)()[%( %{]",
|
||||
"[^%.%:]()(error)()[%( %{]", "[^%.%:]()(getfenv)()[%( %{]", "[^%.%:]()(getmetatable)()[%( %{]",
|
||||
"[^%.%:]()(ipairs)()[%( %{]", "[^%.%:]()loadfile)()[%( %{]", "[^%.%:]()(loadstring)()[%( %{]",
|
||||
"[^%.%:]()(module)()[%( %{]", "[^%.%:]()(next)()[%( %{]", "[^%.%:]()(pairs)()[%( %{]",
|
||||
"[^%.%:]()(pcall)()[%( %{]", "[^%.%:]()(print)()[%( %{]", "[^%.%:]()(rawequal)()[%( %{]",
|
||||
"[^%.%:]()(rawget)()[%( %{]", "[^%.%:]()(rawset)()[%( %{]", "[^%.%:]()(require)()[%( %{]",
|
||||
"[^%.%:]()(select)()[%( %{]", "[^%.%:]()(setfenv)()[%( %{]", "[^%.%:]()(setmetatable)()[%( %{]",
|
||||
"[^%.%:]()(tonumber)()[%( %{]", "[^%.%:]()(tostring)()[%( %{]", "[^%.%:]()(type)()[%( %{]",
|
||||
"[^%.%:]()(unpack)()[%( %{]", "[^%.%:]()(xpcall)()[%( %{]"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return function(lines, color)
|
||||
local ret = {}
|
||||
|
||||
for _,line in ipairs(lines) do
|
||||
local colored = { { line, color.default } }
|
||||
|
||||
for _, patterns in ipairs(syntax) do
|
||||
local name = patterns[1]
|
||||
|
||||
for _, pattern in ipairs(patterns[2]) do
|
||||
local i = 1
|
||||
while i <= #colored do
|
||||
local oldcolor = colored[i][2]
|
||||
|
||||
if oldcolor == color.default then
|
||||
local part = colored[i][1]
|
||||
|
||||
local starti, match, endi = part:match(pattern)
|
||||
if starti then
|
||||
table.remove(colored, i)
|
||||
if starti > 1 then
|
||||
table.insert(colored, i, { part:sub(1, starti-1), oldcolor })
|
||||
i = i + 1
|
||||
end
|
||||
table.insert(colored, i, { match, color[name] or color.default })
|
||||
if endi <= #part then
|
||||
table.insert(colored, i+1, { part:sub(endi, -1), oldcolor })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(ret, colored)
|
||||
end
|
||||
|
||||
return ret
|
||||
end
|
||||
|
|
@ -1,4 +1,10 @@
|
|||
local fs = require("ctr.fs")
|
||||
|
||||
repeat
|
||||
local file = dofile("sdmc:/3ds/ctruLua/openfile.lua")("Choose a Lua file to execute", "/3ds/ctruLua/", ".lua", "exist")
|
||||
if file then dofile(file) end
|
||||
fs.setDirectory("sdmc:/3ds/ctruLua")
|
||||
local file = dofile("openfile.lua")("Choose a Lua file to execute", "/3ds/ctruLua/", ".lua", "exist")
|
||||
if file then
|
||||
fs.setDirectory(file:match("^(.-)[^/]*$"))
|
||||
dofile(file)
|
||||
end
|
||||
until not file
|
||||
|
|
@ -29,9 +29,11 @@ return function(title, curdir, exts, type)
|
|||
--local was3D = gfx.get3D() TODO: implement this thing in ctruLua
|
||||
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()
|
||||
|
|
@ -130,6 +132,7 @@ return function(title, curdir, exts, type)
|
|||
--gfx.set3D(was3D)
|
||||
gfx.color.setDefault(wasDefault)
|
||||
gfx.color.setBackground(wasBackground)
|
||||
gfx.font.setDefault(wasFont)
|
||||
|
||||
if ret then
|
||||
return table.unpack(ret)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue