Advertisement
EkielZan

touchpoint

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