Advertisement
kizz12

guiapi

Apr 22nd, 2015
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.59 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.        
  34.         return button
  35. end
  36.  
  37. function Buttons:toggle( newColor,sec )
  38.  
  39.         self:draw( self.x,self.y,self.width,newColor,self.tcolor)
  40.  
  41.         if sec ~= nil then
  42.                 sleep(sec)
  43.                 self:draw( self.x,self.y,self.width,self.color,self.tcolor )
  44.         end
  45. end
  46.  
  47.  
  48. function Buttons:draw( x,y,width,color,tcolor )
  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.         self.VarName=vName
  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.                 end
  78.         end
  79.         self = nil
  80. end
  81.  
  82. function detect( trigger,tggl )
  83. bClickEvent,bclick,bx,by = os.pullEvent("mouse_click")
  84.     for i = 1,#buttonList do
  85.         if bx >= buttonList[i][2] and bx <= buttonList[i][2] + #buttonList[i][1] and by >= buttonList[i][3] and by <= buttonList[i][3] + buttonList[i][4]  and bclick==1 then
  86.         term.clear()
  87.         term.setBackgroundColor(colors.black)
  88.             if trigger == true then
  89.                 buttonList[i][5]()
  90.             end
  91.         return
  92.         end
  93.     end
  94. end
  95.  
  96. -- Progress Bars
  97.  
  98. barList = {}
  99. Bars = {}
  100. Bars.__index = Bars
  101.  
  102. function createBar( name )
  103.         bar = {}
  104.         setmetatable(bar,Bars)
  105.         bar.name = name
  106.         return bar
  107. end
  108.  
  109. function Bars:setup( x,y,length,color,pcolor,disp,dispbcolor,tcolor )
  110.         self.x,self.y,self.length,self.color,self.pcolor,self.disp,self.dispbcolor,self.tcolor = x,y,length,color,pcolor,disp,dispbcolor,tcolor
  111. end
  112.  
  113. function Bars:draw( x,y,length,color,pcolor,disp,dispbcolor,tcolor )
  114.        
  115.         if self.x == nil and x ~= nil then
  116.                 self.x,self.y,self.length,self.color,self.pcolor,self.disp,self.dispbcolor,self.tcolor = x,y,length,color,pcolor,disp,dispbcolor,tcolor
  117.         end
  118.  
  119.         self.percent = 0
  120.         term.setCursorPos(x,y)
  121.         paintutils.drawLine(x,y,x+ length,y,color)
  122.         term.setCursorPos(x,y)
  123.         if self.percent > 0 then
  124.                 paintutils.drawLine(x,y,x + length*(self.percent/100),y,pcolor)
  125.         end
  126.  
  127.        
  128.         percentString = tostring(self.percent).."%"
  129.         if disp == true then
  130.                 term.setCursorPos(math.max(x + math.ceil(length/2) - math.ceil(#percentString/2),0) + 1,y-1)
  131.                 term.setBackgroundColor(dispbcolor)
  132.                 term.setTextColor(tcolor)
  133.                 write(percentString)
  134.         end
  135. end
  136.  
  137. function Bars:update( percent )
  138.         self.percent = percent
  139.         term.setCursorPos(self.x,self.y)
  140.         paintutils.drawLine(self.x,self.y,self.x + self.length,self.y,self.color)
  141.         term.setCursorPos(self.x,self.y)
  142.         if self.percent > 0 then
  143.                 paintutils.drawLine(self.x,self.y,self.x + self.length*(self.percent/100),self.y,self.pcolor)  
  144.         end
  145.  
  146.        
  147.         percentString = tostring(self.percent).."%"
  148.         if self.disp == true then
  149.                 term.setCursorPos(math.max(self.x + math.ceil(self.length/2) - math.ceil(#percentString/2),0) + 1,self.y-1)
  150.                 term.setBackgroundColor(self.dispbcolor)
  151.                 term.setTextColor(self.tcolor)
  152.                 write(percentString)
  153.         end
  154. end
  155.  
  156. -- Textboxs
  157.  
  158. function newTextBox(x,y,length,bcolor,tcolor)
  159.         typed = {}
  160.         alphabet = "abcdefghijklmnopqrstuvwxyz"
  161.  
  162.         term.setBackgroundColor(bcolor)
  163.         paintutils.drawLine(x, y, x + length - 1, y, bcolor)
  164.         term.setCursorPos(x, y)
  165.         term.setTextColor(tcolor)
  166.         term.write("_")
  167.  
  168.         typing = true
  169.         term.setCursorPos(x,y)
  170.         while typing do
  171.                 event,key = os.pullEvent()
  172.                 if event == "key" then
  173.                         if key >= 0 and key <= 11 then
  174.                                 table.insert(typed,key)
  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.                         else
  187.                                 key = keys.getName(key)
  188.                                 if string.find(alphabet,key) ~= nil then
  189.                                         table.insert(typed,key)
  190.                                 end    
  191.                         end
  192.                         if #typed > length then
  193.                                 table.remove(typed,#typed)
  194.                         else
  195.                                 cx,cy = term.getCursorPos()
  196.                                 term.setBackgroundColor(bcolor)
  197.                                 paintutils.drawLine(x, y, x + length - 1, y, bcolor)
  198.                                 term.setCursorPos(x,y)
  199.                                 term.write(string.gsub(table.concat(typed,""),"space"," "))
  200.                                 if cx < x + length then
  201.                                         term.write("_")
  202.                                 end
  203.                         end
  204.                 end
  205.         end
  206. end
  207.  
  208. -- Boxes
  209.  
  210. Boxs = {}
  211. Boxs.__index = Boxs
  212.  
  213. function createDialogueBox( title,body,boxType )
  214.         boxes = {}
  215.         setmetatable(boxes,Boxs)
  216.         boxes.title = title
  217.         boxes.body = body
  218.         boxes.boxType = boxType
  219.         return boxes
  220. end
  221.  
  222. function Boxs:draw( x,y,width,color,bcolor,tcolor )
  223.         ret = nil
  224.         self.width = width
  225.         self.x = x
  226.         self.y = y
  227.  
  228.         if self.boxType == "yn" then                                                                      -- YN Box
  229.  
  230.                 if type(self.body) ~= "table" then
  231.                         paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
  232.                         term.setCursorPos(x,y)
  233.                         term.setTextColor(tcolor)
  234.                         write(self.title)
  235.  
  236.                         self.len = #self.body
  237.  
  238.                         for i = 1,width do
  239.                                 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
  240.                         end
  241.  
  242.                         term.setCursorPos(x + 1, y + 2)
  243.                         term.write(self.body)
  244.  
  245.  
  246.                         term.setCursorPos(x + 1,y + width)
  247.                         term.setTextColor(tcolor)
  248.                         term.setBackgroundColor(green)
  249.                         write(" Yes ")
  250.  
  251.                         term.setCursorPos(x + len - 4,y + width)
  252.                         term.setBackgroundColor(red)
  253.                         write(" No ")
  254.  
  255.                         repeat
  256.                                 event,click,cx,cy = os.pullEvent("mouse_click")
  257.  
  258.                                 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
  259.                                         ret = true
  260.                                 elseif cx >= x + len - 5 and cx <= x + len - 1  and cy == y + width then
  261.                                         ret = false
  262.                                 end
  263.                         until ret ~= nil
  264.  
  265.                 else
  266.  
  267.                         len = 0
  268.                         for i = 1,#self.body do
  269.                                 if #self.body[i] > len then
  270.                                         len = #self.body[i]
  271.                                 end
  272.                         end
  273.  
  274.                         paintutils.drawLine(x, y, x + len + 1, y, bcolor)
  275.                         term.setCursorPos(x,y)
  276.                         term.setTextColor(tcolor)
  277.                         write(self.title)
  278.  
  279.                         for i = 1,width do
  280.                                 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
  281.                         end
  282.  
  283.                         for i = 1,#self.body do
  284.                                 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
  285.                                 term.write(self.body[i])
  286.                         end
  287.  
  288.                         term.setCursorPos(x + 1,y + width)
  289.                         term.setTextColor(tcolor)
  290.                         term.setBackgroundColor(green)
  291.                         write(" Yes ")
  292.  
  293.                         term.setCursorPos(x + len - 4,y + width)
  294.                         term.setBackgroundColor(red)
  295.                         write(" No ")
  296.  
  297.                         repeat
  298.                                 event,click,cx,cy = os.pullEvent("mouse_click")
  299.  
  300.                                 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
  301.                                         ret = true
  302.                                 elseif cx >= x + len - 5 and cx <= x + len - 1  and cy == y + width then
  303.                                         ret = false
  304.                                 end
  305.                         until ret ~= nil
  306.  
  307.  
  308.                         self.len = len
  309.                 end
  310.  
  311.         elseif self.boxType == "ok" then                                                                  -- Ok Box
  312.                 if type(self.body) ~= "table" then
  313.  
  314.                         paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
  315.                         term.setCursorPos(x,y)
  316.                         term.setTextColor(tcolor)
  317.                         write(self.title)
  318.                         self.len = #self.body
  319.  
  320.                         for i = 1,width do
  321.                                 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
  322.                         end
  323.  
  324.                         term.setCursorPos(x + 1, y + 2)
  325.                         term.write(self.body)
  326.  
  327.                         term.setCursorPos(x + (self.len/2) - 1,y + width)
  328.                         term.setTextColor(tcolor)
  329.                         term.setBackgroundColor(green)
  330.                         write(" Ok ")
  331.  
  332.                        
  333.                         repeat
  334.                                 event,click,cx,cy = os.pullEvent("mouse_click")
  335.  
  336.                                 if cx > x + (self.len/2 - 4) and cx < x + (self.len/2) and cy == y + width then
  337.                                         ret = true
  338.                                 end
  339.                         until ret == true
  340.  
  341.                 else
  342.                        
  343.                         len = 0
  344.                         for i = 1,#self.body do
  345.                                 if #self.body[i] > len then
  346.                                         len = #self.body[i]
  347.                                 end
  348.                         end
  349.                         self.len = len
  350.  
  351.                         paintutils.drawLine(x, y, x + len + 1, y, bcolor)
  352.                         term.setCursorPos(x,y)
  353.                         term.setTextColor(tcolor)
  354.                         write(self.title)
  355.  
  356.                         for i = 1,width do
  357.                                 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
  358.                         end
  359.  
  360.                         for i = 1,#self.body do
  361.                                 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
  362.                                 term.write(self.body[i])
  363.                         end
  364.  
  365.                         term.setCursorPos(x + (self.len/2) - 1,y + width)
  366.                         term.setTextColor(tcolor)
  367.                         term.setBackgroundColor(green)
  368.                         write(" Ok ")
  369.                        
  370.                         repeat
  371.                                 event,click,cx,cy = os.pullEvent("mouse_click")
  372.  
  373.                                 if cx > x + (len/2 - 4) and cx < x + (len/2) and cy == y + width then
  374.                                         ret = true
  375.                                 end
  376.                         until ret == true
  377.                 end
  378.         end
  379.         return ret
  380. end
  381.  
  382. function Boxs:clear( color )
  383.         paintutils.drawLine(self.x, self.y, self.x + self.len + 1, self.y, color)
  384.         for i = 1,self.width do
  385.                 paintutils.drawLine(self.x, self.y + i, self.x + self.len + 1, self.y + i, color)
  386.         end
  387. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement