Advertisement
CCCoder

Gooey API

Sep 27th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. --Gooey is a LimeFyre API.--
  2.  
  3. local width, height = term.getSize( )
  4.  
  5. gooey = { }
  6.  
  7. UI = { }
  8. UIButton = { }
  9. UICheckbox = { }
  10. UIRadio = { }
  11. UIText = { }
  12.  
  13. ActiveUI = { }
  14.  
  15. local protos = { }
  16.  
  17. protos.frame = {
  18.     x = 1,
  19.     y = 1,
  20.     w = width,
  21.     h = height,
  22.     c = colours.white,
  23.     children = { },
  24.     isVisible = true,
  25. }
  26.  
  27. protos.button = {
  28.     x = 1,
  29.     y = 1,
  30.     w = 10,
  31.     h = 3,
  32.     text = "A Button",
  33.     textColour = colours.white,
  34.     bgColour = colours.grey,
  35.     onClick = function( ) end,
  36.     isVisible = true,
  37. }
  38.  
  39. --UI FRAMES--
  40. function gooey.UI:new( x, y, w, h, c )
  41.     local a = { x = x, y = y, w = w, h = h, c = c }
  42.     local o = setmetatable( a, protos.frame )
  43.     setmetatable( o, self )
  44.     self.__index = self
  45.     table.insert( ActiveUI, self )
  46.     return o
  47. end
  48.  
  49. function gooey.UI:move( x, y )
  50.     self.x, self.y = x, y
  51. end
  52.  
  53. function gooey.UI:setColour( colour )
  54.     self.colour = colour
  55. end
  56.  
  57. function gooey.UI:toggle( )
  58.     self.isVisible = not self.isVisible
  59. end
  60.  
  61. function gooey.UI:show( )
  62.     self.isVisible = true
  63. end
  64.  
  65. function gooey.UI:hide( )
  66.     self.isVisible = false
  67. end
  68.  
  69. function gooey.UIButton:new( frame, x, y, text, textColour, bgColour )
  70.     if frame == nil then
  71.         error( "expected frame object, got nil" )
  72.     else
  73.         local w = #text + 2
  74.         local h = 3
  75.         local a = { frame = frame, x = x, y = y, w = w, h = h, text = text, bgColour = bgColour, textColour = textColour }
  76.         local o = setmetatable( a, protos.button )
  77.         setmetatable( o, self )
  78.         self.__index = self
  79.         table.insert( frame.children, self )
  80.         return o
  81.     end
  82. end
  83.  
  84. function gooey.UIButton:checkClick( x, y )
  85.     if x >= self.x and x <= self.w and y >= self.y and y <= self.h then
  86.         self:onClick( )
  87.     end
  88. end
  89.  
  90. function gooey.UIButton:draw( )
  91.     if self.isVisible then
  92.         paintutils.drawFilledBox( self.x, self.y, self.w, self.h, self.bgColour )
  93.         term.setCursorPos( self.x + 1, self.y + 1 )
  94.         print( self.text )
  95.     end
  96. end
  97.  
  98. function gooey.UIButton:setTextColour( colour )
  99.     self.textColour = colour
  100. end
  101.  
  102. function gooey.UIButton:setBackgroundColour( colour )
  103.     self.bgColour = colour
  104. end
  105.  
  106. function gooey.UIButton:toggle( )
  107.     self.isVisible = not self.isVisible
  108. end
  109.  
  110. function gooey.UIButton:show( )
  111.     self.isVisible = true
  112. end
  113.  
  114. function gooey.UIButton:hide( )
  115.     self.isVisible = false
  116. end
  117.  
  118. function gooey.checkClick( x, y )
  119.     for k, v in pairs( ActiveUI ) do
  120.         for a, b in pairs( v.children ) do
  121.             if b.isVisible and b.checkClick ~= nil then
  122.                 b:checkClick( x, y )
  123.             end
  124.         end
  125.     end
  126. end
  127.  
  128. function gooey.draw( )
  129.     for k, v in pairs( ActiveUI ) do
  130.         if v.isVisible then
  131.             paintutils.drawFilledBox( v.x, v.y, v.w, v.h, v.colour )
  132.             for a, b in pairs( v.children ) do
  133.                 b:draw( )
  134.             end
  135.         end
  136.     end
  137. end
  138.  
  139. return gooey
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement