Advertisement
0xSRK6

QGUILib

Feb 23rd, 2025 (edited)
248
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 1 0
  1. local lib = {backgroundColor = colors.black, redir = term.native()}
  2. local logLine = 1
  3. function lib.puWrap(f, ...)
  4.     term.current(lib.redir)
  5.     f(...)
  6.     term.setBackgroundColor(lib.backgroundColor)
  7. end
  8.  
  9. function lib.print(...)
  10.     term.redirect(term.native())
  11.     local _, maxLine = term.getSize()
  12.     if logLine < maxLine then
  13.         term.setCursorPos(1, logLine)
  14.         print(...)
  15.         logLine = logLine + 1
  16.     else
  17.        term.scroll(1)
  18.        term.setCursorPos(1, logLine)
  19.        print(...)
  20.     end
  21.     term.redirect(lib.redir)
  22. end
  23.  
  24. local function createElement(x, y)
  25.     x = x or 1
  26.     y = y or 1
  27.  
  28.     local self = {x = x, y = y}
  29.  
  30.     local draw = function() lib.print("drawed at " .. self.x .. ", " .. self.y) end
  31.     local getPosition = function() return self.x, self.y end
  32.     local setPosition = function(xNew, yNew) self.x = xNew; self.y = yNew end
  33.  
  34.     return {
  35.         draw = draw,
  36.         getPosition = getPosition,
  37.         setPosition = setPosition
  38.     }
  39.  
  40. end
  41.  
  42. function lib.newPixel(x, y, color)
  43.     x = x or 1
  44.     y = y or 1
  45.     color = color or colors.white
  46.  
  47.     local self = {color = color, xprev = nil, yprev = nil}
  48.  
  49.     local o = {}
  50.     setmetatable(o, {__index = createElement(x, y)})
  51.     local super = getmetatable(o).__index
  52.  
  53.     o.setColor = function(newColor) self.color = newColor end
  54.     o.getColor = function() return self.color end
  55.     o.draw = function()
  56.         if self.xprev then lib.puWrap(paintutils.drawPixel, self.xprev, self.yprev, lib.backgroundColor) end
  57.         super.draw()
  58.         local xcur, ycur = o.getPosition()
  59.         lib.puWrap(paintutils.drawPixel, xcur, ycur, self.color)
  60.     end
  61.     o.setPosition = function(xNew, yNew)
  62.         self.xprev, self.yprev = o.getPosition()
  63.         super.setPosition(xNew, yNew)
  64.     end
  65.  
  66.  
  67.     return o
  68. end
  69.  
  70.  
  71.  
  72. function lib.init()
  73.     term.redirect(term.native())
  74.     term.setBackgroundColor(lib.backgroundColor)
  75.     term.clear()
  76.     term.redirect(lib.redir)
  77.     term.setBackgroundColor(lib.backgroundColor)
  78.     term.clear()
  79. end
  80. return lib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement