oblinger

GUI API

May 10th, 2014
4,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.14 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.         end
  78.     end
  79.     self = nil
  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.  
  159.     term.setBackgroundColor(bcolor)
  160.     paintutils.drawLine(x, y, x + length - 1, y, bcolor)
  161.     term.setCursorPos(x, y)
  162.     term.setTextColor(tcolor)
  163.     term.write("_")
  164.  
  165.     typing = true
  166.     term.setCursorPos(x,y)
  167.     while typing do
  168.         event,key = os.pullEvent()
  169.         if event == "key" then
  170.             if key >= 0 and key <= 11 then
  171.                 table.insert(typed,key)
  172.             elseif keys.getName(key) == "enter" then
  173.                 typing = false
  174.                 return string.gsub(table.concat(typed,""),"space"," ")
  175.             elseif keys.getName(key) == "space" then
  176.                 table.insert(typed,keys.getName(key))
  177.             elseif keys.getName(key) == "period" then
  178.                 table.insert(typed,".")
  179.             elseif keys.getName(key) == "comma" then
  180.                 table.insert(typed,",")
  181.             elseif keys.getName(key) == "backspace" then
  182.                 table.remove(typed,#typed)
  183.             else
  184.                 key = keys.getName(key)
  185.                 if string.find(alphabet,key) ~= nil then
  186.                     table.insert(typed,key)
  187.                 end
  188.             end
  189.             if #typed > length then
  190.                 table.remove(typed,#typed)
  191.             else
  192.                 cx,cy = term.getCursorPos()
  193.                 term.setBackgroundColor(bcolor)
  194.                 paintutils.drawLine(x, y, x + length - 1, y, bcolor)
  195.                 term.setCursorPos(x,y)
  196.                 term.write(string.gsub(table.concat(typed,""),"space"," "))
  197.                 if cx < x + length then
  198.                     term.write("_")
  199.                 end
  200.             end
  201.         end
  202.     end
  203. end
  204.  
  205. -- Boxes
  206.  
  207. Boxs = {}
  208. Boxs.__index = Boxs
  209.  
  210. function createDialogueBox( title,body,boxType )
  211.     boxes = {}
  212.     setmetatable(boxes,Boxs)
  213.     boxes.title = title
  214.     boxes.body = body
  215.     boxes.boxType = boxType
  216.     return boxes
  217. end
  218.  
  219. function Boxs:draw( x,y,width,color,bcolor,tcolor )
  220.     ret = nil
  221.     self.width = width
  222.     self.x = x
  223.     self.y = y
  224.  
  225.     if self.boxType == "yn" then                                                                      -- YN Box
  226.  
  227.         if type(self.body) ~= "table" then
  228.             paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
  229.             term.setCursorPos(x,y)
  230.             term.setTextColor(tcolor)
  231.             write(self.title)
  232.  
  233.             self.len = #self.body
  234.  
  235.             for i = 1,width do
  236.                 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
  237.             end
  238.  
  239.             term.setCursorPos(x + 1, y + 2)
  240.             term.write(self.body)
  241.  
  242.  
  243.             term.setCursorPos(x + 1,y + width)
  244.             term.setTextColor(tcolor)
  245.             term.setBackgroundColor(green)
  246.             write(" Yes ")
  247.  
  248.             term.setCursorPos(x + len - 4,y + width)
  249.             term.setBackgroundColor(red)
  250.             write(" No ")
  251.  
  252.             repeat
  253.                 event,click,cx,cy = os.pullEvent("mouse_click")
  254.  
  255.                 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
  256.                     ret = true
  257.                 elseif cx >= x + len - 5 and cx <= x + len - 1  and cy == y + width then
  258.                     ret = false
  259.                 end
  260.             until ret ~= nil
  261.  
  262.         else
  263.  
  264.             len = 0
  265.             for i = 1,#self.body do
  266.                 if #self.body[i] > len then
  267.                     len = #self.body[i]
  268.                 end
  269.             end
  270.  
  271.             paintutils.drawLine(x, y, x + len + 1, y, bcolor)
  272.             term.setCursorPos(x,y)
  273.             term.setTextColor(tcolor)
  274.             write(self.title)
  275.  
  276.             for i = 1,width do
  277.                 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
  278.             end
  279.  
  280.             for i = 1,#self.body do
  281.                 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
  282.                 term.write(self.body[i])
  283.             end
  284.  
  285.             term.setCursorPos(x + 1,y + width)
  286.             term.setTextColor(tcolor)
  287.             term.setBackgroundColor(green)
  288.             write(" Yes ")
  289.  
  290.             term.setCursorPos(x + len - 4,y + width)
  291.             term.setBackgroundColor(red)
  292.             write(" No ")
  293.  
  294.             repeat
  295.                 event,click,cx,cy = os.pullEvent("mouse_click")
  296.  
  297.                 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
  298.                     ret = true
  299.                 elseif cx >= x + len - 5 and cx <= x + len - 1  and cy == y + width then
  300.                     ret = false
  301.                 end
  302.             until ret ~= nil
  303.  
  304.  
  305.             self.len = len
  306.         end
  307.  
  308.     elseif self.boxType == "ok" then                                                                  -- Ok Box
  309.         if type(self.body) ~= "table" then
  310.  
  311.             paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
  312.             term.setCursorPos(x,y)
  313.             term.setTextColor(tcolor)
  314.             write(self.title)
  315.             self.len = #self.body
  316.  
  317.             for i = 1,width do
  318.                 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
  319.             end
  320.  
  321.             term.setCursorPos(x + 1, y + 2)
  322.             term.write(self.body)
  323.  
  324.             term.setCursorPos(x + (self.len/2) - 1,y + width)
  325.             term.setTextColor(tcolor)
  326.             term.setBackgroundColor(green)
  327.             write(" Ok ")
  328.  
  329.            
  330.             repeat
  331.                 event,click,cx,cy = os.pullEvent("mouse_click")
  332.  
  333.                 if cx > x + (self.len/2 - 4) and cx < x + (self.len/2) and cy == y + width then
  334.                     ret = true
  335.                 end
  336.             until ret == true
  337.  
  338.         else
  339.            
  340.             len = 0
  341.             for i = 1,#self.body do
  342.                 if #self.body[i] > len then
  343.                     len = #self.body[i]
  344.                 end
  345.             end
  346.             self.len = len
  347.  
  348.             paintutils.drawLine(x, y, x + len + 1, y, bcolor)
  349.             term.setCursorPos(x,y)
  350.             term.setTextColor(tcolor)
  351.             write(self.title)
  352.  
  353.             for i = 1,width do
  354.                 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
  355.             end
  356.  
  357.             for i = 1,#self.body do
  358.                 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
  359.                 term.write(self.body[i])
  360.             end
  361.  
  362.             term.setCursorPos(x + (self.len/2) - 1,y + width)
  363.             term.setTextColor(tcolor)
  364.             term.setBackgroundColor(green)
  365.             write(" Ok ")
  366.            
  367.             repeat
  368.                 event,click,cx,cy = os.pullEvent("mouse_click")
  369.  
  370.                 if cx > x + (len/2 - 4) and cx < x + (len/2) and cy == y + width then
  371.                     ret = true
  372.                 end
  373.             until ret == true
  374.         end
  375.     end
  376.     return ret
  377. end
  378.  
  379. function Boxs:clear( color )
  380.     paintutils.drawLine(self.x, self.y, self.x + self.len + 1, self.y, color)
  381.     for i = 1,self.width do
  382.         paintutils.drawLine(self.x, self.y + i, self.x + self.len + 1, self.y + i, color)
  383.     end
  384. end
Advertisement
Add Comment
Please, Sign In to add comment