Advertisement
Guest User

laserTouchAPI

a guest
Aug 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.88 KB | None | 0 0
  1. --This is a test version of LaserTouch
  2. --USE AT YOUR OWN RISK!
  3. local function setupLabel(buttonLen, minY, maxY, name)
  4.     local labelTable = {}
  5.     if type(name) == "table" then
  6.         for i = 1, #name do
  7.             labelTable[i] = name[i]
  8.         end
  9.         name = name.label
  10.     elseif type(name) == "string" then
  11.         local buttonText = string.sub(name, 1, buttonLen - 2)
  12.         if #buttonText < #name then
  13.             buttonText = " "..buttonText.." "
  14.         else
  15.             local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
  16.             buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
  17.         end
  18.         for i = 1, maxY - minY + 1 do
  19.             if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
  20.                 labelTable[i] = buttonText
  21.             else
  22.                 labelTable[i] = string.rep(" ", buttonLen)
  23.             end
  24.         end
  25.     end
  26.     return labelTable, name
  27. end
  28.  
  29. local Button = {
  30.     draw = function(self)
  31.         local old = term.redirect(self.mon)
  32.         term.setTextColor(colors.white)
  33.         term.setBackgroundColor(colors.black)
  34.         term.clear()
  35.         for name, buttonData in pairs(self.buttonList) do
  36.             if buttonData.active then
  37.                 term.setBackgroundColor(buttonData.activeColor)
  38.                 term.setTextColor(buttonData.activeText)
  39.             else
  40.                 term.setBackgroundColor(buttonData.inactiveColor)
  41.                 term.setTextColor(buttonData.inactiveText)
  42.             end
  43.             for i = buttonData.yMin, buttonData.yMax do
  44.                 term.setCursorPos(buttonData.xMin, i)
  45.                 term.write(buttonData.label[i - buttonData.yMin + 1])
  46.             end
  47.         end
  48.         if old then
  49.             term.redirect(old)
  50.         else
  51.             term.restore()
  52.         end
  53.     end,
  54.     add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText)
  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 = false,
  66.             inactiveColor = inactiveColor or colors.red,
  67.             activeColor = activeColor or colors.lime,
  68.             inactiveText = inactiveText or colors.white,
  69.             activeText = activeText or colors.white,
  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.                     for k = xMin, xMax do
  77.                         for l = yMin, yMax do
  78.                             if self.clickMap[k][l] == name then
  79.                                 self.clickMap[k][l] = nil
  80.                             end
  81.                         end
  82.                     end
  83.                     self.buttonList[name] = nil
  84.                     error("overlapping button", 2)
  85.                 end
  86.                 self.clickMap[i][j] = name
  87.             end
  88.         end
  89.     end,
  90.     remove = function(self, name)
  91.         if self.buttonList[name] then
  92.             local button = self.buttonList[name]
  93.             for i = button.xMin, button.xMax do
  94.                 for j = button.yMin, button.yMax do
  95.                     self.clickMap[i][j] = nil
  96.                 end
  97.             end
  98.             self.buttonList[name] = nil
  99.         end
  100.     end,
  101.     run = function(self)
  102.         while true do
  103.             self:draw()
  104.             local event = {self:handleEvents(os.pullEvent(self.side == "term" and "mouse_click" or "monitor_touch"))}
  105.             if event[1] == "button_click" then
  106.                 self.buttonList[event[2]].func()
  107.             end
  108.         end
  109.     end,
  110.     handleEvents = function(self, ...)
  111.         local event = {...}
  112.         if #event == 0 then event = {os.pullEvent()} end
  113.         if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term" and event[1] == "monitor_touch" and event[2] == self.side) then
  114.             local clicked = self.clickMap[event[3]][event[4]]
  115.             if clicked and self.buttonList[clicked] then
  116.                 return "button_click", clicked
  117.             end
  118.         end
  119.         return unpack(event)
  120.     end,
  121.     toggleButton = function(self, name, noDraw)
  122.         self.buttonList[name].active = not self.buttonList[name].active
  123.         if not noDraw then self:draw() end
  124.     end,
  125.     flash = function(self, name, duration)
  126.         self:toggleButton(name)
  127.         sleep(tonumber(duration) or 0.15)
  128.         self:toggleButton(name)
  129.     end,
  130.     rename = function(self, name, newName)
  131.         self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
  132.         if not self.buttonList[name] then error("no such button", 2) end
  133.         if name ~= newName then
  134.             self.buttonList[newName] = self.buttonList[name]
  135.             self.buttonList[name] = nil
  136.             for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
  137.                 for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
  138.                     self.clickMap[i][j] = newName
  139.                 end
  140.             end
  141.         end
  142.         self:draw()
  143.     end,
  144. }
  145.  
  146. function new(monSide)
  147.     local buttonInstance = {
  148.         side = monSide or "term",
  149.         mon = monSide and peripheral.wrap(monSide) or term.current(),
  150.         buttonList = {},
  151.         clickMap = {},
  152.     }
  153.     local x, y = buttonInstance.mon.getSize()
  154.     for i = 1, x do
  155.         buttonInstance.clickMap[i] = {}
  156.     end
  157.     setmetatable(buttonInstance, {__index = Button})
  158.     return buttonInstance
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement