Advertisement
kizz12

touchpoint

Apr 24th, 2015
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.66 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) / 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.  
  102.                         self:draw()
  103.                         local event = {self:handleEvents(os.pullEvent(self.side == "term" and "mouse_click" or "monitor_touch"))}
  104.                         if event[1] == "button_click" then
  105.                             term.setCursorPos(1,1)
  106.                             term.clear()
  107.                             term.setBackgroundColor(colors.black)
  108.                             self.buttonList[event[2]].func()
  109.                             term.setCursorPos(1,1)
  110.                             term.clear()
  111.                             term.setBackgroundColor(colors.black)  
  112.                         end
  113.                -- end
  114.         end,
  115.         handleEvents = function(self, ...)
  116.                 local event = {...}
  117.                 if #event == 0 then event = {os.pullEvent()} end
  118.                 if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term" and event[1] == "monitor_touch" and event[2] == self.side) then
  119.                         local clicked = self.clickMap[event[3]][event[4]]
  120.                         if clicked and self.buttonList[clicked] then
  121.                                 return "button_click", clicked
  122.                         end
  123.                 end
  124.                 return unpack(event)
  125.         end,
  126.         toggleButton = function(self, name, noDraw)
  127.                 self.buttonList[name].active = not self.buttonList[name].active
  128.                 if not noDraw then self:draw() end
  129.         end,
  130.         flash = function(self, name, duration)
  131.                 self:toggleButton(name)
  132.                 sleep(tonumber(duration) or 0.15)
  133.                 self:toggleButton(name)
  134.         end,
  135.         rename = function(self, name, newName)
  136.                 self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
  137.                 if not self.buttonList[name] then error("no such button", 2) end
  138.                 if name ~= newName then
  139.                         self.buttonList[newName] = self.buttonList[name]
  140.                         self.buttonList[name] = nil
  141.                         for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
  142.                                 for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
  143.                                         self.clickMap[i][j] = newName
  144.                                 end
  145.                         end
  146.                 end
  147.                 self:draw()
  148.         end,
  149. }
  150.  
  151. function new(monSide)
  152.         local buttonInstance = {
  153.                 side = monSide or "term",
  154.                 mon = monSide and peripheral.wrap(monSide) or term.current(),
  155.                 buttonList = {},
  156.                 clickMap = {},
  157.         }
  158.         local x, y = buttonInstance.mon.getSize()
  159.         for i = 1, x do
  160.                 buttonInstance.clickMap[i] = {}
  161.         end
  162.         setmetatable(buttonInstance, {__index = Button})
  163.         return buttonInstance
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement