RJ-Bradach

Touchpoint API Augment

Jun 18th, 2021 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.08 KB | None | 0 0
  1. -- This is an augmented version of the Touchpoint API by Lyqyd. If he does not want this up here. Please ask me, I will remove it. I apologize in advance
  2. --[[
  3. The MIT License (MIT)
  4.  
  5. Copyright (c) 2013 Lyqyd
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. --]]
  25.  
  26. local function setupLabel(buttonLen, minY, maxY, name)
  27.     local labelTable = {}
  28.     if type(name) == "table" then
  29.         for i = 1, #name do
  30.             labelTable[i] = name[i]
  31.         end
  32.         name = name.label
  33.     elseif type(name) == "string" then
  34.         local buttonText = string.sub(name, 1, buttonLen - 2)
  35.         if #buttonText < #name then
  36.             buttonText = " "..buttonText.." "
  37.         else
  38.             local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
  39.             buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
  40.         end
  41.         for i = 1, maxY - minY + 1 do
  42.             if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
  43.                 labelTable[i] = buttonText
  44.             else
  45.                 labelTable[i] = string.rep(" ", buttonLen)
  46.             end
  47.         end
  48.     end
  49.     return labelTable, name
  50. end
  51.  
  52. local Button = {
  53.     draw = function(self)
  54.         local old = term.redirect(self.mon)
  55.         term.setTextColor(colors.black)
  56.         term.setBackgroundColor(colors.orange)
  57.         term.clear()
  58.         for name, buttonData in pairs(self.buttonList) do
  59.             if buttonData.active then
  60.                 term.setBackgroundColor(buttonData.activeColor)
  61.                 term.setTextColor(buttonData.activeText)
  62.             else
  63.                 term.setBackgroundColor(buttonData.inactiveColor)
  64.                 term.setTextColor(buttonData.inactiveText)
  65.             end
  66.             for i = buttonData.yMin, buttonData.yMax do
  67.                 term.setCursorPos(buttonData.xMin, i)
  68.                 term.write(buttonData.label[i - buttonData.yMin + 1])
  69.             end
  70.         end
  71.         if old then
  72.             term.redirect(old)
  73.         else
  74.             term.restore()
  75.         end
  76.     end,
  77.     add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText)
  78.         local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name)
  79.         if self.buttonList[name] then error("button already exists", 2) end
  80.         local x, y = self.mon.getSize()
  81.         if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end
  82.         self.buttonList[name] = {
  83.             func = func,
  84.             xMin = xMin,
  85.             yMin = yMin,
  86.             xMax = xMax,
  87.             yMax = yMax,
  88.             active = false,
  89.             inactiveColor = inactiveColor or colors.red,
  90.             activeColor = activeColor or colors.lime,
  91.             inactiveText = inactiveText or colors.white,
  92.             activeText = activeText or colors.white,
  93.             label = label,
  94.         }
  95.         for i = xMin, xMax do
  96.             for j = yMin, yMax do
  97.                 if self.clickMap[i][j] ~= nil then
  98.                     --undo changes
  99.                     for k = xMin, xMax do
  100.                         for l = yMin, yMax do
  101.                             if self.clickMap[k][l] == name then
  102.                                 self.clickMap[k][l] = nil
  103.                             end
  104.                         end
  105.                     end
  106.                     self.buttonList[name] = nil
  107.                     error("overlapping button", 2)
  108.                 end
  109.                 self.clickMap[i][j] = name
  110.             end
  111.         end
  112.     end,
  113.     remove = function(self, name)
  114.         if self.buttonList[name] then
  115.             local button = self.buttonList[name]
  116.             for i = button.xMin, button.xMax do
  117.                 for j = button.yMin, button.yMax do
  118.                     self.clickMap[i][j] = nil
  119.                 end
  120.             end
  121.             self.buttonList[name] = nil
  122.         end
  123.     end,
  124.     run = function(self)
  125.         while true do
  126.             --self:draw()
  127.             local event = {self:handleEvents(os.pullEvent(self.side == "term" and "mouse_click" or "monitor_touch"))}
  128.             if event[1] == "button_click" then
  129.                 self.buttonList[event[2]].func()
  130.             end
  131.         end
  132.     end,
  133.     handleEvents = function(self, ...)
  134.         local event = {...}
  135.         local side = event[2]
  136.         if #event == 0 then event = {os.pullEvent()} end
  137.         if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term" and event[1] == "monitor_touch" and event[2] == self.side) then
  138.             local clicked = self.clickMap[event[3]][event[4]]
  139.             if clicked and self.buttonList[clicked] then
  140.                 return "button_click", clicked, side
  141.             end
  142.         end
  143.         return unpack(event)
  144.     end,
  145.     toggleButton = function(self, name, noDraw)
  146.         self.buttonList[name].active = not self.buttonList[name].active
  147.         if not noDraw then self:draw() end
  148.     end,
  149.     flash = function(self, name, duration)
  150.         self:toggleButton(name)
  151.         sleep(tonumber(duration) or 0.15)
  152.         self:toggleButton(name)
  153.     end,
  154.     rename = function(self, name, newName)
  155.         self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
  156.         if not self.buttonList[name] then error("no such button", 2) end
  157.         if name ~= newName then
  158.             self.buttonList[newName] = self.buttonList[name]
  159.             self.buttonList[name] = nil
  160.             for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
  161.                 for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
  162.                     self.clickMap[i][j] = newName
  163.                 end
  164.             end
  165.         end
  166.         self:draw()
  167.     end,
  168. }
  169.  
  170. function new(monSide)
  171.     local buttonInstance = {
  172.         side = monSide or "term",
  173.         mon = monSide and peripheral.wrap(monSide) or term.current(),
  174.         buttonList = {},
  175.         clickMap = {},
  176.     }
  177.     local x, y = buttonInstance.mon.getSize()
  178.     for i = 1, x do
  179.         buttonInstance.clickMap[i] = {}
  180.     end
  181.     setmetatable(buttonInstance, {__index = Button})
  182.     return buttonInstance
  183. end
Add Comment
Please, Sign In to add comment