TheSpicePhantom

touchpoint from Lyqyd

May 7th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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) / 2) + 1 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)
  29.         local old = term.redirect(self.mon)
  30.         term.setTextColor(colors.white)
  31.         term.setBackgroundColor(colors.black)
  32.         term.clear()
  33.         for name, buttonData in pairs(self.buttonList) do
  34.             if buttonData.active then
  35.                 term.setBackgroundColor(buttonData.activeColor)
  36.                 term.setTextColor(buttonData.activeText)
  37.             else
  38.                 term.setBackgroundColor(buttonData.inactiveColor)
  39.                 term.setTextColor(buttonData.inactiveText)
  40.             end
  41.             for i = buttonData.yMin, buttonData.yMax do
  42.                 term.setCursorPos(buttonData.xMin, i)
  43.                 term.write(buttonData.label[i - buttonData.yMin + 1])
  44.             end
  45.         end
  46.         if old then
  47.             term.redirect(old)
  48.         else
  49.             term.restore()
  50.         end
  51.     end,
  52.     add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText)
  53.         local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name)
  54.         if self.buttonList[name] then error("button already exists", 2) end
  55.         local x, y = self.mon.getSize()
  56.         if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end
  57.         self.buttonList[name] = {
  58.             func = func,
  59.             xMin = xMin,
  60.             yMin = yMin,
  61.             xMax = xMax,
  62.             yMax = yMax,
  63.             active = false,
  64.             inactiveColor = inactiveColor or colors.red,
  65.             activeColor = activeColor or colors.lime,
  66.             inactiveText = inactiveText or colors.white,
  67.             activeText = activeText or colors.white,
  68.             label = label,
  69.         }
  70.         for i = xMin, xMax do
  71.             for j = yMin, yMax do
  72.                 if self.clickMap[i][j] ~= nil then
  73.                     --undo changes
  74.                     for k = xMin, xMax do
  75.                         for l = yMin, yMax do
  76.                             if self.clickMap[k][l] == name then
  77.                                 self.clickMap[k][l] = nil
  78.                             end
  79.                         end
  80.                     end
  81.                     self.buttonList[name] = nil
  82.                     error("overlapping button", 2)
  83.                 end
  84.                 self.clickMap[i][j] = name
  85.             end
  86.         end
  87.     end,
  88.     remove = function(self, name)
  89.         if self.buttonList[name] then
  90.             local button = self.buttonList[name]
  91.             for i = button.xMin, button.xMax do
  92.                 for j = button.yMin, button.yMax do
  93.                     self.clickMap[i][j] = nil
  94.                 end
  95.             end
  96.             self.buttonList[name] = nil
  97.         end
  98.     end,
  99.     run = function(self)
  100.         while true do
  101.             self:draw()
  102.             local event = {self:handleEvents(os.pullEvent(self.side == "term" and "mouse_click" or "monitor_touch"))}
  103.             if event[1] == "button_click" then
  104.                 self.buttonList[event[2]].func()
  105.             end
  106.         end
  107.     end,
  108.     handleEvents = function(self, ...)
  109.         local event = {...}
  110.         if #event == 0 then event = {os.pullEvent()} end
  111.         if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term" and event[1] == "monitor_touch" and event[2] == self.side) then
  112.             local clicked = self.clickMap[event[3]][event[4]]
  113.             if clicked and self.buttonList[clicked] then
  114.                 return "button_click", clicked
  115.             end
  116.         end
  117.         return unpack(event)
  118.     end,
  119.     toggleButton = function(self, name, noDraw)
  120.         self.buttonList[name].active = not self.buttonList[name].active
  121.         if not noDraw then self:draw() end
  122.     end,
  123.     flash = function(self, name, duration)
  124.         self:toggleButton(name)
  125.         sleep(tonumber(duration) or 0.15)
  126.         self:toggleButton(name)
  127.     end,
  128.     rename = function(self, name, newName)
  129.         self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
  130.         if not self.buttonList[name] then error("no such button", 2) end
  131.         if name ~= newName then
  132.             self.buttonList[newName] = self.buttonList[name]
  133.             self.buttonList[name] = nil
  134.             for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
  135.                 for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
  136.                     self.clickMap[i][j] = newName
  137.                 end
  138.             end
  139.         end
  140.         self:draw()
  141.     end,
  142. }
  143.  
  144. function new(monSide)
  145.     local buttonInstance = {
  146.         side = monSide or "term",
  147.         mon = monSide and peripheral.wrap(monSide) or term.current(),
  148.         buttonList = {},
  149.         clickMap = {},
  150.     }
  151.     local x, y = buttonInstance.mon.getSize()
  152.     for i = 1, x do
  153.         buttonInstance.clickMap[i] = {}
  154.     end
  155.     setmetatable(buttonInstance, {__index = Button})
  156.     return buttonInstance
  157. end
Add Comment
Please, Sign In to add comment