Advertisement
Guest User

Tuchpoint Mod

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