Advertisement
fames

button

Dec 22nd, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.37 KB | None | 0 0
  1. ButtonObj = {}
  2.  
  3. function ButtonObj:new(screen, x, y, w, h, labelOff, labelOn, cLabelOff, cLabelOn, cBgOff, cBgOn)
  4.     local obj = obj or {}
  5.     setmetatable(obj, {__index = self})
  6.  
  7.     obj.screen = screen
  8.     obj.monitor = screen:getMonitor()
  9.  
  10.     obj.x = x or 1  
  11.     obj.y = y or 1
  12.     obj.w = w or 1
  13.     obj.h = h or 1
  14.  
  15.     obj.xOffset = 0
  16.     obj.yOffset = 0
  17.  
  18.     obj.labelOff = labelOff or "off"
  19.     obj.labelOn = labelOn or obj.labelOff
  20.     obj.cLabelOff = cLabelOff or colors.white
  21.     obj.cLabelOn = cLabelOn or colors.white
  22.     obj.cBgOff = cBgOff or colors.cyan
  23.     obj.cBgOn = cBgOn or colors.green
  24.  
  25.     obj.func = nil
  26.     obj.funcArgs = nil
  27.     obj.isActive = false
  28.     obj.isLocked = false
  29.     obj.canToggle = true    -- this is useful for one-shot buttons
  30.  
  31.     obj.parent = nil
  32.  
  33.     return obj
  34. end
  35.  
  36. function ButtonObj:draw()
  37.     local old = term.redirect(self.monitor)
  38.     local oldTC = self.monitor.getTextColor()
  39.     local oldBC = self.monitor.getBackgroundColor()
  40.  
  41.     local cBg = self.cBgOff
  42.     local cLabel = self.cLabelOff
  43.     local label = self.labelOff
  44.     if self.isActive then
  45.         cBg = self.cBgOn
  46.         cLabel = self.cLabelOn
  47.         label = self.labelOn
  48.     else
  49.         cBg = self.cBgOff
  50.         cLabeL = self.cLabelOff
  51.         label = self.labelOff
  52.     end
  53.  
  54.     if self.isLocked then cBg = colors.gray end
  55.  
  56.     local newX, newY
  57.     if self.parent then
  58.         oX, oY = self.parent:getStartPos()
  59.         newX = self.x + oX
  60.         newY = self.y + oY
  61.     else
  62.         newX = self.x + self.xOffset
  63.         newY = self.y + self.yOffset
  64.     end
  65.  
  66.     paintutils.drawFilledBox(newX , newY, self.w + newX, self.h + newY, cBg)
  67.     local txtOffset = (self.w/2) - (math.floor(string.len(label)/2))
  68.     self.screen:write(label, newX + txtOffset, newY + 1, cBg, cLabel)
  69.  
  70.     -- return to previous colors etc. to not mess up anything after
  71.     self.monitor.setTextColor(oldTC)
  72.     self.monitor.setBackgroundColor(oldBC)
  73.     term.redirect(old)
  74. end
  75.  
  76. function ButtonObj:setLocked(lock)
  77.     self.isLocked = lock or false
  78. end
  79.  
  80. function ButtonObj:setFunction(func, args)
  81.     self.func = func
  82.     self.funcArgs = args
  83. end
  84.  
  85. function ButtonObj:check(monX, monY)
  86.     print("checked "..self.labelOff.." !")
  87.     local newX, newY
  88.     if self.parent then
  89.         oX, oY = self.parent:getStartPos()
  90.         newX = self.x + oX
  91.         newY = self.y + oY
  92.     else
  93.         newX = self.x + self.xOffset
  94.         newY = self.y + self.yOffset
  95.     end
  96.  
  97.     if (monX >= newX and monX <= newX+self.w) and (monY >= newY and monY <= newY+self.h) and not self.isLocked then
  98.         if self.canToggle then
  99.             self.isActive = not self.isActive
  100.         end
  101.         self:execute()
  102.     end
  103. end
  104.  
  105. function ButtonObj:execute()
  106.     local bool = nil
  107.     if self.isActive or (not self.canToggle) then
  108.         bool = true
  109.     else
  110.         bool = false
  111.     end
  112.  
  113.     self.func(bool, self.args) --self.func is the assigned function. this confused the heck out of me when rewriting lol
  114. end
  115.  
  116. function ButtonObj:setActive(active, exec) -- set the button active. this can also execute the function tied to the button
  117.     self.isActive = active or false
  118.     if exec then self:execute() end
  119. end
  120.  
  121. function ButtonObj:setOffset(x, y)
  122.     self.xOffset = x
  123.     self.yOffset = y
  124. end
  125.  
  126. function ButtonObj:setLabel(off, on)
  127.     self.labelOff = off or self.labelOff
  128.     self.labelOn = on or off
  129. end
  130.  
  131. function ButtonObj:setLabelColor(off, on)
  132.     self.cLabelOff = off or self.cLabelOff
  133.     self.cLabelOn = on or off
  134. end
  135.  
  136. function ButtonObj:setBackgroundColor(off, on)
  137.     self.cBgOff = off or self.cBgOff
  138.     self.cBgOn = on or off
  139. end
  140.  
  141. function ButtonObj:setParent(obj)
  142.     local add = function()
  143.         self.parent:addObject(self)
  144.     end
  145.  
  146.     self.parent = obj
  147.     pcall(add)
  148. end
  149.  
  150. function ButtonObj:setCanToggle(x)
  151.     local bool = x
  152.     self.canToggle = bool
  153. end
  154.  
  155. function ButtonObj:getParent()
  156.     return self.parent
  157. end
  158.  
  159. function ButtonObj:setX(x)
  160.     self.x = x
  161. end
  162.  
  163. function ButtonObj:getX()
  164.     return self.x
  165. end
  166.  
  167. function ButtonObj:setY(y)
  168.     self.y = y
  169. end
  170.  
  171. function ButtonObj:getY()
  172.     return self.y
  173. end
  174.  
  175. function ButtonObj:setW(w)
  176.     self.w = w
  177. end
  178.  
  179. function ButtonObj:getW()
  180.     return self.w
  181. end
  182.  
  183. function ButtonObj:setH(h)
  184.     self.h = h
  185. end
  186.  
  187. function ButtonObj:getH()
  188.     return self.h
  189. end
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement