diff --git a/src/gui/label.lua b/src/gui/label.lua new file mode 100644 index 0000000..8bec77a --- /dev/null +++ b/src/gui/label.lua @@ -0,0 +1,43 @@ +return { + new = function(str) + return { + type="label", + content=str + } + end, + draw = function(label,ctx) + local w,_ = term.getSize() + local _,y = term.getCursorPos() + term.setTextColor(ctx.color.secondary) + + -- split words + local content = {} + for word in label.content:gmatch('([^ ]+)') do + table.insert(content,word) + end + + -- Split lines + local len = 0 + local splits = {} + for i=1,#content do + len = len+#content[i]+1 + if len > w/2 or i==#content then + table.insert(splits,{i,len}) + len = 0 + end + end + + -- Render + for i=1,#splits do + term.setCursorPos((w-splits[i][2])/2,y+i-1) + local begin = 1 + if i > 1 then + begin = splits[i-1][1]+1 + end + + for j=begin,splits[i][1] do + term.write(' '..content[j]) + end + end + end +} \ No newline at end of file diff --git a/src/gui/render.lua b/src/gui/render.lua new file mode 100644 index 0000000..535ee05 --- /dev/null +++ b/src/gui/render.lua @@ -0,0 +1,38 @@ +return { + new = function(name) + return {name=name} + end, + draw = function(page,ctx) + local w,h = term.getSize() + + if ctx.redraw then + term.setBackgroundColor(ctx.color.bg) + term.clear() + ctx.redraw = false + end + + -- Widgets + term.setBackgroundColor(ctx.color.bg) + local y = 3+ctx.scroll + for i=1,#page do + term.setCursorPos(0,y+2) + if page[i].type then + if ctx.gui[page[i].type] then + ctx.gui[page[i].type].draw(page[i], ctx) + end + end + _,y = term.getCursorPos() + end + + page.length = y-(4+ctx.scroll) + if page.length < 3 then page.length = 3 end + + -- Header + local line = (' '):rep(w) + term.setBackgroundColor(ctx.color.primary) + for y=1,3 do + term.setCursorPos(1,y+ctx.scroll) + term.write(line) + end + end +} \ No newline at end of file diff --git a/src/init.lua b/src/init.lua index e67301b..2175097 100644 --- a/src/init.lua +++ b/src/init.lua @@ -1,23 +1,100 @@ --- make a copy of package.path -local old_path = package.path - -package.path = string.format( - "/%s/?.lua;/rom/modules/main/?.lua", - fs.getDir(shell.getRunningProgram()) -) - -local function main(...) - -- main init code -end - -local args = table.pack(...) -xpcall(function() - main(table.unpack(args,1,args.n)) -end,function(err) - -- error display - - printError(err) -end) - --- restores package path to original -package.path = old_path +-- make a copy of package.path +local old_path = package.path +local sPath = fs.getDir(shell.getRunningProgram()) +local ctx + +package.path = string.format( + "/%s/?.lua;/rom/modules/main/?.lua", sPath +) +local function init(...) + ctx = {gui={},pages={},current=1,scroll=0,redraw=true} + local sGui = fs.combine(sPath,"gui") + local sData = fs.combine(sPath,"data") + local sPages = fs.combine(sData,"pages") + + -- load widgets + local widgets = fs.list(sGui) + for i=1,#widgets do + local _,nX = widgets[i]:find('%.') + if nX then widgets[i] = widgets[i]:sub(1,nX-1) end + + local tmp = require(fs.combine(sGui,widgets[i])) + ctx.gui[widgets[i]] = {} + for k,v in pairs(tmp) do + ctx.gui[widgets[i]][k] = v + end + end + + -- load pages + local pages = fs.list(sPages) + for i=1,#pages do + local f = fs.open(fs.combine(sPages,pages[i]), 'r') + local content = f.readAll() + f.close() + + content = textutils.unserialise(content) + if type(content) == "table" then + content.length = 0 + if pages[i] == "index.table" then + table.insert(ctx.pages, 1, content) + else + table.insert(ctx.pages, content) + end + end + end + + -- load colors + local f = fs.open(fs.combine(sData,"color.table"), 'r') + local content = f.readAll() + f.close() + + local col = {white=0x1,orange=0x2,magenta=0x4,lightBlue=0x8,yellow=0x10,lime=0x20,pink=0x40,grey=0x80,lightGrey=0x100,cyan=0x200,purple=0x400,blue=0x800,brown=0x1000,green=0x2000,red=0x4000,black=0x8000} + local inferiorcol=col; inferiorcol.gray=col.grey; inferiorcol.lightGray=col.lightGrey + local b,colors = pcall( load("return "..content,"","t",{colours=col,colors=inferiorcol}) ) + if type(colors) == "table" and b then + ctx.color = colors + end + + return ctx +end + +-- INIT +local args = table.pack(...) +xpcall(function() + init(table.unpack(args,1,args.n)) +end,function(err) + printError(err) +end) +-- MAIN +xpcall(function() + parallel.waitForAny( + function() + while true do + ctx.gui.render.draw(ctx.pages[1],ctx) + sleep() + end + end, + function() + while true do + local _,h = term.getSize() + local event = {os.pullEvent()} -- CHANGE TO pullEventRaw later!!!!! + ctx.redraw = true + if event[1] == "mouse_scroll" then + if ctx.pages[1].length > h then + ctx.scroll = ctx.scroll-event[2] + if ctx.scroll > 0 then + ctx.scroll = 0 + elseif ctx.scroll < -(ctx.pages[1].length) then + ctx.scroll = -(ctx.pages[1].length) + end + end + end + end + end + ) +end,function(err) + printError(err) +end) + +-- restores package path to original +package.path = old_path \ No newline at end of file