Advertisement
HydrantHunter

buttonBuddy

Jan 25th, 2017
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.83 KB | None | 0 0
  1. --# buttonBuddy 1.0.17 - a basic button API by Dog
  2. --# pastebin: zkB39pr7
  3. --# For instructions and documentation visit http://www.computercraft.info/forums2/index.php?/topic/28270-buttonbuddy-a-basic-button-api/
  4. local termX, termY = term.getSize()
  5. local bbButtonCount, bbButtons, validColors, fullColor, grayScale = 0, { }, { }, term.isColor(), (_CC_VERSION or _HOST)
  6. if fullColor then
  7.   validColors = { [1] = true; [2] = true; [4] = true; [8] = true; [16] = true; [32] = true; [64] = true; [128] = true; [256] = true; [512] = true; [1024] = true; [2048] = true; [4096] = true; [8192] = true; [16384] = true, [32768] = true; }
  8. elseif grayScale then
  9.   validColors = { [1] = true; [128] = true; [256] = true; [32768] = true; }
  10. else
  11.   validColors = { [1] = true; [32768] = true; }
  12. end
  13. local button = {
  14.   create = function(sText, bbGroup, x, y, w, h, mouseBtn, activate, funct, txtCol, bgCol, inactiveTxtCol, inactiveBgCol)
  15.     if type(sText) == "string" and (type(bbGroup) == "string" or bbGroup == nil) and type(x) == "number" and type(y) == "number" and type(w) == "number" and type(h) == "number" and (type(mouseButton) == "number" or mouseButton == nil) and (type(funct) == "function" or funct == nil) and type(activate) == "boolean" then
  16.       bbButtonCount = bbButtonCount + 1
  17.       w = math.floor(math.min(math.max(1, math.max(#sText, w)), termX))
  18.       h = math.floor(math.max(1, math.min(h, termY)))
  19.       x = math.floor(math.max(1, math.min(x, termX - w + 1)))
  20.       y = math.floor(math.max(1, math.min(y, termY - h + 1)))
  21.       if mouseBtn then
  22.         mouseBtn = math.floor(mouseBtn)
  23.         mouseBtn = (mouseBtn > 0 and mouseBtn < 4) and mouseBtn or 1
  24.       end
  25.       bbButtons[bbButtonCount] = {
  26.         text = sText:sub(1, w);
  27.         group = bbGroup;
  28.         startX = x;
  29.         startY = y;
  30.         width = w;
  31.         height = h;
  32.         mouseButton = mouseBtn;
  33.         active = activate;
  34.         func = type(funct) == "function" and funct or function() return true end;
  35.         txtColor = (type(txtCol) == "number" and validColors[txtCol]) and txtCol or colors.black;
  36.         bgColor = (type(bgCol) == "number" and validColors[bgCol]) and bgCol or colors.white;
  37.         inactiveTextColor = (type(inactiveTextCol) == "number" and validColors[inactiveTextCol]) and inactiveTextCol or ((fullColor or grayScale) and colors.lightGray or colors.white);
  38.         inactiveBgColor = (type(inactiveBgCol) == "number" and validColors[inactiveBgCol]) and inactiveBgCol or ((fullColor or grayScale) and colors.gray or colors.black);
  39.       }
  40.       return true, bbButtonCount
  41.     end
  42.     return false
  43.   end;
  44.  
  45.   check = function(x, y, mouseBtn, group)
  46.     if type(x) == "number" and type(y) == "number" and (type(mouseBtn) == "number" or mouseBtn == nil) and (type(group) == "string" or group == nil) and x > 0 and x <= termX and y > 0 and y <= termY then
  47.       local bbGroup, bbMBtn, startX, startY
  48.       if mouseBtn then
  49.         mouseBtn = math.floor(mouseBtn)
  50.         mouseBtn = (mouseBtn > 0 and mouseBtn < 4) and mouseBtn or 1
  51.       end
  52.       for id = 1, bbButtonCount do
  53.         bbGroup, bbMBtn, startX, startY = bbButtons[id]["group"], bbButtons[id]["mouseButton"], bbButtons[id]["startX"], bbButtons[id]["startY"]
  54.         if (group and group == bbGroup) or not bbGroup then
  55.           if (mouseBtn and (bbMBtn == mouseBtn or not bbMBtn)) or (not mouseBtn and not bbMBtn) then
  56.             if x >= startX and x <= startX + bbButtons[id]["width"] - 1 and y >= startY and y <= startY + bbButtons[id]["height"] - 1 then
  57.               if bbButtons[id]["active"] then
  58.                 local success, returnData = bbButtons[id]["func"](id, mouseBtn)
  59.                 if type(success) == "boolean" then
  60.                   return success, id, returnData
  61.                 else
  62.                   return true, id, returnData or success
  63.                 end
  64.               else
  65.                 return false, id
  66.               end
  67.             end
  68.           end
  69.         end
  70.       end
  71.     end
  72.     return false
  73.   end;
  74.  
  75.   render = function(target, groupOnly)
  76.     local active, line, text, txtLine, txtLen, txtRow, startX, startY, buttonHeight, buttonWidth
  77.     if type(target) == "number" and target > 0 and target <= bbButtonCount then
  78.       startX, startY = bbButtons[target]["startX"], bbButtons[target]["startY"]
  79.       buttonHeight, buttonWidth = bbButtons[target]["height"], bbButtons[target]["width"]
  80.       text = bbButtons[target]["text"]
  81.       txtLen, active = #text, bbButtons[target]["active"]
  82.       txtLine = string.rep(" ", math.floor((buttonWidth - txtLen) / 2)) .. text .. string.rep(" ", math.ceil((buttonWidth - txtLen) / 2))
  83.       term.setTextColor(active and bbButtons[target]["txtColor"] or bbButtons[target]["inactiveTextColor"])
  84.       term.setBackgroundColor(active and bbButtons[target]["bgColor"] or bbButtons[target]["inactiveBgColor"])
  85.       if buttonHeight == 1 then
  86.         term.setCursorPos(startX, startY)
  87.         term.write(txtLine)
  88.       else
  89.         line, txtRow = string.rep(" ", buttonWidth), startY + math.floor(buttonHeight / 2)
  90.         for yPos = startY, startY + buttonHeight - 1 do
  91.           term.setCursorPos(startX, yPos)
  92.           term.write(yPos == txtRow and txtLine or line)
  93.         end
  94.       end
  95.       return true, 1
  96.     elseif type(target) == "string" or target == nil then
  97.       local buttonsRendered, bbGroup = 0
  98.       for id = 1, bbButtonCount do
  99.         bbGroup = bbButtons[id]["group"]
  100.         if (type(target) == "string" and bbGroup == target) or (not bbGroup and not groupOnly) then
  101.           startX, startY = bbButtons[id]["startX"], bbButtons[id]["startY"]
  102.           buttonHeight, buttonWidth = bbButtons[id]["height"], bbButtons[id]["width"]
  103.           text = bbButtons[id]["text"]
  104.           txtLen, active = #text, bbButtons[id]["active"]
  105.           txtLine = string.rep(" ", math.floor((buttonWidth - txtLen) / 2)) .. text .. string.rep(" ", math.ceil((buttonWidth - txtLen) / 2))
  106.           term.setTextColor(active and bbButtons[id]["txtColor"] or bbButtons[id]["inactiveTextColor"])
  107.           term.setBackgroundColor(active and bbButtons[id]["bgColor"] or bbButtons[id]["inactiveBgColor"])
  108.           if buttonHeight == 1 then
  109.             term.setCursorPos(startX, startY)
  110.             term.write(txtLine)
  111.           else
  112.             line, txtRow = string.rep(" ", buttonWidth), startY + math.floor(buttonHeight / 2)
  113.             for yPos = startY, startY + buttonHeight - 1 do
  114.               term.setCursorPos(startX, yPos)
  115.               term.write(yPos == txtRow and txtLine or line)
  116.             end
  117.           end
  118.           buttonsRendered = buttonsRendered + 1
  119.         end
  120.       end
  121.       return true, buttonsRendered
  122.     end
  123.     return false
  124.   end;
  125.  
  126.   setMouseButton = function(id, mouseBtn)
  127.     if type(id) == "number" and id > 0 and id <= bbButtonCount and (type(mouseBtn) == "number" or mouseBtn == nil) then
  128.       if mouseBtn then
  129.         mouseBtn = (math.floor(mouseBtn) > 0 and mouseBtn < 4) and math.floor(mouseBtn) or 1
  130.       end
  131.       bbButtons[id]["mouseButton"] = mouseBtn
  132.       return true, mouseBtn
  133.     end
  134.     return false
  135.   end;
  136.  
  137.   setFunction = function(id, funct)
  138.     if type(id) == "number" and id > 0 and id <= bbButtonCount and (type(funct) == "function" or funct == nil) then
  139.       bbButtons[id]["func"] = funct or function() return true end
  140.       return true
  141.     end
  142.     return false
  143.   end;
  144.  
  145.   setText = function(id, text)
  146.     if type(id) == "number" and id > 0 and id <= bbButtonCount and type(text) == "string" then
  147.       bbButtons[id]["text"] = #text <= (termX - bbButtons[id]["startX"] + 1) and text or text:sub(1, termX - bbButtons[id]["startX"] + 1)
  148.       bbButtons[id]["width"] = math.max(#bbButtons[id]["text"], bbButtons[id]["width"])
  149.       return true, bbButtons[id]["text"], bbButtons[id]["width"]
  150.     end
  151.     return false
  152.   end;
  153.  
  154.   getText = function(id)
  155.     if type(id) == "number" and id > 0 and id <= bbButtonCount then
  156.       return bbButtons[id]["text"]
  157.     end
  158.   end;
  159.  
  160.   setCoords = function(id, x, y)
  161.     if type(id) == "number" and id > 0 and id <= bbButtonCount and type(x) == "number" and type(y) == "number" then
  162.       bbButtons[id]["startX"] = math.floor(math.max(1, math.min(x, termX - bbButtons[id]["width"] + 1)))
  163.       bbButtons[id]["startY"] = math.floor(math.max(1, math.min(y, termY - bbButtons[id]["height"] + 1)))
  164.       return true, bbButtons[id]["startX"], bbButtons[id]["startY"]
  165.     end
  166.     return false
  167.   end;
  168.  
  169.   getCoords = function(id)
  170.     if type(id) == "number" and id > 0 and id <= bbButtonCount then
  171.       return bbButtons[id]["startX"], bbButtons[id]["startY"]
  172.     end
  173.   end;
  174.  
  175.   setSize = function(id, w, h)
  176.     if type(id) == "number" and id > 0 and id <= bbButtonCount and type(w) == "number" and type(h) == "number" then
  177.       bbButtons[id]["width"] = math.floor(math.min(math.max(1, math.max(#bbButtons[id]["text"], w)), termX - bbButtons[id]["startX"] + 1))
  178.       bbButtons[id]["height"] = math.floor(math.max(1, math.min(h, termY - bbButtons[id]["startY"] + 1)))
  179.       --if #bbButtons[id]["text"] > bbButtons[id]["width"] then bbButtons[id]["text"] = bbButtons[id]["text"]:sub(1, bbButtons[id]["width"]) end
  180.       return true, bbButtons[id]["width"], bbButtons[id]["height"]
  181.     end
  182.     return false
  183.   end;
  184.  
  185.   getSize = function(id)
  186.     if type(id) == "number" and id > 0 and id <= bbButtonCount then
  187.       return bbButtons[id]["width"], bbButtons[id]["height"]
  188.     end
  189.   end;
  190.  
  191.   setColors = function(target, txtColor, bgColor, inactiveTxtCol, inactiveBgCol)
  192.     if txtColor or bgColor or inactiveTxtCol or inactiveBgCol then
  193.       local returnValue, changedColors = false, 0
  194.       if type(target) == "number" and target > 0 and target <= bbButtonCount then
  195.         if txtColor and type(txtColor) == "number" and validColors[txtColor] then
  196.           bbButtons[target]["txtColor"] = txtColor
  197.           changedColors = changedColors + 1
  198.           returnValue = true
  199.         end
  200.         if bgColor and type(bgColor) == "number" and validColors[bgColor] then
  201.           bbButtons[target]["bgColor"] = bgColor
  202.           changedColors = changedColors + 1
  203.           returnValue = true
  204.         end
  205.         if inactiveTxtCol and type(inactiveTxtCol) == "number" and validColors[inactiveTxtCol] then
  206.           bbButtons[target]["inactiveTextColor"] = inactiveTxtCol
  207.           changedColors = changedColors + 1
  208.           returnValue = true
  209.         end
  210.         if inactiveBgCol and type(inactiveBgCol) == "number" and validColors[inactiveBgCol] then
  211.           bbButtons[target]["inactiveBgColor"] = inactiveBgCol
  212.           changedColors = changedColors + 1
  213.           returnValue = true
  214.         end
  215.       elseif type(target) == "string" or target == nil then
  216.         for id = 1, bbButtonCount do
  217.           if target == bbButtons[id]["group"] then
  218.             if txtColor and type(txtColor) == "number" and validColors[txtColor] then
  219.               bbButtons[id]["txtColor"] = txtColor
  220.               changedColors = changedColors + 1
  221.               returnValue = true
  222.             end
  223.             if bgColor and type(bgColor) == "number" and validColors[bgColor] then
  224.               bbButtons[id]["bgColor"] = bgColor
  225.               changedColors = changedColors + 1
  226.               returnValue = true
  227.             end
  228.             if inactiveTxtCol and type(inactiveTxtCol) == "number" and validColors[inactiveTxtCol] then
  229.               bbButtons[id]["inactiveTextColor"] = inactiveTxtCol
  230.               changedColors = changedColors + 1
  231.               returnValue = true
  232.             end
  233.             if inactiveBgCol and type(inactiveBgCol) == "number" and validColors[inactiveBgCol] then
  234.               bbButtons[id]["inactiveBgColor"] = inactiveBgCol
  235.               changedColors = changedColors + 1
  236.               returnValue = true
  237.             end
  238.           end
  239.         end
  240.       end
  241.       return returnValue, changedColors
  242.     end
  243.     return false, 0
  244.   end;
  245.  
  246.   getColors = function(id)
  247.     if type(id) == "number" and id > 0 and id <= bbButtonCount then
  248.       return bbButtons[id]["txtColor"], bbButtons[id]["bgColor"], bbButtons[id]["inactiveTextColor"], bbButtons[id]["inactiveBgColor"]
  249.     end
  250.   end;
  251.  
  252.   setActive = function(target, state)
  253.     if type(state) == "boolean" then
  254.       if type(target) == "number" and target > 0 and target <= bbButtonCount then
  255.         bbButtons[target]["active"] = state
  256.         return true, 1
  257.       elseif type(target) == "string" or target == nil then
  258.         local stateCount = 0
  259.         for id = 1, bbButtonCount do
  260.           if target == bbButtons[id]["group"] then
  261.             bbButtons[id]["active"] = state
  262.             stateCount = stateCount + 1
  263.           end
  264.         end
  265.         return true, stateCount
  266.       end
  267.     end
  268.     return false
  269.   end;
  270.  
  271.   getActiveStatus = function(id)
  272.     if type(id) == "number" and id > 0 and id <= bbButtonCount then
  273.       return bbButtons[id]["active"]
  274.     end
  275.   end;
  276.  
  277.   setGroup = function(id, group)
  278.     if type(id) == "number" and id > 0 and id <= bbButtonCount and (type(group) == "string" or group == nil) then
  279.       bbButtons[id]["group"] = group
  280.       return true
  281.     end
  282.     return false
  283.   end;
  284.  
  285.   getGroup = function(id)
  286.     if type(id) == "number" and id > 0 and id <= bbButtonCount then
  287.       return bbButtons[id]["group"]
  288.     end
  289.   end;
  290.  
  291.   getButtonCount = function()
  292.     return bbButtonCount
  293.   end;
  294.  
  295.   delete = function(target)
  296.     if type(target) == "number" and target > 0 and target <= bbButtonCount then
  297.       table.remove(bbButtons, target)
  298.       bbButtonCount = bbButtonCount - 1
  299.       return true, 1
  300.     elseif type(target) == "string" or target == nil then
  301.       local buttonsDeleted = 0
  302.       for id = bbButtonCount, 1, -1 do
  303.         if target == bbButtons[id]["group"] then
  304.           table.remove(bbButtons, id)
  305.           buttonsDeleted = buttonsDeleted + 1
  306.         end
  307.       end
  308.       bbButtonCount = bbButtonCount - buttonsDeleted
  309.       return true, buttonsDeleted
  310.     end
  311.     return false
  312.   end;
  313.  
  314.   deleteButton = function(id) --# Deprecated (see delete)
  315.     if type(id) == "number" and id > 0 and id <= bbButtonCount then
  316.       table.remove(bbButtons, id)
  317.       bbButtonCount = bbButtonCount - 1
  318.       return true
  319.     end
  320.     return false
  321.   end;
  322.  
  323.   deleteGroup = function(group) --# Deprecated (see delete)
  324.     if type(group) == "string" or group == nil then
  325.       local buttonsDeleted = 0
  326.       for id = bbButtonCount, 1, -1 do
  327.         if group == bbButtons[id]["group"] then
  328.           table.remove(bbButtons, id)
  329.           buttonsDeleted = buttonsDeleted + 1
  330.         end
  331.       end
  332.       bbButtonCount = bbButtonCount - buttonsDeleted
  333.       return true, buttonsDeleted
  334.     end
  335.     return false
  336.   end;
  337.  
  338.   deleteAll = function()
  339.     local buttonsDeleted = 0
  340.     for id = bbButtonCount, 1, -1 do
  341.       bbButtons[id] = nil
  342.       buttonsDeleted = buttonsDeleted + 1
  343.     end
  344.     bbButtonCount = 0
  345.     return true, buttonsDeleted
  346.   end;
  347. }
  348. return button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement