newcat

My version of account.username's GUI API

Aug 25th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.49 KB | None | 0 0
  1. -- GUI API
  2.  
  3. black = colors.black
  4. white = colors.white
  5. lightBlue = colors.lightBlue
  6. green = colors.green
  7. yellow = colors.yellow
  8. blue = colors.blue
  9. purple = colors.purple
  10. magenta = colors.magenta
  11. lime = colors.lime
  12. orange = colors.orange
  13. red = colors.red
  14. brown = colors.brown
  15. cyan = colors.cyan
  16. pink = colors.pink
  17. grey = colors.gray
  18. gray = colors.gray
  19. lightGray = colors.lightGray
  20. lightGrey = colors.lightGray
  21.  
  22. -- Buttons
  23.  
  24. buttonList = {}
  25. Buttons = {}
  26. Buttons.__index = Buttons
  27.  
  28. function createButton( name, func )
  29.     button = {}
  30.     setmetatable(button,Buttons)
  31.     button.name = name
  32.     button.action = func
  33.     return button
  34. end
  35.  
  36. function Buttons:toggle( newColor,sec )
  37.  
  38.     self:draw( self.x,self.y,self.width,newColor,self.tcolor)
  39.  
  40.     if sec ~= nil then
  41.         sleep(sec)
  42.         self:draw( self.x,self.y,self.width,self.color,self.tcolor )
  43.     end
  44. end
  45.  
  46.  
  47. function Buttons:draw( x,y,width,color,tcolor )
  48.  
  49.     table.insert(buttonList,{self.name,x,y,width,self.action})
  50.    
  51.     self.x = x
  52.     self.y = y
  53.     self.width = width
  54.     if self.tcolor == nil then
  55.         self.color = color
  56.     end
  57.     self.tcolor = tcolor
  58.  
  59.     for i = 1,width do
  60.         paintutils.drawLine(x, y + i, x + #self.name + 1, y + i, color)
  61.     end
  62.  
  63.     term.setCursorPos(x,y + math.ceil(width/2))
  64.     term.setTextColor(tcolor)
  65.     term.setBackgroundColor(color)
  66.     term.write(" "..self.name.." ")
  67. end
  68.  
  69. function Buttons:trigger()
  70.     buttonList[i][5]()
  71. end
  72.  
  73. function Buttons:remove()
  74.     for i = 1,#buttonList do
  75.         if self.name == buttonList[i][1] then
  76.             table.remove(buttonList,i)
  77.             return true
  78.         end
  79.     end
  80. end
  81.  
  82. function detect( x,y,trigger )
  83.     for i = 1,#buttonList do
  84.         if x >= buttonList[i][2] and x <= buttonList[i][2] + #buttonList[i][1] and y >= buttonList[i][3] and y <= buttonList[i][3] + buttonList[i][4] then
  85.             if trigger == true then
  86.                 buttonList[i][5]()
  87.             end
  88.             return buttonList[i][1]
  89.         end
  90.     end
  91. end
  92.  
  93. -- Progress Bars
  94.  
  95. barList = {}
  96. Bars = {}
  97. Bars.__index = Bars
  98.  
  99. function createBar( name )
  100.     bar = {}
  101.     setmetatable(bar,Bars)
  102.     bar.name = name
  103.     return bar
  104. end
  105.  
  106. function Bars:setup( x,y,length,color,pcolor,disp,dispbcolor,tcolor )
  107.     self.x,self.y,self.length,self.color,self.pcolor,self.disp,self.dispbcolor,self.tcolor = x,y,length,color,pcolor,disp,dispbcolor,tcolor
  108. end
  109.  
  110. function Bars:draw( x,y,length,color,pcolor,disp,dispbcolor,tcolor )
  111.    
  112.     if self.x == nil and x ~= nil then
  113.         self.x,self.y,self.length,self.color,self.pcolor,self.disp,self.dispbcolor,self.tcolor = x,y,length,color,pcolor,disp,dispbcolor,tcolor
  114.     end
  115.  
  116.     self.percent = 0
  117.     term.setCursorPos(x,y)
  118.     paintutils.drawLine(x,y,x+ length,y,color)
  119.     term.setCursorPos(x,y)
  120.     if self.percent > 0 then
  121.         paintutils.drawLine(x,y,x + length*(self.percent/100),y,pcolor)
  122.     end
  123.  
  124.    
  125.     percentString = tostring(self.percent).."%"
  126.     if disp == true then
  127.         term.setCursorPos(math.max(x + math.ceil(length/2) - math.ceil(#percentString/2),0) + 1,y-1)
  128.         term.setBackgroundColor(dispbcolor)
  129.         term.setTextColor(tcolor)
  130.         write(percentString)
  131.     end
  132. end
  133.  
  134. function Bars:update( percent )
  135.     self.percent = percent
  136.     term.setCursorPos(self.x,self.y)
  137.     paintutils.drawLine(self.x,self.y,self.x + self.length,self.y,self.color)
  138.     term.setCursorPos(self.x,self.y)
  139.     if self.percent > 0 then
  140.         paintutils.drawLine(self.x,self.y,self.x + self.length*(self.percent/100),self.y,self.pcolor)  
  141.     end
  142.  
  143.    
  144.     percentString = tostring(self.percent).."%"
  145.     if self.disp == true then
  146.         term.setCursorPos(math.max(self.x + math.ceil(self.length/2) - math.ceil(#percentString/2),0) + 1,self.y-1)
  147.         term.setBackgroundColor(self.dispbcolor)
  148.         term.setTextColor(self.tcolor)
  149.         write(percentString)
  150.     end
  151. end
  152.  
  153. -- Textboxs
  154.  
  155. function newTextBox(x,y,length,bcolor,tcolor)
  156.     typed = {}
  157.     alphabet = "abcdefghijklmnopqrstuvwxyz"
  158.     local shift = false
  159.  
  160.     term.setBackgroundColor(bcolor)
  161.     paintutils.drawLine(x, y, x + length - 1, y, bcolor)
  162.     term.setCursorPos(x, y)
  163.     term.setTextColor(tcolor)
  164.     term.write("_")
  165.  
  166.     typing = true
  167.     term.setCursorPos(x,y)
  168.     while typing do
  169.         event,key = os.pullEvent()
  170.         if event == "key" then
  171.             if key >= 2 and key <= 10 then
  172.                 table.insert(typed,tonumber(key)-1)
  173.             elseif key == "10" then
  174.                 table.insert(typed, "0")
  175.             elseif keys.getName(key) == "enter" then
  176.                 typing = false
  177.                 return string.gsub(table.concat(typed,""),"space"," ")
  178.             elseif keys.getName(key) == "space" then
  179.                 table.insert(typed,keys.getName(key))
  180.             elseif keys.getName(key) == "period" then
  181.                 table.insert(typed,".")
  182.             elseif keys.getName(key) == "comma" then
  183.                 table.insert(typed,",")
  184.             elseif keys.getName(key) == "backspace" then
  185.                 table.remove(typed,#typed)
  186.             elseif keys.getName(key) == "leftShift" or keys.getName(key) == "rightShift" then
  187.                 if shift == true then shift = false else shift = true end
  188.             else
  189.                 key = keys.getName(key)
  190.                 if string.find(alphabet,key) ~= nil then
  191.                     if shift == false then
  192.                         table.insert(typed,key)
  193.                     else
  194.                         table.insert(typed, string.char(string.byte(key) - 32))
  195.                     end
  196.                 end
  197.             end
  198.             if #typed > length then
  199.                 table.remove(typed,#typed)
  200.             else
  201.                 cx,cy = term.getCursorPos()
  202.                 term.setBackgroundColor(bcolor)
  203.                 paintutils.drawLine(x, y, x + length - 1, y, bcolor)
  204.                 term.setCursorPos(x,y)
  205.                 term.write(string.gsub(table.concat(typed,""),"space"," "))
  206.                 if cx < x + length then
  207.                     term.write("_")
  208.                 end
  209.             end
  210.         end
  211.     end
  212. end
  213.  
  214. -- Boxes
  215.  
  216. Boxs = {}
  217. Boxs.__index = Boxs
  218.  
  219. function createDialogueBox( title,body,boxType )
  220.     boxes = {}
  221.     setmetatable(boxes,Boxs)
  222.     boxes.title = title
  223.     boxes.body = body
  224.     boxes.boxType = boxType
  225.     return boxes
  226. end
  227.  
  228. function Boxs:draw( x,y,width,color,bcolor,tcolor )
  229.     self.width = width
  230.     self.x = x
  231.     self.y = y
  232.     local ret = nil
  233.  
  234.     if self.boxType == "yn" then                                                                      -- YN Box
  235.  
  236.         if type(self.body) ~= "table" then
  237.             paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
  238.             term.setCursorPos(x,y)
  239.             term.setTextColor(tcolor)
  240.             write(self.title)
  241.  
  242.             self.len = #self.body
  243.  
  244.             for i = 1,width do
  245.                 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
  246.             end
  247.  
  248.             term.setCursorPos(x + 1, y + 2)
  249.             term.write(self.body)
  250.  
  251.  
  252.             term.setCursorPos(x + 1,y + width)
  253.             term.setTextColor(tcolor)
  254.             term.setBackgroundColor(green)
  255.             write(" Yes ")
  256.  
  257.             term.setCursorPos(x + len - 4,y + width)
  258.             term.setBackgroundColor(red)
  259.             write(" No ")
  260.  
  261.             repeat
  262.                 event,click,cx,cy = os.pullEvent("mouse_click")
  263.  
  264.                 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
  265.                     ret = true
  266.                 elseif cx >= x + len - 5 and cx <= x + len - 1  and cy == y + width then
  267.                     ret = false
  268.                 end
  269.             until ret ~= nil
  270.  
  271.         else
  272.  
  273.             len = 0
  274.             for i = 1,#self.body do
  275.                 if #self.body[i] > len then
  276.                     len = #self.body[i]
  277.                 end
  278.             end
  279.  
  280.             paintutils.drawLine(x, y, x + len + 1, y, bcolor)
  281.             term.setCursorPos(x,y)
  282.             term.setTextColor(tcolor)
  283.             write(self.title)
  284.  
  285.             for i = 1,width do
  286.                 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
  287.             end
  288.  
  289.             for i = 1,#self.body do
  290.                 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
  291.                 term.write(self.body[i])
  292.             end
  293.  
  294.             term.setCursorPos(x + 1,y + width)
  295.             term.setTextColor(tcolor)
  296.             term.setBackgroundColor(green)
  297.             write(" Yes ")
  298.  
  299.             term.setCursorPos(x + len - 4,y + width)
  300.             term.setBackgroundColor(red)
  301.             write(" No ")
  302.  
  303.             repeat
  304.                 event,click,cx,cy = os.pullEvent("mouse_click")
  305.  
  306.                 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
  307.                     ret = true
  308.                 elseif cx >= x + len - 5 and cx <= x + len - 1  and cy == y + width then
  309.                     ret = false
  310.                 end
  311.             until ret ~= nil
  312.  
  313.  
  314.             self.len = len
  315.         end
  316.  
  317.     elseif self.boxType == "ok" then                                                                  -- Ok Box
  318.         if type(self.body) ~= "table" then
  319.  
  320.             paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
  321.             term.setCursorPos(x,y)
  322.             term.setTextColor(tcolor)
  323.             write(self.title)
  324.             self.len = #self.body
  325.  
  326.             for i = 1,width do
  327.                 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
  328.             end
  329.  
  330.             term.setCursorPos(x + 1, y + 2)
  331.             term.write(self.body)
  332.  
  333.             term.setCursorPos(x + (self.len/2) - 1,y + width)
  334.             term.setTextColor(tcolor)
  335.             term.setBackgroundColor(green)
  336.             write(" Ok ")
  337.  
  338.            
  339.             repeat
  340.                 event,click,cx,cy = os.pullEvent("mouse_click")
  341.  
  342.                 if cx > x + (self.len/2 - 4) and cx < x + (self.len/2) and cy == y + width then
  343.                     ret = true
  344.                 end
  345.             until ret == true
  346.  
  347.         else
  348.            
  349.             len = 0
  350.             for i = 1,#self.body do
  351.                 if #self.body[i] > len then
  352.                     len = #self.body[i]
  353.                 end
  354.             end
  355.             self.len = len
  356.  
  357.             paintutils.drawLine(x, y, x + len + 1, y, bcolor)
  358.             term.setCursorPos(x,y)
  359.             term.setTextColor(tcolor)
  360.             write(self.title)
  361.  
  362.             for i = 1,width do
  363.                 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
  364.             end
  365.  
  366.             for i = 1,#self.body do
  367.                 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
  368.                 term.write(self.body[i])
  369.             end
  370.  
  371.             term.setCursorPos(x + (self.len/2) - 1,y + width)
  372.             term.setTextColor(tcolor)
  373.             term.setBackgroundColor(green)
  374.             write(" Ok ")
  375.            
  376.             repeat
  377.                 event,click,cx,cy = os.pullEvent("mouse_click")
  378.  
  379.                 if cx > x + (len/2 - 4) and cx < x + (len/2) and cy == y + width then
  380.                     ret = true
  381.                 end
  382.             until ret == true
  383.         end
  384.     end
  385.     return ret
  386. end
  387.  
  388. function Boxs:clear( color )
  389.     paintutils.drawLine(self.x, self.y, self.x + self.len + 1, self.y, color)
  390.     for i = 1,self.width do
  391.         paintutils.drawLine(self.x, self.y + i, self.x + self.len + 1, self.y + i, color)
  392.     end
  393. end
Advertisement
Add Comment
Please, Sign In to add comment