Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# buttonBuddy 1.0.17 - a basic button API by Dog
- --# pastebin: zkB39pr7
- --# For instructions and documentation visit http://www.computercraft.info/forums2/index.php?/topic/28270-buttonbuddy-a-basic-button-api/
- local termX, termY = term.getSize()
- local bbButtonCount, bbButtons, validColors, fullColor, grayScale = 0, { }, { }, term.isColor(), (_CC_VERSION or _HOST)
- if fullColor then
- 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; }
- elseif grayScale then
- validColors = { [1] = true; [128] = true; [256] = true; [32768] = true; }
- else
- validColors = { [1] = true; [32768] = true; }
- end
- local button = {
- create = function(sText, bbGroup, x, y, w, h, mouseBtn, activate, funct, txtCol, bgCol, inactiveTxtCol, inactiveBgCol)
- 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
- bbButtonCount = bbButtonCount + 1
- w = math.floor(math.min(math.max(1, math.max(#sText, w)), termX))
- h = math.floor(math.max(1, math.min(h, termY)))
- x = math.floor(math.max(1, math.min(x, termX - w + 1)))
- y = math.floor(math.max(1, math.min(y, termY - h + 1)))
- if mouseBtn then
- mouseBtn = math.floor(mouseBtn)
- mouseBtn = (mouseBtn > 0 and mouseBtn < 4) and mouseBtn or 1
- end
- bbButtons[bbButtonCount] = {
- text = sText:sub(1, w);
- group = bbGroup;
- startX = x;
- startY = y;
- width = w;
- height = h;
- mouseButton = mouseBtn;
- active = activate;
- func = type(funct) == "function" and funct or function() return true end;
- txtColor = (type(txtCol) == "number" and validColors[txtCol]) and txtCol or colors.black;
- bgColor = (type(bgCol) == "number" and validColors[bgCol]) and bgCol or colors.white;
- inactiveTextColor = (type(inactiveTextCol) == "number" and validColors[inactiveTextCol]) and inactiveTextCol or ((fullColor or grayScale) and colors.lightGray or colors.white);
- inactiveBgColor = (type(inactiveBgCol) == "number" and validColors[inactiveBgCol]) and inactiveBgCol or ((fullColor or grayScale) and colors.gray or colors.black);
- }
- return true, bbButtonCount
- end
- return false
- end;
- check = function(x, y, mouseBtn, group)
- 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
- local bbGroup, bbMBtn, startX, startY
- if mouseBtn then
- mouseBtn = math.floor(mouseBtn)
- mouseBtn = (mouseBtn > 0 and mouseBtn < 4) and mouseBtn or 1
- end
- for id = 1, bbButtonCount do
- bbGroup, bbMBtn, startX, startY = bbButtons[id]["group"], bbButtons[id]["mouseButton"], bbButtons[id]["startX"], bbButtons[id]["startY"]
- if (group and group == bbGroup) or not bbGroup then
- if (mouseBtn and (bbMBtn == mouseBtn or not bbMBtn)) or (not mouseBtn and not bbMBtn) then
- if x >= startX and x <= startX + bbButtons[id]["width"] - 1 and y >= startY and y <= startY + bbButtons[id]["height"] - 1 then
- if bbButtons[id]["active"] then
- local success, returnData = bbButtons[id]["func"](id, mouseBtn)
- if type(success) == "boolean" then
- return success, id, returnData
- else
- return true, id, returnData or success
- end
- else
- return false, id
- end
- end
- end
- end
- end
- end
- return false
- end;
- render = function(target, groupOnly)
- local active, line, text, txtLine, txtLen, txtRow, startX, startY, buttonHeight, buttonWidth
- if type(target) == "number" and target > 0 and target <= bbButtonCount then
- startX, startY = bbButtons[target]["startX"], bbButtons[target]["startY"]
- buttonHeight, buttonWidth = bbButtons[target]["height"], bbButtons[target]["width"]
- text = bbButtons[target]["text"]
- txtLen, active = #text, bbButtons[target]["active"]
- txtLine = string.rep(" ", math.floor((buttonWidth - txtLen) / 2)) .. text .. string.rep(" ", math.ceil((buttonWidth - txtLen) / 2))
- term.setTextColor(active and bbButtons[target]["txtColor"] or bbButtons[target]["inactiveTextColor"])
- term.setBackgroundColor(active and bbButtons[target]["bgColor"] or bbButtons[target]["inactiveBgColor"])
- if buttonHeight == 1 then
- term.setCursorPos(startX, startY)
- term.write(txtLine)
- else
- line, txtRow = string.rep(" ", buttonWidth), startY + math.floor(buttonHeight / 2)
- for yPos = startY, startY + buttonHeight - 1 do
- term.setCursorPos(startX, yPos)
- term.write(yPos == txtRow and txtLine or line)
- end
- end
- return true, 1
- elseif type(target) == "string" or target == nil then
- local buttonsRendered, bbGroup = 0
- for id = 1, bbButtonCount do
- bbGroup = bbButtons[id]["group"]
- if (type(target) == "string" and bbGroup == target) or (not bbGroup and not groupOnly) then
- startX, startY = bbButtons[id]["startX"], bbButtons[id]["startY"]
- buttonHeight, buttonWidth = bbButtons[id]["height"], bbButtons[id]["width"]
- text = bbButtons[id]["text"]
- txtLen, active = #text, bbButtons[id]["active"]
- txtLine = string.rep(" ", math.floor((buttonWidth - txtLen) / 2)) .. text .. string.rep(" ", math.ceil((buttonWidth - txtLen) / 2))
- term.setTextColor(active and bbButtons[id]["txtColor"] or bbButtons[id]["inactiveTextColor"])
- term.setBackgroundColor(active and bbButtons[id]["bgColor"] or bbButtons[id]["inactiveBgColor"])
- if buttonHeight == 1 then
- term.setCursorPos(startX, startY)
- term.write(txtLine)
- else
- line, txtRow = string.rep(" ", buttonWidth), startY + math.floor(buttonHeight / 2)
- for yPos = startY, startY + buttonHeight - 1 do
- term.setCursorPos(startX, yPos)
- term.write(yPos == txtRow and txtLine or line)
- end
- end
- buttonsRendered = buttonsRendered + 1
- end
- end
- return true, buttonsRendered
- end
- return false
- end;
- setMouseButton = function(id, mouseBtn)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and (type(mouseBtn) == "number" or mouseBtn == nil) then
- if mouseBtn then
- mouseBtn = (math.floor(mouseBtn) > 0 and mouseBtn < 4) and math.floor(mouseBtn) or 1
- end
- bbButtons[id]["mouseButton"] = mouseBtn
- return true, mouseBtn
- end
- return false
- end;
- setFunction = function(id, funct)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and (type(funct) == "function" or funct == nil) then
- bbButtons[id]["func"] = funct or function() return true end
- return true
- end
- return false
- end;
- setText = function(id, text)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and type(text) == "string" then
- bbButtons[id]["text"] = #text <= (termX - bbButtons[id]["startX"] + 1) and text or text:sub(1, termX - bbButtons[id]["startX"] + 1)
- bbButtons[id]["width"] = math.max(#bbButtons[id]["text"], bbButtons[id]["width"])
- return true, bbButtons[id]["text"], bbButtons[id]["width"]
- end
- return false
- end;
- getText = function(id)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then
- return bbButtons[id]["text"]
- end
- end;
- setCoords = function(id, x, y)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and type(x) == "number" and type(y) == "number" then
- bbButtons[id]["startX"] = math.floor(math.max(1, math.min(x, termX - bbButtons[id]["width"] + 1)))
- bbButtons[id]["startY"] = math.floor(math.max(1, math.min(y, termY - bbButtons[id]["height"] + 1)))
- return true, bbButtons[id]["startX"], bbButtons[id]["startY"]
- end
- return false
- end;
- getCoords = function(id)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then
- return bbButtons[id]["startX"], bbButtons[id]["startY"]
- end
- end;
- setSize = function(id, w, h)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and type(w) == "number" and type(h) == "number" then
- bbButtons[id]["width"] = math.floor(math.min(math.max(1, math.max(#bbButtons[id]["text"], w)), termX - bbButtons[id]["startX"] + 1))
- bbButtons[id]["height"] = math.floor(math.max(1, math.min(h, termY - bbButtons[id]["startY"] + 1)))
- --if #bbButtons[id]["text"] > bbButtons[id]["width"] then bbButtons[id]["text"] = bbButtons[id]["text"]:sub(1, bbButtons[id]["width"]) end
- return true, bbButtons[id]["width"], bbButtons[id]["height"]
- end
- return false
- end;
- getSize = function(id)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then
- return bbButtons[id]["width"], bbButtons[id]["height"]
- end
- end;
- setColors = function(target, txtColor, bgColor, inactiveTxtCol, inactiveBgCol)
- if txtColor or bgColor or inactiveTxtCol or inactiveBgCol then
- local returnValue, changedColors = false, 0
- if type(target) == "number" and target > 0 and target <= bbButtonCount then
- if txtColor and type(txtColor) == "number" and validColors[txtColor] then
- bbButtons[target]["txtColor"] = txtColor
- changedColors = changedColors + 1
- returnValue = true
- end
- if bgColor and type(bgColor) == "number" and validColors[bgColor] then
- bbButtons[target]["bgColor"] = bgColor
- changedColors = changedColors + 1
- returnValue = true
- end
- if inactiveTxtCol and type(inactiveTxtCol) == "number" and validColors[inactiveTxtCol] then
- bbButtons[target]["inactiveTextColor"] = inactiveTxtCol
- changedColors = changedColors + 1
- returnValue = true
- end
- if inactiveBgCol and type(inactiveBgCol) == "number" and validColors[inactiveBgCol] then
- bbButtons[target]["inactiveBgColor"] = inactiveBgCol
- changedColors = changedColors + 1
- returnValue = true
- end
- elseif type(target) == "string" or target == nil then
- for id = 1, bbButtonCount do
- if target == bbButtons[id]["group"] then
- if txtColor and type(txtColor) == "number" and validColors[txtColor] then
- bbButtons[id]["txtColor"] = txtColor
- changedColors = changedColors + 1
- returnValue = true
- end
- if bgColor and type(bgColor) == "number" and validColors[bgColor] then
- bbButtons[id]["bgColor"] = bgColor
- changedColors = changedColors + 1
- returnValue = true
- end
- if inactiveTxtCol and type(inactiveTxtCol) == "number" and validColors[inactiveTxtCol] then
- bbButtons[id]["inactiveTextColor"] = inactiveTxtCol
- changedColors = changedColors + 1
- returnValue = true
- end
- if inactiveBgCol and type(inactiveBgCol) == "number" and validColors[inactiveBgCol] then
- bbButtons[id]["inactiveBgColor"] = inactiveBgCol
- changedColors = changedColors + 1
- returnValue = true
- end
- end
- end
- end
- return returnValue, changedColors
- end
- return false, 0
- end;
- getColors = function(id)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then
- return bbButtons[id]["txtColor"], bbButtons[id]["bgColor"], bbButtons[id]["inactiveTextColor"], bbButtons[id]["inactiveBgColor"]
- end
- end;
- setActive = function(target, state)
- if type(state) == "boolean" then
- if type(target) == "number" and target > 0 and target <= bbButtonCount then
- bbButtons[target]["active"] = state
- return true, 1
- elseif type(target) == "string" or target == nil then
- local stateCount = 0
- for id = 1, bbButtonCount do
- if target == bbButtons[id]["group"] then
- bbButtons[id]["active"] = state
- stateCount = stateCount + 1
- end
- end
- return true, stateCount
- end
- end
- return false
- end;
- getActiveStatus = function(id)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then
- return bbButtons[id]["active"]
- end
- end;
- setGroup = function(id, group)
- if type(id) == "number" and id > 0 and id <= bbButtonCount and (type(group) == "string" or group == nil) then
- bbButtons[id]["group"] = group
- return true
- end
- return false
- end;
- getGroup = function(id)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then
- return bbButtons[id]["group"]
- end
- end;
- getButtonCount = function()
- return bbButtonCount
- end;
- delete = function(target)
- if type(target) == "number" and target > 0 and target <= bbButtonCount then
- table.remove(bbButtons, target)
- bbButtonCount = bbButtonCount - 1
- return true, 1
- elseif type(target) == "string" or target == nil then
- local buttonsDeleted = 0
- for id = bbButtonCount, 1, -1 do
- if target == bbButtons[id]["group"] then
- table.remove(bbButtons, id)
- buttonsDeleted = buttonsDeleted + 1
- end
- end
- bbButtonCount = bbButtonCount - buttonsDeleted
- return true, buttonsDeleted
- end
- return false
- end;
- deleteButton = function(id) --# Deprecated (see delete)
- if type(id) == "number" and id > 0 and id <= bbButtonCount then
- table.remove(bbButtons, id)
- bbButtonCount = bbButtonCount - 1
- return true
- end
- return false
- end;
- deleteGroup = function(group) --# Deprecated (see delete)
- if type(group) == "string" or group == nil then
- local buttonsDeleted = 0
- for id = bbButtonCount, 1, -1 do
- if group == bbButtons[id]["group"] then
- table.remove(bbButtons, id)
- buttonsDeleted = buttonsDeleted + 1
- end
- end
- bbButtonCount = bbButtonCount - buttonsDeleted
- return true, buttonsDeleted
- end
- return false
- end;
- deleteAll = function()
- local buttonsDeleted = 0
- for id = bbButtonCount, 1, -1 do
- bbButtons[id] = nil
- buttonsDeleted = buttonsDeleted + 1
- end
- bbButtonCount = 0
- return true, buttonsDeleted
- end;
- }
- return button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement