Advertisement
maxwason

API-autoInfusion

Aug 22nd, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. local function setupLabel(buttonLen, minY, maxY, name)
  2.     local labelTable = {}
  3.     if type(name) == "table" then
  4.         for i = 1, #name do
  5.             labelTable[i] = name[i]
  6.         end
  7.         name = name.label
  8.     elseif type(name) == "string" then
  9.         local buttonText = string.sub(name, 1, buttonLen - 2)
  10.         if #buttonText < #name then
  11.             buttonText = " "..buttonText.." "
  12.         else
  13.             local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
  14.             buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
  15.         end
  16.         for i = 1, maxY - minY + 1 do
  17.             if maxY == minY or i == math.floor((maxY - minY + 1) / 2) then
  18.                 labelTable[i] = buttonText
  19.             else
  20.                 labelTable[i] = string.rep(" ", buttonLen)
  21.             end
  22.         end
  23.     end
  24.     return labelTable, name
  25. end
  26.  
  27. local Button = {
  28.     draw = function(self, blink)
  29.         blink = blink or false
  30.         term.redirect(self.mon)
  31.         term.setBackgroundColor(colors.black)
  32.         term.clear()
  33.         if blink then term.setCursorBlink(true) else term.setCursorBlink(false) end
  34.         for name, buttonData in pairs(self.buttonList) do
  35.             if buttonData.active then
  36.                 term.setBackgroundColor(buttonData.activeColor)
  37.                 term.setTextColor(buttonData.activeTextColor)
  38.             else
  39.                 term.setBackgroundColor(buttonData.inactiveColor)
  40.                 term.setTextColor(buttonData.inactiveTextColor)
  41.             end
  42.             for i = buttonData.yMin, buttonData.yMax do
  43.                 term.setCursorPos(buttonData.xMin, i)
  44.                 term.write(buttonData.label[i - buttonData.yMin + 1])
  45.             end
  46.         end
  47.         term.restore() 
  48.     end,
  49.     debugPrint = function(self)
  50.         for name, buttonData in pairs(self.buttonList) do
  51.             print ("name, ",name," buttonData, ", buttonData)
  52.         end
  53.     end,
  54.     add = function(self, name, func, xMin, xMax, yMin, yMax, inactiveTextColor, activeTextColor, inactiveColor, activeColor, activity)
  55.         local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name)
  56.         if self.buttonList[name] then error("button already exists", 2) end
  57.         local x, y = self.mon.getSize()
  58.         if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end
  59.         self.buttonList[name] = {
  60.             func = func,
  61.             xMin = xMin,
  62.             yMin = yMin,
  63.             xMax = xMax,
  64.             yMax = yMax,
  65.             active = activity or false,
  66.             inactiveTextColor = inactiveTextColor or colors.cyan,
  67.             activeTextColor = activeTextColor or colors.black,
  68.             inactiveColor = inactiveColor or colors.black,
  69.             activeColor = activeColor or colors.black,
  70.             label = label,
  71.         }
  72.         for i = xMin, xMax do
  73.             for j = yMin, yMax do
  74.                 if self.clickMap[i][j] ~= nil then
  75.                     --undo changes
  76.                     print (self.clickMap[i][j])
  77.                     for k = xMin, xMax do
  78.                         for l = yMin, yMax do
  79.                             if self.clickMap[k][l] == name then
  80.                                 print( "overlapping with ",self.clickMap[k][l])
  81.                                 self.clickMap[k][l] = nil
  82.                             end
  83.                         end
  84.                     end
  85.                     self.buttonList[name] = nil
  86.                     error("overlapping button", 2)
  87.                 end
  88.                 self.clickMap[i][j] = name
  89.             end
  90.         end
  91.     end,
  92.     run = function(self)
  93.         while true do
  94.             self:draw()
  95.             local event = {os.pullEvent("monitor_touch")}
  96.             if event[2] == self.side then
  97.                 local clicked = self.clickMap[event[3]][event[4]]
  98.                 if clicked and self.buttonList[clicked] and self.buttonList[clicked].func then
  99.                     self.buttonList[clicked].func()
  100.                 end
  101.             end
  102.         end
  103.     end,
  104.     run2 = function(self, p1)
  105.         self.buttonList[p1].func()
  106.     end,
  107.     handleEvents = function(self, ...)
  108.         local event = {...}
  109.         if #event == 0 then event = {os.pullEvent()} end
  110.         if event[1] == "monitor_touch" and event[2] == self.side then
  111.             local clicked = self.clickMap[event[3]][event[4]]
  112.             if clicked and self.buttonList[clicked] then
  113.                 return "button_click", clicked
  114.             end
  115.         end
  116.         return unpack(event)
  117.     end,
  118.     toggleButton = function(self, name, noDraw)
  119.         self.buttonList[name].active = not self.buttonList[name].active
  120.         if not noDraw then self:draw() end
  121.     end,
  122.     flash = function(self, name)
  123.         self:toggleButton(name)
  124.         sleep(0.15)
  125.         self:toggleButton(name)
  126.     end,
  127.     rename = function(self, name, newName, noDraw)
  128.         self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
  129.         if not self.buttonList[name] then error("no such button", 2) end
  130.         self.buttonList[newName] = self.buttonList[name]
  131.         if name ~= newName then self.buttonList[name] = nil end
  132.         for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
  133.             for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
  134.                 self.clickMap[i][j] = newName
  135.             end
  136.         end
  137.         if not noDraw then self:draw() end
  138.     end,
  139.     remove = function(self, name)
  140.         if not self.buttonList[name] then error("no such button", 2) end
  141.         for i = self.buttonList[name].xMin, self.buttonList[name].xMax do
  142.             for j = self.buttonList[name].yMin, self.buttonList[name].yMax do
  143.                 self.clickMap[i][j] = nil
  144.             end
  145.         end
  146.         self.buttonList[name] = nil
  147.     end,
  148.     redo = function (self, name, func, inactiveTextColor)
  149.         if not self.buttonList[name] then error("no such button", 2) end
  150.         self.buttonList[name].func = func
  151.         self.buttonList[name].inactiveTextColor = inactiveTextColor
  152.     end
  153. }
  154.  
  155. function new(monSide)
  156.     local buttonInstance = {
  157.         side = monSide,
  158.         mon = peripheral.wrap(monSide),
  159.         buttonList = {},
  160.         clickMap = {},
  161.     }
  162.     local x, y = buttonInstance.mon.getSize()
  163.     for i = 1, x do
  164.         buttonInstance.clickMap[i] = {}
  165.     end
  166.     setmetatable(buttonInstance, {__index = Button})
  167.     return buttonInstance, x, y
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement