Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This is an API used for drawing buttons on a ComputerCraft monitor --
- -- How to use the API --
- -- ButtonAPI.initMonitor(side)
- -- ButtonAPI.addButton("button1", 1, 1, 11, 10, "test")
- -- ButtonAPI.addButton("button2", 11, 11, 5, 5, "test02")
- -- ButtonAPI.draw()
- -- ButtonAPI.initEventHandler(eventHandler)
- -- function eventHandler()
- -- while true do
- -- local event, name, x, y = os.pullEvent("button")
- -- print("Event: " .. event .. " Name: " .. name .. " X: " .. x .. " Y: " .. y)
- -- end
- -- end
- -- FUNCTIONS --
- -- addButton(mon, name, x, y, width, height, text, [func], [funcParam], [buttonColor], [textColor]) -> add a new button
- -- removeButton(index) -> remove button
- -- centerTextOnWindow(window, text) -> centers the text in a window
- -- draw() -> draws all the buttons
- -- toggleButton(button) -> toggles the colors: can be given as func to addButton
- -- buttonClickEvent() -> check monitor and call checkButton when monitor is pressed
- -- checkButton(x, y) -> check a if the coordinates are inside a button: returns on eventStack
- -- printButtonInfo() -> printing all the information about all the buttons
- -- initEventHandler(eventHandler) -> function that handles the events
- -- initEventHandler(eventHandler, mainFunc) -> function that handles the events
- -- startParallel(function1, function2) -> start two functions
- -- startParallel(function1, function2, function3) -> start two functions
- -- getMonitor()
- -- setMonitor(monitor)
- -- getScreenSize()
- -- getButtonIndex()
- -- getButton(index)
- -- getFunc(index)
- -- setFunc(index, func)
- -- getFuncParam(index)
- -- setFuncParam(index, funcParam)
- -- getButtonColor(index)
- -- setButtonColor(index, buttonColor)
- -- getTextColor(index)
- -- setTextColor(index, textColor)
- local buttons={}
- function addButton(mon, name, x, y, width, height, text, func, funcParam, buttonColor, textColor)
- print("Adding button...")
- print(name .. ": " .. text .. " (" .. x .. ", " .. y .. "),(" .. width .. ", " .. height .. ")")
- func = func or toggleButton
- params = {displayTime = 1, activeColor = colors.red}
- funcParam = funcParam or params
- buttonColor = buttonColor or colors.green
- textColor = textColor or colors.white
- local button = {}
- button["name"] = name
- button["buttonColor"] = buttonColor
- button["textColor"] = textColor
- button["text"] = text
- button["func"] = func
- button["funcParam"] = funcParam
- button["window"] = window.create(mon, x, y, width, height, false)
- button["window"].setTextColor(textColor)
- button["window"].setBackgroundColor(buttonColor)
- button["window"].clear()
- table.insert(buttons, button)
- end
- function removeButton(index)
- table.remove(index)
- end
- function centerTextOnWindow(window, text)
- local x, y = window.getPosition()
- local width, height = window.getSize()
- window.setCursorPos(math.ceil((width / 2) - (text:len() / 2) + 1), math.ceil(height / 2))
- window.write(text)
- end
- function draw()
- print("Starting drawing...")
- print("Length: ".. #buttons)
- for i = 1, #buttons do
- print("Drawing: " .. buttons[i]["name"])
- buttons[i]["window"].clear()
- centerTextOnWindow(buttons[i]["window"], buttons[i]["text"])
- buttons[i]["window"].setVisible(true)
- end
- print("Drawing done")
- end
- function toggleButton(button)
- button["window"].setBackgroundColor(button["funcParam"]["activeColor"])
- draw()
- sleep(button["funcParam"]["displayTime"])
- button["window"].setBackgroundColor(button["buttonColor"])
- draw()
- end
- function buttonClickEvent()
- while true do
- event, side, x, y = os.pullEvent("monitor_touch")
- checkButton(x, y)
- end
- end
- function checkButton(x, y)
- for i = 1, #buttons do
- local xButton, yButton = buttons[i]["window"].getPosition()
- local width, height = buttons[i]["window"].getSize()
- if x >= xButton and x <= xButton + width and y >= yButton and y <= yButton + height then
- os.queueEvent("button", buttons[i]["name"], x, y)
- buttons[i]["func"](buttons[i])
- end
- end
- end
- function printButtonInfo()
- for i = 1, #buttons do
- print("--Button: " .. buttons[i]["name"])
- print("X: " .. buttons[i]["x"] .. " Y: " .. buttons[i]["y"])
- print("Width: " .. buttons[i]["width"] .. " Height: " .. buttons[i]["height"])
- print("Text: " .. buttons[i]["text"])
- end
- end
- function initEventHandler(eventHandler)
- startParallel(ButtonAPI.buttonClickEvent, eventHandler)
- end
- function initEventHandler(eventHandler, mainFunc)
- startParallel(ButtonAPI.buttonClickEvent, eventHandler, mainFunc)
- end
- function startParallel(function1, function2)
- parallel.waitForAll(function1, function2)
- end
- function startParallel(function1, function2, function3)
- parallel.waitForAll(function1, function2, function3)
- end
- function getButtonIndex(buttonName)
- for i = 1, #buttons do
- if buttons[i]["name"] == buttonName then
- return i
- end
- end
- end
- function getButton(index)
- return buttons[index]
- end
- function getFunc(index)
- return getButton(index)["func"]
- end
- function setFunc(index, func)
- getButton(index)["func"] = func
- end
- function getFuncParam(index)
- return getButton(index)["funcParam"]
- end
- function setFuncParam(index, funcParam)
- getButton(index)["funcParam"] = funcParam
- end
- function getButtonColor(index)
- return getButton(index)["buttonColor"]
- end
- function setButtonColor(index, buttonColor)
- getButton(index)["buttonColor"] = buttonColor
- button["window"].setBackgroundColor(buttonColor)
- end
- function getTextColor(index)
- return getButton(index)["textColor"]
- end
- function setTextColor(index, textColor)
- getButton(index)["textColor"] = textColor
- button["window"].setBackgroundColor(buttonColor)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement