Advertisement
programcreator

TheOS: GUI (Not Interact)

Apr 2nd, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. --[[
  2.     GUI API in ComputerCraft
  3.     by Creator
  4.     Complete rewrite in OOP
  5. ]]--
  6.  
  7. --Button Class--
  8.  
  9. Button = {}
  10.  
  11. newButton = function(_name,_label,_xPos,_yPos,_xLength,_yLength,_fgColor,_bgColor,_returnValue)
  12.     local self = {}
  13.     setmetatable(self,{__index = Button})
  14.     self.name = _name
  15.     self.label = _label
  16.     self.xPos = _xPos
  17.     self.yPos = _yPos
  18.     self.fgColor = _fgColor
  19.     self.bgColor = _bgColor
  20.     self.xLength = _xLength
  21.     self.yLength = _yLength
  22.     self.returnValue = _returnValue
  23.     return self
  24. end
  25.  
  26. Button.draw = function(self)
  27.     --term.setBackgroundColor(self.bgColor)
  28.     paintutils.drawFilledBox(self.xPos,self.yPos,self.xLength-1,self.yLength-1,self.bgColor)
  29.    
  30. end
  31.  
  32. --Variables--
  33. --Functions--
  34.  
  35. function addKey(key,returnValue)
  36.     keysMain[#keysMain+1] = {key,returnValue}
  37. end
  38.  
  39. function resetKeys()
  40.     keysMain = {}
  41. end
  42.  
  43. local function localAddField(minX,minY,maxX,maxY,returnV)
  44.     buttonsMain[#buttonsMain+1] = {minX,minY,minX+maxX-1,minY+maxY-1,returnV}
  45. end
  46.  
  47. function addField(minX,minY,maxX,maxY,returnV)
  48.     if not pcall(localAddField,minX,minY,maxX,maxY,returnV) then
  49.         print([[
  50.         Usage:
  51.         As an argument pass a table structured like this:
  52.         {
  53.             xPos,
  54.             yPos,
  55.             wide,
  56.             height,
  57.             returnValue,
  58.         }
  59.         ]])
  60.     end
  61. end
  62.  
  63. local function localDrawButton(xPos,yPos,wide,height,colorOfButton,inButtonXPosOfLabel,inButtonYPosOfLabel,label,textColor,returnValue)
  64.     if colorOfButton ~= nil then
  65.         paintutils.drawFilledBox(xPos,yPos,xPos+wide-1,yPos+height-1,colorOfButton)
  66.     end
  67.     term.setTextColor(textColor)
  68.     term.setCursorPos(xPos+inButtonXPosOfLabel-1,yPos+inButtonYPosOfLabel-1)
  69.     local textToPrint = string.sub(label,1,xPos+wide-inButtonXPosOfLabel)
  70.     term.write(textToPrint)
  71.     buttonsMain[#buttonsMain+1] = {xPos,yPos,xPos+wide-1,yPos+height-1,returnValue}
  72. end
  73.  
  74. function drawButton(xPos,yPos,wide,height,colorOfButton,inButtonXPosOfLabel,inButtonYPosOfLabel,label,textColor,returnValue)
  75.     if not pcall(localDrawButton,xPos,yPos,wide,height,colorOfButton,inButtonXPosOfLabel,inButtonYPosOfLabel,label,textColor,returnValue) then
  76.         print([[
  77.         Usage:
  78.         As an argument pass a table structured like this:
  79.         {
  80.             xPos,
  81.             yPos,
  82.             wide,
  83.             height,
  84.             colorOfButton,
  85.             inButtonXPosOfLabel,
  86.             inButtonYPosOfLabel,
  87.             label,
  88.             textColor,
  89.             returnValue,
  90.         }
  91.         You can have the option to not use the background color.
  92.         You can do this by simply setting color of button to nil.
  93.         ]])
  94.     end
  95. end
  96.  
  97. function detectButtonOrKeyHit()
  98.     while true do
  99.         local event, button, x, y
  100.         repeat
  101.             event, button, x, y = os.pullEvent()
  102.         until (event == "mouse_click" and buttonsMain ~= nil) or (event == "key" and keysMain ~= nil)
  103.         if event == "mouse_click" then
  104.             for i,v in pairs(buttonsMain) do
  105.                 if v[1] <= x and x <= v[3] and v[2] <= y and y <= v[4] then
  106.                     return {v[5], button, x, y}
  107.                 end
  108.             end
  109.         elseif event == "key" then
  110.             for i,v in pairs(keysMain) do
  111.                 if button == v[1] then
  112.                     return {v[2]}
  113.                 end
  114.             end
  115.         end
  116.     end
  117. end
  118.  
  119. function resetButtons()
  120.     buttonsMain = {}
  121. end
  122.  
  123. local function localCPrint(txt,color,bgColor,xPos,yPos,maxL)
  124.     if not maxL == nil then
  125.         txt = string.sub(txt,1,maxL)
  126.     end
  127.     term.setTextColor(color)
  128.     if bgColor ~= nil then
  129.         term.setBackgroundColor(bgColor)
  130.     end
  131.     term.setCursorPos(xPos,yPos)
  132.     term.write(txt)
  133. end
  134.  
  135. function cPrint(txt,color,bgColor,xPos,yPos,maxL)
  136.     if not pcall(localCPrint,txt,color,bgColor,xPos,yPos,maxL) then
  137.         print([[
  138.             Usage:
  139.             Arguments are as follws:
  140.             {
  141.             Text to print
  142.             Text color
  143.             Background color
  144.             xPos
  145.             yPos
  146.             Maximal lenght of the string
  147.             }
  148.             Maximal lenght of the string can be left "nil" with qoutes
  149.         ]])
  150.     end
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement