Advertisement
john9912

_f framework CC

Aug 27th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.10 KB | None | 0 0
  1. -- _f: screen framework DEVELOPER EDITION
  2. -- recommended, but not required, _l framework
  3. -- Early version, future versions will have compacter code due to reusage of code snippets
  4. -- Start of setup
  5. local _t = {} -- t: text
  6. local _c = {} -- c: TextColor
  7. local _b = {} -- b: BackgroundColor
  8. local _tm = {}
  9. local _cm = {}
  10. local _bm = {}
  11. local _color = {}
  12. _color["0"] = true
  13. _color["1"] = true
  14. _color["2"] = true
  15. _color["3"] = true
  16. _color["4"] = true
  17. _color["5"] = true
  18. _color["6"] = true
  19. _color["7"] = true
  20. _color["8"] = true
  21. _color["9"] = true
  22. _color["a"] = true
  23. _color["b"] = true
  24. _color["c"] = true
  25. _color["d"] = true
  26. _color["e"] = true
  27. _color["f"] = true
  28. setmetatable(_t,_tm)
  29. setmetatable(_c,_cm)
  30. setmetatable(_b,_bm)
  31. _tm.__index = function()
  32.    return " "
  33. end
  34. _cm.__index = function()
  35.    return "0"
  36. end
  37. _bm.__index = function()
  38.    return "f"
  39. end
  40. local _misc = {}
  41. _misc.e = 2  -- change for higher pixel amounts
  42. local _setting = {}
  43. _setting.AttemptFullscreen = false
  44. _setting.forceBlackAndWhite = false
  45. _setting.MaxX = 0
  46. _setting.MaxY = 0
  47. _setting.useErrorLog = true
  48. _setting.errorLocation = "/._f/errorLog"
  49. _setting.errorFile = ""
  50. _setting.errorCount = 0
  51. _setting.abortOnCritical = false
  52. _setting.progName = ""
  53. if term.isColor() and not _setting.forceBlackAndWhite then
  54.    _setting.color = true
  55. end
  56. if _setting.AttemptFullscreen then
  57.    term = term.native()
  58. end
  59. local logError = function(_type,err)
  60.    if type(_type) ~= "number" or type(err) ~= "string" then
  61.       logError(4,"logError incorrect usage: ".._type..","..err..", Abort suggested!") -- meta.
  62.    end
  63.    local errtype = ""
  64.    if _type == -1 then
  65.       errtype = "USR"
  66.    elseif _type == 0 then
  67.       errtype = "MSG"
  68.    elseif _type == 1 then
  69.       errtype = "SYS"
  70.    elseif _type == 2 then
  71.       errtype = "WRN"
  72.    else
  73.       errtype = "ERR"
  74.    end
  75.    _setting.errorFile.writeLine(os.time().." :"..errtype..": "..err)
  76.    _setting.errorFile.flush()
  77.    if errtype == "ERR" and _setting.abortOnCritical then
  78.       error("Critical Error: see log",2)
  79.    end
  80.    return true
  81. end
  82. if false then
  83.    progName = shell.getRunningProgram()
  84. else
  85.    progName = "NA"
  86. end
  87. if _setting.useErrorLog then
  88.    _setting.errorFile = fs.open(_setting.errorLocation.."/"..progName.."-"..os.day()..":"..os.time(),"w")
  89.    logError(0,"Log started")
  90. else
  91.    logError = function(_type,_)
  92.       if _type > 2 then
  93.          error("Critical error: no log",2)
  94.       end
  95.    end
  96. end
  97. if _setting.MaxX < 1 or _setting.MaxY < 1 then
  98.    logError(1,"Using getSize for drawing area")
  99.    _setting.MaxX,_setting.MaxY = term.getSize()
  100. end
  101. --[[while math.pow(10,_misc.e) < setting._setting.MaxX and math.pow(10,_misc.e) < setting._setting.MaxY do
  102.    _misc.e = _misc.e + 1
  103. end]]
  104.  
  105.  
  106. -- End of setup
  107. testError = function(a,b) -- test error logging
  108.    logError(a,b)
  109. end
  110. local _checkValidPos = function(x,y)
  111.    if type(x) ~= "number" or type(y) ~= "number" or x < 1 or x > _setting.MaxX or y < 1 or y > _setting.MaxY then
  112.       return false
  113.    end
  114.    return true
  115. end
  116. local _table = function(x,y) -- internal table formatting
  117.    if _checkValidPos(x,y) == false then
  118.       logError(4,"_table incorrect usage: "..x..","..y..", Defaulting to 1,1")
  119.       return _table(1,1)
  120.    end
  121.    local result = ""
  122.    while #result+#tostring(x) < _misc.e do
  123.       result = result.."0"
  124.    end
  125.    result = result..x
  126.    while #result+#tostring(y) < _misc.e * 2 do
  127.       result = result.."0"
  128.    end
  129.    result = result..y
  130.    return result  
  131. end
  132. -- set functions
  133. set_t = function(x,y,char)
  134.    if type(x) == "number" and type(y) == "number" then
  135.       if #char ~= 1 then
  136.          logError(2,"set_t incorrect usage: character lenght ~= 1 at "..x..","..y)
  137.          char = char:sub(1,1)
  138.       end
  139.       if _checkValidPos(x,y) then
  140.          _t[_table(x,y)] = char
  141.       else
  142.          logError(2,"set_t incorrect usage: "..x..","..y..",")
  143.       end
  144.    else
  145.       logError(2,"set_t incorrect usage: x,y:")
  146.    end
  147. end
  148. set_c = function(x,y,color)
  149.    if type(x) == "number" and type(y) == "number" then
  150.       if #color ~= 1 then
  151.          logError(2,"set_c incorrect usage: character lenght ~= 1 at "..x..","..y)
  152.          char = char:sub(1,1)
  153.       end
  154.       if _color[color] == false then
  155.          logError(2,"set_c incorrect usage: color must be '0' - '9', 'a' to 'f' at "..x..","..y)
  156.       else
  157.          if _checkValidPos(x,y) then
  158.             _c[_table(x,y)] = color
  159.          else
  160.             logError(2,"set_c incorrect usage: "..x..","..y)
  161.          end
  162.       end
  163.    else
  164.       logError(2,"set_c incorrect usage: x,y:")
  165.    end
  166. end
  167. set_b = function(x,y,color)
  168.    if type(x) == "number" and type(y) == "number" then
  169.       if #color ~= 1 then
  170.          logError(2,"set_b incorrect usage: character lenght ~= 1 at "..x..","..y)
  171.          char = char:sub(1,1)
  172.       end
  173.       if _color[color] == false then
  174.          logError(2,"set_b incorrect usage: color must be '0' - '9', 'a' to 'f' at "..x..","..y)
  175.       else  
  176.          if _checkValidPos(x,y) then
  177.             _b[_table(x,y)] = color
  178.          else
  179.             logError(2,"set_b incorrect usage: "..x..","..y)
  180.          end
  181.       end
  182.    else
  183.       logError(2,"set_b incorrect usage: x,y")
  184.    end
  185. end
  186. set_text = function(x,y,text)
  187.    text = tostring(text)
  188.    if _checkValidPos(x,y) == false then
  189.       logError(2,"set_text incorrect usage: "..x..","..y)
  190.    else
  191.       if #text > _setting.MaxX-x+1 then
  192.          logError(1,"set_text: text too long: "..x..","..y)
  193.          text = text:sub(1,_setting.MaxX-x+1)
  194.       end
  195.       local n = 1
  196.       repeat
  197.          set_t(x+n-1,y,text:sub(n,n))
  198.          n = n + 1
  199.       until text:sub(n,n) == ""
  200.    end
  201. end
  202. set_t_all = function(char)
  203.    if type(char) == "string" and #char == 1 then
  204.       local y = 1
  205.       repeat
  206.          local x = 1
  207.          repeat
  208.             set_t(x,y,char)
  209.             x = x + 1
  210.          until x > _setting.MaxX
  211.          y = y + 1
  212.       until y > _setting.MaxY
  213.    else
  214.       logError(2,"set_t_all invalid usage: "..char)
  215.    end
  216. end
  217. set_color = function(color)
  218.    if _color[color] then
  219.       local y = 1
  220.       repeat
  221.          local x = 1
  222.          repeat
  223.             set_c(x,y,color)
  224.             x = x + 1
  225.          until x > _setting.MaxX
  226.          y = y + 1
  227.       until y > _setting.MaxY
  228.    else
  229.       logError(2,"set_color incorrect usage: '0' - '9', 'a' to 'f'")
  230.    end
  231. end
  232. set_background = function(color)
  233.    if _color[color] then
  234.       local y = 1
  235.       repeat
  236.          local x = 1
  237.          repeat
  238.             set_b(x,y,color)
  239.             x = x + 1
  240.          until x > _setting.MaxX
  241.          y = y + 1
  242.       until y > _setting.MaxY
  243.    else
  244.       logError(2,"set_background incorrect usage: '0' - '9', 'a' to 'f'")
  245.    end
  246. end
  247. set_clear = function()
  248.    set_t_all(" ")
  249.    set_color("0")
  250.    set_background("f")
  251. end
  252. set_t_line = function(xOrY,direction,char)
  253.    if type(xOrY) == "number" and type(direction) == "string" and type(char) == "string" and #char == 1 then
  254.       if direction == ("across" or "1") then
  255.          local x = 1
  256.          repeat
  257.             set_t(x,xOrY,char)
  258.             x = x + 1
  259.          until x > _setting.MaxX
  260.       elseif direction == ("down" or "0") then
  261.          local y = 1
  262.          repeat
  263.             set_t(xOrY,y,char)
  264.             y = y + 1
  265.          until y > _setting.MaxY
  266.       else
  267.          logError("set_t_line incorrect usage: direction: "..direction)
  268.       end
  269.    else
  270.       logError(2,"set_t_line incorrect usage: "..xOry..","..direction..","..char)
  271.    end
  272. end
  273. set_c_line = function(xOrY,direction,color)
  274.    if type(xOrY) == "number" and type(direction) == "string" and type(char) == "string" and #color == 1 then
  275.       if direction == ("across" or "1") then
  276.          local x = 1
  277.          repeat
  278.             set_c(x,xOrY,color)
  279.             x = x + 1
  280.          until x > _setting.MaxX
  281.       elseif direction == ("down" or "0") then
  282.          local y = 1
  283.          repeat
  284.             set_c(xOrY,y,color)
  285.             y = y + 1
  286.          until y > _setting.MaxY
  287.       else
  288.          logError("set_c_line incorrect usage: direction: "..direction)
  289.       end
  290.    else
  291.       logError(2,"set_c_line incorrect usage: "..xOry..","..direction..","..char)
  292.    end
  293. end
  294. set_b_line = function(xOrY,direction,color)
  295.    if type(xOrY) == "number" and type(direction) == "string" and type(char) == "string" and #color == 1 then
  296.       if direction == ("across" or "1") then
  297.          local x = 1
  298.          repeat
  299.             set_b(x,xOrY,color)
  300.             x = x + 1
  301.          until x > _setting.MaxX
  302.       elseif direction == ("down" or "0") then
  303.          local y = 1
  304.          repeat
  305.             set_b(xOrY,y,color)
  306.             y = y + 1
  307.          until y > _setting.MaxY
  308.       else
  309.          logError("set_b_line incorrect usage: direction: "..direction)
  310.       end
  311.    else
  312.       logError(2,"set_b_line incorrect usage: "..xOry..","..direction..","..char)
  313.    end
  314. end
  315. set_t_area = function(x1,y1,x2,y2,char)
  316.    if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
  317.       if type(char) == "string" and #char == 1 then
  318.          repeat
  319.             local x = x1
  320.             repeat
  321.                set_t(x,y1,char)
  322.                x = x + 1
  323.             until x > x2
  324.             y1 = y1 + 1
  325.          until y1 > y2
  326.       else
  327.          logError(2,"set_t_area incorrect usage: char ~= 1: "..char)
  328.       end
  329.    else
  330.       logError(2,"set_t_area incorrect usage: "..x1..","..y1..","..x2..","..y2)
  331.    end
  332. end
  333. set_c_area = function(x1,y1,x2,y2,color)
  334.    if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
  335.       if type(char) == "string" and #char == 1 and _color[color] then
  336.          repeat
  337.             local x = x1
  338.             repeat
  339.                set_c(x,y1,color)
  340.                x = x + 1
  341.             until x > x2
  342.             y1 = y1 + 1
  343.          until y1 > y2
  344.       else
  345.          logError(2,"set_c_area incorrect usage: color ~= 1 or color does not exist: "..char)
  346.       end
  347.    else
  348.       logError(2,"set_c_area incorrect usage: "..x1..","..y1..","..x2..","..y2)
  349.    end
  350. end
  351. set_b_area = function(x1,y1,x2,y2,color)
  352.    if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
  353.       if type(char) == "string" and #char == 1 and _color[color] then
  354.          repeat
  355.             local x = x1
  356.             repeat
  357.                set_b(x,y1,color)
  358.                x = x + 1
  359.             until x > x2
  360.             y1 = y1 + 1
  361.          until y1 > y2
  362.       else
  363.          logError(2,"set_b_area incorrect usage: color ~= 1 or color does not exist: "..char)
  364.       end
  365.    else
  366.       logError(2,"set_b_area incorrect usage: "..x1..","..y1..","..x2..","..y2)
  367.    end
  368. end
  369. -- draw functions
  370. local _draw = function(x,y)
  371.    if _checkValidPos(x,y) then
  372.       local a = _table(x,y)
  373.       term.setCursorPos(x,y)
  374.       if _t[a] == nil or _t[a] == "" then
  375.          _t[a] = " "
  376.       end
  377.       if _setting.color and _c[a] and _b[a] then
  378.          term.blit(_t[a],_c[a],_b[a])
  379.       else
  380.          term.blit(_t[a],"0","f")
  381.       end
  382.    else
  383.       logError(4,"_draw incorrect usage: "..x..","..y)
  384.    end
  385. end
  386. draw_pixel = function(x,y)
  387.    if _checkValidPos(x,y) then
  388.       _draw(x,y)
  389.    end
  390. end
  391. draw_part = function(x1,y1,x2,y2)
  392.    if _checkValidPos(x1,y1) and _checkValidPos(x2,y2) and x2+1 > x1 and y2+1 > y1 then
  393.       repeat
  394.          local x = x1
  395.          repeat
  396.             _draw(x,y1)
  397.             x = x + 1
  398.          until x > x2
  399.          y1 = y1 + 1
  400.       until y1 > y2
  401.    else
  402.       logError(2,"draw_part invalid usage: "..x1..","..y1..","..x2..","..y2)
  403.    end
  404. end
  405. draw_all = function()
  406.    draw_part(1,1,_setting.MaxX,_setting.MaxY)
  407. end
  408. -- file handling functions
  409. raw_save = function(path)
  410.    local file = fs.open(path.."._f","w")
  411.    local mtable = {}
  412.    mtable.t = _t
  413.    mtable.c = _c
  414.    mtable.b = _b
  415.    file.writeLine(textutils.serialize(mtable))
  416.    file.close()
  417. end
  418. raw_load = function(path)
  419.    local file = fs.open(path.."._f","r")
  420.    local mtable = textutils.unserialize(file.readAll())
  421.    _t = mtable.t
  422.    _c = mtable.c
  423.    _b = mtable.b
  424. end
  425.  
  426. --Aliases:
  427. draw = draw_pixel
  428. set_col = set_color
  429. set_bkg = set_background
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement