Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Buttons API for Computercraft mod of Minecraft. Create for Daverkex based on Direwolf20 Buttons API.
- --Structure:
- -- |
- -- |
- -- -> variable definitions
- -- |
- -- -> API functions in alphabetical order
- -- |
- -- ->Internal functions in alphabetical order
- --WIP:
- --Only render 1 label
- --buttons in html format?
- --Improve painbackground()
- --Documentation
- local button = {}
- local data = {}
- data["side"] = "top"
- data["textScale"] = 1
- data["textColor"] = colors.white
- data["backgroundColor"] = colors.black
- data["onButtonColor"] = colors.lime
- data["offButtonColor"] = colors.red
- local checkxy, fill, flash, null, paintBackground, paintHeading, paintLabel, paintButton, runFunc --Internal functions definitions
- local mon = peripheral.wrap(data["side"])
- mon.setTextScale(data["textScale"])
- mon.setTextColor(data["textColor"])
- mon.setBackgroundColor(data["backgroundColor"])
- function advanceButton(id, onText, onFunc, xmin, xmax, ymin, ymax, type, offFunc, offText, onButtonColor, offButtonColor, onTextColor, offTextColor)
- button[id] = {}
- button[id]["onText"] = onText
- if onFunc == nil then
- button[id].onFunc = null()
- else
- button[id].onFunc = onFunc
- end
- button[id]["active"] = false
- button[id]["xmin"] = xmin
- button[id]["ymin"] = ymin
- button[id]["xmax"] = xmax
- button[id]["ymax"] = ymax
- if type ~= nil then
- if (type == "button" or type == "switch") then
- button[id]["type"] = type
- else
- print(error("Button type invalid"))
- end
- else
- button[id]["type"] = "button"
- end
- if offFunc == nil then
- button[id].offFunc = null()
- else
- button[id].offFunc = offFunc
- end
- if offText ~= nil then
- button[id]["offText"] = offText
- end
- if onButtonColor ~= nil then
- button[id]["onButtonColor"] = onButtonColor
- end
- if offButtonColor ~= nil then
- button[id]["offButtonColor"] = offButtonColor
- end
- if onTextColor ~= nil then
- button[id]["onTextColor"] = onTextColor
- end
- if offTextColor ~= nil then
- button[id]["offTextColor"] = offTextColor
- end
- end
- -- function changeBackgroundColors(onColor, offColor, id)
- -- if id == nil then
- -- if onColor ~= nil then
- -- data["onButtonColor"] = onColor
- -- end
- -- if offColor ~= nil then
- -- data["offButtonColor"] = offColor
- -- end
- -- else
- -- if onColor ~= nil then
- -- button[id]["onButtonColor"] = onColor
- -- end
- -- if offColor ~= nil then
- -- button[id]["offButtonColor"] = offColor
- -- end
- -- end
- -- end
- function clearTable()
- button = {}
- mon.clear()
- return true
- end
- function clickButton(id)
- if button[id] == nil then
- print (error("Button don't exist"))
- end
- local type = button[id]["type"]
- if type == "button" then
- runFunc(id, "onFunc")
- toggle(id)
- runFunc(id, "offFunc")
- return true
- elseif type == "switch" then
- if button[id]["active"] == true then
- runFunc(id, "offFunc")
- toggle(id)
- else
- runFunc(id, "onFunc")
- toggle(id)
- end
- return true
- end
- end
- function getClick()
- event, side, x, y = os.pullEvent("monitor_touch")
- checkxy(x,y)
- return true
- end
- function getVersion()
- return 0.1
- end
- function heading(text, textColor, backgroundColor)
- data["heading"] = {}
- data["heading"]["text"] = text
- if textColor ~= nil then
- data["heading"]["textColor"] = textColor
- end
- if backgroundColor ~= nil then
- data["heading"]["backgroundColor"] = backgroundColor
- end
- return true
- end
- function label(w, h, text, textColor, backgroundColor)
- data["label"] = {}
- data["label"]["text"] = text
- data["label"]["w"] = w
- data["label"]["h"] = h
- if textColor ~= nil then
- data["label"]["textColor"] = textColor
- end
- if backgroundColor ~= nil then
- data["label"]["backgroundColor"] = backgroundColor
- end
- return true
- end
- function monFormat(textScale, textColor, backgroundColor)
- if (textScale == nil and textColor == nil and backgroundColor == nil) then
- return data["textScale"], data["textColor"], data["backgroundColor"]
- end
- data["textScale"] = textScale
- data["textColor"] = textColor
- data["backgroundColor"] = backgroundColor
- mon.setTextScale(data["textScale"])
- mon.setTextColor(data["textColor"])
- mon.setBackgroundColor(data["backgroundColor"])
- return true
- end
- function monSide(side)
- if side == nil then
- return data["side"]
- else
- data["side"] = side
- mon = peripheral.wrap(data["side"])
- return true
- end
- end
- function newButton(id, text, onFunc, xmin, xmax, ymin, ymax)
- button[id] = {}
- button[id]["onText"] = text
- if onFunc == nil then
- button[id].onFunc = null()
- else
- button[id].onFunc = onFunc
- end
- button[id]["active"] = false
- button[id]["xmin"] = xmin
- button[id]["ymin"] = ymin
- button[id]["xmax"] = xmax
- button[id]["ymax"] = ymax
- button[id]["type"] = "button"
- button[id]["offFunc"] = null()
- end
- function paint()
- if data["backgroundColor"] ~= colors.black then
- paintBackground()
- end
- if data["heading"] ~= nil then
- paintHeading()
- end
- if data["label"] ~= nil then
- paintLabel()
- end
- for id, bData in pairs(button) do
- paintButton(bData)
- end
- return true
- end
- function toggle(id)
- if button[id]["type"] == "switch" then
- button[id]["active"] = not button[id]["active"]
- paint()
- else
- flash(id)
- end
- end
- function checkxy(x, y)
- for id, data in pairs(button) do
- if y >= data["ymin"] and y <= data["ymax"] then
- if x >= data["xmin"] and x <= data["xmax"] then
- return clickButton(id) --Return true if exist button
- end
- end
- end
- return false
- end
- function flash(id)
- button[id]["active"] = not button[id]["active"]
- paint()
- sleep(.15)
- button[id]["active"] = not button[id]["active"]
- paint()
- return true
- end
- function null()
- return false
- end
- function paintBackground()
- local x, y = mon.getSize()
- for i = 0, y do
- for j = 0, x do
- mon.setCursorPos(j, i)
- mon.write(" ")
- end
- end
- return true
- end
- function paintButton(bData)
- local text, textColor, buttonColor
- local on = bData["active"]
- if on == true then
- if bData["onButtonColor"] ~= nil then
- buttonColor = bData["onButtonColor"]
- else
- buttonColor = data["onButtonColor"]
- end
- if bData["onTextColor"] ~= nil then
- textColor = bData["onTextColor"]
- else
- textColor = data["textColor"]
- end
- text = bData["onText"]
- else
- if bData["offButtonColor"] ~= nil then
- buttonColor = bData["offButtonColor"]
- else
- buttonColor = data["offButtonColor"]
- end
- if bData["offTextColor"] ~= nil then
- textColor = bData["offTextColor"]
- else
- textColor = data["textColor"]
- end
- if bData["offText"] ~= nil then
- text = bData["offText"]
- else
- text = bData["onText"]
- end
- end
- mon.setBackgroundColor(buttonColor)
- mon.setTextColor(textColor)
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) / 2)
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) / 2) + 1
- for j = bData["ymin"], bData["ymax"] do
- mon.setCursorPos(bData["xmin"], j)
- if j == yspot then
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
- if k == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(data["backgroundColor"])
- mon.setTextColor(data["textColor"])
- end
- function paintHeading()
- local w, h = mon.getSize()
- mon.setCursorPos((w - string.len(data["heading"]["text"])) / 2 + 1, 1)
- if data["heading"]["textColor"] ~= nil then
- mon.setTextColor(data["heading"]["textColor"])
- end
- if data["heading"]["backgroundColor"] ~= nil then
- mon.setBackgroundColor(data["heading"]["backgroundColor"])
- end
- mon.write(data["heading"]["text"])
- if data["heading"]["textColor"] ~= nil then
- mon.setTextColor(data["textColor"])
- end
- if data["heading"]["backgroundColor"] ~= nil then
- mon.setBackgroundColor(data["backgroundColor"])
- end
- end
- function paintLabel()
- mon.setCursorPos(data["label"]["w"], data["label"]["h"])
- if data["label"]["textColor"] ~= nil then
- mon.setTextColor(data["label"]["textColor"])
- end
- if data["label"]["backgroundColor"] ~= nil then
- mon.setBackgroundColor(data["label"]["backgroundColor"])
- end
- mon.write(data["label"]["text"])
- if data["label"]["textColor"] ~= nil then
- mon.setTextColor(data["textColor"])
- end
- if data["label"]["backgroundColor"] ~= nil then
- mon.setBackgroundColor(data["backgroundColor"])
- end
- end
- function runFunc(id, func)
- return button[id][func]()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement