mast3rillusion

graphics.lua

Jul 27th, 2021 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.69 KB | None | 0 0
  1. button = {
  2.     bkgColor = colors.blue,
  3.     tColor = colors.white,
  4.     text = "Text",
  5.     w = 6,
  6.     h = 3,
  7.     textx = 1,
  8.     texty = 1,
  9.     x = 2,
  10.     y = 2,
  11.     visible = false,
  12.    
  13.     trigger = function()
  14.         print("Default trigger function. :)")
  15.     end,
  16.    
  17.     new = function(self)
  18.         local new = {}
  19.         setmetatable(new, {__index = self})
  20.         return new
  21.     end,
  22.    
  23.     detect = function(self, x, y, trig)
  24.         if self.visible then
  25.             if x >= self.x and x <= (self.x + self.w) and y >= self.y and y <= (self.y + self.h) then
  26.                 if trig then
  27.                     self.trigger()
  28.                 end
  29.                 return true
  30.             end
  31.         end
  32.     end,
  33.    
  34.     draw = function(self)
  35.         local x = self.x
  36.         local y = self.y
  37.         local bkgColor = self.bkgColor
  38.         local tColor = self.tColor
  39.         local text = self.text
  40.         local w = self.w
  41.         local h = self.h
  42.         local textx = self.textx
  43.         local texty = self.texty
  44.         local tColor = self.tColor
  45.         local oldBkgColor = term.getBackgroundColor()
  46.         local oldTColor = term.getTextColor()
  47.         term.setBackgroundColor(bkgColor)
  48.         term.setTextColor(tColor)
  49.         term.setCursorPos(x, y)
  50.         for i=0, h do
  51.             for i=0, w - 1 do
  52.                 term.write(" ")
  53.             end
  54.             term.setCursorPos(x, y + i)
  55.         end
  56.         term.setCursorPos(x + textx, y + texty)
  57.         term.setTextColor(tColor)
  58.         term.write(text)
  59.         term.setBackgroundColor(oldBkgColor)
  60.         term.setTextColor(oldTColor)
  61.         self.visible = true
  62.         return true
  63.     end
  64. }
  65.  
  66. label = {
  67.     bkgColor = colors.black,
  68.     tColor = colors.white,
  69.     text = "Label",
  70.     x = 2,
  71.     y = 2,
  72.    
  73.     new = function(self)
  74.         local new = {}
  75.         setmetatable(new, {__index = self})
  76.         return new
  77.     end,
  78.    
  79.     draw = function(self)
  80.         local bkgColor = self.bkgColor
  81.         local tColor = self.tColor
  82.         local text = self.text
  83.         local x = self.x
  84.         local y = self.y
  85.         local oldBkgColor = term.getBackgroundColor()
  86.         local oldTColor = term.getTextColor()
  87.         term.setCursorPos(x, y)
  88.         term.setBackgroundColor(bkgColor)
  89.         term.setTextColor(tColor)
  90.         term.write(text)
  91.         term.setBackgroundColor(oldBkgColor)
  92.         term.setTextColor(oldTColor)
  93.         return true
  94.     end,
  95.    
  96.     drawCentered = function(self, xoff)
  97.         local bkgColor = self.bkgColor
  98.         local tColor = self.tColor
  99.         local text = self.text
  100.         local termw, termh = term.getSize()
  101.         local oldBkgColor = term.getBackgroundColor()
  102.         local oldTColor = term.getTextColor()
  103.        
  104.         local x = termw / 2 - #text / 2
  105.         if type(xoff) == "number" then
  106.             x = x + xoff
  107.         end
  108.         term.setCursorPos(x, self.y)
  109.         term.setBackgroundColor(bkgColor)
  110.         term.setTextColor(tColor)
  111.         term.write(text)
  112.         term.setBackgroundColor(oldBkgColor)
  113.         term.setTextColor(oldTColor)
  114.         return true
  115.     end,
  116.    
  117.     drawCornered = function(self, horcorner, vercorner)
  118.         local bkgColor = self.bkgColor
  119.         local tColor = self.tColor
  120.         local text = self.text
  121.         local x = self.x
  122.         local y = self.y
  123.         local oldBkgColor = term.getBackgroundColor()
  124.         local oldTColor = term.getTextColor()
  125.         local w, h = term.getSize()
  126.        
  127.         if vercorner == "top" then
  128.             if horcorner == "left" then
  129.                 local termx, termy = 1, 1
  130.                 term.setCursorPos(termx, termy)
  131.                 term.setBackgroundColor(bkgColor)
  132.                 term.setTextColor(tColor)
  133.                 term.write(text)
  134.                 term.setBackgroundColor(oldBkgColor)
  135.                 term.setTextColor(oldTColor)
  136.                 return true
  137.             elseif horcorner == "right" then
  138.                 local termx = w - #text + 1
  139.                 local termy = 1
  140.                 term.setCursorPos(termx, termy)
  141.                 term.setBackgroundColor(bkgColor)
  142.                 term.setTextColor(tColor)
  143.                 term.write(text)
  144.                 term.setBackgroundColor(oldBkgColor)
  145.                 term.setTextColor(oldTColor)
  146.                 return true
  147.             end
  148.             return false
  149.         elseif vercorner == "bottom" then
  150.             if horcorner == "left" then
  151.                 local termx = 1
  152.                 local termy = h
  153.                 term.setCursorPos(termx, termy)
  154.                 term.setBackgroundColor(bkgColor)
  155.                 term.setTextColor(tColor)
  156.                 term.write(text)
  157.                 term.setBackgroundColor(oldBkgColor)
  158.                 term.setTextColor(oldTColor)
  159.                 return true
  160.             elseif horcorner == "right" then
  161.                 local termx = w - #text + 1
  162.                 local termy = h
  163.                 term.setCursorPos(termx, termy)
  164.                 term.setBackgroundColor(bkgColor)
  165.                 term.setTextColor(tColor)
  166.                 term.write(text)
  167.                 term.setBackgroundColor(oldBkgColor)
  168.                 term.setTextColor(oldTColor)
  169.                 return true
  170.             end
  171.             return false
  172.         end
  173.         return false
  174.     end
  175. }
  176.  
  177. checkbox = {
  178.     text = "Check Box",
  179.     x = 2,
  180.     y = 2,
  181.     checked = false,
  182.     bkgColor = colors.black,
  183.     w = 0,
  184.     h = 0,
  185.    
  186.     new = function(self)
  187.         local new = {}
  188.         setmetatable(new, {__index = self})
  189.         return new
  190.     end,
  191.    
  192.     draw = function(self)
  193.         local bkgColor = self.bkgColor
  194.         local text = self.text
  195.         local x = self.x
  196.         local y = self.y
  197.         term.setCursorPos(x, y)
  198.         if self.checked == false then
  199.             term.setBackgroundColor(colors.gray)
  200.             term.write(" ")
  201.         else
  202.             term.setBackgroundColor(colors.blue)
  203.             term.write(" ")
  204.         end
  205.         term.setBackgroundColor(bkgColor)
  206.         term.write(" ")
  207.         term.write(text)
  208.         w = 2 + string.len(text)
  209.         h = 1
  210.     end,
  211.  
  212.     trigger = function()
  213.         print("Default trigger function. :)")
  214.     end,
  215.    
  216.     detect = function(self, x, y, trig)
  217.         if x >= self.x and x <= (self.x + self.w) and y >= self.y and y <= (self.y + self.h) then
  218.             if self.checked == false then
  219.                 self.checked = true
  220.                 self:draw()
  221.                 if trig then
  222.                     self.trigger()
  223.                 end
  224.             else
  225.                 self.checked = false
  226.                 self:draw()
  227.                 if trig then
  228.                     self.trigger()
  229.                 end
  230.             end
  231.         end
  232.     end,
  233. }
  234.  
  235. backgroundimg = {
  236.     path = "",
  237.    
  238.     new = function(self)
  239.         local new = {}
  240.         setmetatable(new, {__index = self})
  241.         return new
  242.     end,
  243.    
  244.     draw = function(self)
  245.         local image = paintutils.loadImage(self.path)
  246.         paintutils.drawImage(image, 1, 1)
  247.     end
  248. }
  249.  
  250. textinput = {
  251.     bkgColor = colors.white,
  252.     tColor = colors.black,
  253.     phColor = colors.gray,
  254.     x = 2,
  255.     y = 2,
  256.     text = "",
  257.     placeholder = "",
  258.     pwdchar = nil,
  259.     w = 10,
  260.  
  261.     new = function(self)
  262.         local new = {}
  263.         setmetatable(new, {__index = self})
  264.         return new
  265.     end,
  266.    
  267.     draw = function(self)
  268.         term.setCursorPos(self.x, self.y)
  269.         term.setBackgroundColor(self.bkgColor)
  270.         term.setTextColor(self.phColor)
  271.         for i=1, self.w do
  272.             term.write(" ")
  273.         end
  274.         term.setCursorPos(self.x, self.y)
  275.         term.write(self.placeholder)
  276.         return true
  277.     end,
  278.    
  279.     detect = function(self, x, y)
  280.         if x >= self.x and x <= (self.x + self.w) and y == self.y then
  281.             term.setCursorPos(self.x, self.y)
  282.             term.setBackgroundColor(self.bkgColor)
  283.             term.setTextColor(self.tColor)
  284.             for i=1, self.w do
  285.                 term.write(" ")
  286.             end
  287.             term.setCursorPos(self.x, self.y)
  288.             self.text = self.pwdchar ~= nil and read(self.pwdchar) or read()
  289.         end
  290.     end
  291.    
  292. }
  293.  
  294. function fillScreen(color)
  295.     term.clear()
  296.     term.setCursorPos(1, 1)
  297.     local w, h = term.getSize()
  298.     paintutils.drawFilledBox(1, 1, w, h, color)
  299. end
  300.  
Add Comment
Please, Sign In to add comment