WillaRL_

buttonAPI for OC

Dec 9th, 2020 (edited)
1,665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. --By Willa_/WillaRL_
  2.  
  3. local event = require("event")
  4. local process = require("process")
  5. local component = require("component")
  6. local gpu = component.gpu
  7.  
  8.  
  9.  
  10. local buttonAPI = {}
  11. buttonAPI.__index = buttonAPI
  12. setmetatable(buttonAPI, {__call = function(cls,...) return cls.new(...) end,})
  13.  
  14.  
  15. local buttonAPIarr = {}
  16. buttonAPIarr.__index = buttonAPIarr
  17. setmetatable(buttonAPIarr, {__call = function(cls,...) return cls.new(...) end,})
  18.  
  19.  
  20.  
  21.  
  22. --------------------------
  23. --START CLASS buttonAPIarr
  24. --------------------------
  25.  
  26. function buttonAPIarr.new()
  27.   local self = setmetatable({}, buttonAPIarr)
  28.  
  29.   self.buttons = {}
  30.   return self
  31. end
  32.  
  33. function buttonAPIarr:add(label, behaviour, x2, x1, y2, y1, colourInit, colourClked, length, func)
  34.   local newButton = buttonAPI(label, behaviour, x2, x1, y2, y1, colourInit, colourClked, length, func)
  35.   table.insert(self.buttons, newButton)
  36. end
  37.  
  38. function buttonAPIarr:click(x,y)
  39.   for i, button in ipairs(self.buttons) do
  40.     button:click(x,y)
  41.   end
  42. end
  43.      
  44. ------------------------
  45. --END CLASS buttonAPIarr
  46. ------------------------
  47.  
  48.  
  49. -----------------------
  50. --START CLASS buttonAPI
  51. -----------------------
  52.  
  53. function buttonAPI.new(label, behaviour, x2, x1, y2, y1, colourInit, colourClked, length, func)
  54.   local self = setmetatable({}, buttonAPI)
  55.  
  56.   self.xMin = x1
  57.   self.yMin = y1
  58.   self.xMax = x2
  59.   self.yMax = y2
  60.  
  61.   self.colourInit = colourInit
  62.   self.colourClked = colourClked
  63.  
  64.   self.label = label
  65.   self.behaviour = behaviour
  66.   self.length = length
  67.   self.func = func
  68.   self.clicked = false
  69.   self:draw()
  70.  
  71.   return self
  72. end
  73.  
  74.  
  75. function buttonAPI:draw()
  76.   if(self.clicked) then
  77.     self:__draw(self.colourClked)
  78.   else
  79.     self:__draw(self.colourInit)
  80.   end
  81.  
  82. end
  83.  
  84.  
  85. function buttonAPI:__draw(colour)
  86.   local oldColour = gpu.setBackground(colour, false)
  87.   gpu.fill(self.xMin,self.yMin,self.xMax-self.xMin,self.yMax-self.yMin, " ")
  88.  
  89.   local midX = math.ceil( (self.xMax - self.xMin-1)/2) + self.xMin
  90.   local midY = math.floor((self.yMax - self.yMin-1)/2) + self.yMin
  91.  
  92.   local labelLen = string.len(self.label)
  93.   local labelMid = math.ceil(labelLen/2)
  94.  
  95.   local startPoint = midX-labelLen+labelMid
  96.   gpu.set(startPoint,midY,self.label)
  97.  
  98.   gpu.setBackground(oldColour)
  99. end
  100.  
  101.  
  102. function buttonAPI:check(x,y)
  103.   if(x >= self.xMin and x <= (self.xMax - 1)) and (y >= self.yMin and y <= (self.yMax - 1)) then
  104.     return true
  105.   else
  106.     return false
  107.   end
  108. end
  109.  
  110.  
  111. function buttonAPI:clickToggle()
  112.   if (self.clicked) then
  113.     self.clicked = false
  114.     self:draw()
  115.     return true
  116.  
  117.   else
  118.     self.clicked = true
  119.     self:draw()
  120.     return true
  121.   end
  122. end
  123.  
  124.  
  125. function buttonAPI:clickPulse()
  126.   self.clicked = true
  127.   self:draw()
  128.   os.sleep(self.length)
  129.   self.clicked = false
  130.   self:draw()
  131. end
  132.  
  133. function buttonAPI:click(x,y)
  134.   if(self:check(x,y)) then
  135.     self.func()
  136.  
  137.     if(self.behaviour == 0) then
  138.       self:clickPulse()
  139.       return true
  140.  
  141.     elseif(self.behaviour == 1) then
  142.       self:clickToggle()
  143.       return true
  144.  
  145.     end
  146.   end
  147.   return false
  148. end
  149.  
  150. ---------------------
  151. --END CLASS buttonAPI
  152. ---------------------
  153.  
  154. return buttonAPIarr
Advertisement
Add Comment
Please, Sign In to add comment