Advertisement
FakoTheGreat

Potential Touchpoint for OC

Nov 1st, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 KB | None | 0 0
  1. local API = {}
  2. local component = require("component")
  3. local colors = require("colors")
  4. local term = require("term")
  5.  
  6. mon = component.gpu
  7.  
  8. local function setupLabel(buttonLen, minY, maxY, name)
  9.     local labelTable = {}
  10.     if type(name) == "table" then
  11.         for i = 1, #name do
  12.             labelTable[i] = name[i]
  13.         end
  14.         name = name.label
  15.     elseif type(name) == "string" then
  16.         local buttonText = string.sub(name, 1, buttonLen - 2)
  17.         if #buttonText < #name then
  18.             buttonText = " "..buttonText.." "
  19.         else
  20.             local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
  21.             buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
  22.         end
  23.         for i = 1, maxY - minY + 1 do
  24.             if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
  25.                 labelTable[i] = buttonText
  26.             else
  27.                 labelTable[i] = string.rep(" ", buttonLen)
  28.             end
  29.         end
  30.     end
  31.     return labelTable, name
  32. end
  33.  
  34. local Button = {
  35.     draw = function(self)
  36.         mon.setForeground(colors.white)
  37.         mon.setBackground(colors.black)
  38.     local x, y = mon.getResolution()
  39.         mon.fill(0, 0, x, y, " ")
  40.         for name, buttonData in pairs(self.buttonList) do
  41.             if buttonData.active then
  42.                 mon.setBackground(buttonData.activeColor)
  43.             else
  44.                 mon.setBackground(buttonData.inactiveColor)
  45.             end
  46.             for i = buttonData.yMin, buttonData.yMax do
  47.                 term.setCursor(buttonData.xMin, i)
  48.                 term.write(buttonData.label[i - buttonData.yMin + 1])
  49.             end
  50.         end
  51.     end,
  52.     add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor)
  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 = mon.getResolution()
  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.             label = label,
  67.         }
  68.         for i = xMin, xMax do
  69.             for j = yMin, yMax do
  70.                 if self.clickMap[i][j] ~= nil then
  71.                     --undo changes
  72.                     for k = xMin, xMax do
  73.                         for l = yMin, yMax do
  74.                             if self.clickMap[k][l] == name then
  75.                                 self.clickMap[k][l] = nil
  76.                             end
  77.                         end
  78.                     end
  79.                     self.buttonList[name] = nil
  80.                     error("overlapping button", 2)
  81.                 end
  82.                 self.clickMap[i][j] = name
  83.             end
  84.         end
  85.     end,
  86.     run = function(self)
  87.         while true do
  88.             self:draw()
  89.             local event = {event.pull(1,"touch")}
  90.             local clicked = self.clickMap[event[3]][event[4]]
  91.             if clicked and self.buttonList[clicked] and self.buttonList[clicked].func then
  92.                 self.buttonList[clicked].func()
  93.             end
  94.         end
  95.     end,
  96.     handleEvents = function(self, ...)
  97.         local event = {...}
  98.         if #event == 0 then event = {event.pull()} 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 API.create()
  133.     local buttonInstance = {
  134.         buttonList = {},
  135.         clickMap = {},
  136.     }
  137.     local x, y = mon.getResolution()
  138.     for i = 1, x do
  139.         buttonInstance.clickMap[i] = {}
  140.     end
  141.     setmetatable(buttonInstance, {__index = Button})
  142.     return buttonInstance
  143. end
  144.  
  145. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement