Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- _f: screen framework DEVELOPER EDITION
- -- recommended, but not required, _l framework
- -- Early version, future versions will have compacter code due to reusage of code snippets
- -- Start of setup
- local _t = {} -- t: text
- local _c = {} -- c: TextColor
- local _b = {} -- b: BackgroundColor
- local _tm = {}
- local _cm = {}
- local _bm = {}
- local _color = {}
- _color["0"] = true
- _color["1"] = true
- _color["2"] = true
- _color["3"] = true
- _color["4"] = true
- _color["5"] = true
- _color["6"] = true
- _color["7"] = true
- _color["8"] = true
- _color["9"] = true
- _color["a"] = true
- _color["b"] = true
- _color["c"] = true
- _color["d"] = true
- _color["e"] = true
- _color["f"] = true
- setmetatable(_t,_tm)
- setmetatable(_c,_cm)
- setmetatable(_b,_bm)
- _tm.__index = function()
- return " "
- end
- _cm.__index = function()
- return "0"
- end
- _bm.__index = function()
- return "f"
- end
- local _misc = {}
- _misc.e = 2 -- change for higher pixel amounts
- local _setting = {}
- _setting.AttemptFullscreen = false
- _setting.forceBlackAndWhite = false
- _setting.MaxX = 0
- _setting.MaxY = 0
- _setting.useErrorLog = true
- _setting.errorLocation = "/._f/errorLog"
- _setting.errorFile = ""
- _setting.errorCount = 0
- _setting.abortOnCritical = false
- _setting.progName = ""
- if term.isColor() and not _setting.forceBlackAndWhite then
- _setting.color = true
- end
- if _setting.AttemptFullscreen then
- term = term.native()
- end
- local logError = function(_type,err)
- if type(_type) ~= "number" or type(err) ~= "string" then
- logError(4,"logError incorrect usage: ".._type..","..err..", Abort suggested!") -- meta.
- end
- local errtype = ""
- if _type == -1 then
- errtype = "USR"
- elseif _type == 0 then
- errtype = "MSG"
- elseif _type == 1 then
- errtype = "SYS"
- elseif _type == 2 then
- errtype = "WRN"
- else
- errtype = "ERR"
- end
- _setting.errorFile.writeLine(os.time().." :"..errtype..": "..err)
- _setting.errorFile.flush()
- if errtype == "ERR" and _setting.abortOnCritical then
- error("Critical Error: see log",2)
- end
- return true
- end
- if false then
- progName = shell.getRunningProgram()
- else
- progName = "NA"
- end
- if _setting.useErrorLog then
- _setting.errorFile = fs.open(_setting.errorLocation.."/"..progName.."-"..os.day()..":"..os.time(),"w")
- logError(0,"Log started")
- else
- logError = function(_type,_)
- if _type > 2 then
- error("Critical error: no log",2)
- end
- end
- end
- if _setting.MaxX < 1 or _setting.MaxY < 1 then
- logError(1,"Using getSize for drawing area")
- _setting.MaxX,_setting.MaxY = term.getSize()
- end
- --[[while math.pow(10,_misc.e) < setting._setting.MaxX and math.pow(10,_misc.e) < setting._setting.MaxY do
- _misc.e = _misc.e + 1
- end]]
- -- End of setup
- testError = function(a,b) -- test error logging
- logError(a,b)
- end
- local _checkValidPos = function(x,y)
- if type(x) ~= "number" or type(y) ~= "number" or x < 1 or x > _setting.MaxX or y < 1 or y > _setting.MaxY then
- return false
- end
- return true
- end
- local _table = function(x,y) -- internal table formatting
- if _checkValidPos(x,y) == false then
- logError(4,"_table incorrect usage: "..x..","..y..", Defaulting to 1,1")
- return _table(1,1)
- end
- local result = ""
- while #result+#tostring(x) < _misc.e do
- result = result.."0"
- end
- result = result..x
- while #result+#tostring(y) < _misc.e * 2 do
- result = result.."0"
- end
- result = result..y
- return result
- end
- -- set functions
- set_t = function(x,y,char)
- if type(x) == "number" and type(y) == "number" then
- if #char ~= 1 then
- logError(2,"set_t incorrect usage: character lenght ~= 1 at "..x..","..y)
- char = char:sub(1,1)
- end
- if _checkValidPos(x,y) then
- _t[_table(x,y)] = char
- else
- logError(2,"set_t incorrect usage: "..x..","..y..",")
- end
- else
- logError(2,"set_t incorrect usage: x,y:")
- end
- end
- set_c = function(x,y,color)
- if type(x) == "number" and type(y) == "number" then
- if #color ~= 1 then
- logError(2,"set_c incorrect usage: character lenght ~= 1 at "..x..","..y)
- char = char:sub(1,1)
- end
- if _color[color] == false then
- logError(2,"set_c incorrect usage: color must be '0' - '9', 'a' to 'f' at "..x..","..y)
- else
- if _checkValidPos(x,y) then
- _c[_table(x,y)] = color
- else
- logError(2,"set_c incorrect usage: "..x..","..y)
- end
- end
- else
- logError(2,"set_c incorrect usage: x,y:")
- end
- end
- set_b = function(x,y,color)
- if type(x) == "number" and type(y) == "number" then
- if #color ~= 1 then
- logError(2,"set_b incorrect usage: character lenght ~= 1 at "..x..","..y)
- char = char:sub(1,1)
- end
- if _color[color] == false then
- logError(2,"set_b incorrect usage: color must be '0' - '9', 'a' to 'f' at "..x..","..y)
- else
- if _checkValidPos(x,y) then
- _b[_table(x,y)] = color
- else
- logError(2,"set_b incorrect usage: "..x..","..y)
- end
- end
- else
- logError(2,"set_b incorrect usage: x,y")
- end
- end
- set_text = function(x,y,text)
- text = tostring(text)
- if _checkValidPos(x,y) == false then
- logError(2,"set_text incorrect usage: "..x..","..y)
- else
- if #text > _setting.MaxX-x+1 then
- logError(1,"set_text: text too long: "..x..","..y)
- text = text:sub(1,_setting.MaxX-x+1)
- end
- local n = 1
- repeat
- set_t(x+n-1,y,text:sub(n,n))
- n = n + 1
- until text:sub(n,n) == ""
- end
- end
- set_t_all = function(char)
- if type(char) == "string" and #char == 1 then
- local y = 1
- repeat
- local x = 1
- repeat
- set_t(x,y,char)
- x = x + 1
- until x > _setting.MaxX
- y = y + 1
- until y > _setting.MaxY
- else
- logError(2,"set_t_all invalid usage: "..char)
- end
- end
- set_color = function(color)
- if _color[color] then
- local y = 1
- repeat
- local x = 1
- repeat
- set_c(x,y,color)
- x = x + 1
- until x > _setting.MaxX
- y = y + 1
- until y > _setting.MaxY
- else
- logError(2,"set_color incorrect usage: '0' - '9', 'a' to 'f'")
- end
- end
- set_background = function(color)
- if _color[color] then
- local y = 1
- repeat
- local x = 1
- repeat
- set_b(x,y,color)
- x = x + 1
- until x > _setting.MaxX
- y = y + 1
- until y > _setting.MaxY
- else
- logError(2,"set_background incorrect usage: '0' - '9', 'a' to 'f'")
- end
- end
- set_clear = function()
- set_t_all(" ")
- set_color("0")
- set_background("f")
- end
- set_t_line = function(xOrY,direction,char)
- if type(xOrY) == "number" and type(direction) == "string" and type(char) == "string" and #char == 1 then
- if direction == ("across" or "1") then
- local x = 1
- repeat
- set_t(x,xOrY,char)
- x = x + 1
- until x > _setting.MaxX
- elseif direction == ("down" or "0") then
- local y = 1
- repeat
- set_t(xOrY,y,char)
- y = y + 1
- until y > _setting.MaxY
- else
- logError("set_t_line incorrect usage: direction: "..direction)
- end
- else
- logError(2,"set_t_line incorrect usage: "..xOry..","..direction..","..char)
- end
- end
- set_c_line = function(xOrY,direction,color)
- if type(xOrY) == "number" and type(direction) == "string" and type(char) == "string" and #color == 1 then
- if direction == ("across" or "1") then
- local x = 1
- repeat
- set_c(x,xOrY,color)
- x = x + 1
- until x > _setting.MaxX
- elseif direction == ("down" or "0") then
- local y = 1
- repeat
- set_c(xOrY,y,color)
- y = y + 1
- until y > _setting.MaxY
- else
- logError("set_c_line incorrect usage: direction: "..direction)
- end
- else
- logError(2,"set_c_line incorrect usage: "..xOry..","..direction..","..char)
- end
- end
- set_b_line = function(xOrY,direction,color)
- if type(xOrY) == "number" and type(direction) == "string" and type(char) == "string" and #color == 1 then
- if direction == ("across" or "1") then
- local x = 1
- repeat
- set_b(x,xOrY,color)
- x = x + 1
- until x > _setting.MaxX
- elseif direction == ("down" or "0") then
- local y = 1
- repeat
- set_b(xOrY,y,color)
- y = y + 1
- until y > _setting.MaxY
- else
- logError("set_b_line incorrect usage: direction: "..direction)
- end
- else
- logError(2,"set_b_line incorrect usage: "..xOry..","..direction..","..char)
- end
- end
- set_t_area = function(x1,y1,x2,y2,char)
- if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
- if type(char) == "string" and #char == 1 then
- repeat
- local x = x1
- repeat
- set_t(x,y1,char)
- x = x + 1
- until x > x2
- y1 = y1 + 1
- until y1 > y2
- else
- logError(2,"set_t_area incorrect usage: char ~= 1: "..char)
- end
- else
- logError(2,"set_t_area incorrect usage: "..x1..","..y1..","..x2..","..y2)
- end
- end
- set_c_area = function(x1,y1,x2,y2,color)
- if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
- if type(char) == "string" and #char == 1 and _color[color] then
- repeat
- local x = x1
- repeat
- set_c(x,y1,color)
- x = x + 1
- until x > x2
- y1 = y1 + 1
- until y1 > y2
- else
- logError(2,"set_c_area incorrect usage: color ~= 1 or color does not exist: "..char)
- end
- else
- logError(2,"set_c_area incorrect usage: "..x1..","..y1..","..x2..","..y2)
- end
- end
- set_b_area = function(x1,y1,x2,y2,color)
- if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
- if type(char) == "string" and #char == 1 and _color[color] then
- repeat
- local x = x1
- repeat
- set_b(x,y1,color)
- x = x + 1
- until x > x2
- y1 = y1 + 1
- until y1 > y2
- else
- logError(2,"set_b_area incorrect usage: color ~= 1 or color does not exist: "..char)
- end
- else
- logError(2,"set_b_area incorrect usage: "..x1..","..y1..","..x2..","..y2)
- end
- end
- -- draw functions
- local _draw = function(x,y)
- if _checkValidPos(x,y) then
- local a = _table(x,y)
- term.setCursorPos(x,y)
- if _t[a] == nil or _t[a] == "" then
- _t[a] = " "
- end
- if _setting.color and _c[a] and _b[a] then
- term.blit(_t[a],_c[a],_b[a])
- else
- term.blit(_t[a],"0","f")
- end
- else
- logError(4,"_draw incorrect usage: "..x..","..y)
- end
- end
- draw_pixel = function(x,y)
- if _checkValidPos(x,y) then
- _draw(x,y)
- end
- end
- draw_part = function(x1,y1,x2,y2)
- if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
- repeat
- local x = x1
- repeat
- _draw(x,y1)
- x = x + 1
- until x > x2
- y1 = y1 + 1
- until y1 > y2
- else
- logError(2,"draw_part invalid usage: "..x1..","..y1..","..x2..","..y2)
- end
- end
- draw_all = function()
- draw_part(1,1,_setting.MaxX,_setting.MaxY)
- end
- -- file handling functions
- raw_save = function(path)
- local file = fs.open(path.."._f","w")
- local mtable = {}
- mtable.t = _t
- mtable.c = _c
- mtable.b = _b
- file.writeLine(textutils.serialize(mtable))
- file.close()
- end
- raw_load = function(path)
- local file = fs.open(path.."._f","r")
- local mtable = textutils.unserialize(file.readAll())
- _t = mtable.t
- _c = mtable.c
- _b = mtable.b
- end
- --Aliases:
- draw = draw_pixel
- set_col = set_color
- set_bkg = set_background
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement